LCD1602程序
掃描二維碼
隨時隨地手機看文章
配置:
ATMega16L@3.3V@7.3728MHz
1602BLCD@5.0V@6lines
Compiler:WinAVR20060125
我自己搭的萬用板,1602用5V供電,M16用3.3V供電。為了使LCD盡快投入運轉(zhuǎn),同時也因為懶惰...便參考了網(wǎng)站上很多的1602的帖子,包括網(wǎng)站收集帖,程序下載后稍加修改,編譯通過,下載到M16中,總是沒有反應(yīng)。
在這個過程中,我確信我的1602硬件是沒有問題的,因為我手頭上有個以前用51做的時間溫度計(1602+18B20),放到上面都能正常顯示。
整個過程中,感覺不管是網(wǎng)站收集的精華帖,還是普通帖子里面的1602程序,冗長繁雜...比如我在一個帖子中,看到的一個1602的初始化函數(shù),居然用了10幾行代碼!我在51中,不過也就4句而已啊...
后來,遇到了下面的程序,簡明扼要,一次成功。當然,這也是從論壇中COPY的,我做了一些修改:
#include
#defineLCD_EN_PORTPORTC
#defineLCD_RW_PORTPORTC
#defineLCD_RS_PORTPORTC
#defineLCD_DATA_PORTPORTA
#defineLCD_DATA_DDRDDRA
#defineLCD_DATA_PINPINA
//LCD的r/w腳直接接GND
#defineLCD_EN0x80//portd7out
#defineLCD_RS0x40//portc6out
#defineLCD_DATA0xF0//porta4/5/6/7out
/*--------------------------------------------------------------------------------------------------
PublICfunctionprototypes
--------------------------------------------------------------------------------------------------*/
voidLCD_init(void);
voidLCD_en_write(void);
voidLCD_write_char(unsignedcommand,unsigneddata);
voidLCD_set_xy(unsignedcharx,unsignedchary);
voidLCD_write_string(unsignedcharX,unsignedcharY,unsignedchar*s);
voiddelay_nus(unsignedintn);
voiddelay_nms(unsignedintn);
voidLCD_init(void)//液晶初始化
{
delay_nms(15);
DDRA|=LCD_DATA;//數(shù)據(jù)為輸出
DDRC|=LCD_RS|LCD_EN;//置位RS.EN
LCD_write_char(0x28,0);//4位顯示
LCD_write_char(0x0c,0);//顯示開
LCD_write_char(0x01,0);//清屏
delay_nms(60);
}
voidLCD_write_string(unsignedcharX,unsignedcharY,unsignedchar*s)
{
LCD_set_xy(X,Y);//寫地址
while(*s){
LCD_write_char(0,*s);
s++;
}
}
voidLCD_set_xy(unsignedcharx,unsignedchary)//寫地址函數(shù)
{
unsignedcharaddress;
if(y==0)
address=0x80+x;
else
address=0xc0+x;
LCD_write_char(address,0);
}
voidLCD_en_write(void)//液晶使能
{
LCD_EN_PORT|=LCD_EN;
delay_nus(1);
LCD_EN_PORT&=~LCD_EN;
}
voidLCD_write_char(unsignedcommand,unsigneddata)//寫數(shù)據(jù)
{
unsignedcommand_temp,data_temp;
command_temp=command;
data_temp=data;
delay_nus(16);
if(command==0){
LCD_RS_PORT|=LCD_RS;//RS=1
LCD_DATA_PORT&=0X0f;
LCD_DATA_PORT|=data_temp&0xf0;//寫高四位
LCD_en_write();
data_temp=data_temp<<4;
LCD_DATA_PORT&=0X0f;
LCD_DATA_PORT|=data_temp&0xF0;//寫低四位
LCD_en_write();
}
else{
LCD_RS_PORT&=~LCD_RS;//RS=0
LCD_DATA_PORT&=0X0f;
LCD_DATA_PORT|=command_temp&0xF0;//寫高四位
LCD_en_write();
command_temp=command_temp<<4;
LCD_DATA_PORT&=0x0F;
LCD_DATA_PORT|=command_temp&0xF0;//寫低四位
LCD_en_write();
}
}
intmain(void)
{
LCD_init();
LCD_write_string(0,0,"Hello,AVRWORLD!!!");
LCD_write_string(0,1,"hitro@tom.com");
while(1);
}
/*-----------------------------------------------------------------------
延時函數(shù)
系統(tǒng)時鐘:8M
-----------------------------------------------------------------------*/
voiddelay_1us(void)//1us延時函數(shù)
{
asm("nop");
}
voiddelay_nus(unsignedintn)//Nus延時函數(shù)
{
unsignedinti=0;
for(i=0;i
}
voiddelay_1ms(void)//1ms延時函數(shù)
{
unsignedinti;
for(i=0;i<1356;i++);
}
voiddelay_nms(unsignedintn)//Nms延時函數(shù)
{
unsignedinti=0;
for(i=0;i
}