查看: 5484|回复: 1

[原创] [Raspberry Pi]PCF8574扩展IO口实现I2C转LCD1602(C语言)

[复制链接]
  • TA的每日心情
    奋斗
    2023-7-8 16:17
  • 签到天数: 971 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2015-11-26 21:09:21 | 显示全部楼层 |阅读模式
    分享到:
    上次很偶然的秒到了I2C转LCD1602,
    秒到IIC控制LCD1602转换版到货啦!
    https://www.cirmall.com/bbs/forum ... 45305&fromuid=23447

    [Raspberry Pi]PCF8574扩展IO口实现I2C转LCD1602(Python)
    https://www.cirmall.com/bbs/foru ... 9&fromuid=23447

    这次同样也是做扩展,不过是利用wiringPi IO扩展映射功能的实现,实现起来更加的方便
    代码如下
    1. #include <wiringPi.h>
    2. #include <pcf8574.h>
    3. #include <lcd.h>

    4. #include <stdio.h>
    5. #include <stdlib.h>

    6. //扩展PCF8574  起始PIN地址
    7. // PCF8754     64+8
    8. #define AF_BASE 64
    9. #define        AF_RS                (AF_BASE + 0)
    10. #define        AF_RW                (AF_BASE + 1)
    11. #define        AF_E                (AF_BASE + 2)
    12. #define AF_LED      (AF_BASE + 3)

    13. #define        AF_DB4                (AF_BASE + 4)
    14. #define        AF_DB5                (AF_BASE + 5)
    15. #define        AF_DB6                (AF_BASE + 6)
    16. #define        AF_DB7                (AF_BASE + 7)

    17. // Global lcd handle:
    18. static int lcdHandle;

    19. int main(void)
    20. {   
    21.     int i;
    22.     int count;
    23.     //wiringPi初始化
    24.     wiringPiSetup();

    25.     //PCF8754 初始化,此时 PCF8754 的I2C 地址为 0x27
    26.     pcf8574Setup(AF_BASE,0x27);
    27.    
    28.     lcdHandle = lcdInit (2, 16, 4, AF_RS, AF_E, AF_DB4,AF_DB5,AF_DB6,AF_DB7, 0,0,0,0) ;
    29.    
    30.     if (lcdHandle < 0)
    31.     {
    32.         fprintf (stderr, "lcdInit failed\n") ;
    33.         exit (EXIT_FAILURE) ;
    34.     }
    35.    
    36.     for(i=0;i<8;i++)
    37.           pinMode(AF_BASE+i,OUTPUT);  //将拓展出的IO口全部置为输出模式
    38.     digitalWrite(AF_LED,1);         //将107引脚置为高电平,点亮LCD背光灯
    39.     digitalWrite(AF_RW,0);          //将R/W置为低电平,将LCD设置为写入状态
    40.    
    41.     printf ("Raspberry Pi - PCF8754 Test\n");
    42.     lcdHome(lcdHandle);   //将LCD的光标归位
    43.     lcdClear(lcdHandle);  //清屏
    44.     lcdPosition (lcdHandle, 0, 0) ;
    45.     lcdPuts (lcdHandle, "  QQ1696933323 ") ;
    46.     lcdPosition (lcdHandle, 0, 1) ;
    47.     //lcdPuts (lcdHandle, "  Standard LCD ") ;
    48.    
    49.     FILE *fp;
    50.     char temp_char[15]; //树莓派温度
    51.     char Total[20]; //总内存量
    52.     char Free[20]; //空闲内存量
    53.    
    54.     while(1)
    55.     {   
    56.         //lcdPosition (lcdHandle, 15, 1) ;
    57.         //lcdPrintf(lcdHandle,"%d",count++);                                       //自动循环加1
    58.         //delay(1000);
    59.         
    60.         fp=fopen("/sys/class/thermal/thermal_zone0/temp","r"); //读取树莓派温度
    61.         fgets(temp_char,9,fp);
    62.         float Temp;
    63.         Temp=atof(temp_char)/1000;
    64.         lcdPosition (lcdHandle, 0, 1);
    65.         lcdPrintf (lcdHandle, "%3.1fC", Temp) ;
    66.         fclose(fp);

    67.         fp=fopen("/proc/meminfo","r"); //读取内存信息
    68.         fgets(Total,19,fp);
    69.         fgets(Total,4,fp);
    70.         fgets(Free,9,fp);
    71.         fgets(Free,19,fp);
    72.         fgets(Free,4,fp);
    73.         lcdPosition (lcdHandle, 7, 1);
    74.         lcdPrintf (lcdHandle, "%3d/%3dMB", atoi(Free),atoi(Total)) ;
    75.         fclose(fp);
    76.         
    77.         delay(1000);
    78.         //sleep(1);
    79.     }
    80.    
    81.     return 0;
    82. }
    复制代码
    效果也是差不多这样的跟
    [Raspberry Pi]PCF8574扩展IO口实现I2C转LCD1602(Python)
    https://www.cirmall.com/bbs/foru ... 9&fromuid=23447

    这次的没有拍图,不好意思哦
    1305298e1pkqpuopog40qj.jpg
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2020-5-27 15:41
  • 签到天数: 868 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2015-12-1 15:36:46 | 显示全部楼层
    写写分享,我的LCD后面是插针
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-26 14:13 , Processed in 0.128821 second(s), 18 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.