一、题目来源
我的题目说明是来自这个博主的博客:(感谢)https://blog.csdn.net/fanjufei123456/article/details/105134291
二、代码实现
main.c文件内容
#include <STC15F2K60S2.H>
#include "port.h"
#include "key.h"
#include "onewire.h"
unsigned char code smg_duan[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40};
unsigned char code smg_wei[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char menu[][8] = {{11, 0, 11, 10, 10, 10, 0, 0}, {11, 10, 10, 10, 10, 11, 10, 10}};
unsigned char oper_mode, disp_index, Tmax = 30, Tmin = 20, temperature, range, input[4], input_cnt;
unsigned char key_val;
unsigned int timer2_count;
bit keyflag, tick_200ms;
void Timer2Init(void) //1000微秒@11.0592MHz
{
AUXR |= 0x04; //定时器时钟1T模式
T2L = 0xCD; //设置定时初值
T2H = 0xD4; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
IE2 |= 0x04; //开定时器2中断
}
//中断服务程序
void t2int() interrupt 12 //中断入口
{
static unsigned char i;
LED_Com(0x00);
LED_Bit(smg_wei[i]);
LED_Com(smg_duan[menu[disp_index][i]]);
if(++i == 8)
i = 0;
if(timer2_count % 20 == 0)
{
key_val = KeyScan();
keyflag = 1;
}
if(timer2_count % 200 == 0)
tick_200ms = 1;
if(++timer2_count == 1000)
{
timer2_count = 0;
}
}
void Set_clear(void)
{
unsigned char i;
for(i = 0; i < 4; ++i)
input[i] = 10;
input_cnt = 0;
menu[1][1] = 10;
menu[1][2] = 10;
menu[1][6] = 10;
menu[1][7] = 10;
}
void main(void)
{
unsigned char tmp_max, tmp_min, cnt_200ms;
bit led_800ms, led_400ms, led_200ms;
Relay(0);
Buzzer(0);
LED(0x00);
Timer2Init();
EA = 1;
Gettemperature();
while(1)
{
if(keyflag)
{
keyflag = 0;
if(0 <= key_val && key_val <= 9 && oper_mode == 1 && input_cnt < 4)
input[input_cnt++] = key_val;
else if(key_val == 10)
{
if(oper_mode == 0)
{
oper_mode = 1;
disp_index = 1;
Set_clear();
}
else if(oper_mode == 1)
{
if(input_cnt == 4)
{
tmp_max = input[0]*10 + input[1];
tmp_min = input[2]*10 + input[3];
if(tmp_max <= tmp_min)
LED(0x02);//L2常亮
else
{
oper_mode = 0;
disp_index = 0;
Tmax = tmp_max;
Tmin = tmp_min;
Set_clear();
}
}
else
{
oper_mode = 0;
disp_index = 0;
Set_clear();
}
}
else if(oper_mode == 1 && input_cnt < 4)
{
}
}
else if(key_val == 11 && oper_mode == 1)//清除键
{
Set_clear();
LED(0x00);
}
}
if(tick_200ms)
{
tick_200ms = 0;
if(oper_mode == 0)//温度显示模式
{
temperature = Gettemperature();
++cnt_200ms;
if(temperature < Tmin)
{
range = 0;
Relay(0);
led_800ms = 1;
LED(cnt_200ms/2%2);
}
else if(temperature > Tmax)
{
Relay(1);
led_200ms = 1;
range = 2;
LED(cnt_200ms/8%2);
}
else if(Tmin <= temperature && temperature <= Tmax)
{
range = 1;
Relay(0);
led_400ms = 1;
LED(cnt_200ms/4%2);
}
menu[0][1] = range;
menu[0][6] = temperature / 10;
menu[0][7] = temperature % 10;
}
else if(oper_mode == 1)//温度上下限设置模式
{
menu[1][1] = input[0];
menu[1][2] = input[1];
menu[1][6] = input[2];
menu[1][7] = input[3];
}
}
}
}
port.c文件内容
#include <STC15F2K60S2.H>
#include <intrins.h>
#include "port.h"
unsigned char bdata control;
sbit relay = control^4;
sbit buzzer = control^6;
void Relay(unsigned char k)
{
relay = k;
P0 = control;
P2 = 0xa0;
_nop_();
P2 = 0x00;
}
void Buzzer(unsigned char k)
{
buzzer = k;
P0 = control;
P2 = 0xa0;
_nop_();
P2 = 0x00;
}
void LED(unsigned char k)
{
P0 = ~k;
P2 = 0x80;
_nop_();
P2 = 0x00;
}
void LED_Bit(unsigned char k)
{
P0 = k;
P2 = 0xc0;
_nop_();
P2 = 0x00;
}
void LED_Com(unsigned char k)
{
P0 = ~k;
P2 = 0xe0;
_nop_();
P2 = 0x00;
}
key.c文件内容
#include <STC15F2K60S2.H>
#include "key.h"
unsigned char KeyScan(void)
{
static unsigned char tmp2;
unsigned char key1, key2, readData, val, tmp1;
P44 = 1; P42 = 1; P3 = 0xf0;
if(P44 == 0)
key1 = 0x80;
else if(P42 == 0)
key1 = 0x40;
else if(P35 == 0)
key1 = 0x20;
else if(P34 == 0)
key1 = 0x10;
else
key1 = 0x00;
P44 = 0; P42 = 0; P3 = 0x0f;
key2 = ~P3 & 0x0f;
readData = key1 | key2;
tmp1 = readData &(readData ^ tmp2);
tmp2 = readData;
switch(tmp1)
{
case 0x88:val = 9;break;
case 0x84:val = 6;break;
case 0x82:val = 3;break;
case 0x81:val = 0;break;
case 0x48:val = 10;break;//设置
case 0x44:val = 7;break;
case 0x42:val = 4;break;
case 0x41:val = 1;break;
case 0x28:val = 11;break;//清除
case 0x24:val = 8;break;
case 0x22:val = 5;break;
case 0x21:val = 2;break;
default:val = 12;break;
}
return val;
}
onewire.c文件内容
/*
ԌѲ˵ķ: եПȽ֯ԌѲ
ɭݾ۷: Keil uVision 4.10
Ӳݾ۷: CT107եƬܺ؛ۏʵѵƽ̨(ҿާֱ12MHz) STC89C52RCեƬܺ
ɕ ǚ: 2011-8-9
*/
#include "reg52.h"
#include "onewire.h"
sbit DQ = P1^4; //եПޓࠚ
//եПғʱگ˽
void Delay_OneWire(unsigned int t) //STC89C52RC
{
t *= 7;
while(t--);
}
//ͨڽեПвDS18B20дһٶؖޚ
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//ՓDS18B20ׁȡһٶؖޚ
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20ʨѸԵʼۯ
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
float Gettemperature(void)
{
unsigned char th, tl;
unsigned int temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(400);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
tl = Read_DS18B20();
th = Read_DS18B20();
temp = th << 8 | tl;
return temp * 0.0625f;
}