|
利用wiringPi IO扩展映射功能的实现,实现起来更加的方便
代码如下
- #include <wiringPi.h>
- #include <pcf8574.h>
- #include <lcd.h>
- #include <stdio.h>
- #include <stdlib.h>
- //扩展PCF8574 起始PIN地址
- // PCF8754 64+8
- #define AF_BASE 64
- #define AF_RS (AF_BASE + 0)
- #define AF_RW (AF_BASE + 1)
- #define AF_E (AF_BASE + 2)
- #define AF_LED (AF_BASE + 3)
- #define AF_DB4 (AF_BASE + 4)
- #define AF_DB5 (AF_BASE + 5)
- #define AF_DB6 (AF_BASE + 6)
- #define AF_DB7 (AF_BASE + 7)
- // Global lcd handle:
- static int lcdHandle;
- int main(void)
- {
- int i;
- int count;
- //wiringPi初始化
- wiringPiSetup();
- //PCF8754 初始化,此时 PCF8754 的I2C 地址为 0x27
- pcf8574Setup(AF_BASE,0x27);
- lcdHandle = lcdInit (2, 16, 4, AF_RS, AF_E, AF_DB4,AF_DB5,AF_DB6,AF_DB7, 0,0,0,0) ;
- if (lcdHandle < 0)
- {
- fprintf (stderr, "lcdInit failed\n") ;
- exit (EXIT_FAILURE) ;
- }
- for(i=0;i<8;i++)
- pinMode(AF_BASE+i,OUTPUT); //将拓展出的IO口全部置为输出模式
- digitalWrite(AF_LED,1); //将107引脚置为高电平,点亮LCD背光灯
- digitalWrite(AF_RW,0); //将R/W置为低电平,将LCD设置为写入状态
- printf ("Raspberry Pi - PCF8754 Test\n");
- lcdHome(lcdHandle); //将LCD的光标归位
- lcdClear(lcdHandle); //清屏
- lcdPosition (lcdHandle, 0, 0) ;
- lcdPuts (lcdHandle, " QQ1696933323 ") ;
- lcdPosition (lcdHandle, 0, 1) ;
- //lcdPuts (lcdHandle, " Standard LCD ") ;
- FILE *fp;
- char temp_char[15]; //树莓派温度
- char Total[20]; //总内存量
- char Free[20]; //空闲内存量
- while(1)
- {
- //lcdPosition (lcdHandle, 15, 1) ;
- //lcdPrintf(lcdHandle,"%d",count++); //自动循环加1
- //delay(1000);
- fp=fopen("/sys/class/thermal/thermal_zone0/temp","r"); //读取树莓派温度
- fgets(temp_char,9,fp);
- float Temp;
- Temp=atof(temp_char)/1000;
- lcdPosition (lcdHandle, 0, 1);
- lcdPrintf (lcdHandle, "%3.1fC", Temp) ;
- fclose(fp);
- fp=fopen("/proc/meminfo","r"); //读取内存信息
- fgets(Total,19,fp);
- fgets(Total,4,fp);
- fgets(Free,9,fp);
- fgets(Free,19,fp);
- fgets(Free,4,fp);
- lcdPosition (lcdHandle, 7, 1);
- lcdPrintf (lcdHandle, "%3d/%3dMB", atoi(Free),atoi(Total)) ;
- fclose(fp);
- delay(1000);
- //sleep(1);
- }
- return 0;
- }
复制代码
效果也是差不多这样的跟
[Raspberry Pi]PCF8574扩展IO口实现I2C转LCD1602(Python)
https://www.eeboard.com/bbs/forum ... 9&fromuid=23447
这次的没有拍图,不好意思哦
|
|