#include
#include
#define uchar unsigned char
#define uint unsigned int
#define NOP _nop_()
#define tiMER0_INITIAL_VALUE 5000 //5ms定时
#define SEG_PORT P0
#define KEY_PORT P1
#define KEY_MASK 0x0f //按键掩码
#define KEY_SEARCH_STATUS 0 //查询按键状态
#define KEY_ACK_STATUS 1 //确认按键状态
#define KEY_REALEASE_STATUS 2 //释放按键状态
#define KEY1 1
#define KEY2 2
#define KEY3 3
#define KEY4 4
***it LS164_DATA=P0^4;
***it LS164_CLK=P0^5;
uchar TimeCount_200ms=0; //计数器
uchar TimeCount_1s=0;
uchar segcurposmark=0; //被选中的数码管
uchar countvalue=0;
uchar segcurposition=0; //当前选中的数码管
uchar di***uf[4]={0}; //数码管显示数据缓冲
bit Timer0_5ms_Event=0; //定时器中断事件
bit Timer0_1s_Event=0;
bit bfls=0; //闪烁标志位,即200ms中断事件
bit bset_time=0; //设置标志位
uchar code segcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x36,0x7d,0x07,0x7f,0x6f,0x40,0x46,
0x00,0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}; //数码管字型码0~9,-,-1,全灭,0.~9.
uchar code segposition[4]={0xf7,0xfb,0xfd,0xfe}; //片选数码管组
void LS164_Send(uchar byte); //74LS164发送单个字节数据 */
/*....................................子函数........................................*/
void Timer0_Init(void) // T/C初始化
{
TH0=(65536-TIMER0_INITIAL_VALUE )/256;
TL0=(65536-TIMER0_INITIAL_VALUE )%256;
TMOD=0x01;
}
void Timer0_Start(void) //启动T/C
{
ET0=1;
TR0=1;
}
void Port_Init(void) //I/O中初始化
{
P0=P1=P2=P3=0xff;
}
void DS18B20_to_DisplayBuf(void) //处理转换结果并送显示缓冲区
{
di***uf[3]=countvalue/1000%10; //显示百位
di***uf[2]=countvalue/100%10; //显示十位
di***uf[1]=countvalue/10%10; //显示个位
di***uf[0]=countvalue%10; //显示小数
}
void DS18B20_Display(void) //显示温度
{
uchar t;
SEG_PORT=0x0f; //熄灭所有数码管,防止拖尾现象
if(bset_time)
{
if(bfls) //闪烁
t=segcode[12];
else
t=segcode[di***uf[segcurposition]];
}
else
t=segcode[di***uf[segcurposition]];
LS164_Send(t);
SEG_PORT=segposition[segcurposition];
if(segcurposition>3)
segcurposition=0;
segcurposition++;
}
void LS164_Send(uchar byte)
{
uchar i;
for(i=0;i<8;i++)
{
if(byte&(1<<(7-i)))
LS164_DATA=1;
else
LS164_DATA=0;
LS164_CLK=0;
LS164_CLK=1;
}
}
uchar Key_Read(void) // 读取键值
{
static uchar keystatus=KEY_SEARCH_STATUS,keycurpress=0;
uchar keyvalue;
uchar i;
keyvalue=(~KEY_PORT)&KEY_MASK; //检测有无键按下
switch(keystatus)
{
case KEY_SEARCH_STATUS: //按键查询状态
{
if(keyvalue)
keystatus=KEY_ACK_STATUS; //下一个状态为确认状态
return(0);
}
break;
case KEY_ACK_STATUS: //按键确认状态
{
if(!keyvalue) //没有键按下
keystatus=KEY_SEARCH_STATUS;
else
{
for(i=0;i<4;i++) //搜索哪一个键按下
{
if(keyvalue&(0x01<
{
keycurpress=KEY1+i;
break;
}
}
keystatus=KEY_REALEASE_STATUS; //下一个状态为释放状态
}
return(0);
}
break;
case KEY_REALEASE_STATUS: //按键释放状态
{
if(!keyvalue)
{
keystatus=KEY_SEARCH_STATUS;
return(keycurpress);
}
else
return(0);
}
break;
default: return(0);break;
}
}
void main()
{
Port_Init();
Timer0_Init();
Timer0_Start();
DS18B20_to_DisplayBuf();
EA=1;
while(1)
{
DS18B20_to_DisplayBuf();
if(Timer0_5ms_Event)
{
Timer0_5ms_Event=0;
switch(Key_Read())
{
case KEY1:
{
bset_time=~bset_time;
segcurposmark=0;
}
break;
case KEY2:
{
if(segcurposmark>3)
segcurposmark=0;
segcurposmark++;
}
break;
case KEY3:
{
if(!bset_time)
break;
if(countvalue>=9999)
{
countvalue=0;
}
if(segcurposmark==0)
{
countvalue+=1;
}
else if(segcurposmark==1)
countvalue+=10;
else if(segcurposmark==2)
countvalue+=100;
else
countvalue+=1000;
}
break;
case KEY4:
{
if(!bset_time)
break;
if(countvalue<=0)
{
countvalue=9999;
}
if(segcurposmark==0)
{
countvalue+=1;
}
else if(segcurposmark==1)
countvalue-=10;
else if(segcurposmark==2)
countvalue-=100;
else
countvalue-=1000;
}
break;
default:break;
}
}
else if(Timer0_1s_Event)
{
Timer0_1s_Event=0;
if(!bset_time)
{
if(++countvalue>=9999)
countvalue=0;
}
}
}
}
void Timer0_IRQ(void) interrupt 1
{
TH0=(65536-TIMER0_INITIAL_VALUE )/256;
TL0=(65536-TIMER0_INITIAL_VALUE )%256;
Timer0_5ms_Event=1;
//DS18B20_to_DisplayBuf();
DS18B20_Display();
if(++TimeCount_200ms >= 40) //200ms,flash flag
{
TimeCount_200ms=0;
bfls=~bfls;
}
if(++TimeCount_1s>=200)
{
TimeCount_1s=0;
Timer0_1s_Event=1;
}
}
|