标题:
ATmeg16单片机+LCD12864温度检测显示程序(并行控制,不带字库)
[打印本页]
作者:
HI-XM
时间:
2018-12-17 16:45
标题:
ATmeg16单片机+LCD12864温度检测显示程序(并行控制,不带字库)
LCD12864温度检测显示程序(ATmega16)
制作出来的实物图如下:
捕获.PNG
(2.45 MB, 下载次数: 116)
下载附件
LCD12864
2018-12-17 16:42 上传
单片机源程序如下:
/*---------------------------------------------------------------
ATmega64并行控制不带字库的12864程序
---------------------------------------------------------------
实验内容:LCD12864
---------------------------------------------------------------
硬件连接:
LCD12864 -------- ATmega64
1.GND -------- GND
2.VCC -------- VCC
3.V0 -------- NC
4.RS(CS) -------- PB0
5.R/W(SID) -------- PB1
6.E(SCLK) -------- PB2
7.D0 -------- PA0
8.D1 -------- PA1
9.D2 -------- PA2
10.D3 -------- PA3
11.D4 -------- PA4
12.D5 -------- PA5
13.D6 -------- PA6
14.D7 -------- PA7
15.PSB -------- VCC
16.NC -------- NC
17.RST -------- VCC
18.NC -------- NC
19.LED+ -------- VCC
20.LED- -------- GND
编译烧写该程序到ATmega64
上电,如果操作正确,这时您可以看到显示的内容了
---------------------------------------------------------------*/
//头文件定义
#include<iom16v.h>
#include<macros.h>
//#include<string.h>
//#include <stdio.h>
//#include<stdlib.h>
//宏定义
#define uchar unsigned char
#define uint unsigned int
//LCD12864液晶显示(数据线端口)
#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(数据线端口)
#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)
//数组定义
/*
unsigned char dis1[]={"黄俊华,啊蠢。"};
unsigned char dis2[]={"曾志成,啊成。"};
unsigned char dis3[]={"梁毓毅,啊毓。"};
unsigned char dis4[]={"柳艺明,啊明。"};
unsigned char dis1[]={"温度检测"};
unsigned char dis2[]={"℃"};
*/
//温度18b20(变量定义)
unsigned char dat1=0x00;//保存读出的温度 L
unsigned char dat2=0x00;//保存读出的温度 H
unsigned long int dat=0;//保存读出的温度 XS
unsigned char flag=0;//错误标志位
unsigned char keyvalue=0;//返回值变量
unsigned char tempH=30;//温度H
unsigned char tempL=20;//温度L
//按键定义
unsigned char key1=0;
unsigned char key2=0;
//unsigned char key3=0;
//unsigned char key4=0;
//**********************************************************************//
//************************* IO 端口定义 **********************//
//**********************************************************************//
void IO_init(void)
{
DDRA = 0XFF;
DDRB = 0XFF;
//DDRC = 0XFF;
//DDRD = 0XFF;
//PORTA = 0X00;
//PORTB = 0X00;
//PORTC = 0X00;
//PORTD = 0X00;
}
//**********************************************************************//
//************************* 延时函数 **********************//
//**********************************************************************//
void delayms(uint z) //8M晶振下,延时1ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=1333;y>0;y--);
}
//**********************************************************************//
//************************* LCD12864 **********************//
//**********************************************************************//
void LCD_clear(void)//清屏函数
{
write_com(0x01);
delayms(5);
}
void lcd_en(void) //en端产生一个高电平脉冲,控制LCD写时序
{
delayms(5);//延时5ms
en_h;
delayms(5);//延时5ms
en_l;
}
void write_com(uchar com)//向LCD12864写命令
{
rs_l;
rw_l;
en_h;
delayms(5);//延时5ms
PORTA=com;
lcd_en();//写入命令
}
void write_dat(uchar dat)//向LCD12864写数据
{
rs_h;
rw_l;
en_h;
delayms(5);//延时5ms
PORTA=dat;
lcd_en();//写入数据
}
void LCD_init(void)//LCD显示屏初始化函数
{
write_com(0x30);//设置8位数据总线,DB7~DB0;
delayms(5);//延时5ms
write_com(0x0c);//开显示,光标不显示;
delayms(5);//延时5ms
write_com(0x01);//清屏
delayms(5);//延时5ms
}
void LCD_pos(uchar x,uchar y)//字符显示初始地址设置
{
uchar pos;
if(x==0)//第一行显示
{
x=0x80;
}
else if(x==1)//第二行显示
{
x=0x90;
}
else if(x==2)//第三行显示
{
x=0x88;
}
else if(x==3)//第四行显示
{
x=0x98;
}
pos=x+y;
write_com(pos);
}
void LCD_write_str(uchar x,uchar y,uchar *s)//在第X行Y列开始显示,指针*S所指向的字符串
{
LCD_pos(x,y);//设置初始字符显示地址
while(*s)//逐次写入显示字符,直到最后一个字符"/0"
{
write_dat(*s);//写入当前字符并显示
s++;//地址指针加1,指向下一个待写字符
}
}
void LCD_write_char(uchar x,uchar y,uchar Wdata)//在第X行Y列开始显示Wdata所对应的单个字符
{
LCD_pos(x,y);//设置初始字符显示地址
write_dat(Wdata);//写入当前字符并显示
}
//**********************************************************************//
//************************* 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_display(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();//读温度值的高字节
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 温度检测"); //字符:18B20 温度检测
LCD_write_str(1,0,"负温度:"); //字符:负温度
LCD_write_str(1,6,"℃"); //符号:℃
LCD_write_char(1,4,0x30+dat/100);
LCD_write_char(1,5,0x30+dat%100/10);
}
if(flag==0)//正温度显示
{
LCD_write_str(0,0,"18B20 温度检测"); //字符:18B20 温度检测
LCD_write_str(1,0,"正温度:"); //字符:正温度
LCD_write_str(1,6,"℃"); //符号:℃
LCD_write_char(1,4,0x30+dat/100);
LCD_write_char(1,5,0x30+dat%100/10);
}
}
void tempH_Setting(void)//最高温度设置显示
{
LCD_write_str(0,1,"18B20 (高)"); //字符:18B20 高温度设置
LCD_write_str(1,0,"温度设置:"); //字符:高温度设置
LCD_write_str(1,7,"℃"); //符号:℃
LCD_write_char(1,5,0x30+tempH%100/10);
LCD_write_char(1,6,0x30+tempH%10);
}
void tempL_Setting(void)//最低温度设置显示
{
LCD_write_str(0,1,"18B20 (低)"); //字符:18B20 低温度设置
LCD_write_str(1,0,"温度设置:"); //字符:18B20 低温度设置
LCD_write_str(1,7,"℃"); //符号:℃
LCD_write_char(1,5,0x30+tempL%100/10);
LCD_write_char(1,6,0x30+tempL%10);
}
void temp_police(void)//温度报警
{
if(dat/10>=tempH)//最高检测温度>=设定温度:D1灯亮
{
PORTC&=~BIT(0);
LCD_write_str(3,0,"D1: 亮");
}
else//反之:灯灭
{
PORTC|= BIT(0);
LCD_write_str(3,0,"D1: 灭");
}
if(dat/10<=tempL)//最低检测温度<=设定温度:D2灯亮
{
PORTC&=~BIT(1);
LCD_write_str(3,4,"D2: 亮");
}
else//反之:灯灭
{
PORTC|= BIT(1);
LCD_write_str(3,4,"D2: 灭");
}
}
//**********************************************************************//
//*************************** 按键扫描 ************************//
//**********************************************************************//
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();//最高温度设置显示
temp_police();//温度报警
//temp_display();//温度显示
keyvalue=1;//按键最高温度返回值:1
break;
case 2:
tempL_Setting();//最低温度设置显示
temp_police();//温度报警
//temp_display();//温度显示
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();//最高温度设置显示
temp_police();//温度报警
//temp_display();//温度显示
}
else if(keyvalue==2)
{
tempL++;
if(tempL>=100)
{
tempL=100;
}
tempL_Setting();//最低温度设置显示
temp_police();//温度报警
//temp_display();//温度显示
}
//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();//最高温度设置显示
temp_police();//温度报警
//temp_display();//温度显示
}
else if(keyvalue==2)
{
tempL--;
if(tempL<=10)
{
tempL=10;
}
tempL_Setting();//最低温度设置显示
temp_police();//温度报警
//temp_display();//温度显示
}
//while(!(PIND&BIT(3)));//等待按键抬起
}
}
}
//**********************************************************************//
//*************************** 主函数 ************************//
//**********************************************************************//
void main()
{
uchar i;
IO_init();//端口初始化
LCD_init();//LCD显示屏初始化函数
while(1)
{
keys();//按键扫描
if(keyvalue==0)
{
temp_display();//温度显示
temp_police();//温度报警
}
}
}
复制代码
所有资料51hei提供下载:
LCD12864(ATmeg16).zip
(58.13 KB, 下载次数: 38)
2018-12-17 16:44 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
dzc2018
时间:
2018-12-26 13:47
谢谢分享
作者:
51单片机1234
时间:
2018-12-28 16:38
谢谢楼主分享
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1