標(biāo)題: lcd1602.c文件 [打印本頁]

作者: 1345498984    時(shí)間: 2017-7-31 22:20
標(biāo)題: lcd1602.c文件
#include <reg51.h>
#include <intrins.h>
#define INT8U  unsigned char
#define INT16U unsigned int
sbit RS = P1^0;        //寄存器選擇線
sbit RW = P1^1;        //讀/寫控制線
sbit EN = P2^5;        //使能控制
//-----------------------------------------------------------------
// 延時(shí)
//-----------------------------------------------------------------
void delay_ms(INT16U ms)
{
    INT8U i; while(ms--) for(i = 0; i < 120; i++);
}

//-----------------------------------------------------------------
// 忙等待
//-----------------------------------------------------------------
void Busy_Wait()
{
    INT8U LCD_Status;                        //液晶狀態(tài)字節(jié)變量
    do
    {
         P0 = 0xFF;
        EN = 0;RS = 0;RW = 1;
        EN = 1;LCD_Status = P0;
        EN = 0;

    } while (LCD_Status & 0x80);            //液晶忙繼續(xù)循環(huán)
}

//-----------------------------------------------------------------
// 寫LCD命令
//-----------------------------------------------------------------
void Write_LCD_Command(INT8U cmd)
{
    Busy_Wait();
    EN = 0;RS = 0;RW = 0;
    P0 = cmd;
    EN = 1;_nop_();EN = 0;     

}

//-----------------------------------------------------------------
// 發(fā)送數(shù)據(jù)
//-----------------------------------------------------------------
void Write_LCD_Data(INT8U dat)
{
    Busy_Wait();
    EN = 0;RS = 1;RW = 0;
    P0 = dat;
    EN = 1;_nop_();EN = 0;   
}

//-----------------------------------------------------------------
// LCD初始化
//-----------------------------------------------------------------
void Initialize_LCD()
{
    Write_LCD_Command(0x38); delay_ms(1);    //置功能,8位,雙行,5*7
    Write_LCD_Command(0x01); delay_ms(1);    //清屏
    Write_LCD_Command(0x06); delay_ms(1);    //字符進(jìn)入模式:屏幕不動(dòng),字符后移
    Write_LCD_Command(0x0C); delay_ms(1);    //顯示開,關(guān)光標(biāo)
}

//-----------------------------------------------------------------
// 在指定位置顯示字符串
//-----------------------------------------------------------------
void LCD_ShowString(INT8U r, INT8U c,INT8U *str)
{
    INT8U i = 0;
    code INT8U DDRAM[] = {0x80,0xC0};        //1602LCD兩行的起始DDRAM地址
    Write_LCD_Command(DDRAM[r] | c);        //設(shè)置顯示起始位置

    for ( i = 0; str[i] && i < 16 ;i++)        //輸出字符串
      Write_LCD_Data(str[i]);

    for (; i < 16; i++)                         //不足一行時(shí)用空格填充
      Write_LCD_Data(' ');
}





歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1