基于MEGA16的LCD1602驅(qū)動(dòng)程序(四位數(shù)據(jù)線)
掃描二維碼
隨時(shí)隨地手機(jī)看文章
//編譯器:ICC-AVRv6.31A
//目標(biāo)芯片:M16
//時(shí)鐘:8.0000MHz
/*-------------------------------------------------------------
LCD引腳定義
1---GND
2---VCC
3---VO
4---RS
5---RW
6---EN
7到14--D0-D7
15--背景燈+
16--背景燈-
-----------------------------------------------------------------*/
#include
#include
/*---------------------------------------------------------------
下面是AVR與LCD連接信息
PA2->RS
PA3->EN
地->RW
PA4->D4
PA5->D5
PA6->D6
PA7->D7
要使用本驅(qū)動(dòng),改變下面配置信息即可
-----------------------------------------------------------------*/
#defineLCD_EN_PORTPORTA//以下2個(gè)要設(shè)為同一個(gè)口
#defineLCD_EN_DDRDDRA
#defineLCD_RS_PORTPORTA//以下2個(gè)要設(shè)為同一個(gè)口
#defineLCD_RS_DDRDDRA
#defineLCD_DATA_PORTPORTA//以下3個(gè)要設(shè)為同一個(gè)口
#defineLCD_DATA_DDRDDRA//一定要用高4位
#defineLCD_DATA_PINPINA
#defineLCD_RS(1<
------------------------------------------------------------------------------*/
voidLCD_init(void);
voidLCD_en_write(void);
voidLCD_write_command(unsignedcharcommand);
voidLCD_write_data(unsignedchardata);
voidLCD_set_xy(unsignedcharx,unsignedchary);
voidLCD_write_string(unsignedcharX,unsignedcharY,unsignedchar*s);
voidLCD_write_char(unsignedcharX,unsignedcharY,unsignedchardata);
voiddelay_nus(unsignedintn);
voiddelay_nms(unsignedintn);
==============================================================================
#include"LCD.h"
voidLCD_init(void)//液晶初始化
{
LCD_DATA_DDR|=LCD_DATA;//數(shù)據(jù)口方向?yàn)檩敵?br/>LCD_EN_DDR|=LCD_EN;//設(shè)置EN方向?yàn)檩敵?br/>LCD_RS_DDR|=LCD_RS;//設(shè)置RS方向?yàn)檩敵?br/>LCD_write_command(0x28);
LCD_en_write();
delay_nus(40);
LCD_write_command(0x28);//4位顯示
LCD_write_command(0x0c);//顯示開
LCD_write_command(0x01);//清屏
delay_nms(2);
}
voidLCD_en_write(void)//液晶使能
{
LCD_EN_PORT|=LCD_EN;
delay_nus(1);
LCD_EN_PORT&=~LCD_EN;
}
voidLCD_write_command(unsignedcharcommand)//寫指令
{
delay_nus(16);
LCD_RS_PORT&=~LCD_RS;//RS=0
LCD_DATA_PORT&=0X0f;//清高四位
LCD_DATA_PORT|=command&0xf0;//寫高四位
LCD_en_write();
command=command<<4;//低四位移到高四位
LCD_DATA_PORT&=0x0f;//清高四位
LCD_DATA_PORT|=command&0xf0;//寫低四位
LCD_en_write();
}
voidLCD_write_data(unsignedchardata)//寫數(shù)據(jù)
{
delay_nus(16);
LCD_RS_PORT|=LCD_RS;//RS=1
LCD_DATA_PORT&=0X0f;//清高四位
LCD_DATA_PORT|=data&0xf0;//寫高四位
LCD_en_write();
data=data<<4;//低四位移到高四位
LCD_DATA_PORT&=0X0f;//清高四位
LCD_DATA_PORT|=data&0xf0;//寫低四位
LCD_en_write();
}
voidLCD_set_xy(unsignedcharx,unsignedchary)//寫地址函數(shù)
{
unsignedcharaddress;
if(y==0)address=0x80+x;
elseaddress=0xc0+x;
LCD_write_command(address);
}
voidLCD_write_string(unsignedcharX,unsignedcharY,unsignedchar*s)//列x=0~15,行y=0,1
{
LCD_set_xy(X,Y);//寫地址
while(*s)//寫顯示字符
{
LCD_write_data(*s);
s++;
}
}
voidLCD_write_char(unsignedcharX,unsignedcharY,unsignedchardata)//列x=0~15,行y=0,1
{
LCD_set_xy(X,Y);//寫地址
LCD_write_data(data);
}
=======================================================================
voiddelay_1us(void)//1us延時(shí)函數(shù)
{
asm("nop");
}
voiddelay_nus(unsignedintn)//Nus延時(shí)函數(shù)
{
unsignedinti=0;
for(i=0;i
voiddelay_1ms(void)//1ms延時(shí)函數(shù)
{
unsignedinti;
for(i=0;i<1140;i++);
}
voiddelay_nms(unsignedintn)//Nms延時(shí)函數(shù)
{
unsignedinti=0;
for(i=0;i
=========================================================================
#include"LCD.h"
voidinit_devICes(void)
{
CLI();//dISAbleallinterrupts
LCD_init();
MCUCR=0x00;
GICR=0x00;
TIMSK=0x00;//timerinterruptsources
SEI();//re-enableinterrupts
}
voidmain(void)
{
init_devices();
LCD_write_string(1,0,"Hi!!");
for(;;)
{
LCD_write_string(0,1,"archeng504");
LCD_write_char(6,0,'8');
}
}
補(bǔ)充:一個(gè)自定義圖案的程序。
1602中還可以自定義8個(gè)字符圖案
圖案規(guī)格5*8
下面是一個(gè)向上的箭頭↑
000|00100//0x04
|
000|01110//0x0e
|
000|10101//0x15
|
000|00100//0x04
|
000|00100//0x04
|
000|00100//0x04
|
000|00100//0x04
|
000|00000//0x00
unsignedchardata[8]={0x04,0x0e,0x15,0x04,0x04,0x04,0x04,0x00};//↑
unsignedchardata1[8]={0x04,0x04,0x04,0x04,0x15,0x0e,0x04,0x00};//↓
voidmy_signs(unsignedcharascii,unsignedchar*z)//0x00
{
unsignedcharaddress=0,i,temp=0;//ascii為與要定義的圖案對(duì)應(yīng)的ascii碼值(0x00到
temp=(ascii&0x07)<<3;//0x007間任選)
for(i=0;i<8;i++)
{
address=0x40+temp+i;
LCD_write_command(address);
delay_nus(1);
LCD_write_data(*(z++));
delay_nus(1);
}
}
//建立圖案
voidLCD_init(void)//液晶初始化
{
LCD_DATA_DDR|=LCD_DATA;//數(shù)據(jù)為輸出
LCD_EN_DDR|=LCD_EN;//設(shè)置RS.EN方向
LCD_RS_DDR|=LCD_RS;
LCD_write_command(0x28);//4位顯示
LCD_en_write();
delay_nms(15);
LCD_write_command(0x28);//4位顯示
LCD_write_command(0x0c);//顯示開
my_signs(0x00,data);//↑//建立圖案
my_signs(0x01,data1);//↓
LCD_write_command(0x01);//清屏
}
顯示自定義圖案
LCD_write_char(9,0,0x00);//↑
LCD_write_char(9,0,0x01);//↓