51单片机系列之LCD1602

名称:LCD1602液晶屏显示(并口) 
编写:付新 
日期:2012/5/9 
平台:Keil 4, Ly-51S学习板 
引脚定义如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK 
与51连接:RS-P2.4 RW-P2.5 EN-P2.6 DB-P0 
-----------------------------------------------------*/  
  
#include<reg52.h>  
  
#define DB P0  
sbit RS =P2^4;  
sbit RW =P2^5;  
sbit EN =P2^6;  
  
/*判忙函数*/  
bit LCD_check_busy() {  
    DB = 0xFF;  
    RS = 0;  
    RW = 1;  
    EN = 0;  
    EN = 1;  
    return  (bit)(DB & 0x80);  
}  
  
/*写入命令函数*/  
void LCD_write_com(unsigned char com) {  
    while(LCD_check_busy());//忙则等待  
    RS = 0;  
    RW = 0;  
    EN = 1;  
    DB = com;  
    EN = 0;  
}  
  
/*写入数据函数*/  
void LCD_write_data(unsigned char dat) {  
    while(LCD_check_busy());//忙则等待  
    RS = 1;  
    RW = 0;  
    EN = 1;  
    DB = dat;  
    EN = 0;  
}  
  
/*在y(0-1)行,x列(0-15)写入字符串函数*/  
void LCD_write_string(unsigned char x, unsigned char y, unsigned char *s) {  
    if(0==y) LCD_write_com(0x80 + x);  
    else LCD_write_com(0xC0 + x);  
    while(*s) {  
        LCD_write_data(*s);  
        s ++;  
    }  
}  
  
/*写入字符函数*/  
void LCD_write_char(unsigned char x, unsigned char y, unsigned char dat) {  
    if(0==y) LCD_write_com(0x80 + x);  
    else LCD_write_com(0xC0 + x);  
    LCD_write_data(dat);  
}  
  
/*清屏函数*/  
void LCD_clear() {  
    LCD_write_com(0x01);  
}  
  
/*初始化函数*/  
void LCD_init() {  
    LCD_write_com(0x38);/*显示模式设置*/   
    LCD_write_com(0x08);/*显示关闭*/   
    LCD_write_com(0x01);/*显示清屏*/   
    LCD_write_com(0x06);/*显示光标移动设置*/   
    LCD_write_com(0x0C);/*显示开及光标设置*/      
}  
  
/*主函数*/  
void main() {  
    while(1) {  
        LCD_init();  
        LCD_write_char(0,0,'*');  
        LCD_write_string(1,0,"I'm Chinese.**");  
        LCD_write_string(0,1,"Happy,hellofuxin");  
        LCD_write_char(15,0,'!');  
        while(1);  
    }  
}  

 

posted @ 2013-03-09 08:43  顶顶顶顶  阅读(211)  评论(0)    收藏  举报