标题:
分享:LCD1602温度检测显示程序(ATmega16)
[打印本页]
作者:
HI-XM
时间:
2018-12-17 16:50
标题:
分享:LCD1602温度检测显示程序(ATmega16)
LCD1602温度检测显示程序(ATmega16)
单片机源程序如下:
//**********************************************************************//
//************************* 头文件定义 **********************//
//**********************************************************************//
#include<iom16v.h>
#include <macros.h>
//宏定义
#define uchar unsigned char
#define uint unsigned int
//温度18b20(数据线端口)
#define tmp (PINB&BIT(PB3))
#define temp_h PORTB |= BIT(PB3)
#define temp_l PORTB &=~BIT(PB3)
#define temp_o DDRB |= BIT(PB3)
#define temp_i DDRB &=~BIT(PB3)
//LCD1602液晶显示(数据线端口)
#define rs_h PORTB |= BIT(PB0)//数据/命令选择
#define rs_l PORTB &=~BIT(PB0)
#define rw_h PORTB |= BIT(PB1)//读/写选择
#define rw_l PORTB &=~BIT(PB1)
#define en_h PORTB |= BIT(PB2)//使能信号
#define en_l PORTB &=~BIT(PB2)
//温度18b20(变量定义)
unsigned char dat1=0x00;//保存读出的温度 L
unsigned char dat2=0x00;//保存读出的温度 H
unsigned long int dat=0;//保存读出的温度 XS
unsigned char flag=0;//错误标志位
//按键定义
unsigned char key1=0;
unsigned char key2=0;
//unsigned char key3=0;
//unsigned char key4=0;
//返回值变量
unsigned char keyvalue=0;
//温度H
unsigned char tempH=30;
//温度L
unsigned char tempL=20;
//**********************************************************************//
//************************* IO 端口定义 **********************//
//**********************************************************************//
void IO_init(void)
{
DDRA = 0XFF;
DDRB = 0XF0;
DDRC = 0XFF;
DDRD = 0XFF;
PORTA = 0X00;
PORTB = 0X00;
PORTC = 0XFF;
PORTD = 0XFF;
}
//**********************************************************************//
//************************* 延时函数 **********************//
//**********************************************************************//
void delayms(uint z) //8M晶振下,延时1ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=1333;y>0;y--);
}
//**********************************************************************//
//*************************** 18B20 ************************//
//**********************************************************************//
void Ds18b20_reset(void)//DS18B20初始化
{
uint count;
temp_o;
temp_l;
for(count=700;count>0;count--);//延时480us
temp_h;
temp_i;//不须配置PORT内部上拉电阻,MCU输入输出自动切换
while((tmp==0x08));//&&(i>0)) i--;
for(count=700;count>0;count--);//延时480us
}
void Ds18b20_write(uchar dat)//向DS18B20写一个字节
{
uchar count;
uchar i;
temp_o;
for(i=8;i>0;i--)
{
temp_l;
for(count=2;count>0;count--);
//temp_h;//不能有此语句
if(dat&0x01==0x01)
temp_h;
else
temp_l;
for(count=120;count>0;count--);//延时60us
temp_h;
dat>>=1;
}
}
uchar Ds18b20_read(void)//从DS18B20读一个字节
{
uchar i,datt;
uchar count;
for(i=8;i>0;i--)
{
datt>>=1;
temp_o;
temp_l;
for(count=2;count>0;count--);
temp_h;//此语句必须有,参考datasheet的P15
for(count=1;count>0;count--);
temp_i;
if(tmp==0x08)
datt|=0x80;
for(count=120;count>0;count--); //延时60us
}
return datt;
}
void temp_Read(void)//温度读取
{
Ds18b20_reset();//DS18B20初始化
Ds18b20_write(0xcc);//跳过ROM
Ds18b20_write(0x44);//发送温度转换命令
delayms(1000);//延时1s,等待温度转换完成
Ds18b20_reset();//DS18B20初始化
Ds18b20_write(0xcc);//跳过ROM
Ds18b20_write(0xbe);//发送读温度寄存器命令
dat1=Ds18b20_read();//读温度值的低字节
dat2=Ds18b20_read();//读温度值的高字节
}
void temp_display(void)//温度显示
{
if(dat2>=240)//dat2温度值的高字节为1时为负温度
{
dat=(~(dat2*256+dat1)+1)*0.625;//负温度:取反加一,保留一位小数
flag=1;
}
else
{
dat=(dat2*256+dat1)*0.625;
flag=0;
}
if(flag==1)//负温度显示
{
LCD_write_str(0,0," 18B20 ");
LCD_write_str(3,1,"Temp:");
LCD_write_str(8,1,"-");// 符号“- ”
LCD_write_char(9,1,0x30+dat/1000);
LCD_write_char(10,1,0x30+dat%1000/100);
LCD_write_char(11,1,0x30+dat%100/10);
LCD_write_str (12,1,".");// 符号“. ”
LCD_write_char(13,1,0x30+dat%10);
}
if(flag==0)//正温度显示
{
LCD_write_str(0,0," 18B20 ");
LCD_write_str(3,1,"Temp:");
LCD_write_str(8,1," ");// 符号“+ ”
LCD_write_char(9,1,0x30+dat/1000);
LCD_write_char(10,1,0x30+dat%1000/100);
LCD_write_char(11,1,0x30+dat%100/10);
LCD_write_str (12,1,".");// 符号“. ”
LCD_write_char(13,1,0x30+dat%10);
}
}
void tempH_Setting(void)//最高温度设置显示
{
LCD_write_str(0,0," temp(H)Setting ");
LCD_write_char(6,1,0x30+tempH%1000/100);
LCD_write_char(7,1,0x30+tempH%100/10);
LCD_write_char(8,1,0x30+tempH%10);
}
void tempL_Setting(void)//最低温度设置显示
{
LCD_write_str(0,0," temp(L)Setting ");
LCD_write_char(6,1,0x30+tempL%1000/100);
LCD_write_char(7,1,0x30+tempL%100/10);
LCD_write_char(8,1,0x30+tempL%10);
}
void temp_police(void)//温度报警
{
if(dat/10>=tempH)//最高检测温度>=设定温度:灯亮
{
PORTC&=~BIT(7);
}
else
{
PORTC|= BIT(7);
}
if(dat/10<=tempL)//最低检测温度<=设定温度:灯亮
{
PORTC&=~BIT(6);
}
else
{
PORTC|= BIT(6);
}
}
//**********************************************************************//
//*************************** LCD1602 ************************//
//**********************************************************************//
void LCD_init(void)//LCD显示屏初始化函数
{
DDRA = 0xFF; //I/O口方向设置
DDRB|=BIT(PB0)|BIT(PB1)|BIT(PB2);
delayms(15); //上电延时一段时间,使供电稳定
Write_Instruction(0x38); //8bit interface,2line,5*7dots
delayms(5);
Write_Instruction(0x38);
delayms(5);
Write_Instruction(0x38);
Write_Instruction(0x08); //关显示,不显光标,光标不闪烁
Write_Instruction(0x01); //清屏
delayms(5);
Write_Instruction(0x04); //写一字符,整屏显示不移动
//Write_Instruction(0x05); //写一字符,整屏右移
//Write_Instruction(0x06); //写一字符,整屏显示不移动
//Write_Instruction(0x07); //写一字符,整屏左移
delayms(5);
//Write_Instruction(0x0B); //关闭显示(不显示字符,只有背光亮)
Write_Instruction(0x0C); //开显示,光标、闪烁都关闭
//Write_Instruction(0x0D); //开显示,不显示光标,但光标闪烁
//Write_Instruction(0x0E); //开显示,显示光标,但光标不闪烁
//Write_Instruction(0x0F); //开显示,光标、闪烁均显示
}
void lcd_en(void) //en端产生一个高电平脉冲,控制LCD写时序
{
delayms(1);
en_h;
delayms(1);
en_l;
}
void LCD_clear(void)//清屏函数
{
Write_Instruction(0x01);
delayms(5);
}
void Write_Instruction(uchar com)//向LCD1602写命令
{
rs_l;
rw_l;
en_h;
PORTA=com;
lcd_en();//写入命令
}
void lcd_dat(uchar dat)//向LCD1602写数据
{
rs_h;
rw_l;
en_h;
PORTA=dat;
lcd_en();//写入数据
}
void LCD_SET_XY(uchar X,uchar Y)//字符显示初始地址设置
{
uchar address;
if(Y==0)
address=0x80+X;//Y=0,表示在第一行显示,地址基数为0x80
else
address=0xc0+X;//Y非0时,表时在第二行显示,地址基数为0XC0
Write_Instruction(address);//写指令,设置显示初始地址
}
void LCD_write_str(uchar X,uchar Y,uchar *s)//在第X行Y列开始显示,指针*S所指向的字符串
{
LCD_SET_XY(X,Y);//设置初始字符显示地址
while(*s)//逐次写入显示字符,直到最后一个字符"/0"
{
lcd_dat(*s);//写入当前字符并显示
s++;//地址指针加1,指向下一个待写字符
}
}
void LCD_write_char(uchar X,uchar Y,uchar Wdata)//在第X行Y列开始显示Wdata所对应的单个字符
{
LCD_SET_XY(X,Y);//写地址
lcd_dat(Wdata);//写入当前字符并显示
}
void LCD_Start(void)//LCD启动界面
{
LCD_write_str(0,0," 18B20 ");
LCD_write_str(0,1," temp display ");
}
//**********************************************************************//
//*************************** 按键扫描 ************************//
//**********************************************************************//
uchar keys(void)
{
if(!(PIND&BIT(0)))//温度显示
{
delayms(20);
if(!(PIND&BIT(0)))
{
LCD_clear();//LCD清屏
keyvalue=0;
while(!(PIND&BIT(0)));//等待按键抬起
}
}
if(!(PIND&BIT(1)))//最高、最低温度设置选择
{
delayms(20);
if(!(PIND&BIT(1)))
{
LCD_clear();//LCD清屏
key1=key1+1;
switch(key1)
{
case 1:
tempH_Setting();//最高温度设置显示
keyvalue=1;//按键最高温度返回值:1
break;
case 2:
tempL_Setting();//最低温度设置显示
keyvalue=2;//按键最低温度返回值:2
key1=0;//按键清零
break;
}
while(!(PIND&BIT(1)));//等待按键抬起
}
}
if(!(PIND&BIT(2)))//温度加
{
delayms(20);
if(!(PIND&BIT(2)))
{
if(keyvalue==1)
{
tempH++;
if(tempH>=100)
{
tempH=100;
}
tempH_Setting();//最高温度设置显示
}
else if(keyvalue==2)
{
tempL++;
if(tempL>=100)
{
tempL=100;
}
tempL_Setting();//最低温度设置显示
}
//while(!(PIND&BIT(2)));//等待按键抬起
}
}
if(!(PIND&BIT(3)))//温度减
{
delayms(20);
if(!(PIND&BIT(3)))
{
if(keyvalue==1)
{
tempH--;
if(tempH<=10)
{
tempH=10;
}
tempH_Setting();//最高温度设置显示
}
else if(keyvalue==2)
{
tempL--;
if(tempL<=10)
{
tempL=10;
}
tempL_Setting();//最低温度设置显示
}
//while(!(PIND&BIT(3)));//等待按键抬起
}
}
}
//**********************************************************************//
//*************************** 主函数 ************************//
//**********************************************************************//
void main(void)
{
uchar key_j;
IO_init(); //端口初始化
LCD_init(); //LCD初始化
LCD_clear();//LCD清屏
//LCD_Start();//LCD启动界面
while(1)
{
keys();
if(keyvalue==0)
{
temp_Read();//温度读取
temp_display();//温度显示
temp_police();//温度报警
}
}
}
复制代码
所有资料51hei提供下载:
LCD1602&DS18B20温度测试.zip
(254.85 KB, 下载次数: 89)
2018-12-17 16:49 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
哈哈哈哈哈哈看
时间:
2019-6-10 22:50
没有.h文件吗
作者:
哈哈哈哈哈哈看
时间:
2019-6-13 17:36
熔丝位怎么设置啊
作者:
W_Cartman
时间:
2019-7-18 15:05
谢谢楼主分享!
作者:
1030370987
时间:
2019-9-24 11:09
谢谢大佬的帮助!!!感谢大佬!!!
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1