程序是:发送给单片机01时,单片机发送“Turn on ad!”给上位机,开启ad转换,并显示AD的电压,在数码管的前三位显示ad的数字量。
但发送01给单片机后,拧动电位器后,二极管会改变,但数码管一直显示8.8.8.(会闪);求帮忙解决!!!!
还有用adc0804芯片来实现ad转换,我到现在还有点搞不懂!!!支招!!!
- #include <reg52.h>
- #include <intrins.h>
- #include <stdio.h>
- #define uchar unsigned char
- sbit dula=P2^6;
- sbit wela=P2^7;
- sbit adwr=P3^6;
- sbit adrd=P3^7;
-
- uchar a,b,flag,flag_ad,flag_smg,ad_vol,t0;
- float num;
- uchar code table[]={
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71};
- void delay(uchar z)
- {
- uchar x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void display(uchar num)
- {
- uchar bai,shi,ge;
- bai=num/100;
- shi=num%100/10;
- ge=num%10;
- wela=1;
- P0=0x7e;
- wela=0;
- P0=0xff;
- dula=1;
- dula=table[bai];
- dula=0;
- delay(5);
- wela=1;
- P0=0x7d;
- wela=0;
- P0=0xff;
- dula=1;
- dula=table[shi];
- dula=0;
- delay(5);
- wela=1;
- P0=0x7b;
- wela=0;
- P0=0xff;
- dula=1;
- dula=table[ge];
- dula=0;
- delay(5);
- }
- uchar ad()
- {
- uchar adval;
- wela=1;
- P0=0x7f;
- adwr=1;
- adwr=0;
- adwr=1;
- delay(10);
- P1=0xff;
- adrd=1;
- adrd=0;
- adval=P1;
- adrd=1;
- P1=adval;
- return adval;
- }
- void main()
- {
- EA=1;
- TMOD=0x21;
- TH1=0xfd;
- TL1=0xfd;
- TR1=1;
- TH0=0x4c;
- TL0=0x00;
- ET0=1;
- SM0=0;
- SM1=1;
- REN=1;
- ES=1;
- while(1)
- {
- if(flag==1)
- {
- flag=0;
- ES=0;
- TI=1;
- switch(b)
- {
- case 0 : puts("Turn on ad!\n");TR0=1;break;
- case 1 : puts("Turn off ad!\n");TR0=0;break;
- case 2 : puts("Error!\n");break;
- }
- while(!TI);
- TI=0;
- ES=1;
- }
- if(flag_smg==1)
- {
- ES=0;
- flag_smg=0;
- ad_vol=ad();
- num=(float)ad_vol*5.0/256.0;
- TI=1;
- printf("The voltagle is %fV\n",num);
- while(!TI);
- TI=0;
- ES=1;
- }
- display(ad_vol);
- }
- }
- void timer0() interrupt 1
- {
- TH0=0x4c;
- TL0=0x00;
- t0++;
- if(t0==20)
- {
- t0=0;
- flag_smg=1;
- }
- }
- void uart() interrupt 4
- {
- RI=0;
- a=SBUF;
- flag=1;
- if(a==1)
- b=0;
- else if(a==2)
- b=1;
- else
- b=2;
- }
复制代码
|