LCD1602的第一个显示程序

#include <reg52.h>

typedef unsigned int uint;
typedef unsigned char uchar;


void delay(uint z){
	uint x,y;
	for(x=z;x>0;--x)
		for(y=110;y>0;--y);
}

uchar code table[]="I Love MCU!";
uchar code table1[]="gibbon2000pro";

sbit lcd_en_port=P3^4;
sbit lcd_rs_port=P3^5;
sbit lcd_rw_port=P3^6;
sbit dula=P2^6;//数码管的段选信号
sbit wela=P2^7; //数码管的位选信号


void lcd_busy_wait() /*LCD1602 忙等待*/
{
    lcd_rs_port = 0;
    lcd_rw_port = 1;
    lcd_en_port = 1;
    P0 = 0xff;
    while (P0&0x80);
    lcd_en_port = 0; 

}
void lcd_command_write(uchar com){
	lcd_rs_port=0;
	lcd_rw_port=0;
	lcd_en_port=0;
	P0=com;
	lcd_en_port=1;
	lcd_en_port=0;
}

void lcd_data_write(uchar _data){
	lcd_rs_port=1;
	lcd_rw_port=0;
	lcd_en_port=0;
	P0=_data;
	lcd_en_port=1;
	lcd_en_port=0;
}

void lcd_char_write(uchar posit,uchar _data){
	uchar row,col,addr=0x00;
	row=(posit>>4)&0x01;
	col=posit&0x0f;
	addr=col;
	if(row){
		addr+=0x40;
	}
	addr+=0x80;
	lcd_busy_wait();
	lcd_command_write(addr);
	lcd_busy_wait();
	lcd_data_write(_data);
	P0=0xff;
}

void lcd_system_reset() /*LCD1602 初始化*/
{
	dula=1;
	wela=1;
    delay(15);
    lcd_command_write(0x38);
    delay(5);
    lcd_command_write(0x38);
    delay(5);
    lcd_command_write(0x38);
	//have to read
	lcd_busy_wait();
    lcd_command_write(0x01);
	lcd_busy_wait();
    lcd_command_write(0x06);
	lcd_busy_wait();
    lcd_command_write(0x0c); 
	P0 = 0xff; /*释放数据端口*/
}

void main(){
	uchar num;
	lcd_system_reset();
	lcd_busy_wait();
	lcd_command_write(0x80);
	for(num=0;num<11;++num){
		//lcd_char_write(num,table[num]);
		lcd_busy_wait();
		lcd_data_write(table[num]);
	}
	lcd_busy_wait();
	lcd_command_write(0x80+0x40);
	for(num=0;num<13;++num){
		//lcd_char_write(0x10+num,table1[num]);
		lcd_busy_wait();
		lcd_data_write(table1[num]);
	}
	while(1);
}


posted on 2011-01-03 00:25  盐味  阅读(933)  评论(0编辑  收藏  举报