【博流BL606P音视频开发板】 + 驱动led数码管
[复制链接]
1.接线原理图。
TM1638模块主要由三根线进行控制,分别是STB,CLK和DIO三个接口。现在用GPIO3,GPIO4以及GPIO5来进行控制。

2.程序
程序由两部分组成,tm1638和main主程序组成。
TM1638由头文件和c文件,分别如下:
tm1638头文件,主要定义三个接口控制以及log打印
-
- #ifndef TM1638_H_
- #define TM1638_H_
-
- #define dio_l() hal_gpio_output_low(&gpio_dio)
- #define dio_h() hal_gpio_output_high(&gpio_dio)
-
- #define clk_l() hal_gpio_output_low(&gpio_clk)
- #define clk_h() hal_gpio_output_high(&gpio_clk)
-
- #define stb_l() hal_gpio_output_low(&gpio_stb)
- #define stb_h() hal_gpio_output_high(&gpio_stb)
-
- void init_tm1638(void);
-
- void disp_led(uint8_t *dat);
- void disp_log(void);
-
- #endif
tm1638.c文件,主要定义主板和tm163模块进行控制,以及数码显示。
- #include <aos/aos.h>
- #include <stdio.h>
- #include <sys_clk.h>
- #include "app_main.h"
- #include <drv/gpio.h>
-
- #include <aos/hal/gpio.h>
- #include <drv/pin.h>
- #include "tm1638.h"
-
- gpio_dev_t gpio_stb = { GPIO_PIN_3, OUTPUT_PUSH_PULL, NULL };
- gpio_dev_t gpio_clk= { GPIO_PIN_4, OUTPUT_PUSH_PULL, NULL };
- gpio_dev_t gpio_dio = { GPIO_PIN_5, OUTPUT_PUSH_PULL, NULL };
-
- uint8_t const tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
- uint8_t ds_log[8]={0x40,0x7c,0x38,0x7d,0x3f,0x7d,0x73,0x40};
-
- void tm1638_WriteDat(uint8_t dat)
- {
- uint8_t i;
- for(i=0;i<8;i++)
- {
- clk_l();
- if(dat&0X01)
- {
- dio_h();
- }
- else
- {
- dio_l();
- }
- dat>>=1;
- clk_h();
- }
- }
-
- void tm1638_WriteCmd(uint8_t cmd)
- {
- stb_l();
- tm1638_WriteDat(cmd);
- stb_h();
- }
-
- void tm1638_Write_Addr(uint8_t addr,uint8_t dat)
- {
- tm1638_WriteCmd(0x44);
- stb_l();
- tm1638_WriteDat(0xc0|addr);
- tm1638_WriteDat(dat);
- stb_h();
- }
-
- void tm1638_Write_LED(uint8_t LED_flag)
- {
- uint8_t 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 init_tm1638(void)
- {
- uint8_t i;
- csi_pin_set_mux( GPIO_PIN_3, PIN_FUNC_GPIO);
- csi_pin_set_mux( GPIO_PIN_4, PIN_FUNC_GPIO);
- csi_pin_set_mux( GPIO_PIN_5, PIN_FUNC_GPIO);
- hal_gpio_init(&gpio_stb);
- hal_gpio_init(&gpio_clk);
- hal_gpio_init(&gpio_dio);
-
- hal_gpio_output_low(&gpio_stb);
- hal_gpio_output_low(&gpio_clk);
- hal_gpio_output_low(&gpio_dio);
-
-
- tm1638_WriteCmd(0x8b);
- tm1638_WriteCmd(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[0]);
- }
- }
-
- void disp_led(uint8_t *dat)
- {
- uint8_t i;
- for(i=0;i<8;i++)
- {
- tm1638_Write_Addr(i<<1,tab[dat]);
- }
- }
-
- void disp_log(void)
- {
- uint8_t i;
- for(i=0;i<8;i++)
- {
- tm1638_Write_Addr(i<<1,ds_log);
- }
- }
-
-
main.c文件,给定程序主控制。
-
-
-
- #include <aos/aos.h>
- #include <stdio.h>
- #include <sys_clk.h>
- #include "app_main.h"
- #include <drv/gpio.h>
- #include "led.h"
- #include "tm1638.h"
-
- int main(int argc, char *argv[])
- {
- int i=0;
- uint8_t dispbuf[8];
- uint32_t cnt=0;
-
- board_yoc_init();
- init_led();
- init_tm1638();
-
- printf("\r\napp start core clock %d........\r\n", soc_get_cur_cpu_freq());
-
- disp_log();
- aos_msleep(2000);
-
-
-
-
- for(i=0;i<8;i++)
- {
- dispbuf=0;
- }
- while(1)
- {
- i++;
- printf("zhanghui %d\r\n",i);
-
- set_ledr();
- clr_ledg();
- clr_ledb();
- aos_msleep(100);
-
- clr_ledr();
- set_ledg();
- clr_ledb();
- aos_msleep(100);
-
- clr_ledr();
- clr_ledg();
- set_ledb();
- aos_msleep(100);
-
- clr_ledr();
- clr_ledg();
- clr_ledb();
- aos_msleep(100);
-
- if(cnt>999)
- {
- cnt=0;
- }
- cnt++;
- dispbuf[4]=cnt/1000;
- dispbuf[5]=(cnt%1000)/100;
- dispbuf[6]=((cnt%1000)%100)/10;
- dispbuf[7]=((cnt%1000)%100)%10;
- disp_led(dispbuf);
- }
- return 0;
- }
-
-
3效果
总体感觉还是很不错!
|