LCD12864温度检测显示程序(ATmega16)
制作出来的实物图如下:
单片机源程序如下:
/*---------------------------------------------------------------
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
#include
//#include
//#include
//#include
//宏定义
#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
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』