電壓表實(shí)驗(yàn)(AD轉(zhuǎn)換)
1 /*電壓表 精確到小數(shù)點(diǎn)后面三位*/
2 /*連線:電位器接IN0,AD_CS接CS0,CS1接KEY/LED_CS*/
3 #include
4 typedef unsigned char uchar;
5 xdata uchar LED_CS _at_ 0x9000;
6 xdata uchar LED_OUTSEG _at_ 0x9004;
7 xdata uchar LED_OUTBIT _at_ 0x9002;
8
9 xdata uchar AD_CS _at_ 0x8000;
10 xdata uchar AD_MODE _at_ 0x8003;
11
12 code unsigned char LEDMAP[] = { // 八段管顯示碼
13 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
14 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
15 };
16 code unsigned char LEDWED[] = {0x20,0x10,0x08,0x04,0x02,0x01};
17 int num[6];
18 void delay(int ms)
19 {
20 int i,j;
21 for(i=0;i 22 for(j=0;j<14;j++) 23 ; 24 } 25 void adf() 26 { 27 AD_CS = 0; 28 delay(10); 29 } 30 void main() 31 { 32 int led_c=0; 33 unsigned char temp; 34 AD_MODE = 0x82; 35 AD_CS = 0; 36 while(1) 37 { 38 adf(); 39 delay(10); 40 num[0] = AD_CS*5/256; 41 num[1] = AD_CS*5%256*10/256; 42 num[2] = AD_CS*5%256*10%256*10/256; 43 num[3] = AD_CS*5%256*10%256*10%256*10/256; 44 temp = LEDMAP[num[led_c]]; 45 if(led_c == 0) 46 temp = temp|0x80; 47 LED_OUTBIT = LEDWED[led_c]; 48 LED_OUTSEG = temp; 49 led_c = (led_c+1)%4; 50 delay(10); 51 } 52 }