- #include "STC8G.H"
- #include "intrins.h"
- #define uint unsigned int
- #define uchar unsigned char
- sbit led=P5^4; //用P5^4口采集5V信号
- uchar data dis_buf[4]; //显示缓存
- uchar code ledduan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
- 0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf,0x7f};
- uchar i;
- uint num;
- void delay_ms(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=1150;y>0;y--);
- }
- void datapros() //数据处理函数
- {
- float tp;
- tp=num;
- num=tp*1.0/255*500;
- dis_buf[3]=ledduan[num/1000];
- dis_buf[2]=ledduan[num%1000/100] & 0x7f;
- dis_buf[1]=ledduan[num%100/10];
- dis_buf[0]=ledduan[num%10];
- if((dis_buf[2]>=1)&&(dis_buf[2]<=2)) //设置显示范围内执行的功能
- led=0;
- else
- led=1;
- P1=0xff;
- P3&=0x07; //P3=P3&0X07(0000 0111)
- P1=dis_buf[i];
- P3|=0x10<<i; //P3=P3|0X10左移i位
- i++;
- i%=4; //i=i%4
- }
- void main()
- {
- P1M0=0X00;
- P1M1=0X00;
- P2M0=0X00;
- P2M1=0X00;
- P3M0=0X00;
- P3M1=0X00;
- P4M0=0X00;
- P4M1=0X00;
- P5M0=0X00;
- P5M1=0X10;
-
- P_SW2|=0x80;
- ADCTIM=0x3f;
- P_SW2&=0x7f;
- ADCCFG=0x0f;
- ADC_CONTR=0x84;
- while(1)
- {
- ADC_CONTR |= 0x40;
- _nop_();
- _nop_();
- while(!(ADC_CONTR&0x20));
- ADC_CONTR&=~0x20;
- num=ADC_RES;
- datapros();
- delay_ms(1);
- }
- }
复制代码
|