上图:
(原文件名:未命名.JPG)
(原文件名:未命名2.JPG)
(原文件名:未命名3.JPG)
程序:
#include<AT89X52.h>
//lcd part
#define LINE1 0
#define LINE2 1
#define LINE1_HEAD 0x80
#define LINE2_HEAD 0xC0
#define CLEARSCREEN LCD_en_com(0x01)
/*************************************/
//change this part at different board
sbit DB7=P2^6;
sbit DB6=P2^5;
sbit DB5=P2^4;
sbit DB4=P2^3;
sbit LCD1602_RS=P2^0; //data command select 1 data 0 command pin 4
sbit LCD1602_RW=P2^1; //read write select 1 read 0 write pin 5
sbit LCD1602_EN=P2^2; //LCD enable signal pin 6
/********************************************************************/
void delay(unsigned int n)
{
unsigned int i=0,j=0;
for (i=n;i>0;i--)
for (j=0;j<12;j++);
}
void LCD_en_com(unsigned char command)
{
DB7=(command>>7)%2;
DB6=(command>>6)%2;
DB5=(command>>5)%2;
DB4=(command>>4)%2;
LCD1602_RS = 0; //RS 0
LCD1602_RW = 0; //RW 0
LCD1602_EN = 1; //EN --|/__ Write command
delay(2);
LCD1602_EN = 0;
DB7=(command>>3)%2;
DB6=(command>>2)%2;
DB5=(command>>1)%2;
DB4=(command>>0)%2;
LCD1602_RS = 0; //RS 0
LCD1602_RW = 0; //RW 0
LCD1602_EN = 1; //EN --|/__ Write command
delay(2);
LCD1602_EN = 0;
}
void LCD_en_dat(unsigned char command)
{
DB7=(command>>7)%2;
DB6=(command>>6)%2;
DB5=(command>>5)%2;
DB4=(command>>4)%2;
LCD1602_RS = 1; //RS 1
LCD1602_RW = 0; //RW 0
LCD1602_EN = 1; //EN --|/__ Write data
delay(2);
LCD1602_EN = 0;
DB7=(command>>3)%2;
DB6=(command>>2)%2;
DB5=(command>>1)%2;
DB4=(command>>0)%2;
LCD1602_RS = 1; //RS 1
LCD1602_RW = 0; //RW 0
LCD1602_EN = 1; //EN --|/__ Write data
delay(2);
LCD1602_EN = 0;
}
void LCD_set_xy(unsigned char x,unsigned char y)
{
unsigned char address;
if(y == LINE1)
address = LINE1_HEAD + x;
else
address = LINE2_HEAD + x;
delay(10);
LCD_en_com(address);
}
void LCD_write_string(unsigned char x,unsigned char y,unsigned char *s)
{
LCD_set_xy(x,y);
while(*s)
{
LCD_en_dat(*s);
s++;
}
}
void LCD_init(void)
{
delay(50);
LCD_en_com(0x33);//原来1602初始化成4位数据线之前必需先初始化成8位(此时命令发送方式是8位格式,但数据线只需接4位)
delay(10);
LCD_en_com(0x32);//然后再改到4位线宽,这样初始化才稳定
delay(10);
LCD_en_com(0x28);//四线模式设置
delay(10);
LCD_en_com(0x0c);//显示开--对应开关显示控制指令
delay(10);
CLEARSCREEN;
delay(30);
}
/********************************************************************/
void main(void)
{
LCD_init();
LCD_write_string(0,LINE1,"0123456789abcdef");
LCD_write_string(3,LINE2,"Advanced");
while(1);
}
-----------------------------------------------------------------------
void LCD_init(void)
{
delay(50);
LCD_en_com(0x33);//原来1602初始化成4位数据线之前必需先初始化成8位(此时命令发送方式是8位格式,但数据线只需接4位)
delay(10);
LCD_en_com(0x32);//然后再改到4位线宽,这样初始化才稳定
delay(10);
LCD_en_com(0x28);//四线模式设置
delay(10);
LCD_en_com(0x0c);//显示开--对应开关显示控制指令
delay(10);
CLEARSCREEN;
delay(30);
}
其实LCD_en_com(0x33);这段根本是多余的关键是下面两段
LCD_en_com(0x32);
delay(10);
LCD_en_com(0x28);
delay(10);
才是真正起作用的关键!
一周热门 更多>