各路大神,求解求解!
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
sbit DQ=P1^4;
sbit DQ1=P1^5;
sbit DQ2=P1^6;
sbit squ=P1^0;
sbit red=P1^1;
sbit green=P1^2;
sbit beep=P1^3;
sbit k1=P3^0;
sbit k2=P3^1;
sbit k3=P3^2;
sbit k4=P3^3;
sbit k5=P3^4;
sbit RS = P2^4;
sbit RW = P2^5;
sbit E = P2^6;
sbit RES = P2^1;
sbit PSB = P2^3;
sbit PAUSE = P3^0;
#define DataPort P0
bit ReadTempFlag;//定义读时间标志
unsigned char curr,currold,k5num,flag,flag1;
char displaytemp[16];//定义显示区域临时存储数组
int tempH,tempL,tempH1,tempL1,tempH2,tempL2,f;
int temp;
float temperature;
void Init_Timer(void);//定时器初始化
/*------------------------------------------------
uS延时函数,含有输入参数 unsigned char t,无返回值
unsigned char 是定义无符号字符变量,其值的范围是
0~255 这里使用晶振12M,精确延时请使用汇编,大致延时
长度如下 T=tx2+5 uS
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS延时函数,含有输入参数 unsigned char t,无返回值
unsigned char 是定义无符号字符变量,其值的范围是
0~255 这里使用晶振12M,精确延时请使用汇编
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
unsigned char code user16x16[]={ //箭头图片
0x00,0x00,0x20,0x00,0x30,0x00,0x38,0x00,0x3C,0x00,0x3E,0x00,0x3F,0x00,0x3F,0x80,
0x3F,0xC0,0x3F,0x80,0x3F,0x00,0x3E,0x00,0x3C,0x00,0x38,0x00,0x30,0x00,0x20,0x00,
};
unsigned char code *MainMenu[]=
{
{" 1.温度采集"},
{" 2.方波可调"},
};
unsigned char code pic1[]= // 图片代码 显示福州大学阳光学院
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x06,0x0C,0x00,0x04,0x00,0x80,0x02,0x90,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x02,0x30,0x04,0x44,0x00,0x80,0x02,0x20,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x04,0x04,0x44,0x00,0x80,0x00,0x24,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x02,0x7C,0x04,0x44,0x00,0x80,0x07,0xDF,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x3E,0x4C,0x05,0x54,0x00,0xBC,0x18,0x44,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x30,0x15,0x54,0x0F,0xC0,0x13,0xE0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x06,0x1F,0x14,0x44,0x00,0x80,0x00,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x0D,0xF3,0x04,0x44,0x00,0xC0,0x00,0x7E,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x14,0x9E,0x04,0x44,0x01,0x40,0x1F,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x24,0x92,0x08,0x44,0x01,0x20,0x00,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x9E,0x08,0x04,0x02,0x10,0x00,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x62,0x10,0x04,0x04,0x1C,0x00,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x04,0x08,0x02,0x01,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x00,0x01,0x80,0x05,0x20,0x00,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x3C,0x00,0x01,0x10,0x04,0x40,0x3C,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x24,0x1C,0x01,0x30,0x00,0x48,0x28,0x78,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x24,0xE8,0x05,0x40,0x0F,0xBE,0x29,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x24,0x88,0x01,0x00,0x30,0x88,0x2A,0x20,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x24,0x88,0x01,0xF8,0x27,0xC0,0x2D,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x3C,0xF8,0x1E,0x80,0x01,0x00,0x24,0x30,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x24,0x88,0x02,0x80,0x00,0xFC,0x23,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x20,0x8C,0x04,0x80,0x3F,0x80,0x20,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x20,0xF8,0x0C,0x82,0x00,0x80,0x21,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x20,0x08,0x08,0x82,0x00,0x80,0x22,0x44,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x20,0x00,0x10,0x46,0x00,0x80,0x24,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x20,0x00,0x60,0x7C,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
/*------------------------------------------------
检测忙位
------------------------------------------------*/
void Check_Busy()
{
RS=0;
RW=1;
E=1;
DataPort=0xff;
while((DataPort&0x80)==0x80);//忙则等待
E=0;
}
/*------------------------------------------------
写命令
------------------------------------------------*/
void Write_Cmd(unsigned char Cmd)
{
Check_Busy();
RS=0;
RW=0;
E=1;
DataPort=Cmd;
DelayUs2x(5);
E=0;
DelayUs2x(5);
}
/*------------------------------------------------
写数据
------------------------------------------------*/
void Write_Data(unsigned char Data)
{
Check_Busy();
RS=1;
RW=0;
E=1;
DataPort=Data;
DelayUs2x(5);
E=0;
DelayUs2x(5);
}
/*------------------------------------------------
液晶屏初始化
------------------------------------------------*/
void Init_ST7920()
{
DelayMs(40); //大于40MS的延时程序
PSB=1; //设置为8BIT并口工作模式
DelayMs(1); //延时
RES=0; //复位
DelayMs(1); //延时
RES=1; //复位置高
DelayMs(10);
Write_Cmd(0x30); //选择基本指令集
DelayUs2x(50); //延时大于100us
Write_Cmd(0x30); //选择8bit数据流
DelayUs2x(20); //延时大于37us
Write_Cmd(0x0c); //开显示(无游标、不反白)
DelayUs2x(50); //延时大于100us
Write_Cmd(0x01); //清除显示,并且设定地址指针为00H
DelayMs(15); //延时大于10ms
Write_Cmd(0x06); //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位,光标从右向左加1位移动
DelayUs2x(50); //延时大于100us
}
/*------------------------------------------------
用户自定义字符
------------------------------------------------*/
void CGRAM()
{
int i;
Write_Cmd(0x30);
Write_Cmd(0x40);
for(i=0;i<16;i++)
{
Write_Data(user16x16[i*2]);
Write_Data(user16x16[i*2+1]);
}
}
/*------------------------------------------------
显示用户自定义字符
------------------------------------------------*/
void DisplayCGRAM(unsigned char x,unsigned char y)
{
switch(y)
{
case 1: Write_Cmd(0x80+x);break;
case 2: Write_Cmd(0x90+x);break;
case 3: Write_Cmd(0x88+x);break;
case 4: Write_Cmd(0x98+x);break;
default:break;
}
Write_Data(00);
Write_Data(00);
}
/*------------------------------------------------
写入字符函数
------------------------------------------------*/
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 1)
{
Write_Cmd(0x80 + x); //表示第一行
}
else if(y==2)
{
Write_Cmd(0x90 + x); //表示第二行
}
else if(y==3)
{
Write_Cmd(0x88+x); //表示第三行
}
else if(y==4)
{
Write_Cmd(0x98+x); //表示第四行
}
Write_Data( Data);
}
/*------------------------------------------------
写入字符串函数
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 1)
{
Write_Cmd(0x80 + x); //表示第一行
}
else if(y==2)
{
Write_Cmd(0x90 + x); //表示第二行
}
else if(y==3)
{
Write_Cmd(0x88+x); //表示第三行
}
else if(y==4)
{
Write_Cmd(0x98+x); //表示第四行
}
while (*s)
{
Write_Data( *s);
s ++;
}
}
/*------------------------------------------------
显示字符串
x:横坐标值,范围0~8
y:纵坐标值,范围1~4
------------------------------------------------*/
void LCD_PutString(unsigned char x,unsigned char y,unsigned char code *s)
{
switch(y)
{
case 1: Write_Cmd(0x80+x);break;
case 2: Write_Cmd(0x90+x);break;
case 3: Write_Cmd(0x88+x);break;
case 4: Write_Cmd(0x98+x);break;
default:break;
}
while(*s>0)
{
Write_Data(*s);
s++;
DelayUs2x(50);
}
}
/*------------------------------------------------
清屏
------------------------------------------------*/
void ClrScreen()
{
Write_Cmd(0x01);
DelayMs(15);
}
/*------------------------------------------------
显示图片
------------------------------------------------*/
void LCD_PutGraphic(unsigned char code *img)
{
int i,j;
//显示上半屏内容设置
for(i=0;i<32;i++)
{
Write_Cmd(0x80 + i); //SET 垂直地址 VERTICAL ADD
Write_Cmd(0x80); //SET 水平地址 HORIZONTAL ADD
for(j=0;j<16;j++)
{
Write_Data(*img);
img++;
}
}
//显示下半屏内容设置
for(i=0;i<32;i++)
{
Write_Cmd(0x80 + i); //SET 垂直地址 VERTICAL ADD
Write_Cmd(0x88); //SET 水平地址 HORIZONTAL ADD
for(j=0;j<16;j++)
{
Write_Data(*img);
img++;
}
}
}
/*------------------------------------------------
设置到绘图模式
------------------------------------------------*/
void SetGraphicMode()
{
Write_Cmd(0x36); //选择8bit数据流 图形模式
DelayUs2x(20);
}
/*------------------------------------------------
18b20初始化
------------------------------------------------*/
bit Init_DS18B20(void)
{
bit dat=0;
DQ = 1; //DQ复位
DelayUs2x(5); //稍做延时
DQ = 0; //单片机将DQ拉低
DelayUs2x(200); //精确延时 大于 480us 小于960us
DelayUs2x(200);
DQ = 1; //拉高总线
DelayUs2x(50); //15~60us 后 接收60-240us的存在脉冲
dat=DQ; //如果x=0则初始化成功, x=1则初始化失败
DelayUs2x(25); //稍作延时返回
return dat;
}
bit Init_DS18B20_a(void)
{
bit dat=0;
DQ1 = 1; //DQ1复位
DelayUs2x(5); //稍做延时
DQ1 = 0; //单片机将DQ1拉低
DelayUs2x(200); //精确延时 大于 480us 小于960us
DelayUs2x(200);
DQ1 = 1; //拉高总线
DelayUs2x(50); //15~60us 后 接收60-240us的存在脉冲
dat=DQ1; //如果x=0则初始化成功, x=1则初始化失败
DelayUs2x(25); //稍作延时返回
return dat;
}
bit Init_DS18B20_b(void)
{
bit dat=0;
DQ2 = 1; //DQ1复位
DelayUs2x(5); //稍做延时
DQ2 = 0; //单片机将DQ1拉低
DelayUs2x(200); //精确延时 大于 480us 小于960us
DelayUs2x(200);
DQ2 = 1; //拉高总线
DelayUs2x(50); //15~60us 后 接收60-240us的存在脉冲
dat=DQ2; //如果x=0则初始化成功, x=1则初始化失败
DelayUs2x(25); //稍作延时返回
return dat;
}
/*------------------------------------------------
读取一个字节
------------------------------------------------*/
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
DelayUs2x(25);
}
return(dat);
}
unsigned char ReadOneChar_a(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ1 = 0; // 给脉冲信号
dat>>=1;
DQ1 = 1; // 给脉冲信号
if(DQ1)
dat|=0x80;
DelayUs2x(25);
}
return(dat);
}
unsigned char ReadOneChar_b(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ2 = 0; // 给脉冲信号
dat>>=1;
DQ2 = 1; // 给脉冲信号
if(DQ2)
dat|=0x80;
DelayUs2x(25);
}
return(dat);
}
/*------------------------------------------------
写入一个字节
------------------------------------------------*/
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
DelayUs2x(25);
DQ = 1;
dat>>=1;
}
DelayUs2x(25);
}
void WriteOneChar_a(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ1 = 0;
DQ1 = dat&0x01;
DelayUs2x(25);
DQ1 = 1;
dat>>=1;
}
DelayUs2x(25);
}
void WriteOneChar_b(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ2 = 0;
DQ2 = dat&0x01;
DelayUs2x(25);
DQ2 = 1;
dat>>=1;
}
DelayUs2x(25);
}
/*------------------------------------------------
读取温度
------------------------------------------------*/
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
DelayMs(10);
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar(); //低位
b=ReadOneChar(); //高位
b<<=8;
t=a+b;
return(t);
}
unsigned int ReadTemperature_a(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20_a();
WriteOneChar_a(0xCC); // 跳过读序号列号的操作
WriteOneChar_a(0x44); // 启动温度转换
DelayMs(10);
Init_DS18B20_a();
WriteOneChar_a(0xCC); //跳过读序号列号的操作
WriteOneChar_a(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar_a(); //低位
b=ReadOneChar_a(); //高位
b<<=8;
t=a+b;
return(t);
}
unsigned int ReadTemperature_b(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20_b();
WriteOneChar_b(0xCC); // 跳过读序号列号的操作
WriteOneChar_b(0x44); // 启动温度转换
DelayMs(10);
Init_DS18B20_b();
WriteOneChar_b(0xCC); //跳过读序号列号的操作
WriteOneChar_b(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar_b(); //低位
b=ReadOneChar_b(); //高位
b<<=8;
t=a+b;
return(t);
}
/*------------------------------------------------
串口通讯初始化
------------------------------------------------*/
void UART_Init(void)
{
SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重装
TH1 = 0xFD; // TH1: 重装值 9600 波特率 晶振 11.0592MHz
TR1 = 1; // TR1: timer 1 打开
//EA = 1; //打开总中断
//ES = 1; //打开串口中断
TI=1;
}
/*------------------------------------------------
调用显示更新1
------------------------------------------------*/
void DisplayUpdata(void)
{
unsigned char num;
ClrScreen();
num=sizeof(MainMenu)/sizeof(MainMenu[0]);//判断数组中数值个数
if((0+(curr/4)*4)<num)
{
LCD_PutString(0,2,MainMenu[0+(curr/4)*4]);
LCD_PutString(0,1,"----功能选择---- ");
LCD_PutString(0,4,"---------------- ");
}
else //如果超出数组最大元素,则写空信息,不判断此信息可能会出现乱码
LCD_PutString(0,2,"");
if((1+(curr/4)*4)<num)
{
LCD_PutString(0,3,MainMenu[1+(curr/4)*4]);
LCD_PutString(0,1,"----功能选择---- ");
LCD_PutString(0,4,"---------------- ");
}
else
LCD_PutString(0,3,"");
// if((2+(curr/4)*4)<num)
// {
// LCD_PutString(0,4,MainMenu[2+(curr/4)*4]);
// LCD_PutString(0,1,"----模式选择---- ");
//
// }
// else
// LCD_PutString(0,4,"");
// if((3+(curr/4)*4)<num)
// LCD_PutString(0,4,MainMenu[3+(curr/4)*4]);
// else
// LCD_PutString(0,4,"");
DisplayCGRAM(0,curr%4+1);
}
/*------------------------------------------------
按键选择函数
------------------------------------------------*/
void keyscan()
{
if(curr!=currold) //光标位置变化,则更新显示
{
DisplayUpdata();
currold=curr;
}
if(k2==0)
{
DelayMs(10);
if(k2==0)
{
red=0;
green=0;
curr=1;
TR0=0;
k5num=0;
CGRAM(); //写入自定义字符
DisplayUpdata();
LCD_PutString(0,1,"----功能选择---- ");
LCD_PutString(0,4,"---------------- ");
flag=1;
while(!k2);
}
}
if(k3==0&&flag==1) //如果检测到低电平,说明按键按下
{
DelayMs(10); //延时去抖,一般10-20ms
if(k3==0) //再次确认按键是否按下,没有按下则退出
{
while(!k3);//如果确认按下按键等待按键释放,没有释放则一直等待
if(curr<sizeof(MainMenu)/sizeof(MainMenu[0]-1))//判断数组中数值个数
{
curr++;
k5num=0;
red=0;
}
}
}
if(k4==0&&flag==1) //如果检测到低电平,说明按键按下
{
DelayMs(10); //延时去抖,一般10-20ms
if(k4==0) //再次确认按键是否按下,没有按下则退出
{
while(!k4);//如果确认按下按键等待按键释放,没有释放则一直等待
if(curr>1)
{
curr--;
k5num=0;
red=0;
}
}
}
if(k1==0&&curr==1)
{
DelayMs(10);
if(k1==0)
{
red=0;
green=0;
TR0=1;
k5num=0;
ReadTempFlag=1;
flag=0;
Write_Cmd(0x0c);
ClrScreen();
LCD_PutString(0,1,"温度");
LCD_PutString(3,1,"最高");
LCD_PutString(6,1,"最低");
LCD_PutString(2,2,"C");
LCD_PutString(2,3,"C");
LCD_PutString(2,4,"C");
// sprintf(displaytemp," %3d %3d",tempH,tempL);
// LCD_Write_String(3,2,displaytemp);//显示第二行
ReadTempFlag=0;
while(!k1);
}
}
if(k5==0&&flag==0) //选择温度调节 光标显示
{
DelayMs(50);
if(k5==0)
{
k5num++;
while(!k5);
if(k5num==1)
{
TR0=0;
Write_Cmd(0x94);
Write_Cmd(0x0f);
}
if(k5num==2)
{
Write_Cmd(0x97);
Write_Cmd(0x0f);
}
if(k5num==3)
{
Write_Cmd(0x8c);
Write_Cmd(0x0f);
}
if(k5num==4)
{
Write_Cmd(0x8f);
Write_Cmd(0x0f);
}
if(k5num==5)
{
Write_Cmd(0x9c);
Write_Cmd(0x0f);
}
if(k5num==6)
{
Write_Cmd(0x9f);
Write_Cmd(0x0f);
}
if(k5num==7)
{
k5num=0;
Write_Cmd(0x0c);
TR0=0;
}
}
}
if(k4==0&&k5num==1) //第一路温度上限增加
{
DelayMs(10);
if(k4==0&&k5num==1)
{
tempH++;
if(tempH==126)
tempH=-55;
sprintf(displaytemp," %3d %3d",tempH,tempL);
LCD_Write_String(3,2,displaytemp);
Write_Cmd(0x94);
Write_Cmd(0x0f);
while(!k4);
}
}
if(k3==0&&k5num==1) //第一路温度上限减小
{
DelayMs(10);
if(k3==0&&k5num==1)
{
tempH--;
if(tempH==-55)
tempH=126;
sprintf(displaytemp," %3d %3d",tempH,tempL);
LCD_Write_String(3,2,displaytemp);
Write_Cmd(0x94);
Write_Cmd(0x0f);
while(!k3);
}
}
if(k4==0&&k5num==2) //第一路温度下限增加
{
DelayMs(100);
if(k4==0&&k5num==2)
{
tempL++;
if(tempL==126)
tempL=-55;
sprintf(displaytemp," %3d %3d",tempH,tempL);
LCD_Write_String(3,2,displaytemp);
Write_Cmd(0x97);
Write_Cmd(0x0f);
while(!k4);
}
}
if(k3==0&&k5num==2) //第一路温度下限减小
{
DelayMs(100);
if(k3==0&&k5num==2)
{
tempL--;
if(tempL==-55)
tempL=126;
sprintf(displaytemp," %3d %3d ",tempH,tempL);
LCD_Write_String(3,2,displaytemp);
Write_Cmd(0x97);
Write_Cmd(0x0f);
while(!k4);
}
}
if(k4==0&&k5num==3) //第二路温度上限增加
{
DelayMs(10);
if(k4==0&&k5num==3)
{
tempH1++;
if(tempH1==126)
tempH1=-55;
sprintf(displaytemp," %3d %3d",tempH1,tempL1);
LCD_Write_String(3,3,displaytemp);
Write_Cmd(0x8c);
Write_Cmd(0x0f);
while(!k4);
}
}
if(k3==0&&k5num==3) //第二路温度上限减小
{
&n
#include<stdio.h>
#include<intrins.h>
sbit DQ=P1^4;
sbit DQ1=P1^5;
sbit DQ2=P1^6;
sbit squ=P1^0;
sbit red=P1^1;
sbit green=P1^2;
sbit beep=P1^3;
sbit k1=P3^0;
sbit k2=P3^1;
sbit k3=P3^2;
sbit k4=P3^3;
sbit k5=P3^4;
sbit RS = P2^4;
sbit RW = P2^5;
sbit E = P2^6;
sbit RES = P2^1;
sbit PSB = P2^3;
sbit PAUSE = P3^0;
#define DataPort P0
bit ReadTempFlag;//定义读时间标志
unsigned char curr,currold,k5num,flag,flag1;
char displaytemp[16];//定义显示区域临时存储数组
int tempH,tempL,tempH1,tempL1,tempH2,tempL2,f;
int temp;
float temperature;
void Init_Timer(void);//定时器初始化
/*------------------------------------------------
uS延时函数,含有输入参数 unsigned char t,无返回值
unsigned char 是定义无符号字符变量,其值的范围是
0~255 这里使用晶振12M,精确延时请使用汇编,大致延时
长度如下 T=tx2+5 uS
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS延时函数,含有输入参数 unsigned char t,无返回值
unsigned char 是定义无符号字符变量,其值的范围是
0~255 这里使用晶振12M,精确延时请使用汇编
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
unsigned char code user16x16[]={ //箭头图片
0x00,0x00,0x20,0x00,0x30,0x00,0x38,0x00,0x3C,0x00,0x3E,0x00,0x3F,0x00,0x3F,0x80,
0x3F,0xC0,0x3F,0x80,0x3F,0x00,0x3E,0x00,0x3C,0x00,0x38,0x00,0x30,0x00,0x20,0x00,
};
unsigned char code *MainMenu[]=
{
{" 1.温度采集"},
{" 2.方波可调"},
};
unsigned char code pic1[]= // 图片代码 显示福州大学阳光学院
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x06,0x0C,0x00,0x04,0x00,0x80,0x02,0x90,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x02,0x30,0x04,0x44,0x00,0x80,0x02,0x20,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x04,0x04,0x44,0x00,0x80,0x00,0x24,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x02,0x7C,0x04,0x44,0x00,0x80,0x07,0xDF,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x3E,0x4C,0x05,0x54,0x00,0xBC,0x18,0x44,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x30,0x15,0x54,0x0F,0xC0,0x13,0xE0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x06,0x1F,0x14,0x44,0x00,0x80,0x00,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x0D,0xF3,0x04,0x44,0x00,0xC0,0x00,0x7E,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x14,0x9E,0x04,0x44,0x01,0x40,0x1F,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x24,0x92,0x08,0x44,0x01,0x20,0x00,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x9E,0x08,0x04,0x02,0x10,0x00,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x62,0x10,0x04,0x04,0x1C,0x00,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x04,0x08,0x02,0x01,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x00,0x01,0x80,0x05,0x20,0x00,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x3C,0x00,0x01,0x10,0x04,0x40,0x3C,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x24,0x1C,0x01,0x30,0x00,0x48,0x28,0x78,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x24,0xE8,0x05,0x40,0x0F,0xBE,0x29,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x24,0x88,0x01,0x00,0x30,0x88,0x2A,0x20,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x24,0x88,0x01,0xF8,0x27,0xC0,0x2D,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x3C,0xF8,0x1E,0x80,0x01,0x00,0x24,0x30,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x24,0x88,0x02,0x80,0x00,0xFC,0x23,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x20,0x8C,0x04,0x80,0x3F,0x80,0x20,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x20,0xF8,0x0C,0x82,0x00,0x80,0x21,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x20,0x08,0x08,0x82,0x00,0x80,0x22,0x44,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x20,0x00,0x10,0x46,0x00,0x80,0x24,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x20,0x00,0x60,0x7C,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
/*------------------------------------------------
检测忙位
------------------------------------------------*/
void Check_Busy()
{
RS=0;
RW=1;
E=1;
DataPort=0xff;
while((DataPort&0x80)==0x80);//忙则等待
E=0;
}
/*------------------------------------------------
写命令
------------------------------------------------*/
void Write_Cmd(unsigned char Cmd)
{
Check_Busy();
RS=0;
RW=0;
E=1;
DataPort=Cmd;
DelayUs2x(5);
E=0;
DelayUs2x(5);
}
/*------------------------------------------------
写数据
------------------------------------------------*/
void Write_Data(unsigned char Data)
{
Check_Busy();
RS=1;
RW=0;
E=1;
DataPort=Data;
DelayUs2x(5);
E=0;
DelayUs2x(5);
}
/*------------------------------------------------
液晶屏初始化
------------------------------------------------*/
void Init_ST7920()
{
DelayMs(40); //大于40MS的延时程序
PSB=1; //设置为8BIT并口工作模式
DelayMs(1); //延时
RES=0; //复位
DelayMs(1); //延时
RES=1; //复位置高
DelayMs(10);
Write_Cmd(0x30); //选择基本指令集
DelayUs2x(50); //延时大于100us
Write_Cmd(0x30); //选择8bit数据流
DelayUs2x(20); //延时大于37us
Write_Cmd(0x0c); //开显示(无游标、不反白)
DelayUs2x(50); //延时大于100us
Write_Cmd(0x01); //清除显示,并且设定地址指针为00H
DelayMs(15); //延时大于10ms
Write_Cmd(0x06); //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位,光标从右向左加1位移动
DelayUs2x(50); //延时大于100us
}
/*------------------------------------------------
用户自定义字符
------------------------------------------------*/
void CGRAM()
{
int i;
Write_Cmd(0x30);
Write_Cmd(0x40);
for(i=0;i<16;i++)
{
Write_Data(user16x16[i*2]);
Write_Data(user16x16[i*2+1]);
}
}
/*------------------------------------------------
显示用户自定义字符
------------------------------------------------*/
void DisplayCGRAM(unsigned char x,unsigned char y)
{
switch(y)
{
case 1: Write_Cmd(0x80+x);break;
case 2: Write_Cmd(0x90+x);break;
case 3: Write_Cmd(0x88+x);break;
case 4: Write_Cmd(0x98+x);break;
default:break;
}
Write_Data(00);
Write_Data(00);
}
/*------------------------------------------------
写入字符函数
------------------------------------------------*/
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 1)
{
Write_Cmd(0x80 + x); //表示第一行
}
else if(y==2)
{
Write_Cmd(0x90 + x); //表示第二行
}
else if(y==3)
{
Write_Cmd(0x88+x); //表示第三行
}
else if(y==4)
{
Write_Cmd(0x98+x); //表示第四行
}
Write_Data( Data);
}
/*------------------------------------------------
写入字符串函数
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 1)
{
Write_Cmd(0x80 + x); //表示第一行
}
else if(y==2)
{
Write_Cmd(0x90 + x); //表示第二行
}
else if(y==3)
{
Write_Cmd(0x88+x); //表示第三行
}
else if(y==4)
{
Write_Cmd(0x98+x); //表示第四行
}
while (*s)
{
Write_Data( *s);
s ++;
}
}
/*------------------------------------------------
显示字符串
x:横坐标值,范围0~8
y:纵坐标值,范围1~4
------------------------------------------------*/
void LCD_PutString(unsigned char x,unsigned char y,unsigned char code *s)
{
switch(y)
{
case 1: Write_Cmd(0x80+x);break;
case 2: Write_Cmd(0x90+x);break;
case 3: Write_Cmd(0x88+x);break;
case 4: Write_Cmd(0x98+x);break;
default:break;
}
while(*s>0)
{
Write_Data(*s);
s++;
DelayUs2x(50);
}
}
/*------------------------------------------------
清屏
------------------------------------------------*/
void ClrScreen()
{
Write_Cmd(0x01);
DelayMs(15);
}
/*------------------------------------------------
显示图片
------------------------------------------------*/
void LCD_PutGraphic(unsigned char code *img)
{
int i,j;
//显示上半屏内容设置
for(i=0;i<32;i++)
{
Write_Cmd(0x80 + i); //SET 垂直地址 VERTICAL ADD
Write_Cmd(0x80); //SET 水平地址 HORIZONTAL ADD
for(j=0;j<16;j++)
{
Write_Data(*img);
img++;
}
}
//显示下半屏内容设置
for(i=0;i<32;i++)
{
Write_Cmd(0x80 + i); //SET 垂直地址 VERTICAL ADD
Write_Cmd(0x88); //SET 水平地址 HORIZONTAL ADD
for(j=0;j<16;j++)
{
Write_Data(*img);
img++;
}
}
}
/*------------------------------------------------
设置到绘图模式
------------------------------------------------*/
void SetGraphicMode()
{
Write_Cmd(0x36); //选择8bit数据流 图形模式
DelayUs2x(20);
}
/*------------------------------------------------
18b20初始化
------------------------------------------------*/
bit Init_DS18B20(void)
{
bit dat=0;
DQ = 1; //DQ复位
DelayUs2x(5); //稍做延时
DQ = 0; //单片机将DQ拉低
DelayUs2x(200); //精确延时 大于 480us 小于960us
DelayUs2x(200);
DQ = 1; //拉高总线
DelayUs2x(50); //15~60us 后 接收60-240us的存在脉冲
dat=DQ; //如果x=0则初始化成功, x=1则初始化失败
DelayUs2x(25); //稍作延时返回
return dat;
}
bit Init_DS18B20_a(void)
{
bit dat=0;
DQ1 = 1; //DQ1复位
DelayUs2x(5); //稍做延时
DQ1 = 0; //单片机将DQ1拉低
DelayUs2x(200); //精确延时 大于 480us 小于960us
DelayUs2x(200);
DQ1 = 1; //拉高总线
DelayUs2x(50); //15~60us 后 接收60-240us的存在脉冲
dat=DQ1; //如果x=0则初始化成功, x=1则初始化失败
DelayUs2x(25); //稍作延时返回
return dat;
}
bit Init_DS18B20_b(void)
{
bit dat=0;
DQ2 = 1; //DQ1复位
DelayUs2x(5); //稍做延时
DQ2 = 0; //单片机将DQ1拉低
DelayUs2x(200); //精确延时 大于 480us 小于960us
DelayUs2x(200);
DQ2 = 1; //拉高总线
DelayUs2x(50); //15~60us 后 接收60-240us的存在脉冲
dat=DQ2; //如果x=0则初始化成功, x=1则初始化失败
DelayUs2x(25); //稍作延时返回
return dat;
}
/*------------------------------------------------
读取一个字节
------------------------------------------------*/
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
DelayUs2x(25);
}
return(dat);
}
unsigned char ReadOneChar_a(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ1 = 0; // 给脉冲信号
dat>>=1;
DQ1 = 1; // 给脉冲信号
if(DQ1)
dat|=0x80;
DelayUs2x(25);
}
return(dat);
}
unsigned char ReadOneChar_b(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ2 = 0; // 给脉冲信号
dat>>=1;
DQ2 = 1; // 给脉冲信号
if(DQ2)
dat|=0x80;
DelayUs2x(25);
}
return(dat);
}
/*------------------------------------------------
写入一个字节
------------------------------------------------*/
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
DelayUs2x(25);
DQ = 1;
dat>>=1;
}
DelayUs2x(25);
}
void WriteOneChar_a(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ1 = 0;
DQ1 = dat&0x01;
DelayUs2x(25);
DQ1 = 1;
dat>>=1;
}
DelayUs2x(25);
}
void WriteOneChar_b(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ2 = 0;
DQ2 = dat&0x01;
DelayUs2x(25);
DQ2 = 1;
dat>>=1;
}
DelayUs2x(25);
}
/*------------------------------------------------
读取温度
------------------------------------------------*/
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
DelayMs(10);
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar(); //低位
b=ReadOneChar(); //高位
b<<=8;
t=a+b;
return(t);
}
unsigned int ReadTemperature_a(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20_a();
WriteOneChar_a(0xCC); // 跳过读序号列号的操作
WriteOneChar_a(0x44); // 启动温度转换
DelayMs(10);
Init_DS18B20_a();
WriteOneChar_a(0xCC); //跳过读序号列号的操作
WriteOneChar_a(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar_a(); //低位
b=ReadOneChar_a(); //高位
b<<=8;
t=a+b;
return(t);
}
unsigned int ReadTemperature_b(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20_b();
WriteOneChar_b(0xCC); // 跳过读序号列号的操作
WriteOneChar_b(0x44); // 启动温度转换
DelayMs(10);
Init_DS18B20_b();
WriteOneChar_b(0xCC); //跳过读序号列号的操作
WriteOneChar_b(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar_b(); //低位
b=ReadOneChar_b(); //高位
b<<=8;
t=a+b;
return(t);
}
/*------------------------------------------------
串口通讯初始化
------------------------------------------------*/
void UART_Init(void)
{
SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重装
TH1 = 0xFD; // TH1: 重装值 9600 波特率 晶振 11.0592MHz
TR1 = 1; // TR1: timer 1 打开
//EA = 1; //打开总中断
//ES = 1; //打开串口中断
TI=1;
}
/*------------------------------------------------
调用显示更新1
------------------------------------------------*/
void DisplayUpdata(void)
{
unsigned char num;
ClrScreen();
num=sizeof(MainMenu)/sizeof(MainMenu[0]);//判断数组中数值个数
if((0+(curr/4)*4)<num)
{
LCD_PutString(0,2,MainMenu[0+(curr/4)*4]);
LCD_PutString(0,1,"----功能选择---- ");
LCD_PutString(0,4,"---------------- ");
}
else //如果超出数组最大元素,则写空信息,不判断此信息可能会出现乱码
LCD_PutString(0,2,"");
if((1+(curr/4)*4)<num)
{
LCD_PutString(0,3,MainMenu[1+(curr/4)*4]);
LCD_PutString(0,1,"----功能选择---- ");
LCD_PutString(0,4,"---------------- ");
}
else
LCD_PutString(0,3,"");
// if((2+(curr/4)*4)<num)
// {
// LCD_PutString(0,4,MainMenu[2+(curr/4)*4]);
// LCD_PutString(0,1,"----模式选择---- ");
//
// }
// else
// LCD_PutString(0,4,"");
// if((3+(curr/4)*4)<num)
// LCD_PutString(0,4,MainMenu[3+(curr/4)*4]);
// else
// LCD_PutString(0,4,"");
DisplayCGRAM(0,curr%4+1);
}
/*------------------------------------------------
按键选择函数
------------------------------------------------*/
void keyscan()
{
if(curr!=currold) //光标位置变化,则更新显示
{
DisplayUpdata();
currold=curr;
}
if(k2==0)
{
DelayMs(10);
if(k2==0)
{
red=0;
green=0;
curr=1;
TR0=0;
k5num=0;
CGRAM(); //写入自定义字符
DisplayUpdata();
LCD_PutString(0,1,"----功能选择---- ");
LCD_PutString(0,4,"---------------- ");
flag=1;
while(!k2);
}
}
if(k3==0&&flag==1) //如果检测到低电平,说明按键按下
{
DelayMs(10); //延时去抖,一般10-20ms
if(k3==0) //再次确认按键是否按下,没有按下则退出
{
while(!k3);//如果确认按下按键等待按键释放,没有释放则一直等待
if(curr<sizeof(MainMenu)/sizeof(MainMenu[0]-1))//判断数组中数值个数
{
curr++;
k5num=0;
red=0;
}
}
}
if(k4==0&&flag==1) //如果检测到低电平,说明按键按下
{
DelayMs(10); //延时去抖,一般10-20ms
if(k4==0) //再次确认按键是否按下,没有按下则退出
{
while(!k4);//如果确认按下按键等待按键释放,没有释放则一直等待
if(curr>1)
{
curr--;
k5num=0;
red=0;
}
}
}
if(k1==0&&curr==1)
{
DelayMs(10);
if(k1==0)
{
red=0;
green=0;
TR0=1;
k5num=0;
ReadTempFlag=1;
flag=0;
Write_Cmd(0x0c);
ClrScreen();
LCD_PutString(0,1,"温度");
LCD_PutString(3,1,"最高");
LCD_PutString(6,1,"最低");
LCD_PutString(2,2,"C");
LCD_PutString(2,3,"C");
LCD_PutString(2,4,"C");
// sprintf(displaytemp," %3d %3d",tempH,tempL);
// LCD_Write_String(3,2,displaytemp);//显示第二行
ReadTempFlag=0;
while(!k1);
}
}
if(k5==0&&flag==0) //选择温度调节 光标显示
{
DelayMs(50);
if(k5==0)
{
k5num++;
while(!k5);
if(k5num==1)
{
TR0=0;
Write_Cmd(0x94);
Write_Cmd(0x0f);
}
if(k5num==2)
{
Write_Cmd(0x97);
Write_Cmd(0x0f);
}
if(k5num==3)
{
Write_Cmd(0x8c);
Write_Cmd(0x0f);
}
if(k5num==4)
{
Write_Cmd(0x8f);
Write_Cmd(0x0f);
}
if(k5num==5)
{
Write_Cmd(0x9c);
Write_Cmd(0x0f);
}
if(k5num==6)
{
Write_Cmd(0x9f);
Write_Cmd(0x0f);
}
if(k5num==7)
{
k5num=0;
Write_Cmd(0x0c);
TR0=0;
}
}
}
if(k4==0&&k5num==1) //第一路温度上限增加
{
DelayMs(10);
if(k4==0&&k5num==1)
{
tempH++;
if(tempH==126)
tempH=-55;
sprintf(displaytemp," %3d %3d",tempH,tempL);
LCD_Write_String(3,2,displaytemp);
Write_Cmd(0x94);
Write_Cmd(0x0f);
while(!k4);
}
}
if(k3==0&&k5num==1) //第一路温度上限减小
{
DelayMs(10);
if(k3==0&&k5num==1)
{
tempH--;
if(tempH==-55)
tempH=126;
sprintf(displaytemp," %3d %3d",tempH,tempL);
LCD_Write_String(3,2,displaytemp);
Write_Cmd(0x94);
Write_Cmd(0x0f);
while(!k3);
}
}
if(k4==0&&k5num==2) //第一路温度下限增加
{
DelayMs(100);
if(k4==0&&k5num==2)
{
tempL++;
if(tempL==126)
tempL=-55;
sprintf(displaytemp," %3d %3d",tempH,tempL);
LCD_Write_String(3,2,displaytemp);
Write_Cmd(0x97);
Write_Cmd(0x0f);
while(!k4);
}
}
if(k3==0&&k5num==2) //第一路温度下限减小
{
DelayMs(100);
if(k3==0&&k5num==2)
{
tempL--;
if(tempL==-55)
tempL=126;
sprintf(displaytemp," %3d %3d ",tempH,tempL);
LCD_Write_String(3,2,displaytemp);
Write_Cmd(0x97);
Write_Cmd(0x0f);
while(!k4);
}
}
if(k4==0&&k5num==3) //第二路温度上限增加
{
DelayMs(10);
if(k4==0&&k5num==3)
{
tempH1++;
if(tempH1==126)
tempH1=-55;
sprintf(displaytemp," %3d %3d",tempH1,tempL1);
LCD_Write_String(3,3,displaytemp);
Write_Cmd(0x8c);
Write_Cmd(0x0f);
while(!k4);
}
}
if(k3==0&&k5num==3) //第二路温度上限减小
{
&n