名称:LCD1602日期:2011.6.1
内容:通过标准程序动态显示字符
#include<reg52.h>
sbit RS = P2^4;
sbit RW = P2^5;
sbit EN = P2^6;
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
void delay_us(unsigned int n) //微秒级延时函数,如果需要高精度延时 请嵌入汇编
{
if (n == 0)
{
return ;
}
while (--n);
}
void delay_ms(unsigned chari) //毫秒级延时函数
{
unsigned char a, b;
for (a = 1; a < i; a++)
{
for (b = 1; b; b++)
{; }
}
}
void LCD_write_com(unsigned charcom) //写命令函数
{
RS_CLR;
RW_CLR;
EN_SET;
P0 = com;
delay_us(5);
EN_CLR;
}
void LCD_write_Data(unsigned charData) //写数据函数
{
RS_SET;
RW_CLR;
EN_SET;
P0 = Data;
delay_us(5);
EN_CLR;
}
voidLCD_clear(void) //清屏函数
{
LCD_write_com(0x01);
delay_ms(5);}
void LCD_write_char(unsigned char x,unsignedchar y,unsigned char Data) //写字符函数(推荐)
{
if (y == 0)
{
LCD_write_com(0x80 +x);
}
else
{
LCD_write_com(0xC0 +x);
}
LCD_write_Data( Data);
}
void LCD_write_str(unsigned char x,unsigned chary,unsigned char *s) //写字符串函数(推荐)
{
if (y == 0)
{
LCD_write_com(0x80 +x);
}
else
{
LCD_write_com(0xC0 +x);
}
while (*s)
{
LCD_write_Data(*s);
s++;
}
}
void LCD_init(void)//初始化函数
{
LCD_write_com(0x38);
delay_ms(5);
LCD_write_com(0x38);
delay_ms(5);
LCD_write_com(0x38);
delay_ms(5);
LCD_write_com(0x38);
LCD_write_com(0x08);
LCD_write_com(0x01);
LCD_write_com(0x06);
delay_ms(5);
LCD_write_com(0x0C);
}
void main(void) //主函数
{
unsigned char i;
unsigned char *p;
delay_ms(100);
LCD_init();
while (1)
{
i = 1;
p = "haixiang MCU";
LCD_clear();
LCD_write_str(2,0,"Welcome to");
delay_ms(250);
while (*p)//用指针来存放字符串,并且作为循环判断的标志,可以实现第二行的循环显示
{
LCD_write_char(i,1,*p);
i ++;
p ++;
delay_ms(250);
}
delay_ms(250);
}
}