//LED灯有bug,懒得找,功能基本实现
#include "tim.h"
#include "key.h"
#include "seg.h"
#include "onewire.h"
#include "i2c.h"
#include "stdio.h"
unsigned char pucSeg_Buf[10]=0, pucSeg_Code[8]=' ', ucSeg_Pos;
unsigned int uiFre_Val,uiFre_Valcun;
unsigned int ulms,ulms1,ulms2,ulms5;
unsigned int uiFre_Echo;
unsigned char mode=0;//mode判断界面,mode1判断回显
bit mode1=0,mode2=0;//控制是否进入回显,mode2判断是否进入判断界面
unsigned int ya=100,yacun=0,fazhi=15;//电压
unsigned char ucKey_Dly, ucKey_Old;//储存按键判断
unsigned long ulms3=0;//储存按键时长
unsigned int te=0,tecun;//温度
unsigned long ulKey_Time=0;
unsigned char yayu=0;//电压阈值
void Key_Proc(void);
unsigned char leddo=0;
bit led1=0;
bit led=0;
void Timer0Init(void) // 16 位计数器模式
{
AUXR &= 0x7F; // 定时器时钟 12T 模式
TMOD |= 5; // 设置 16 位计数器模式
TL0 = 0; // 设置计数初值
TH0 = 0; // 设置计数初值
TF0 = 0; // 清除 TF0 标志
TR0 = 1; // 开始计数
}
void Delay100ms() //@11.0592MHz
{
unsigned char i, j, k;
i = 5;
j = 52;
k = 195;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void show()
{
int i=0;
Led_Disp(leddo);
for(;i<10;i++)
{
pucSeg_Buf[i]=' ';
}
if(mode1==0)//数据界面
{
if(mode==0)
{
leddo&=0xf0;
leddo|=(0x04);
pucSeg_Buf[0]='U';
sprintf(pucSeg_Buf+6, "%2.1f", (unsigned char)ya/255.0*5);//电压显示
}
if(mode==1)
{
leddo&=0xf0;
leddo|=(0x02);
sprintf(pucSeg_Buf, "F %5u", (unsigned int)uiFre_Val);//频率显示
}
if(mode==2)//温度
{
leddo&=0xf0;
leddo|=(0x01);
pucSeg_Buf[0]='C';
sprintf(pucSeg_Buf+3, " %04.2f", (int)te/16.0);//打入字符串,以字符形式储存
}
}
else//回显
{
leddo&=0xf0;
pucSeg_Buf[0]='H';
if(mode==0)
{
pucSeg_Buf[1]='U';
sprintf(pucSeg_Buf+6, "%2.1f", (unsigned char)yacun/255.0*5.0);//电压显示
}
if(mode==1)
{
sprintf(pucSeg_Buf+1, "F %5u", (unsigned int)uiFre_Valcun);//频率显示
}
if(mode==2)
{
te=DS18B20_Read();
pucSeg_Buf[1]='C';
sprintf(pucSeg_Buf+3, " %05.2f", (int)tecun/16.0);//打入字符串,以字符形式储存
}
}
if(mode2==1)
{
pucSeg_Buf[0]='P';
sprintf(pucSeg_Buf+6, "%2.1f", (unsigned char)fazhi/10.0);//电压显示
}
Seg_Tran(pucSeg_Buf,pucSeg_Code);
}
void cun()//key5功能
{
yacun=ya;
uiFre_Valcun=uiFre_Val;
tecun=DS18B20_Read();
write_eeprom(0x01,yacun);
Delay100ms();
write_eeprom(0x02,tecun %256);
Delay100ms();
write_eeprom(0x03,tecun/256);
Delay100ms();
write_eeprom(0x06,uiFre_Valcun & 0xff);
Delay100ms();
write_eeprom(0x07,uiFre_Valcun/256);
Delay100ms();
}
void keydo(unsigned char key_value)//按键执行
{
if(key_value==4&&mode2==0)
{
mode+=1;
mode%=3;
}
if(key_value==5)
{
cun();
}
if(key_value==6)
{
if(mode2==0)//
{
mode1=~mode1;
}
else//电压阈值界面的key6
{
if(fazhi<50)
{
fazhi+=1;
}
}
}
if(key_value==7)
{
mode2=~mode2;
}
}
void main(void)
{
Cls_Peripheral();
Timer0Init();
Timer1Init();
Delay100ms();
tecun =read_eeprom(0x03)*256;
Delay100ms();
tecun +=read_eeprom(0x02);
Delay100ms();
yacun = read_eeprom(0x01);
Delay100ms();Delay100ms();
uiFre_Valcun = (read_eeprom(0x07)*256);
Delay100ms();
uiFre_Valcun += read_eeprom(0x06);
TR1=1;
while(1)
{
Key_Proc();
if(ulms2==250)
{
Key_Read();
}
if(ulms1==10)
{
show();
}
if(ulms2==50)//温度检测
{
te=DS18B20_Read();
}
if(ulms2==150)//da
{
ya=Pcf8591_adc();
if(ya>fazhi*5)
{
led1=1;
}
else
{
led1=0;
}
}
}
}
void Time_1(void) interrupt 3
{
ulms5++;
ulms3++;
ulms2++;
ulms++;
ulms1++;
if(++ucKey_Dly == 10)
ucKey_Dly = 0;
if(ulms1>100)
{
ulms1=0;
}
if(ulms5>200)
{
if(led1==1)
{
leddo|=led;
leddo<<=7;
led=~led;
}
else
{
leddo&=0x7f;
}
ulms5=0;
}
if(ulms==1000)
{
uiFre_Val=(TH0<<8)+TL0;
TH0=0;
TL0=0;
ulms=0;
}
if(ulms2>300)
{
ulms2=0;
}
Seg_Disp(pucSeg_Code, ucSeg_Pos);
if(++ucSeg_Pos == 8) ucSeg_Pos = 0;
}
void Key_Proc(void)
{
unsigned char ucKey_Val, ucKey_Down, ucKey_Up;
if(ucKey_Dly) return;
ucKey_Dly = 1;
ucKey_Val = Key_Read();
ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val);//^按位异或
ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Old = ucKey_Val;
if(ucKey_Down)//判断old是否和新的一样,一样说明是同一个,不会进入
{
ulKey_Time = ulms3;
keydo(ucKey_Down);
}
if(ucKey_Old && (ulms3 - ulKey_Time > 800))
{
ulKey_Time = ulms3;
ulKey_Time-=400;
keydo(ucKey_Old);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)