簡(jiǎn)易數(shù)字直流電壓表電路及程序
p1.0-Clock
p1.1-Data out
p1.2-CS
*/
#include reg51.h
#include intrins.h
#include math.h
sbit ad_clk=P1^0;
sbit ad_dout=P1^1;
sbit ad_cs=P1^2;
unsigned char data re_data;
unsigned int data T_cnt;
double volt,xishu;
char code led_seg_code[10]={0x3f,0x06,0x05b,0x04f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//led_seg_code[0-9]代表0-9的7段碼
//---------------
void delay(unsigned int i)//延時(shí)
{ while(--i); }
//---------------
void led_show(double f)
{ unsigned char i,s;
unsigned int k,*j;
if (f2)
{ k=f;//取整數(shù) 部分
i=k;
P0=led_seg_code[i] | 0x80;
P2=0xfb;
delay(50);
i=f*10;
i=i%10;
P0=led_seg_code[i];
P2=0xfd;
delay(50);
i=f*100; //取小數(shù)后1位
i=i%10;
P0=led_seg_code[i];
P2=0xfe;
delay(50);
else //超限報(bào)警,顯示“---”
{ P0=0x40;
P2=0xfb;
delay(50);
//---------
P2=0xfd;
delay(50);
//-----------
P2=0xfe;
delay(50);
}
}
//----讀取ADC轉(zhuǎn)換器TLC549的數(shù)據(jù)----
unsigned char receive_data()
{unsigned char i,d;
d=0;
ad_cs=1; // /CS置高,片選無效
ad_clk=0;
ad_cs=0;
_nop_();
for(i0;i8;i++)
{ ad_clk=1;
d=d1;
if (ad_dout)
{ d++;}
ad_clk=0;
}
ad_cs=1;
for(i=0;i10;i++) // 適當(dāng)延時(shí)超過17us
_nop_();
return d;
}
//-------------
main()
{T_cnt=0;
TMOD=0x01; //定時(shí)器設(shè)置T0
TH0=0x4c; //50000us=(65536-0x4c00)*12/11.0592
TL0=00;
EA=0;
TR0=1;
re_data=0x00;
volt=0;
xishu=2.5/255.0;
//-----------------------------
while(1)
{ if(TF0==1)
{ TF0=0;
TH0=0x4c; //50000us=(65536-0x4c00)*12/11.0592
TL0=0x0;
if (T_cnt2) //T0為50毫秒溢出一次,每100ms讀取一次AD轉(zhuǎn)換器數(shù)據(jù)
{T_cnt++;}
else
{ T_cnt=0;//計(jì)時(shí)到100ms了
re_data=receive_data();
//轉(zhuǎn)換為電壓值
volt=re_data;
volt=volt*xishu;
}
}
led_show(volt);
}
}本文引用地址:http://www.ex-cimer.com/article/172700.htm
DIY機(jī)械鍵盤相關(guān)社區(qū):機(jī)械鍵盤DIY
評(píng)論