標(biāo)題: 單片機(jī)通過(guò)PCF8574T模塊驅(qū)動(dòng)程序 1602/2004LCD [打印本頁(yè)]

作者: ynzsc001    時(shí)間: 2020-1-9 09:56
標(biāo)題: 單片機(jī)通過(guò)PCF8574T模塊驅(qū)動(dòng)程序 1602/2004LCD
單片機(jī)通過(guò) PCF8574T 模塊驅(qū)動(dòng) 1602/2004 LCD,有需要的朋友看看。//-------------------------------------------------------------------------------------------------------------------------------------------------------

#include <reg52.h>
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int


//#define                        L1                0x80            // 第一行寫入地址
//#define                        L2                0xc0            // 第二行寫入地址
        
sbit SCL = P2^0;
sbit SDA = P2^1;


//char ADDR = 0x4E;    // PCF8574  T  模塊的地址碼
  char ADDR = 0x7e;    // PCF8574   AT  模塊的地址碼


//***************************** 延時(shí) y  ms ***********************************************


void delay1(int y)   //
{
         ;
        while(y--)
        {
        unsigned char a,b,c;
        for(c=1;c>0;c--)
        for(b=142;b>0;b--)
        for(a=2;a>0;a--);
        }
}






//******************************** IIC 串口開始 ********************************************


void IIC_start(void)
{
        SDA=1;
        _nop_();
        SCL=1;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        SDA=0;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        SCL=0;
}




//********************************** IIC 串口寫1個(gè)字節(jié) ******************************************


void IIC_writeByte(char temp)
{
        char i;
        for(i=0;i<8;i++)
        {
                SDA=(bit)(temp & 0x80) ;   // 根據(jù)規(guī)定1602的數(shù)據(jù)最高位必須為  1  
                temp <<=1;
                _nop_();
                _nop_();
                SCL=1;
                _nop_();
                _nop_();
                _nop_();
                _nop_();
                _nop_();
                SCL=0;
        }
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        SDA=1;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        SCL=1;
        _nop_();
        _nop_();
        _nop_();
        while(SDA);
        _nop_();
        SCL=0;
}




//******************************** 1602寫命令 ********************************************


void LCD_write_command(char comm)
{
        char tmp;
        IIC_start();          // 串口開始
        IIC_writeByte(ADDR);  // 先選PCF 8574T 的地址  (應(yīng)該是相當(dāng)于選中的意思吧)
        
        tmp = comm & 0xF0;    // 與0xf0 應(yīng)該是取第四位的意思吧
        tmp |= 0x0C;         //保留高4位為指令的高四位,低四位為   RS = 0, RW = 0, EN = 1  
        IIC_writeByte(tmp);  //從串口送出
        delay1(20);
        tmp &= 0xFB;        //Make EN = 0
        IIC_writeByte(tmp);
        
        tmp = (comm & 0x0F) << 4 ;  //將指令的低四位 送到高位置保存
        tmp |= 0x0C;        //RS = 0, RW = 0, EN = 1
        IIC_writeByte(tmp);
        delay1(20);
        tmp &= 0xFB; // Make EN = 0
        IIC_writeByte(tmp);
        
}
//******************************** 1602寫數(shù)據(jù) ********************************************


void LCD_write_data(char data1)
{
        char tmp;
        IIC_start();
        IIC_writeByte(ADDR);   // 先選PCF 8574T 的地址  (應(yīng)該是相當(dāng)于選中的意思吧)
        
        tmp = data1 & 0xF0;
        tmp |= 0x0D; //RS = 0, RW = 0, EN = 1
        IIC_writeByte(tmp);
        delay1(20);
        tmp &= 0xFB; //Make EN = 0
        IIC_writeByte(tmp);
        
        tmp = (data1 & 0x0F) << 4 ;
        tmp |= 0x0D; //RS = 0, RW = 0, EN = 1
        IIC_writeByte(tmp);
        delay1(20);
        tmp &= 0xFB ; // Make EN = 0
        IIC_writeByte(tmp);
}


//******************************** 1602初始化 ********************************************


void Init_Lcd(void)
{
        LCD_write_command(0x33); //將8位總線轉(zhuǎn)為4位總線
        delay1(50) ;
        LCD_write_command(0x32); //
        delay1(50) ;
        LCD_write_command(0x28); // 4位數(shù)據(jù)線,顯示2行,5*7點(diǎn)陣字符  !如果是0x38  則為8位數(shù)據(jù)線,顯示2行,5*7點(diǎn)陣字符
        delay1(50) ;
        LCD_write_command(0x0C); // 開顯示,關(guān)閉光標(biāo),不閃爍
        delay1(50) ;  
        LCD_write_command(0x06); // 設(shè)定輸入方式,增量不位移
        delay1(50) ;
        LCD_write_command(0x01); // 清屏
        delay1(50) ;
}








//*************************************** 在指定位置顯示字符串 *************************************


void Write_LCD(int x, int y, char *str)
{
        char addr;
        if( x < 0)
        {
                x = 0;
        }
        if(x > 15)
        {
                x = 15;
        }
        if(y<0)
        {
                y = 0;
        }
        if(y > 1)
        {
                y = 1;
        }
        
        addr = 0x80 + 0x40 * y + x;   // Move cursor  移動(dòng)光標(biāo)
        LCD_write_command(addr);
        while (*str)
        {
                LCD_write_data(*str++);
        }
}


//-------------------------------------------- 顯示字符串的函數(shù) ----------------------------------------------------


void LCD_write_word(unsigned char *s)                  //顯示字符串的函數(shù)
{
        while(*s>0)
        {
                LCD_write_data(*s);
                s++;
        }
}




//********************************* 指定位置顯示一個(gè)字符*******************************************


/*
void Print_Char (unsigned char line,unsigned char num,unsigned char date)
{
                LCD_write_command(line+num);
                LCD_write_data(date);
}


*/




//按指定位置顯示一個(gè)字符(針對(duì)1602液晶)-用在溫度顯示


void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF;                 //限制X不能大于15,Y不能大于1
if (Y) X |= 0x40;        //當(dāng)要顯示第二行時(shí)地址碼+0x40;
X |= 0x80;               // 算出指令碼
LCD_write_command(X);    //這里不檢測(cè)忙信號(hào),發(fā)送地址碼
LCD_write_data(DData);
}








作者: ynzsc001    時(shí)間: 2020-1-9 09:58
STC 89C52  、STC12C5A60S2 單片機(jī)均可使用。
作者: 電氣電子    時(shí)間: 2020-2-3 22:50
非常好用 一次成功,感謝
11.0592M晶振
作者: minjunshuyi    時(shí)間: 2020-3-5 21:46
很好,借鑒一下,a0,a1,a2要改那里?
作者: tyz535    時(shí)間: 2020-4-27 11:07
太感謝了,找了那么久,終于有個(gè),能顯示的了
作者: wwh2382    時(shí)間: 2021-8-4 17:39
不錯(cuò),一次成功,感謝樓主的無(wú)私奉獻(xiàn)
作者: andyhallo    時(shí)間: 2022-1-16 20:32
感謝樓主的無(wú)私奉獻(xiàn)
作者: mentooo    時(shí)間: 2023-2-7 14:03
主函數(shù)都沒有,怎么運(yùn)行的?
作者: peiyingxuan    時(shí)間: 2023-7-26 09:01
#在這里快速回復(fù)#應(yīng)該加上這個(gè):
作者: peiyingxuan    時(shí)間: 2023-7-26 09:02
void main() {                 Init_Lcd();                 DisplayOneChar(0,0,'P');                 Write_LCD(0,1,"PeiYingXuan");                 while(1); }




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