LCD 1602采用4线并口通信,连接方式是DB4-DB7接P1.4-P1.7;RS:P2.7;RW:P2.6
EN:P2.5;注意初始化的设置。
在万利MedWin 3.0开发环境上调试通过!
#include
/********************************************************************/
#define LCDIO P1
sbit LCD1602_RS=P2^7;
sbit LCD1602_RW=P2^6;
sbit LCD1602_EN=P2^5;
/********************************************************************/
void LCD_delay(void);
void LCD_en_command(unsigned char command);
void LCD_en_dat(unsigned char temp);
void LCD_set_xy( unsigned char x, unsigned char y );
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);
void LCD_init(void);
/********************************************************************/
void delay_nms(unsigned int n);
/********************************************************************/
void main(void)
{
LCD_init();
while(1 )
{
LCD_en_command(0x01);
delay_nms(2);
LCD_write_string(0,0," create by ");
LCD_write_string(0,1," fenyman ");
delay_nms(200);
LCD_en_command(0x01);
delay_nms(2);
LCD_write_string(0,0," LCD1602 ");
LCD_write_string(0,1," test ");
delay_nms(200);
}
}
/******************** LCD PART *************************************/
void LCD_delay(void)
{
unsigned char i;
for(i=40;i>0;i--)
;
}
/********************************************************************/
void LCD_en_command(unsigned char command)
{
LCD1602_RS=0;
LCD1602_RW=0;
LCD1602_EN=0;
LCDIO=(command & 0xf0);
LCD1602_EN=1;
LCD_delay();
LCD1602_EN=0;
LCDIO=(command & 0x0f)<<4;
LCD1602_EN=1;
LCD_delay();
LCD1602_EN=0;
}
/********************************************************************/
void LCD_en_dat(unsigned char dat)
{
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_EN=0;
LCDIO=(dat & 0xf0);
LCD1602_EN=1;
LCD_delay();
LCD1602_EN=0;
LCDIO=(dat & 0x0f)<<4;
LCD1602_EN=1;
LCD_delay();
LCD1602_EN=0;
}
/********************************************************************/
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y ==0)
address = 0x80 + x;
else
address = 0xC0 + x;
LCD_en_command(address);
}
/********************************************************************/
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
{
LCD_set_xy( x, y );
LCD_en_dat(dat);
}
/********************************************************************/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y ); //set address
while (*s) // write character
{
LCDIO=*s;
LCD_en_dat(*s);
s ++;
}
}
/********************************************************************/
void LCD_init(void)
{
LCD_en_command(0x01);
delay_nms(5);
LCD_en_command(0x01);
delay_nms(5);
LCD_en_command(0x28);
delay_nms(5);
LCD_en_command(0x28);
delay_nms(5);
LCD_en_command(0x28);
delay_nms(5);
LCD_en_command(0x0C);
delay_nms(5);
LCD_en_command(0x80);
delay_nms(5);
LCD_en_command(0x01);
delay_nms(5);
}
/********************************* *********************************/
void delay_nms(unsigned int n)
{
unsigned int i=0,j=0;
for (i=n;i>0;i--)
for (j=0;j<1140;j++);
}
/********************************************************************/
上一篇:51单片机串口使用方法与设计实验
下一篇:LCD1602四线编程方法程序设计实例
推荐阅读最新更新时间:2024-03-16 15:51