目录
①L4(参数设置错误:max>min)亮,直到下一次设置正确灭:
②按键加、减总是不对:注意jia = 0、jian = 0、flat = 0释放状态!!!
一、自己写过程中遇到的问题及解决:
①L4(参数设置错误:max>min)亮,直到下一次设置正确灭:
亮灭状态写在按键里:进入数据显示界面前判断L4状态
②按键加、减总是不对:注意jia = 0、jian = 0、flat = 0释放状态!!!
图中红框部分,如果写 if (max <= 0) max = 0; 上板子就会出错,max=0还会继续减。(没搞懂)
二、自己写的代码:(所有功能均实现)
#include "reg52.h"
#include "iic.h"
#include "onewire.h"
unsigned char code SMG_duanma[31] = {
// 0 1 2 3 4 5 6 7
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
// 8 9
0x80,0x90,//无小数点
// 0.10 1.11 2.12 3.13 4.14 5.15 6.16 7.17
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,
// 8.18 9.19
0x00,0x10,//(+10)小数点!
// A20 B21 C22 D23 E24 F25
0x88,0x80,0xc6,0xc0,0x86,0x8e,
// -26 .27 关28 U29 P30
0xbf,0x7f,0xff,0xc1,0x8c}; //共阳数码管
unsigned char yi, er, san, si, wu, liu, qi, ba;
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
unsigned int temp = 0;//温度
unsigned char max = 30; min = 20;//范围0-100
unsigned char max_flat = 0; min_flat = 0;//max,min数据缓存
unsigned char jiemian = 1;//1:(数据)显示界面 / 2:(参数)设置界面
unsigned char jia = 0;//加
unsigned char jian = 0;//减
unsigned char flat = 0;//加、减按键状态变量
unsigned char dat_set = 1;//参数切换 1:min(默认) / 2:max
unsigned char liang = 0;//1:L4亮 max<min
void InitHC138(unsigned char n)
{
switch(n)
{
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
}
}
void Delayms(unsigned int ms)
{
unsigned int i, j;
for (i = ms; i > 0; i--)
for (j = 845; j > 0; j--);
}
/*======================初始化系统==================================================
=================================================================================*/
void InitSystem()
{
InitHC138(4);
P0 = 0xff;
InitHC138(5);
P0 = 0x00;
}
/*======================数码管======================================================
=================================================================================*/
void SMG_Bit(unsigned pos, unsigned char dat)
{
InitHC138(7);
P0 = 0xff;//消影
InitHC138(6);
P0 = 0x01 << pos;
InitHC138(7);
P0 = dat;
}
void SMG_All(unsigned char dat)
{
InitHC138(6);
P0 = 0xff;
InitHC138(7);
P0 = dat;
}
void ShowSMG()
{
SMG_Bit(0, SMG_duanma[yi]);
Delayms(1);
SMG_Bit(1, SMG_duanma[er]);
Delayms(1);
SMG_Bit(2, SMG_duanma[san]);
Delayms(1);
SMG_Bit(3, SMG_duanma[si]);
Delayms(1);
SMG_Bit(4, SMG_duanma[wu]);
Delayms(1);
SMG_Bit(5, SMG_duanma[liu]);
Delayms(1);
SMG_Bit(6, SMG_duanma[qi]);
Delayms(1);
SMG_Bit(7, SMG_duanma[ba]);
Delayms(1);
SMG_All(0xff);//最后一位数码管太亮
}
/*======================按键========================================================
S4:“界面切换” (上电默认数据显示界面)
①(数据)显示界面(进入:max>min,否则自动恢复之前的有效参数,进入数据界面)
②(参数)设置界面(进入:参数默认选择min)
S5:“参数切换” 切换选择温度max 和温度min
S6:“加” 当前选择的温度参数 +1 (仅在参数设置界面(jiemian == 2)有效、注意范围)
S7:“减” 当前选择的温度参数 -1 (仅在参数设置界面有效、注意范围)
=================================================================================*/
void ScanKeys()
{
if (S7 == 0)//“减”
{
Delayms(5);
if (S7 == 0)
{
if (jiemian == 2)
{
if ((jian == 0) && (flat == 0))
{
flat = 1;
jian = 1;
}
}
}
while(!S7)
{
ShowSMG();
}
}
else if (S6 == 0)//“加”
{
Delayms(5);
if (S6 == 0)
{
if (jiemian == 2)
{
if ((jia == 0) && (flat == 0))
{
flat = 1;
jia = 1;
}
}
}
while(!S6)
{
ShowSMG();
}
}
else if (S5 == 0)//“参数切换”
{
Delayms(5);
if (S5 == 0)
{
if (jiemian == 2)
{
if (dat_set == 1) dat_set = 2;//max
else if (dat_set == 2) dat_set = 1;//min
}
}
while(!S5)
{
ShowSMG();
}
}
else if (S4 == 0)//“界面切换”
{
Delayms(5);
if (S4 == 0)
{
if (jiemian == 1)
{
if (max >= min)//有效数据才缓存
{
max_flat = max; min_flat = min;//(进入设置界面前)缓存设置前的max,min
}
jiemian = 2;//进入(参数)设置界面
}
else if (jiemian == 2)
{
if (max < min)//参数不合理,自动恢复设置前的有效参数
{
liang = 1;
max = max_flat; min = min_flat;
}
else if (max >= min)
{
liang = 0;
}
jiemian = 1;//进入(数据)显示界面
}
}
while(!S4)
{
ShowSMG();
}
}
}
/*======================DS18B20========================================================
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
=================================================================================*/
void ds18b20_read_temp()
{
unsigned char gao, di;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
//Delayms(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
di = Read_DS18B20();
gao = Read_DS18B20();
init_ds18b20();
temp = gao;
temp = temp << 8;
temp = temp | di;
temp = temp >> 4;//移除小数位
}
/*======================DA输出========================================================
=================================================================================*/
void DA_Out(unsigned char dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
//******************************主函数************************************************
void main()
{
InitSystem();
yi = 22; er = 28; san = 28; si = 28; wu = 28; liu = 28; qi = temp/10; ba = temp%10;
while(1)
{
ShowSMG();
ScanKeys();
ds18b20_read_temp();
//数据显示界面
if (jiemian == 1)
{
yi = 22; er = 28; san = 28; si = 28; wu = 28; liu = 28; qi = temp/10; ba = temp%10;
}
//参数设置界面(进入默认先设置min)
else if (jiemian == 2)
{
if (liang == 1)
{
InitHC138(4); P0 = 0xf7;
}
else if (liang == 0)
{
InitHC138(4); P0 = 0xff;
}
yi = 30; er = 28; san = 28; si = max/10; wu = max%10; liu = 28; qi = min/10; ba = min%10;
if (dat_set == 1)//min设置
{
if ((jian == 1) && (flat == 1))//-
{
flat = 0; jian = 0;//释放状态!!!!!
min = min - 1;
if (min <= 0) min = 0;
}
else if ((jia == 1) && (flat == 1))//+
{
flat = 0; jia = 0;//释放状态!!!!!
min = min + 1;
if (min >= 99) min = 99;
}
}
else if (dat_set == 2)//max设置
{
if ((jian == 1) && (flat == 1))//-
{
flat = 0; jian = 0;//释放状态!!!!!
if ((max > 0) && (max < 100)) max = max - 1;
}
else if ((jia == 1) && (flat == 1))//+
{
flat = 0; jia = 0;//释放状态!!!!!
max = max + 1;
if (max >= 99) max = 99;
}
}
}
//DA输出
if (jiemian == 1)
{
if (temp > max) DA_Out(204);//4V
else if ((temp >= min) && (temp <= max)) DA_Out(153);//3V
else if (temp < min) DA_Out(102);//2V
}
//LED指示灯功能
if (jiemian == 1)
{
if (liang == 0)//参数设置正确
{
if (temp > max)//L1 1111 1110
{
InitHC138(4); P0 = 0xfe;
}
else if ((temp >= min) && (temp <= max))//L2 1111 1101
{
InitHC138(4); P0 = 0xfd;
}
else if (temp < min)//L3 1111 1011
{
InitHC138(4); P0 = 0xfb;
}
}
else if (liang == 1)//L4亮(参数设置有误)
{
if (temp > max)//L1 1111 0110
{
InitHC138(4); P0 = 0xf6;
}
else if ((temp >= min) && (temp <= max))//L2 1111 0101
{
InitHC138(4); P0 = 0xf5;
}
else if (temp < min)//L3 1111 0011
{
InitHC138(4); P0 = 0xf3;
}
}
}
else if (jiemian == 2)
{
if (liang == 1)
{
InitHC138(4); P0 = 0xf7;
}
else if (liang == 0)
{
InitHC138(4); P0 = 0xff;
}
}
}
}