此程序为基于TM1640 驱动LED灯进行累加流水
单片机源程序如下:
- #include "ucos_ii.h"
- #include "driver_i2c1640.h"
- #include "driver_sys.h"
- #include "sys_time.h"
- //位选地址
- #define DIG1 0xc0
- #define DIG2 0xc1
- #define DIG3 0xc2
- #define DIG4 0xc3
- #define DIG5 0xc4
- #define DIG6 0xc5
- #define DIG7 0xc6
- #define DIG8 0xc7
- #define DIG9 0xc8
- #define DIG10 0xc9
- #define DIG11 0xca
- #define DIG12 0xcb
- #define DIG13 0xcc
- #define DIG14 0xcd
- #define DIG15 0xce
- #define DIG16 0xcf
- //显示亮度级别
- #define LEVEL_OFF 0x80
- #define LEVEL_1 0x88
- #define LEVEL_2 0x89
- #define LEVEL_3 0x8a
- #define LEVEL_4 0x8b
- #define LEVEL_5 0x8c
- #define LEVEL_6 0x8d
- #define LEVEL_7 0x8e
- #define LEVEL_8 0x8f
- #define DATA_CMD_ADDR_INC 0x40 //数据命令设置 地址自动加1
- #define DATA_CMD_ADDR_FIXED 0x44 //固定地址
- #define addr_TM1640 (0xc0+0x01)
- #define DSPTM1640_DIS 0
- #define DSPTM1640_ENB 1
- #define TM1640_DELAY delay_us(2)
- /***************************TM1640显示缓冲区*********************************/
- unsigned char led_buffer[48]={0}; //8位数字缓冲区 //18个灯缓冲区
- /******************************************************************************
- * Function Name --> IIC_GPIO_Init
- * Description --> GPIO初始化
- * Input --> none
- * Output --> none
- * Reaturn --> none
- ******************************************************************************/
- void IIC_1640_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
- GPIO_InitStructure.GPIO_Pin = IIC_1642_SCL_PIN|IIC_1642_SDA_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_Init(IIC_1642_SCL_PORT, &GPIO_InitStructure);
- IIC_Init_1640LC();
- }
- //IIC初始化
- void IIC_Init_1640LC(void)
- {
- IIC_1642_SDA_H;
- IIC_1642_SCL_H;
-
- IIC_Start_1640LC();
- IIC_Send_Byte_1640LC(DATA_CMD_ADDR_INC);//设置地址自动+1模式
- IIC_Stop_1640LC();
- IIC_Start_1640LC();
- IIC_Send_Byte_1640LC(LEVEL_8);//显示亮度
- IIC_Stop_1640LC();
- led_clr();
- }
- //产生IIC起始信号
- void IIC_Start_1640LC(void)
- {
- IIC_1642_SCL_H;
- TM1640_DELAY;
- IIC_1642_SDA_H;
- TM1640_DELAY;
- IIC_1642_SDA_L;//START:when CLK is high,DATA change form high to low
- TM1640_DELAY;
- }
- //产生IIC停止信号
- void IIC_Stop_1640LC(void)
- {
- IIC_1642_SCL_L;
- TM1640_DELAY;
- IIC_1642_SDA_L;//STOP:when CLK is high DATA change form low to high
- TM1640_DELAY;
- IIC_1642_SCL_H;
- TM1640_DELAY;
- IIC_1642_SDA_H;//发送I2C总线结束信号
- TM1640_DELAY;
- }
- void IIC_Send_Byte_1640LC(u8 txd)
- {
- u8 t;
- for(t=0;t<8;t++)
- {
- IIC_1642_SCL_L;//拉低时钟开始数据传输
- TM1640_DELAY;
- if(txd&0x01)
- {
- IIC_1642_SDA_H;
- TM1640_DELAY; //对TEA5767这三个延时都是必须的
- }
- else
- {
- IIC_1642_SDA_L;
- TM1640_DELAY; //对TEA5767这三个延时都是必须的
- }
- IIC_1642_SCL_H;
- txd = txd>>1;
- TM1640_DELAY; //对TEA5767这三个延时都是必须的
- }
- }
- /*将TM1640显示缓冲区写入到芯片中*/
- /*可能需要在语句间增加必要的延时*/
- /*如果128个点全部用到,需要修改变量I的值*/
- void TM1640_display(uint8_t start_address,uint8_t data)
- {
- uint8_t i;
- IIC_Start_1640LC();
- IIC_Send_Byte_1640LC(DATA_CMD_ADDR_FIXED); //设置起始地址
- IIC_Stop_1640LC();
- IIC_Start_1640LC();
- IIC_Send_Byte_1640LC(start_address); //设置起始地址
- IIC_Send_Byte_1640LC(data);
- IIC_Stop_1640LC();
- IIC_Start_1640LC();
- IIC_Send_Byte_1640LC(LEVEL_8); //设置起始地址
- IIC_Stop_1640LC();
- }
- void deal_Disp()
- {
- u8 i,j;
- u16 led_data=0xE0;
- for(j=0;j<6;j++)
- {
- led_data |= (1<<j);
- for(i=0;i<5;i++)
- {
- //最后、显示
- TM1640_display(DIG1+i,led_data);
- OSTimeDlyHMSM(0, 0, 0, 100);
- }
- }
- led_clr();
- }
- void led_clr()
- {
- uint8_t i;
- u16 led_data=0;
- for(i=0;i<16;i++)
- {
- TM1640_display(DIG1+i,led_data);
- }
- }
- void led_green_full()
- {
- uint8_t i;
- u16 led_data=0xFF;
- u16 red_data = 0x00;
- for(i=0;i<5;i++)
- {
- TM1640_display(DIG11+i,led_data);
- }
- for(i=0;i<10;i++)
- {
- TM1640_display(DIG1+i,red_data);
- }
- }
- void led_red_full()
- {
- uint8_t i;
- u16 led_data=0xFF;
- u16 blue_data = 0x00;
- for(i=0;i<5;i++)
- {
- TM1640_display(DIG6+i,led_data);
- }
- for(i=0;i<5;i++)
- {
- TM1640_display(DIG1+i,blue_data);
- }
- for(i=0;i<5;i++)
- {
- TM1640_display(DIG11+i,blue_data);
- }
- }
- void led_blue_full()
- {
- uint8_t i;
- u16 led_data=0xFF;
- u16 green_data = 0x00;
- for(i=0;i<5;i++)
- {
- TM1640_display(DIG1+i,led_data);
- }
- for(i=0;i<10;i++)
- {
- TM1640_display(DIG6+i,green_data);
- }
- }
复制代码
- #ifndef _DRIVER_I2C1640_H
- #define _DRIVER_I2C1640_H
- #include "stdlib.h"
- #include "stm32f2xx.h"
- typedef unsigned char u8;
- typedef unsigned short u16;
- typedef unsigned int u32;
- /* IIC_SCL时钟端口、引脚定义 */
- #define IIC_1642_SCL_PORT GPIOF
- #define IIC_1642_SCL_PIN (GPIO_Pin_1)
- #define IIC_1642_SCL_PORT_RCC RCC_AHB1Periph_GPIOF
- /* IIC_SDA时钟端口、引脚定义 */
- #define IIC_1642_SDA_PORT GPIOF
- #define IIC_1642_SDA_PIN (GPIO_Pin_0)
- #define IIC_1642_SDA_PORT_RCC RCC_AHB1Periph_GPIOF
- /******************************************************************************
- 位带操作
- ******************************************************************************/
- #define IIC_1642_SCL_H GPIO_SetBits(IIC_1642_SCL_PORT, IIC_1642_SCL_PIN)
- #define IIC_1642_SCL_L GPIO_ResetBits(IIC_1642_SCL_PORT, IIC_1642_SCL_PIN)
- #define IIC_1642_SDA_H GPIO_SetBits(IIC_1642_SDA_PORT, IIC_1642_SDA_PIN)
- #define IIC_1642_SDA_L GPIO_ResetBits(IIC_1642_SDA_PORT, IIC_1642_SDA_PIN)
- #define IN_1642_SDA GPIO_ReadInputDataBit(IIC_1642_SDA_PORT, IIC_1642_SDA_PIN) //IIC读取数据用
- //IIC所有操作函数
- void IIC_1640_GPIO_Init(void);
- void IIC_Init_1640LC(void); //初始化IIC的IO口
- void IIC_Start_1640LC(void); //发送IIC开始信号
- void IIC_Stop_1640LC(void); //发送IIC停止信号
- void IIC_Send_Byte_1640LC(u8 txd);
- void IIC_Send_Byte_1640(unsigned char name,unsigned char display);
- void deal_Disp();
- void led_clr();
- void led_green_full();
- void led_red_full();
- void led_blue_full();
- #endif
复制代码
|