|
LCD12864溫度檢測(cè)顯示程序(ATmega16)
制作出來(lái)的實(shí)物圖如下:
LCD12864
單片機(jī)源程序如下:
- /*---------------------------------------------------------------
- ATmega64并行控制不帶字庫(kù)的12864程序
- ---------------------------------------------------------------
- 實(shí)驗(yàn)內(nèi)容: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
- 編譯燒寫(xiě)該程序到ATmega64
- 上電,如果操作正確,這時(shí)您可以看到顯示的內(nèi)容了
- ---------------------------------------------------------------*/
- //頭文件定義
- #include<iom16v.h>
- #include<macros.h>
- //#include<string.h>
- //#include <stdio.h>
- //#include<stdlib.h>
- //宏定義
- #define uchar unsigned char
- #define uint unsigned int
- //LCD12864液晶顯示(數(shù)據(jù)線端口)
- #define rs_h PORTB |= BIT(PB0)//數(shù)據(jù)/命令選擇
- #define rs_l PORTB &=~BIT(PB0)
- #define rw_h PORTB |= BIT(PB1)//讀/寫(xiě)選擇
- #define rw_l PORTB &=~BIT(PB1)
- #define en_h PORTB |= BIT(PB2)//使能信號(hào)
- #define en_l PORTB &=~BIT(PB2)
- //溫度18b20(數(shù)據(jù)線端口)
- #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)
- //數(shù)組定義
- /*
- unsigned char dis1[]={"黃俊華,啊蠢。"};
- unsigned char dis2[]={"曾志成,啊成。"};
- unsigned char dis3[]={"梁毓毅,啊毓。"};
- unsigned char dis4[]={"柳藝明,啊明。"};
- unsigned char dis1[]={"溫度檢測(cè)"};
- unsigned char dis2[]={"℃"};
- */
- //溫度18b20(變量定義)
- unsigned char dat1=0x00;//保存讀出的溫度 L
- unsigned char dat2=0x00;//保存讀出的溫度 H
- unsigned long int dat=0;//保存讀出的溫度 XS
- unsigned char flag=0;//錯(cuò)誤標(biāo)志位
- 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;
- }
- //**********************************************************************//
- //************************* 延時(shí)函數(shù) **********************//
- //**********************************************************************//
- void delayms(uint z) //8M晶振下,延時(shí)1ms
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=1333;y>0;y--);
- }
- //**********************************************************************//
- //************************* LCD12864 **********************//
- //**********************************************************************//
- void LCD_clear(void)//清屏函數(shù)
- {
- write_com(0x01);
- delayms(5);
- }
- void lcd_en(void) //en端產(chǎn)生一個(gè)高電平脈沖,控制LCD寫(xiě)時(shí)序
- {
- delayms(5);//延時(shí)5ms
- en_h;
- delayms(5);//延時(shí)5ms
- en_l;
- }
- void write_com(uchar com)//向LCD12864寫(xiě)命令
- {
- rs_l;
- rw_l;
- en_h;
- delayms(5);//延時(shí)5ms
- PORTA=com;
- lcd_en();//寫(xiě)入命令
- }
- void write_dat(uchar dat)//向LCD12864寫(xiě)數(shù)據(jù)
- {
- rs_h;
- rw_l;
- en_h;
- delayms(5);//延時(shí)5ms
- PORTA=dat;
- lcd_en();//寫(xiě)入數(shù)據(jù)
- }
- void LCD_init(void)//LCD顯示屏初始化函數(shù)
- {
- write_com(0x30);//設(shè)置8位數(shù)據(jù)總線,DB7~DB0;
- delayms(5);//延時(shí)5ms
- write_com(0x0c);//開(kāi)顯示,光標(biāo)不顯示;
- delayms(5);//延時(shí)5ms
- write_com(0x01);//清屏
- delayms(5);//延時(shí)5ms
- }
- void LCD_pos(uchar x,uchar y)//字符顯示初始地址設(shè)置
- {
- 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列開(kāi)始顯示,指針*S所指向的字符串
- {
- LCD_pos(x,y);//設(shè)置初始字符顯示地址
- while(*s)//逐次寫(xiě)入顯示字符,直到最后一個(gè)字符"/0"
- {
- write_dat(*s);//寫(xiě)入當(dāng)前字符并顯示
- s++;//地址指針加1,指向下一個(gè)待寫(xiě)字符
- }
- }
- void LCD_write_char(uchar x,uchar y,uchar Wdata)//在第X行Y列開(kāi)始顯示W(wǎng)data所對(duì)應(yīng)的單個(gè)字符
- {
- LCD_pos(x,y);//設(shè)置初始字符顯示地址
- write_dat(Wdata);//寫(xiě)入當(dāng)前字符并顯示
- }
- //**********************************************************************//
- //************************* 18B20 **********************//
- //**********************************************************************//
- void Ds18b20_reset(void)//DS18B20初始化
- {
- uint count;
- temp_o;
- temp_l;
- for(count=700;count>0;count--);//延時(shí)480us
- temp_h;
- temp_i;//不須配置PORT內(nèi)部上拉電阻,MCU輸入輸出自動(dòng)切換
- while((tmp==0x08));//&&(i>0)) i--;
- for(count=700;count>0;count--);//延時(shí)480us
- }
- void Ds18b20_write(uchar dat)//向DS18B20寫(xiě)一個(gè)字節(jié)
- {
- uchar count;
- uchar i;
- temp_o;
- for(i=8;i>0;i--)
- {
- temp_l;
- for(count=2;count>0;count--);
- //temp_h;//不能有此語(yǔ)句
- if(dat&0x01==0x01)
- temp_h;
- else
- temp_l;
- for(count=120;count>0;count--);//延時(shí)60us
- temp_h;
- dat>>=1;
- }
- }
- uchar Ds18b20_read(void)//從DS18B20讀一個(gè)字節(jié)
- {
- 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;//此語(yǔ)句必須有,參考datasheet的P15
- for(count=1;count>0;count--);
- temp_i;
- if(tmp==0x08)
- datt|=0x80;
- for(count=120;count>0;count--); //延時(shí)60us
- }
- return datt;
- }
- void temp_display(void)//溫度顯示
- {
- Ds18b20_reset();//DS18B20初始化
- Ds18b20_write(0xcc);//跳過(guò)ROM
- Ds18b20_write(0x44);//發(fā)送溫度轉(zhuǎn)換命令
- delayms(1000);//延時(shí)1s,等待溫度轉(zhuǎn)換完成
- Ds18b20_reset();//DS18B20初始化
- Ds18b20_write(0xcc);//跳過(guò)ROM
- Ds18b20_write(0xbe);//發(fā)送讀溫度寄存器命令
- dat1=Ds18b20_read();//讀溫度值的低字節(jié)
- dat2=Ds18b20_read();//讀溫度值的高字節(jié)
-
- if(dat2>=240)//dat2溫度值的高字節(jié)為1時(shí)為負(fù)溫度
- {
- dat=(~(dat2*256+dat1)+1)*0.625;//負(fù)溫度:取反加一,保留一位小數(shù)
- flag=1;
- }
- else
- {
- dat=(dat2*256+dat1)*0.625;
- flag=0;
- }
- /******************************************************* 正負(fù)溫度顯示 **/
- if(flag==1)//負(fù)溫度顯示
- {
- LCD_write_str(0,0,"18B20 溫度檢測(cè)"); //字符:18B20 溫度檢測(cè)
- LCD_write_str(1,0,"負(fù)溫度:"); //字符:負(fù)溫度
- LCD_write_str(1,6,"℃"); //符號(hào):℃
- 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 溫度檢測(cè)"); //字符:18B20 溫度檢測(cè)
- LCD_write_str(1,0,"正溫度:"); //字符:正溫度
- LCD_write_str(1,6,"℃"); //符號(hào):℃
- LCD_write_char(1,4,0x30+dat/100);
- LCD_write_char(1,5,0x30+dat%100/10);
- }
- }
- void tempH_Setting(void)//最高溫度設(shè)置顯示
- {
- LCD_write_str(0,1,"18B20 (高)"); //字符:18B20 高溫度設(shè)置
- LCD_write_str(1,0,"溫度設(shè)置:"); //字符:高溫度設(shè)置
- LCD_write_str(1,7,"℃"); //符號(hào):℃
- LCD_write_char(1,5,0x30+tempH%100/10);
- LCD_write_char(1,6,0x30+tempH%10);
- }
- void tempL_Setting(void)//最低溫度設(shè)置顯示
- {
- LCD_write_str(0,1,"18B20 (低)"); //字符:18B20 低溫度設(shè)置
- LCD_write_str(1,0,"溫度設(shè)置:"); //字符:18B20 低溫度設(shè)置
- LCD_write_str(1,7,"℃"); //符號(hào):℃
- LCD_write_char(1,5,0x30+tempL%100/10);
- LCD_write_char(1,6,0x30+tempL%10);
- }
- void temp_police(void)//溫度報(bào)警
- {
- if(dat/10>=tempH)//最高檢測(cè)溫度>=設(shè)定溫度:D1燈亮
- {
- PORTC&=~BIT(0);
- LCD_write_str(3,0,"D1: 亮");
- }
- else//反之:燈滅
- {
- PORTC|= BIT(0);
- LCD_write_str(3,0,"D1: 滅");
- }
-
- if(dat/10<=tempL)//最低檢測(cè)溫度<=設(shè)定溫度: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)))//最高、最低溫度設(shè)置選擇
- {
- delayms(20);
- if(!(PIND&BIT(1)))
- {
- LCD_clear();//LCD清屏
- key1=key1+1;
- switch(key1)
- {
- case 1:
- tempH_Setting();//最高溫度設(shè)置顯示
- temp_police();//溫度報(bào)警
- //temp_display();//溫度顯示
- keyvalue=1;//按鍵最高溫度返回值:1
- break;
- case 2:
- tempL_Setting();//最低溫度設(shè)置顯示
- temp_police();//溫度報(bào)警
- //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();//最高溫度設(shè)置顯示
- temp_police();//溫度報(bào)警
- //temp_display();//溫度顯示
- }
- else if(keyvalue==2)
- {
- tempL++;
- if(tempL>=100)
- {
- tempL=100;
- }
- tempL_Setting();//最低溫度設(shè)置顯示
- temp_police();//溫度報(bào)警
- //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();//最高溫度設(shè)置顯示
- temp_police();//溫度報(bào)警
- //temp_display();//溫度顯示
- }
- else if(keyvalue==2)
- {
- tempL--;
- if(tempL<=10)
- {
- tempL=10;
- }
- tempL_Setting();//最低溫度設(shè)置顯示
- temp_police();//溫度報(bào)警
- //temp_display();//溫度顯示
- }
- //while(!(PIND&BIT(3)));//等待按鍵抬起
- }
- }
- }
- //**********************************************************************//
- //*************************** 主函數(shù) ************************//
- //**********************************************************************//
- void main()
- {
- uchar i;
- IO_init();//端口初始化
- LCD_init();//LCD顯示屏初始化函數(shù)
- while(1)
- {
- keys();//按鍵掃描
- if(keyvalue==0)
- {
- temp_display();//溫度顯示
- temp_police();//溫度報(bào)警
- }
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
LCD12864(ATmeg16).zip
(58.13 KB, 下載次數(shù): 38)
2018-12-17 16:44 上傳
點(diǎn)擊文件名下載附件
|
評(píng)分
-
查看全部評(píng)分
|