標(biāo)題:
單片機(jī)華氏—攝氏溫度轉(zhuǎn)換的儀器實(shí)驗(yàn)
[打印本頁(yè)]
作者:
liuda
時(shí)間:
2015-1-21 23:05
標(biāo)題:
單片機(jī)華氏—攝氏溫度轉(zhuǎn)換的儀器實(shí)驗(yàn)
是用51hei單片機(jī)開(kāi)發(fā)板 電路圖詳見(jiàn):
http://www.torrancerestoration.com/f/51hei-5.pdf
的數(shù)碼管部分, 用2個(gè)74hc573 鎖存,p0口作為數(shù)據(jù)口 ,p3.6和p3.7分別是段和位的鎖存端口. 還有p2口有3個(gè)按鍵 詳見(jiàn)以下程序的定義:
#include<reg52.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
uchar code SEG7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
sbit D=P3^6;
sbit V=P3^7;
uchar ACT[4]={0xfe,0xfd,0xfb,0xf7};
//=====================
sbit P2_4=P2^4;
sbit P2_5=P2^5;
sbit P2_6=P2^6;
int c,f;
int temp;
uchar status;
//==============
void key_s1(void);
void key_s2(void);
void key_s4(void);
int conv(int fin);
void delay_1ms(void);
//====================
void main(void)
{int temp_f,temp_c;
while(1)
{
key_s1();
key_s2();
key_s4();
if(status==0)
{
if(f<0)
{temp_f=abs(f);
D=1;P0=SEG7[temp_f%10];D=0;P0=0xff;V=1;P0=ACT[3];V=0;delay_1ms();
D=1;P0=SEG7[(temp_f%100)/10];D=0;P0=0xff;V=1;P0=ACT[2];V=0;delay_1ms();
D=1;P0=SEG7[(temp_f/100)%10];D=0;P0=0xff;V=1;P0=ACT[1];V=0;delay_1ms();
D=1;P0=0x40;D=0;P0=0xff;V=1;P0=ACT[0];V=0;delay_1ms();
}
else
{
D=1;P0=SEG7[f%10];D=0;P0=0xff;V=1;P0=ACT[3];V=0;delay_1ms();
D=1;P0=SEG7[(f%100)/10];D=0;P0=0xff;V=1;P0=ACT[2];V=0;delay_1ms();
D=1;P0=SEG7[(f/100)%10];D=0;P0=0xff;V=1;P0=ACT[1];V=0;delay_1ms();
}
}
else
{
c=conv(f);
if(c<0)
{temp_c=abs(c);
D=1;P0=SEG7[temp_c%10];D=0;P0=0xff;V=1;P0=ACT[3];V=0;delay_1ms();
D=1;P0=SEG7[(temp_c%100)/10];D=0;P0=0xff;V=1;P0=ACT[2];V=0;delay_1ms();
D=1;P0=SEG7[(temp_c/100)%10];D=0;P0=0xff;V=1;P0=ACT[1];V=0;delay_1ms();
D=1;P0=0x40;D=0;P0=0xff;V=1;P0=ACT[0];V=0;delay_1ms();
}
else
{
D=1;P0=SEG7[c%10];D=0;P0=0xff;V=1;P0=ACT[3];V=0;delay_1ms();
D=1;P0=SEG7[(c%100)/10];D=0;P0=0xff;V=1;P0=ACT[2];V=0;delay_1ms();
D=1;P0=SEG7[(c/100)%10];D=0;P0=0xff;V=1;P0=ACT[1];V=0;delay_1ms();
}
}
}
}
//================
void key_s1(void)
{
if(!P2_4)
{if(temp>50)temp=0;
if(temp==0)f++;
if(f>999)f=999;
temp++;
}
}
//===============
void key_s2(void)
{
if(!P2_5)
{if(temp>50)temp=0;
if(temp==0)f--;
if(f<-500)f=-500;
temp++;
}
}
//============
void key_s4(void)
{
if(!P2_6)status=1;
else status=0;
}
//===========
int conv(int fin)
{int ddata;
ddata=fin-32;
ddata=(ddata*5)/9;
return ddata;
}
//============
void delay_1ms(void)
{
uint k;
for(k=0;k<121;k++);
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1