|
发表于 2013-1-17 13:38:45
|
显示全部楼层
|
电脑可调温度控制器源程序
温度控制器v1.5
显示为三个共阳极led
温度传感器用单总线ds18b20
cpu为2051,四个按键,分别为up,down,set
温度调节上限为125度,下限为-55度
只能用于单只18b20
本软件仅供学习与参考,引用时请注明九游会棋牌的版权
http:// /> 明浩 2004/1/2
-------------------------------*/
#include
#include
#define key_up p3_0 //上调温度
#define key_down p3_1 //下调温度
#define key_set p1_7 //设定键(温度设定,长按开电源)
#define relayoutport p3_5 //继电器输出
#define ledport p1 //led控制口
#define ledonec p3_2 //led ds1控制(百位)
#define ledtwoc p3_3 //led ds2控制(十位)
#define ledthreec p3_4 //led ds3控制(个位)
#define tmport p3_7 //ds1820 dataport
unsigned char code leddis[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf}; //0-9的led笔划,0xff为空,0xf7为负号
static unsigned char bdata statereg; //可位寻址的状态寄存器
sbit ds1820on = statereg^0; //ds1820是否存在
sbit settf = statereg^1; //是否是在温度设置状态
sbit keysetdown = statereg^2; //是否已按过set键标识
sbit powtf = statereg^3; //电源电源标识
sbit keytf = statereg^4; //键盘是否允许
//sbit keysetdowning = statereg^5; //set是否正在按下
static unsigned char bdata tlv _at_ 0x0029; //温度变量高低位
static unsigned char bdata thv _at_ 0x0028;
static signed char tmv; //转换后的温度值
static unsigned char keyv,tempkeyv; //键值
static signed char tmromv _at_ 0x0027; //高温限制
static signed char tmsetv _at_ 0x0026; //温度设定值
static unsigned char ksdnum; //set键连按时的采集次数
static unsigned char intnum,intnum2,intnum3; //中断发生次数,intnum用于set长按检测,intnum2用于设定状态时led闪烁
static signed char led_one,led_two,led_three; //led的显示位 led_one为十位,led_two为个位
static unsigned char sign; //负号标识
void main(void)
{
void initds1820(void); //定义函数
void romds1820(void);
void tmvds1820(void);
void tmrds1820(void);
void tmwds1820(void);
void tmreds1820(void);
void tmerds1820(void);
void readds1820(void);
void writeds1820(void);
void delay_510(void);
void delay_110(void);
void delay_10ms(void);
void delay_4s(void);
void v2tov(void);
statereg = 0; //初始化变量
settf = 1;
powtf = 1; //关电源
thv = 0;
tlv = 0;
tmv = 0;
keyv = 0;
tempkeyv = 0;
ksdnum = 0;
intnum = 0;
intnum2 = 0;
intnum3 = 0;
led_one = 0;
led_two = 0;
initds1820(); //初始化
romds1820(); //跳过rom
tmerds1820(); //e2pram中温度上限值调入ram
initds1820(); //初始化
romds1820(); //跳过rom
tmrds1820(); //读出温度指令
readds1820(); //读出温度值和上限值
tmsetv = tmromv; //拷贝保存在ds18b20rom里的上限值到tmsetv
ea = 1; //允许cpu中断
et0 = 1; //定时器0中断打开
tmod = 0x1; //设定时器0为模式1,16位模式
th0=0xb1;
tl0=0xdf; //设定时值为20000us(20ms)
tr0 = 1; //开始定时
while(1);
}
//定时器0中断外理中键扫描和显示
void keyanddis_time0(void) interrupt 1 using 2
{
th0=0xb1;
tl0=0xdf; //设定时值为20000us(20ms)
ledport = 0xff;
if (!key_up)
keyv = 1;
if (!key_down)
keyv = 2;
if (!key_set)
keyv = 3;
//keysetdowning = 0; //清除
if (keyv != 0) //有键按下
{
delay_10ms(); //延时防抖 按下10ms再测
if (!key_up)
tempkeyv = 1;
if (!key_down)
tempkeyv = 2;
if (!key_set)
tempkeyv = 3;
if (keyv == tempkeyv) //两次值相等为确定接下了键
{
if (keyv == 3) //按下set键,如在set状态就退出,否则进入
{
//keysetdowning = 1; //表明set正在按下
powtf = 0; //电源标识开
if (!keytf)
if (settf)
{
settf = 0; //标识位标识退出设定
initds1820(); //初始化
romds1820(); //跳过rom
tmwds1820(); //写温度上限指令
writeds1820(); //写温度上限到ds18b20rom
writeds1820(); //写温度上限到ds18b20rom
writeds1820(); //写温度上限到ds18b20rom
initds1820(); //初始化
romds1820(); //跳过rom
tmreds1820(); //温度上限值copy回e2pram
}
else
settf = 1;
if (!keysetdown) //没有第一次按下set时,keysetdown标识置1
keysetdown = 1;
else
ksdnum = ksdnum + 1; //前一秒内有按过set则开始计数
}
if (settf) //在set状态下
{
if ((keyv == 1) && (!keytf))
tmsetv = tmsetv + 1; //上调温度
if ((keyv == 2) && (!keytf))
tmsetv = tmsetv - 1; //下调温度
if (tmsetv <= -55) //限制温度上下限
tmsetv = -55;
if (tmsetv >= 125)
tmsetv = 125;
}
if ((!keytf) && (intnum3 == 0)) keytf = 1; //当键盘处于可用时,锁定
}
if (keysetdown) //在2秒内按下了set则计中断发生次数用于长按set时计时用
intnum = intnum + 1;
if (intnum > 55) //中断发生了55次时(大约1.2秒)75为1.5秒左右
{
intnum = 0;
keysetdown = 0;
if (ksdnum == 55) //如一直长按了set1.2秒左右
{
relayoutport = 1; //关闭继电器输出
powtf = 1; //电源标识关
ledonec = 0;
ledtwoc = 0;
ledthreec = 0;
ledport = 0xbf; //显示"--"
delay_4s(); //延时
ledonec = 1;
ledtwoc = 1; //关显示
ledthreec = 1;
delay_4s();
intnum = 0;
intnum2 = 0;
intnum3 = 0;
}
ksdnum = 0;
}
}
keyv = 0;
tempkeyv = 0; //清空变量准备下次键扫描
if (!powtf)
{
initds1820(); //初始化
romds1820(); //跳过rom
tmvds1820(); //温度转换指令
delay_510();
delay_510(); //延时等待转换完成
initds1820(); //初始化
romds1820(); //跳过rom
tmrds1820(); //读出温度指令
readds1820(); //读出温度值
v2tov(); //转换显示值
if (tmv > tmsetv) //根据采集到的温度值控制继电器
{
relayoutport = 0;
}
else
{
relayoutport = 1;
}
该贴已经同步到 |
, , ,
|