单片机源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define MAIN_Fosc 11059200UL
- typedef unsigned int INT16U;
- sfr P4 = 0xe8;
- sbit DAOUT =P4^4;
- sbit DOUT = P2^5 ;
- sbit DIN = P2^0 ;
- sbit CS = P3^7 ;
- sbit DCLK = P2^1 ;
- sbit DU = P2^6 ;
- sbit WE = P2^7 ;
- uchar count,index;
- uchar code SMGduan[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
- //数码管位选码
- uchar code SMGwei[] = {0xfe, 0xfd, 0xfb, 0xf7};
- uint voltage;
- void display(uint i){
- uchar q, b, s, g;
- static uchar wei;
- q = i / 1000;
- b = i % 1000 / 100;
- s = i % 100 / 10;
- g = i % 10;
- // P0 = 0XFF;//清除断码
- WE = 1;//打开位选锁存器
- P0 = SMGwei[wei];
- WE = 0;//锁存位选数据
- // P0 = 0XFF;//清除断码
- switch(wei)
- {
- case 0: DU = 1; P0 = SMGduan[q]; DU = 0; break;//0x80加上小数点
- case 1: DU = 1; P0 = SMGduan[b]; DU = 0; break; [/b][b]
- case 2: DU = 1; P0 = SMGduan[s]; DU = 0; break;[/s][s]
- case 3: DU = 1; P0 = SMGduan[g]; DU = 0; break;
- }
- wei++;
- if(wei == 4)
- wei = 0;
- }
- void initP1(){
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- TMOD|=0x01;
- TH0 = 0xED;
- TL0 = 0xFF; //定时5ms
- }
- void initP3(){
- EA = 1;
- ET1 = 1;
- TR1 = 1;
- TMOD|= 0x20;
- TH1 = 0xDC ;
- TL1 = 0xDC ;
- }
- void delayMS(INT16U Time){
- INT16U i;
- do{
- i=MAIN_Fosc/96000;
- while(--i);
- }while(--Time);
- }
- void SPLWrite(uchar DATE){
- uchar i;
- for(i=0;i<8;i++){
- DCLK=0;
- if(DATE & 0x80)
- DIN = 1;
- else DIN = 0;
- DCLK = 1;
- DATE<<=1;
- }
- DIN=0;
- }
- uint ADvalue(){
- uchar i;
- uint value;
- for(i=0;i<12;i++){
- value<<=1;
- DCLK = 1;
- DCLK = 0;
- if(DOUT)
- value|=0x01;
- }
- return(value);
- }
- uint ADInput(uchar DATE){
- uint value1;
- CS = 0;
- SPLWrite(DATE);
- DCLK = 0;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- // CS = 1;
- value1= ADvalue();
- //value1 = ADvalue();
- return(value1);
- }
- void main(){
- initP1();
- initP3();
- count=255;
- while(1){
- voltage = ADInput(0xe4);
- // voltage=voltage * 1.2207 ;
- delayMS(1000);
- }
- }
- void interP1() interrupt 1{
- TH0 = 0xED;
- TL0 = 0xFF; //定时5ms
- display(voltage);
- }
- void interP3() interrupt 3{
- index++;
- if(index<=count)
- DAOUT = 1;
- else DAOUT = 0;
- }
复制代码 |