找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4158|回复: 4
收起左侧

C51单片机IIC接口PCF8574T模块驱动程序 1602/2004LCD

  [复制链接]
ID:683123 发表于 2021-2-22 20:14 | 显示全部楼层 |阅读模式
借鉴ynzsc001用户提供的前期代码加以改善。亲测89,12,15系列都可以用。几个显示函数比较方便。分享给大家
webwxgetmsgimg.jpg

单片机源程序如下:
  1. #include <reg52.h>
  2. #include "intrins.h"
  3. #define uchar unsigned char
  4. #define uint unsigned int

  5. sbit SCL = P3^0;
  6. sbit SDA = P3^1;
  7. uchar i;

  8. char ADDR = 0x4E;    // PCF8574  T  
  9. //  char ADDR = 0x7e;    // PCF8574   AT  //如是后缀AT的用这个地址
  10. uchar code CGCODE[]={
  11. 0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,//年0x00
  12. 0x0F,0x09,0x0F,0x09,0x0F,0x09,0x13,0x00,//月0x01
  13. 0x0F,0x09,0x09,0x0f,0x09,0x09,0x0f,0x00,//日0x02
  14. 0x18,0x1B,0x04,0x04,0x04,0x04,0x04,0x03,//摄氏度0x03
  15. 0x1F,0x0A,0x0A,0x1F,0x0A,0x0A,0x0A,0x12,//开0x04
  16. 0x11,0x0A,0x1F,0x04,0x1F,0x04,0x0A,0x11,//关0x05
  17. 0x00,0x04,0x04,0x04,0x04,0x15,0x0E,0x04,//向下0x06
  18. 0x00,0x04,0x0E,0x15,0x04,0x04,0x04,0x04//向上0x07

  19. };

  20. //***************************** 延迟函数  ms ***********************************************
  21. void delay1(int y)   //
  22. {
  23.          ;
  24.         while(y--)
  25.         {
  26.         unsigned char a,b,c;
  27.         for(c=1;c>0;c--)
  28.         for(b=142;b>0;b--)
  29.         for(a=2;a>0;a--);
  30.         }
  31. }


  32. //******************************** IIC开始********************************************


  33. void IIC_start(void)
  34. {
  35.    SDA=1;
  36.     _nop_();
  37.     SCL=1;
  38.     _nop_();_nop_(); _nop_();_nop_();_nop_();
  39.     SDA=0;
  40.     _nop_();_nop_();_nop_();_nop_();_nop_();
  41.     SCL=0;
  42. }

  43. //********************************** IIC 写字节 ******************************************


  44. void IIC_writeByte(char temp)
  45. {
  46.   char i;
  47.   for(i=0;i<8;i++)
  48.     {
  49.        SDA=(bit)(temp & 0x80) ;   //
  50.        temp <<=1;
  51.        _nop_();_nop_();
  52.        SCL=1;
  53.        _nop_();_nop_();_nop_();_nop_(); _nop_();
  54.        SCL=0;
  55.     }
  56.         _nop_(); _nop_();_nop_();_nop_();
  57.    SDA=1;
  58.    _nop_(); _nop_(); _nop_();_nop_();
  59.    SCL=1;
  60.          _nop_();_nop_();_nop_();
  61.    while(SDA);
  62.    _nop_();
  63.    SCL=0;
  64. }
  65. //******************************** 1602写命令 ********************************************
  66. void LCD_write_command(char comm)
  67. {
  68.         char tmp;
  69.         IIC_start();         
  70.         IIC_writeByte(ADDR);   
  71.         tmp = comm & 0xF0;   
  72.         tmp |= 0x0C;         
  73.         IIC_writeByte(tmp);  
  74.         delay1(20);
  75.         tmp &= 0xFB;        //Make EN = 0
  76.         IIC_writeByte(tmp);
  77.         tmp = (comm & 0x0F) << 4 ;  
  78.         tmp |= 0x0C;        //RS = 0, RW = 0, EN = 1
  79.         IIC_writeByte(tmp);
  80.         delay1(20);
  81.         tmp &= 0xFB; // Make EN = 0
  82.         IIC_writeByte(tmp);

  83. }
  84. //******************************** 1602写数据 ********************************************


  85. void LCD_write_data(char data1)
  86. {
  87.         char tmp;
  88.         IIC_start();
  89.         IIC_writeByte(ADDR);  
  90.         tmp = data1 & 0xF0;
  91.         tmp |= 0x0D; //RS = 0, RW = 0, EN = 1
  92.         IIC_writeByte(tmp);
  93.         delay1(20);
  94.         tmp &= 0xFB; //Make EN = 0
  95.         IIC_writeByte(tmp);  
  96.         tmp = (data1 & 0x0F) << 4 ;
  97.         tmp |= 0x0D; //RS = 0, RW = 0, EN = 1
  98.         IIC_writeByte(tmp);
  99.         delay1(20);
  100.         tmp &= 0xFB ; // Make EN = 0
  101.         IIC_writeByte(tmp);
  102. }


  103. //******************************** 1602初始化 ********************************************
  104. void UserDiy()
  105. {        
  106.    unsigned char i;
  107.    LCD_write_command(0x40);                        //设置CGRAM地址,自定义字符共8个
  108.    for(i=0;i<64;i++)
  109.    {
  110.      LCD_write_data(CGCODE[i]);
  111.    }                                
  112. }

  113. void Init_Lcd(void)
  114. {
  115.         LCD_write_command(0x33); //
  116.         delay1(50) ;
  117.         LCD_write_command(0x32); //
  118.         delay1(50) ;
  119.         LCD_write_command(0x28); //
  120.         delay1(50) ;
  121.         LCD_write_command(0x0C); //
  122.         delay1(50) ;  
  123.         LCD_write_command(0x06); //
  124.         delay1(50) ;
  125.         LCD_write_command(0x01); //
  126.         delay1(50) ;
  127.                                 UserDiy();
  128. }

  129. /*****************次方函数***********************/
  130. int NumberPow(int x,int y)
  131. {
  132.         unsigned char i;
  133.         int Result=1;
  134.         for(i=0;i<y;i++)
  135.         {
  136.                 Result*=x;
  137.         }
  138.         return Result;
  139. }

  140. //************************设置第几行第几列***************************
  141. void SetHL(unsigned x,unsigned y)
  142. {
  143.         if(x==1)
  144.                 LCD_write_command(0x80|(y-1));
  145.         if(x==2)
  146.                 LCD_write_command(0x80|(y-1)+0x40);
  147. }
  148. //**************************第几行,第几列,显示数字,数字几位************************
  149. void LCDShowNumber(unsigned char x,unsigned char y,unsigned int Number,unsigned char NumberLength)
  150. {
  151.         unsigned char i;
  152.         SetHL(x,y);
  153.         for(i=NumberLength;i>0;i--)
  154.         {
  155.                 LCD_write_data('0'+Number/NumberPow(10,i-1)%10);   /*加'0'是将数字转换成ASCLL码*/
  156.         }
  157. }
  158. //*************************************** 第x行第x列写字串符 *************************************

  159. void LCDShowString(unsigned char x,unsigned char y,unsigned char String[])  /*unsigned char Char[]=unsigned char *Char*/
  160. {
  161.         unsigned char i;
  162.         SetHL(x,y);
  163.         for(i=0;String[i]!='\0';i++)    /*Char[]{"abc"}=Char[]{'a','b','c','\0'},'\0'为结束标志位*/
  164.         {
  165.                 LCD_write_data(String[i]);
  166.         }
  167. }

  168. /*第几行第几列显示一个字符*/
  169. void LCDShowChar(unsigned char x,unsigned char y,unsigned char Char)
  170. {
  171.         SetHL(x,y);
  172.         LCD_write_data(Char);
  173. }

  174. /********************************主函数****************************/
  175. void main()
  176. {
  177.         Init_Lcd();
  178.         LCDShowChar(1,1,0x00);
  179.         LCDShowChar(1,2,0x01);
  180.         LCDShowChar(1,3,0x02);
  181.         LCDShowChar(1,4,0x03);
  182.         LCDShowChar(1,5,0x04);
  183.         LCDShowChar(1,6,0x05);
  184.         LCDShowChar(1,7,0x06);
  185.         LCDShowChar(1,8,0x07);
  186.         LCDShowString(1,9,"abcdefgf");
  187.         while(1)
  188.         {
  189.                 for(i=1;i<100;i++)
  190.                         {
  191.                          LCDShowNumber(1,2,i,3);
  192.                          delay1(1000);
  193.                         }
  194.                         i=0;
  195.         }
  196. }
复制代码

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:683123 发表于 2021-2-22 20:17 | 显示全部楼层
lCD2004第三行地址是0X14,第四行地址是0x54;
回复

使用道具 举报

ID:947166 发表于 2021-7-5 17:15 | 显示全部楼层
我用的stc12c5a60s2,没效果,是要对延时进行调整么
回复

使用道具 举报

ID:974414 发表于 2021-11-8 20:55 | 显示全部楼层
感谢楼主分享程序,不过里面的delay1函数是毫秒级别,在包括初始化,写cmd,写dat的函数里面根本不用毫秒级的延迟,建议使用
void delay(u16 us)
{
        while(us--);
}
回复

使用道具 举报

ID:546224 发表于 2022-7-24 09:35 | 显示全部楼层
在STC15W408AS上亲测可用
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表