/*=======================================================
20×4字符液晶主程序,编译软件(ICCAVR_6.31)
CPU内部晶振8M
数据线B0~B7接PORTB, E=D7 RW=D6 RS=D5
=========================================================
接线图如下:
_______________ ______________
| –1|GND |
| –2|+5V |
| –3|V0 |
| | |
PD5|——–4|RS |
PD6|——–5|RW |
PD7|——–6|E |
CPU | | |
ATmmega16L PB0|——–7|D0 LCD20x4 |
PB1|——–8|D1 |
PB2|——–9|D2 |
PB3|——-10|D3 |
PB4|——-11|D4 |
PB5|——-12|D5 |
PB6|——-13|D6 |
PB7|——-14|D7 |
| | |
| –15|+LED |
| –16|-LED |
_______________| |______________|
========================================================*/
#i nclude
#define RS_H asm(“sbi 0x12,5”) //RS设为高电平
#define RS_L asm(“cbi 0x12,5”) //RS设为低电平
#define RW_H asm(“sbi 0x12,6”) //RW设为高电平
#define RW_L asm(“cbi 0x12,6”) //RW设为低电平
#define E_H asm(“sbi 0x12,7”) //E设为高电平
#define E_L asm(“cbi 0x12,7”) //E设为低电平
//=======================================================
//微秒级延时程序
void delay_us(int time)
{
do
{
time–;
}
while (time > 1);
}
//=======================================================
//毫秒级延时程序
void delay_ms(unsigned int time)
{
while(time != 0)
{
delay_us(1000);
time–;
}
}
//=======================================================
//读取lcd是否内部操作(繁忙)状况
char Lcd_Busy()
{
char r;
DDRB = 0x00; //端口B设为输入方法
E_L;RS_L;RW_H; //E=0(致能),RS=0(指令),RW=1(读)
delay_us(2); //液晶延时子程序
E_H;
delay_us(2); //液晶延时子程序
r = PINB & 0x80; //读取lcd_da
E_L;
DDRB=0xff; //端口B设为输出方法
return r; //读取成果回来
}
//=======================================================
//向Lcd发送指令程序
void Lcd_Command(unsigned char Command)
{
while(Lcd_Busy()); //判别lcd是否内部操作状况
E_L;RS_L;RW_L; //E=0(致能),RS=0(指令),RW=0(写)
delay_us(2); //液晶延时子程序
E_H;
PORTB = Command; //向Lcd发送指令
delay_us(2); //液晶延时子程序
E_L;
}
//=======================================================
//向lcd写入一个字符程序
void Lcd_Write(unsigned char Da
{
while(Lcd_Busy()); //判别lcd是否内部操作状况
E_L;RS_H;RW_L; //E=0(致能),RS=1(数据),RW=0(写)
delay_us(2); //液晶延时子程序
E_H;
PORTB = Da
delay_us(2); //液晶延时子程序
E_L;
}
/*=======================================================
LCD第1行显现地址1~20(0x80~0x93)
LCD第2行显现地址1~20(0xc0~0xd3)
LCD第3行显现地址1~20(0x94~0xa7)
LCD第4行显现地址1~20(0xd4~0xe7)
=======================================================*/
//初始化LCD_8位接口,4行x20字符的工作方法
void Lcd_Init()
{
DDRB = 0xff; //端口B设为输出方法
DDRD = 0xff; //端口D设为输出方法
Lcd_Command(0x38); //
Lcd_Command(0x38); //
Lcd_Command(0x38); //
Lcd_Command(0x38); //
Lcd_Command(0x08); //令显现器off
Lcd_Command(0x01); //铲除显现器
Lcd_Command(0x06); //令LCD每接收到1Byte数据后,AC主动加1
Lcd_Command(0x0C); //令光标,0x0c=不显现,0x0d=显现闪烁.
}
//=======================================================
//写ASCII字符串程序
void asc_tran(unsigned char *asc)
{
while((*asc) != 0) //判别字是否完毕
{
Lcd_Write(*asc); //向lcd写入字符串
asc++; //移下一个字符
}
}
//=======================================================
//测验主LCD主程序
void main()
{
Lcd_Init(); //初始化LCD
while(1)
{
Lcd_Command(0x83); //设置显现位址
asc_tran(“—–@_@——“); //显现字符串
Lcd_Command(0x97); //设置显现位址
asc_tran(“www.ouravr.com”); //显现字符串
delay_ms(1000); //推迟1秒
Lcd_Command(0x01); //铲除显现器
Lcd_Command(0x83); //设置显现位址
asc_tran(“—–^_^——“); //显现字符串
Lcd_Command(0x94); //设置显现位址
asc_tran(“http://59.36.96.196”); //显现字符串
delay_ms(1000); //推迟
Lcd_Command(0x01); //铲除显现器
}
}