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
|