1.设计要求:使用stm32f103zet6精英开发板 采用TM1628驱动31个LED灯。
2.TM1628芯片要点
注意:无论是驱动共阴极数码管还是驱动共阳极数码管,SEG引脚只能接LED的阳极,GRID只能接LED的阴极,不可反接。
3.电路原理图:
4.程序代码:
.c文件
#include "TM1628.h"
u8 const CODE[]={0xC0,0xC2,0xC4,0xC6,0xC8,0xCA,0xCC};//GRID1 GRID2 GRID3 GRID4 GRID5 GRID6 GRID7
/*************************************
函数名称:Tm1628init
函数说明:TM1628初始化
函数参数:NULL
函数返回:NULL
*************************************/
void Tm1628init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //
GPIO_Init(GPIOB, &GPIO_InitStructure); //
GPIO_SetBits(GPIOB,GPIO_Pin_5);
Tm1628_ClearDisplay(); //清屏
Tm1628_GrayScale(2); //亮度2
}
/*************************************
函数名称:Tm1628_Write_Bit
函数说明:写单个字节
函数参数:命令返回
函数返回:NULL
*************************************/
void Tm1628_Write_Bit(uint8_t data)
{
uint8_t i;
for(i = 0; i < 8; i++)
{
if((data & 0x01) == 1)
{
Tm1628_DIO_H;
}
else
{
Tm1628_DIO_L;
}
Tm1628_CKL_L;
Tm1628_CKL_H;
data = data >> 1;
}
}
/*************************************
函数名称:Tm1628_Write_Command
函数说明:写命令
函数参数:命令参数
函数返回:NULL
*************************************/
void Tm1628_Write_Command(u8 unm)
{
Tm1628_STB_H;
Tm1628_STB_L;
Tm1628_Write_Bit(unm);
}
/*************************************
函数名称:Tm1628_Continuous
函数说明:固定写显示
函数参数:(1)add 地址 (2)data 数据
函数返回:NULL
*************************************/
void Tm1628_Continuous(uint8_t add,uint8_t data)
{
Tm1628_Write_Command(0x03);
Tm1628_Write_Command(0x44);
Tm1628_Write_Command(add);
Tm1628_Write_Bit(data);
Tm1628_Write_Command(0x8F);
Tm1628_STB_H;
}
/*************************************
函数名称:Tm1628_ClearDisplay
函数说明:清屏
函数参数:NULL
函数返回:NULL
*************************************/
void Tm1628_ClearDisplay(void)
{
uint8_t i;
Tm1628_Write_Command(0x03); //7位10段
for(i = 0; i < 7; i++)
{
Tm1628_Write_Command(CODE[i]);
Tm1628_Write_Bit(0x00);
Tm1628_STB_H;
}
}
/*************************************
函数名称:Tm1628_GrayScale
函数说明:用于亮度调节 0 - 9
函数参数:亮度 0 - 9
函数返回:NULL
*************************************/
void Tm1628_GrayScale(uint8_t data)
{
switch(data)
{
case(0): Tm1628_Write_Command(GrayScale_ON); break;
case(1): Tm1628_Write_Command(GrayScale1); break;
case(2): Tm1628_Write_Command(GrayScale2); break;
case(3): Tm1628_Write_Command(GrayScale3); break;
case(4): Tm1628_Write_Command(GrayScale4); break;
case(5): Tm1628_Write_Command(GrayScale5); break;
case(6): Tm1628_Write_Command(GrayScale6); break;
case(7): Tm1628_Write_Command(GrayScale7); break;
case(8): Tm1628_Write_Command(GrayScale8); break;
}
}
.h文件
#ifndef __TM1628_H
#define __TM1628_H
#include "sys.h"
#include "delay.h"
#define Tm1628_CKL_H GPIO_WriteBit(GPIOB,GPIO_Pin_6,Bit_SET)
#define Tm1628_CKL_L GPIO_WriteBit(GPIOB,GPIO_Pin_6,Bit_RESET)
#define Tm1628_DIO_H GPIO_WriteBit(GPIOB,GPIO_Pin_7,Bit_SET)
#define Tm1628_DIO_L GPIO_WriteBit(GPIOB,GPIO_Pin_7,Bit_RESET)
#define Tm1628_STB_H GPIO_WriteBit(GPIOB,GPIO_Pin_5,Bit_SET)
#define Tm1628_STB_L GPIO_WriteBit(GPIOB,GPIO_Pin_5,Bit_RESET)
#define GrayScale_OFF 0x80 //关显示
#define GrayScale_ON 0x81 //开显示
#define GrayScale1 0x88 //灰度等级1
#define GrayScale2 0x89 //灰度等级2
#define GrayScale3 0x8A //灰度等级3
#define GrayScale4 0x8B //灰度等级4
#define GrayScale5 0x8C //灰度等级5
#define GrayScale6 0x8D //灰度等级6
#define GrayScale7 0x8E //灰度等级7
#define GrayScale8 0x8F //灰度等级8
void Tm1628init(void); //TM1628初始化
void Tm1628_Fixed(uint8_t data, uint8_t add); //固定写地址 data 地址 add 数据
void Tm1628_Continuous(uint8_t add,uint8_t data);
void Tm1628_ClearDisplay(void); //清屏
void Tm1628_GrayScale(uint8_t data); //亮度调节
void Tm1628_Write_Command(u8 unm);
#endif
main主函数
int main(void)
{
RCC_Configuration();
Tm1628init();//TM1628初始化程序
while(1)
{
Tm1628_Continuous(0xCA,0xFF); //GRID6
Tm1628_Continuous(0xC8,0xFF); //GRID5
Tm1628_Continuous(0xC6,0xFF); //GRID4
Tm1628_Continuous(0xC4,0xFF); //GRID3
Tm1628_Continuous(0xC2,0xFF); //GRID2
Tm1628_Continuous(0xC0,0xFF); //GRID1
}
}
5.实物效果图