单片机源程序如下:
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit cs=P1^2;
sbit clk=P1^0;
sbit di=P1^1;
uchar num,num1,i,a,b,c,e,f,g;
int wendu;
sbit we = P2^7; //位定义数码管位选锁存器接口
sbit du = P2^6; //位定义数码管段选锁存器接口
sbit pay_plus=P1^4;
sbit pay_minus=P1^3;
sbit confirm=P1^5;
sbit LED=P1^6;
int TimerCount=0,time_count=0;
int L_set_wendu=0;
int H_set_wendu=20;
bit confirm_scan=1;
bit pay_plus_scan=1;
bit pay_minus_scan=1;
//数码管段选表
uchar code leddata[]={
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x40, //"-"
0x00, //熄灭
0x00 //自定义
};
void delay(unsigned long x)
{
uchar y;
for(;x>0;x--)
for(y=110;y>0;y--);
}
uchar ADC0832()
{
cs=1;
clk=0;
cs=0;
di=1;
clk=1;
clk=0;
di=1;
clk=1;
clk=0;
di=0;
clk=1;
clk=0;
di=1;
for(i=0;i<8;i++)
{
clk=1;
clk=0;
num=num<<1;
num|=(uchar)di;
}
cs=1;
return num;
}
int SG=0;
void Key()
{
if(confirm!=confirm_scan)
{
if(confirm==0) //下降沿
{ SG++;
// step++;
if(SG>=3)
SG=0;
}
confirm_scan=confirm;
}
}
void Timer0_Init(void)
{
TMOD |= 0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main()
{
pay_plus=1;
pay_minus=1;
confirm=1;
Timer0_Init();
while(1)
{Key();
ADC0832();
if(num<=38)
wendu=num-69;
if(num>38&&num<=70)
wendu=num-70;
if(num>70)
wendu=num-71;
if(SG==0)
{
if(wendu>=0)
{
a=wendu/100;
b=wendu%100/10;
c=wendu%10;
if(a>0)
{
P0 = 0xfd;//1111 1101 只选通第二位数码管
we = 1; //打开位选
we = 0; //关闭位选
P0 = leddata[a]; //显示2
du = 1;//打开段选
du = 0; //关闭段选
delay(2);//延时
}
if(a!=0||b>0)
{
P0 = 0xfb;// 1111 1011只选通第三位数码管
we = 1; //打开位选
we = 0; //关闭位选
P0 = leddata[b]; //显示3
du = 1;//打开段选
du = 0; //关闭段选
delay(2);//延时
}
P0 = 0xf7;//1111 0111 只选通第四位数码管
we = 1; //打开位选
we = 0; //关闭位选
P0 = leddata[c]; //显示4
du = 1;//打开段选
du = 0; //关闭段选
delay(2);//延时
}
}
if(SG==1) //设定上限报警
{
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』