51單片機(jī)-液晶1602
初始化設(shè)置:
RAM地址映射圖:
寫操作時(shí)序:
實(shí)現(xiàn)兩行顯示,從左向右滑動。
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS = P1^0;
sbit RW = P1^1;
sbit E = P1^2;
uint i;
uchar code table[] = "I LOVE MCU!";
uchar code table1[]="WWW.TXMCU.COM";
void delay(uint t){
int i,j;
for(i = 0; i < t; i++)
for(j = 200; j > 0; j--)
;
}
void lcd_com(uchar s){
RS = 0; //低電平,寫指令
P2 = s; //傳數(shù)據(jù)
delay(14); //看時(shí)序圖,數(shù)據(jù)需要穩(wěn)定一段時(shí)間
E = 1; //給一個(gè)高脈沖,發(fā)送命令
delay(14); //如圖,高脈沖延時(shí)一段時(shí)間,確保命令發(fā)送
E = 0; //發(fā)送結(jié)束E置為低電平
}
void lcd_data(uchar s){
RS = 1;
P2 = s;
delay(14);
E = 1;
delay(14);
E = 0;
}
void init(){
RS = 1; //先發(fā)指令,在初始時(shí)刻RS是高,E和RW是低
E = 0;
RW = 0;
lcd_com(0x38); //設(shè)置為16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)接口
lcd_com(0x0f); //開顯示,顯示光標(biāo),光標(biāo)閃爍
lcd_com(0x06); //讀寫一個(gè)字符后地址指針加一
lcd_com(0x01);
lcd_com(0x80+0x10); //顯示屏幕對應(yīng)一塊RAM,屏幕外面還有一塊RAM
for(i = 0; i < 11; i++){ //設(shè)置地址為屏幕外的RAM,以便實(shí)現(xiàn)滾動效果
RS = 0;
lcd_data(table[ i]);
delay(40);
}
lcd_com(0x80+0x50);
for(i = 0; i < 13; i++){
RS = 0;
lcd_data(table1[ i]);
delay(40);
}
for(i = 0; i < 13; i++){
lcd_com(0x18); //0x18,向左移動顯示,光標(biāo)跟著移動
delay(200);
}
}
void main(){
init();
while(1);
}
評論