51微控制器lcd程式碼

Aikenlan發表於2016-09-20
#include "../delay/delay.h"
#include "lcd.h"

/*====================================================
                 判斷寫入命令/資料函式
=====================================================*/
void lcd_write(unsigned char byte, unsigned char flag)
{
    if(flag)
	{
	    RS = 1;        //選擇資料暫存器
	}
	else
	{
	    RS = 0;        //選擇指令暫存器
	}
	
	RW = 0;            //寫
	E = 1;             //選中LCD
	LCDPORT = byte;
	delay_us(5);       //穩定
	E = 0;             //失能LCD
}

/*====================================================
                    LCD初始化函式
=====================================================*/
void lcd_init()
{
    delay_ms(15);
	lcd_write(0x38,LCD_WRITE_COM);    //顯示8位資料介面,兩行顯示,5*7點陣字元
	delay_ms(5);
	lcd_write(0x38,LCD_WRITE_COM);
	delay_ms(5);
	lcd_write(0x38,LCD_WRITE_COM);
	delay_ms(5);
	lcd_write(0x38,LCD_WRITE_COM);
	delay_ms(5);
	lcd_write(0x08,LCD_WRITE_COM);    //關閉顯示,關閉游標的閃爍
	delay_ms(5);
	lcd_write(0x01,LCD_WRITE_COM);    //清屏
	delay_ms(5);
	lcd_write(0x06,LCD_WRITE_COM);    //設定指標方式,畫面不平移
	delay_ms(5);
	lcd_write(0x0c,LCD_WRITE_COM);    //開啟顯示
	delay_ms(5);
}

/*====================================================================================
                                 寫入字元函式
函式原型:void lcd_dis_byte(unsigned char x, unsigned char y, unsigned char byte);
說明:x為行(1——2),y為列(1——8)。
=====================================================================================*/
void lcd_dis_byte(unsigned char x, unsigned char y, unsigned char byte)
{
    unsigned char add;
	
    if(((0 == x) || (x > 2)) || ((0 == y) || (y > 16)))
	{
	    return ;
	}
	
    add = 0x80 + (x - 1) * 0x40 + (y - 1);
	
	lcd_write(add,LCD_WRITE_COM);	     
	lcd_write(byte,LCD_WRITE_DATA);
}

/*====================================================================================
                                  寫入字串函式
函式原型:void lcd_dis_str(unsigned char x, unsigned char y, unsigned char *disdata);
說明:x為行(1——2),y為列(1——8)。
=====================================================================================*/
void lcd_dis_str(unsigned char x, unsigned char y, unsigned char *disdata)
{
    unsigned char add;
	
    if(((0 == x) || (x > 2)) || ((0 == y) || (y > 16)))
	{
	    return ;
	}
	
    add = 0x80 + (x - 1) * 0x40 + (y - 1);
	lcd_write(add,LCD_WRITE_COM);
	
    while(*disdata != '\0')
	{	    
	    lcd_write(*disdata,LCD_WRITE_DATA);
		disdata++;
	}
}

#if 0
/*===========================================================================
                                時間顯示函式
============================================================================*/
void lcd_dis_time()
{
    lcd_write(0x80 + 4,LCD_WRITE_COM);
	lcd_write((hour / 10) + 0x30,LCD_WRITE_DATA);
	lcd_write((hour % 10) + 0x30,LCD_WRITE_DATA);
	lcd_write((min / 10) + 0x30,LCD_WRITE_DATA);
	lcd_write((min % 10) + 0x30,LCD_WRITE_DATA);
	lcd_write((sec / 10) + 0x30,LCD_WRITE_DATA);
	lcd_write((sec % 10) + 0x30,LCD_WRITE_DATA);
}
#endif

相關文章