我的树莓派2开始时加了一个无线模块的和hdmi转ga模块的,之前的使用都是街上屏幕,鼠标,键盘,登录。可是有时候却需要显示器接到笔记本电脑上,而树莓派可以通过ssh登录。
那么把树莓派的显示器,鼠标键盘拔掉,直接上电启动,在笔记本上ssh登录不是更好吗?可是由于我这边有两个无线热点可用,而且IP地址是自动获取的,所以就想到了加一个LCD显示SSID和IP地址。
1.首先是硬件接线
最普通的2X16的HDD44780的LCD模块。
LCD 各个引脚的定义:
- Ground
- VCC - 5v not 3.3v
- Contrast adjustment (VO) from potentiometer
- Register Select (RS). RS=0: Command, RS=1: Data
- Read/Write (R/W). R/W=0: Write, R/W=1: Read (we won’t use this pin)
- Clock (Enable). Falling edge triggered
- Bit 0 (Not used in 4-bit operation)
- Bit 1 (Not used in 4-bit operation)
- Bit 2 (Not used in 4-bit operation)
- Bit 3 (Not used in 4-bit operation)
- Bit 4
- Bit 5
- Bit 6
- Bit 7
- Backlight LED Anode (+)
- Backlight LED Cathode (-)
重要的是3要接到电位器的中间,我手头上没有电位器,就直接接到gnd上了,另外,没有用面包板,直接用杜邦线接的,是足够的。我的几个中重要的接线是
- Ground----------------------------------接0V
- VCC - -------------------------------------接5V
- Contrast adjustment (VO) from potentiometer----------------接了0V(如有电位器应接电位器)
- Register Select (RS). RS=0: Command, RS=1: Data-------------------------接28(wiringPI 编号)
- Read/Write (R/W). R/W=0: Write, R/W=1: Read (we won’t use this pin)------接0V
- Clock (Enable). Falling edge triggered-----------------------------------------------接29(wiringPI编号)
- Bit 0 (Not used in 4-bit operation)
- Bit 1 (Not used in 4-bit operation)
- Bit 2 (Not used in 4-bit operation)
- Bit 3 (Not used in 4-bit operation)
- Bit 4-------------------------------------------------------------------------------------------接25
- Bit 5------------------------------------------------------------------------------------------接24
- Bit 6-----------------------------------------------------------------------------------接23
- Bit 7--------------------------------------------------------------------------------接22
- Backlight LED Anode (+)----------------------------------------------------接5V
- Backlight LED Cathode (-)---------------------------------------------------接0V
参考:https://projects.drogon.net/raspberry-pi/wiringpi/lcd-library/
参考http://www.geekfan.net/5588/
线接好上电就能看到上半屏全部点亮。
2.程序调试
安装wiringpi的步骤就不写了,参看前面的文章。
思路就是
先写一个脚本,把SSID和IP写入两个文件中。然后在程序中调用脚本,读取这两个文件的内容。
脚本如下:
sleep 10 //此处为了等待系统启动后再执行
ifconfig wlan0|grep 'netmask'|sed 's/inet//g'|sed 's/netmask.*//g'|awk '{print $1}'>ip.dat
iwconfig wlan0|grep SSID|awk '{print $4}'|sed 's/ESSID\://g'|sed 's/\"//g'>ssid.dat
修改LCD的参考代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <wiringPi.h>
#include <lcd.h>
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
// Global lcd handle:
static int lcdHandle ;
int main (int argc, char *argv[])
{
int i ;
int lcd ;
int bits, rows, cols ;
struct tm *t ;
time_t tim ;
char buf [32] ;
/* if (argc != 4)
return usage (argv [0]) ;
printf ("Raspberry Pi LCD test\n") ;
printf ("=====================\n") ;
bits = atoi (argv [1]) ;
cols = atoi (argv [2]) ;
rows = atoi (argv [3]) ;
*/
bits=4;
cols=16;
rows=2;
if (!((rows == 1) || (rows == 2) || (rows == 4)))
{
fprintf (stderr, "%s: rows must be 1, 2 or 4\n", argv [0]) ;
return EXIT_FAILURE ;
}
if (!((cols == 16) || (cols == 20)))
{
fprintf (stderr, "%s: cols must be 16 or 20\n", argv [0]) ;
return EXIT_FAILURE ;
}
wiringPiSetup () ;
if (bits == 4)
lcdHandle = lcdInit (rows, cols, 4, 28,29, 25,24,23,22,0,0,0,0) ;
else
lcdHandle = lcdInit (rows, cols, 8, 11,10, 0,1,2,3,4,5,6,7) ;
if (lcdHandle < 0)
{
fprintf (stderr, "%s: lcdInit failed\n", argv [0]) ;
return -1 ;
}
//add by su
system("./writedata");
FILE *fp;
char strip[16];
char strssid[16];
//for (;;)
//{
// sleep 100;
if((fp=fopen("ip.dat","rt"))==NULL) strcpy(strip,"Open IP error");
else {fgets(strip,16,fp); fclose(fp);}
if((fp=fopen("ssid.dat","rt"))==NULL) strcpy(strssid,"Open SSID error");
else {fgets(strssid,16,fp); fclose(fp);}
lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, strssid) ;
lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, strip) ;
return 0 ;
}
3加入自动启动
写一个运行脚本命名为runlcd内容为
cd /root/lcd
./lcd
然后在/etc/rc.local中加入这个脚本
就能开机启动了。
评论