0
修改了代码,现在效果好了点,但是还是不稳定。我尝试了两种一种是自己写的,另一种是借助DEREK MOLLOY写的GPIO库文件,还有他写的一本bbb的书,放在附件里面了。但是效果都是一样的,不稳定,有时候好用,有时候不好用。最终实现的效果是,命令行下,通过传入参数可以控制lcd1602的显示:
看看我的实验效果:
看看清除界面:
此时的命令是
借助,这个sftp软件,我把 源码从bbb上下载下来了
- #include
- #include
- #include
- #include //define O_WRONLY and O_RDONLY
- #define SYSFS_GPIO_DIR "/sys/class/gpio"
- #define MAX_BUF 64
- #define SLOTS "/sys/devices/bone_capemgr.9/slots"
- #define UART1 "/dev/ttyO1"
- //unsigned char iysheng[20]="** iyysheng **";
- typedef unsigned char uchar;
- void port_init_out(int num);
- void port_init_in(int num);
- void port_out_h(int num);
- void port_out_l(int num);
- int port_in(int num,char numvalue);
- void wr4port(uchar com);
- void wrcom(uchar com);
- void wrdat(uchar com);
- void lcd_init();
- void busy();
- #define EN_H port_out_h(45)
- #define EN_L port_out_l(45)
- #define RW_H port_out_h(44)
- #define RW_L port_out_l(44)
- #define RS_H port_out_h(23)
- #define RS_L port_out_l(23)
- void main(int argc,char *argv[])
- {
- int i,j;
- port_init_out(66);
- port_init_out(67);
- port_init_out(68);
- port_init_out(69);
- port_init_out(45);
- port_init_out(44);
- port_init_out(23);
- if(!strcmp(argv[1],"on"))
- port_out_h(66);
- else if(!strcmp(argv[1],"off"))
- port_out_l(66);
- else if(!strcmp(argv[1],"clean"))
- {
- lcd_init();
- wrcom(0x01);
- }
- else if((!strcmp(argv[1],"lcd"))&&(!strcmp(argv[2],"1")))
- {
- lcd_init();
- usleep(10000);
- wrcom(0x80);
- while(*(argv[3]+i)!='\0')
- {
- wrdat(*(argv[3]+i));
- i++;
- }
- }
- else if((!strcmp(argv[1],"lcd"))&&(!strcmp(argv[2],"2")))
- {
- lcd_init();
- usleep(10000);
- wrcom(0xc0);
- while(*(argv[3]+i)!='\0')
- {
- wrdat(*(argv[3]+i));
- i++;
- }
- }
- port_init_in(66);
- port_init_in(67);
- port_init_in(68);
- port_init_in(69);
- port_init_in(45);
- port_init_in(44);
- port_init_in(23);
-
- }
- void busy()
- {
- port_init_in(69);
- RS_L;
- RW_H;
- EN_H;
- usleep(1000);
- while(1==port_in(69,'1'));
- EN_L;
- port_init_out(69);
- }
- void lcd_init()
- {
- usleep(5000);
- wrcom(0x28);
- usleep(5000);
- wrcom(0x0f);
- usleep(5000);
- wrcom(0x06);
- //usleep(5000);
- //wrcom(0x01);
- usleep(5000);
- wrcom(0x80);
- usleep(5000);
- }
- void wr4port(uchar com)
- {
- uchar value,i;
- i=0;
- switch (i)
- {
- case 0:value=com&0x01;if(value==1) port_out_h(66);else port_out_l(66);i++;
- case 1:value=com&0x02;if(value==0x02) port_out_h(67);else port_out_l(67);i++;
- case 2:value=com&0x04;if(value==0x04) port_out_h(68);else port_out_l(68);i++;
- case 3:value=com&0x08;if(value==0x08) port_out_h(69);else port_out_l(69);i++;
- default: break;
- }
- }
- void wrcom(uchar com)
- {
- busy();
- printf("no busyn");
- usleep(1000);
- EN_L;
- RS_L;
- RW_L;
- wr4port(com>>4);
- usleep(1000);
- EN_H;
- usleep(5000);
- EN_L;
-
- wr4port(com);
- usleep(1000);
- EN_H;
- usleep(5000);
- EN_L;
- }
- void wrdat(uchar com)
- {
- busy();
- usleep(1000);
- printf("no busyn");
- EN_L;
- RS_H;
- RW_L;
- wr4port(com>>4);
- usleep(1000);
- EN_H;
- usleep(5000);
- EN_L;
-
- wr4port(com);
- usleep(1000);
- EN_H;
- usleep(5000);
- EN_L;
- }
- void port_init_out(int num)
- {
- int fd,len;
- char buf[MAX_BUF];
- char portnum[64];
- sprintf(portnum,"%d",num);
- fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
- len = snprintf(buf,sizeof(buf),portnum);
- write(fd,buf,len);
- close(fd);
- sprintf(portnum,"/sys/class/gpio/gpio%d/direction",num);
- snprintf(buf,sizeof(buf),portnum);
- fd = open(buf, O_WRONLY);
- write(fd, "out", 4);
- close(fd);
- }
- void port_init_in(int num)
- {
- int fd,len;
- char buf[MAX_BUF];
- char portnum[64];
- sprintf(portnum,"%d",num);
- fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
- len = snprintf(buf,sizeof(buf),portnum);
- write(fd,buf,len);
- close(fd);
- sprintf(portnum,"/sys/class/gpio/gpio%d/direction",num);
- snprintf(buf,sizeof(buf),portnum);
- fd = open(buf, O_WRONLY);
- write(fd, "in", 3);
- close(fd);
- }
- void port_out_h(int num)
- {
- int fd;
- char buf[MAX_BUF];
- char portnum[64];
- sprintf(portnum,"/sys/class/gpio/gpio%d/value",num);
- snprintf(buf,sizeof(buf),portnum);
- fd = open(buf, O_WRONLY);
- write(fd, "1", 2);
- close(fd);
- }
- void port_out_l(int num)
- {
- int fd;
- char buf[MAX_BUF];
- char portnum[64];
- sprintf(portnum,"/sys/class/gpio/gpio%d/value",num);
- snprintf(buf,sizeof(buf),portnum);
- fd = open(buf, O_WRONLY);
- write(fd, "0", 2);
- close(fd);
- }
- int port_in(int num,char numvalue)
- {
- int fd,value;
- char portvalue;
- char buf[MAX_BUF];
- char portnum[64];
- sprintf(portnum,"/sys/class/gpio/gpio%d/value",num);
- snprintf(buf,sizeof(buf),portnum);
- fd = open(buf, O_RDONLY);
- read(fd, &portvalue, 1);
- close(fd);
- if(portvalue==numvalue)
- return 1;
- else
- return 0;
- }
复制代码
这个SFTP软件,叫Cyberduck,需要可以自行去下载,免费的。
|
|