用164和138利用串口控制2個4位數碼管顯示 但顯示是全8加小數點 是串口數據轉換的問題嗎?各位大蝦請教下 代碼如下
#include <pic.h> #include <math.h>
__CONFIG(0xd822);
#define data RC7 //164 #define clk RC6
#define A0 RA3 //138 #define A1 RA4 #define A2 RA5
#define _nop_() asm("nop")
#define uchar unsigned char #define uint unsigned int
uchar dis_7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x67,0x7f,0x6f};
char A[4],ww,qw,bw,sw,gw; unsigned int x;
void DISP_FOUR(char *A); void DISP_ONE(char *A); void BCD(unsigned int r1); void DELAY(unsigned int n);
main(void) { TRISA=0b00111000; TRISC=0b00000000; PORTC=0; x=1234; while(1) { BCD(x); A[0]=gw; A[1]=sw; A[2]=bw; A[3]=qw; DISP_FOUR(A); x++; if(x>9999) x=0; DELAY(500); } }
void DISP_ONE(char *A) { char i,j; j=dis_7[*A]; for(i=0;i<8;i++) { clk=0; if((j&0x80)==0x80) data=1; else data=0; _nop_(); clk=1; j=j<<1; } data=0; }
void DISP_FOUR(char *A) { DISP_ONE(A++); A0=1; A1=0; A2=0; DISP_ONE(A++); A0=0; A1=1; A2=0; DISP_ONE(A++); A0=1; A1=1; A2=0; DISP_ONE(A); A0=0; A1=0; A2=1; }
void BCD(unsigned int r1) { ww=0;qw=0;bw=0;sw=0;gw=0; while(r1>=10000) {r1-=10000; ww++;} while(r1>=1000) {r1-=1000; qw++;} while(r1>=100) {r1-=100; bw++;} while(r1>=10) {r1-=10; sw++;} gw=r1; }
void DELAY(unsigned int n) { unsigned int j; char k; for (j=0;j<n;j++) for (k=246;k>0;k--) _nop_(); }
|