【正点原子RV1126 AI Linux开发板】 驱动LED数码管
[复制链接]
测试使用GPIO驱动LED数码管。
一、硬件电路
1.1、TM1638模块
1.2、驱动模块时序图
1.3、开发板连接
使用下面上个引脚来驱动TM1638
二、程序
2.1、tm1638.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <semaphore.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <malloc.h>
#include <termios.h>
#include "math.h"
#include <stdbool.h>
#include <sys/time.h>
#include "tm1638.h"
#define STB_GPIO_INDEX "92" //GPIO2_D4
#define CLK_GPIO_INDEX "94" //GPIO2_D6
#define DIO_GPIO_INDEX "18" //GPIO0_C2
int init_port(void)
{
int fd;
char gpio_path_direction[35] = {0};
//初始化STB
fd = open("/sys/class/gpio/export", O_WRONLY);
if(fd < 0) {
return 1;
}
write(fd, STB_GPIO_INDEX, strlen(STB_GPIO_INDEX));
close(fd);
//初始化CLK
fd = open("/sys/class/gpio/export", O_WRONLY);
if(fd < 0) {
return 2;
}
write(fd, CLK_GPIO_INDEX, strlen(CLK_GPIO_INDEX));
close(fd);
//初始化DIO
fd = open("/sys/class/gpio/export", O_WRONLY);
if(fd < 0) {
return 3;
}
write(fd, DIO_GPIO_INDEX, strlen(DIO_GPIO_INDEX));
close(fd);
//设置端口STB方向
sprintf(gpio_path_direction, "/sys/class/gpio/gpio%s/direction", STB_GPIO_INDEX);
fd = open(gpio_path_direction, O_WRONLY);
if(fd < 0)
{
return 4;
}
write(fd, "out", strlen("out"));
close(fd);
//设置端口CLK方向
sprintf(gpio_path_direction, "/sys/class/gpio/gpio%s/direction", CLK_GPIO_INDEX);
fd = open(gpio_path_direction, O_WRONLY);
if(fd < 0)
{
return 5;
}
write(fd, "out", strlen("out"));
close(fd);
//设置端口DIO方向
sprintf(gpio_path_direction, "/sys/class/gpio/gpio%s/direction", DIO_GPIO_INDEX);
fd = open(gpio_path_direction, O_WRONLY);
if(fd < 0)
{
return 6;
}
write(fd, "out", strlen("out"));
close(fd);
return 0;
}
int stb_l(void)
{
int fd;
char gpio_value[35] = {0};
sprintf(gpio_value, "/sys/class/gpio/gpio%s/value", STB_GPIO_INDEX);
fd = open(gpio_value, O_WRONLY);
if(fd < 0)
{
return 1;
}
write(fd, "0", 1);
close(fd);
}
int stb_h(void)
{
int fd;
char gpio_value[35] = {0};
sprintf(gpio_value, "/sys/class/gpio/gpio%s/value", STB_GPIO_INDEX);
fd = open(gpio_value, O_WRONLY);
if(fd < 0)
{
return 1;
}
write(fd, "1", 1);
close(fd);
}
int clk_l(void)
{
int fd;
char gpio_value[35] = {0};
sprintf(gpio_value, "/sys/class/gpio/gpio%s/value", CLK_GPIO_INDEX);
fd = open(gpio_value, O_WRONLY);
if(fd < 0)
{
return 1;
}
write(fd, "0", 1);
close(fd);
}
int clk_h(void)
{
int fd;
char gpio_value[35] = {0};
sprintf(gpio_value, "/sys/class/gpio/gpio%s/value", CLK_GPIO_INDEX);
fd = open(gpio_value, O_WRONLY);
if(fd < 0)
{
return 1;
}
write(fd, "1", 1);
close(fd);
}
int dio_l(void)
{
int fd;
char gpio_value[35] = {0};
sprintf(gpio_value, "/sys/class/gpio/gpio%s/value", DIO_GPIO_INDEX);
fd = open(gpio_value, O_WRONLY);
if(fd < 0)
{
return 1;
}
write(fd, "0", 1);
close(fd);
}
int dio_h(void)
{
int fd;
char gpio_value[35] = {0};
sprintf(gpio_value, "/sys/class/gpio/gpio%s/value", DIO_GPIO_INDEX);
fd = open(gpio_value, O_WRONLY);
if(fd < 0)
{
return 1;
}
write(fd, "1", 1);
close(fd);
}
/*
#define dio_l() gpio_reset_io1_6();
#define dio_h() gpio_set_io1_6();
#define clk_l() gpio_reset_io1_4();
#define clk_h() gpio_set_io1_4();
#define stb_l() gpio_reset_io1_3();
#define stb_h() gpio_set_io1_3();
*/
unsigned char const tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F, 0x77,0x00,0x5E,0x48};//0123456789 A_d=
unsigned char const tabdp[]={0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF};
void tm1638_WriteDat(unsigned char dat)
{
int i;
for(i=0;i<8;i++)
{
clk_l();
if(dat&0X01)
{
dio_h();
}
else
{
dio_l();
}
dat>>=1;
clk_h();
}
}
void tm1638_Write_cmd(unsigned char cmd)
{
stb_l();
tm1638_WriteDat(cmd);
stb_h();
}
void tm1638_Write_Addr(unsigned char addr,unsigned char dat)
{
tm1638_Write_cmd(0x44);
stb_l();
tm1638_WriteDat(0xc0|addr);
tm1638_WriteDat(dat);
stb_h();
}
void tm1638_Write_LED(unsigned char LED_flag)
{
int i;
for(i=0;i<8;i++)
{
if(LED_flag&(1<<i))
{
tm1638_Write_Addr(2*i+1,1);
}
else
{
tm1638_Write_Addr(2*i+1,0);
}
}
}
void tm1638_init(void)
{
int res=0;
unsigned char i;
res=init_port();
printf("res=%d\r\n",res);
dio_h();
clk_h();
stb_h();
tm1638_Write_cmd(0x8b);
tm1638_Write_cmd(0x40);
/*stb_l();
tm1638_WriteDat(0xc0);
for(i=0;i<16;i++)
{
tm1638_WriteDat(0x00);
}
stb_h();*/
for(i=0;i<8;i++)
{
tm1638_Write_Addr(i<<1,tab[12]);
}
}
void disp_led(unsigned char *dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
tm1638_Write_Addr(i<<1,tab[dat[i]]);
/*if(i==6)
{
tm1638_Write_Addr(i<<1,tabdp[dat[i]]);
}
else
{
tm1638_Write_Addr(i<<1,tab[dat[i]]);
}*/
}
}
2.2、main.c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include "tm1638.h"
unsigned char dispdat[8];
unsigned char i=0;
int js=0;
int main(int argc, char *argv[])
{
tm1638_init();
printf("tm1638_init\r\n");
while(1)
{
js++;
if(js>9999)
{
js=0;
}
//tm1638 disp
dispdat[0]=0;
dispdat[1]=0;
dispdat[2]=0;
dispdat[3]=0;
dispdat[4]=js/1000;
dispdat[5]=js%1000/100;
dispdat[6]=js%1000%100/10;
dispdat[7]=js%1000%100%10;
disp_led(dispdat);
usleep(10000);
}
}
三、程序运行
编译后,运行程序
led
|