上图:
(原文件名:未命名.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);
}
lCD1602 (原文件名:1602.png)
#include<reg2051.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define lcd P3
sbit en=P1^7;
sbit rs=P1^6;
sbit rw=P1^5;
uchar buffer[16];
void delay(uchar x)
{
uchar i,j;
for(i=x;i>0;i--)
for(j=120;j>0;j--);
}
void delay1(unsigned long v)
{
while(v--);
}
void w_data(uchar input)
{
delay1(100);
rs=1;
rw=0;
en=0;
lcd=(input&0xf0)>>4;
en=1;
delay1(100);
en=0;
lcd=input&0x0f;
en=1;
delay1(100);
en=0;
}
void w_code(uchar input)
{
delay1(100);
rs=0;
rw=0;
en=0;
lcd=(input&0xf0)>>4;
en=1;
delay1(100);
en=0;
lcd=input&0x0f;
en=1;
delay1(100);
en=0;
}
void lcd_init(void)
{
lcd=0x00;
w_code(0x03);
delay1(5);
w_code(0x03);
delay1(5);
w_code(0x02);
delay1(5);
w_code(0x03);
delay1(5);
w_code(0x28);
w_code(0x08);
w_code(0x06);
w_code(0x01);
w_code(0x0c);
}
void get_xy(uchar x,uchar y)
{
if(y==1)
{
w_code(0xc0+x);
}
else
{
w_code(0x80+x);
}
}
void w_str(uchar x,uchar y,uchar *p)
{
get_xy(x,y);
while(*p)
{
w_data(*p);
p++;
}
delay(5);
}
void main(void)
{
lcd_init();
w_str(0,0,"The volt is: ");
w_str(6,1,"12.34567 V");
while(1);
}
学到不少东西~
听过有人用两个I/O口驱动1602,嘻嘻
-----------------------------------------------------------------------
很好啊
一周热门 更多>