bsp_nixie_tube.h
#ifndef __BSP_NIXIE_TUBE_H
#define __BSP_NIXIE_TUBE_H
#include "stm32f10x.h"
#define LED_SHOW 1
#define get_bit(dat, bit) ((dat & (1<<bit))>>bit)
void Nixie_Tube_GPIO_Config(void);
void Show_Number(uint8_t number1, uint8_t number2, uint8_t number3, uint8_t number4);
#endif // __BSP_NIXIE_TUBE_H
bsp_nixie_tube.c
#include "bsp_nixie_tube.h"
// 共阳数码管真值表 0 1 2 3 4 5 6 7 8 9 A b C d E F H L ^ U P
uint8_t smgduan[21] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,
0x88,0x83,0xC6,0xA1,0x86,0x8E,0x89, 0xC7, 0xC8, 0xC1,0x8C};
// 初始化GPIO引脚
void Nixie_Tube_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
// 配置5个按键的引脚
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_15 ;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_4 |
GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 |
GPIO_Pin_9 | GPIO_Pin_13 | GPIO_Pin_14;
GPIO_Init(GPIOB, &GPIO_InitStruct);
}
// 延迟
void delay_ms(u16 time)
{
u16 i=0;
while(time--)
{
i=8000; //自己定义
while(i--) ;
}
}
// 数码管上显示的数据
void Show_Number(uint8_t number1, uint8_t number2, uint8_t number3, uint8_t number4)
{
// 取数值
uint8_t number_1 = smgduan[number1];
uint8_t number_2 = smgduan[number2];
uint8_t number_3 = smgduan[number3];
uint8_t number_4 = smgduan[number4];
GPIO_ResetBits(GPIOB, GPIO_Pin_14); // led1片选
// 点亮对应的数码管led灯
GPIO_SetBits(GPIOB, (GPIO_Pin_6 & (get_bit(number_1, 0) << 6)) | // a
(GPIO_Pin_8 & (get_bit(number_1, 1) << 8)) | // b
(GPIO_Pin_5 & (get_bit(number_1, 2) << 5)) | // c
(GPIO_Pin_3 & (get_bit(number_1, 4) << 3)) | // e
(GPIO_Pin_4 & (get_bit(number_1, 5) << 4)) | // f
(GPIO_Pin_7 & (get_bit(number_1, 6) << 7))); // g
GPIO_SetBits(GPIOA, GPIO_Pin_15 & (get_bit(number_1, 3) << 15)); // d
// GPIO_SetBits(GPIOB, GPIO_Pin_6 & (get_bit(number_1, 0) << 6)); // a
// GPIO_SetBits(GPIOB, GPIO_Pin_8 & (get_bit(number_1, 1) << 8)); // b
// GPIO_SetBits(GPIOB, GPIO_Pin_5 & (get_bit(number_1, 2) << 5)); // c
// GPIO_SetBits(GPIOA, GPIO_Pin_15 & (get_bit(number_1, 3) << 15)); // d 不能点亮
// GPIO_SetBits(GPIOB, GPIO_Pin_3 & (get_bit(number_1, 4) << 3)); // e 不能点亮
// GPIO_SetBits(GPIOB, GPIO_Pin_4 & (get_bit(number_1, 5) << 4)); // f 不能点亮
// GPIO_SetBits(GPIOB, GPIO_Pin_7 & (get_bit(number_1, 6) << 7)); // g
delay_ms(1);
GPIO_ResetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_5 |
GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7);
GPIO_ResetBits(GPIOA, GPIO_Pin_15);
GPIO_SetBits(GPIOB, GPIO_Pin_14); //关闭
GPIO_ResetBits(GPIOB, GPIO_Pin_13); // led2片选
GPIO_SetBits(GPIOB, (GPIO_Pin_6 & (get_bit(number_2, 0) << 6)) | // a
(GPIO_Pin_8 & (get_bit(number_2, 1) << 8)) | // c
(GPIO_Pin_5 & (get_bit(number_2, 2) << 5)) | // e
(GPIO_Pin_3 & (get_bit(number_2, 4) << 3)) | // f
(GPIO_Pin_4 & (get_bit(number_2, 5) << 4)) | // g
(GPIO_Pin_7 & (get_bit(number_2, 6) << 7))); // a
GPIO_SetBits(GPIOA, GPIO_Pin_15 & (get_bit(number_2, 3) << 15)); // d
delay_ms(1);
GPIO_ResetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_5 |
GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7);
GPIO_ResetBits(GPIOA, GPIO_Pin_15);
GPIO_SetBits(GPIOB, GPIO_Pin_13); // led2片选
GPIO_ResetBits(GPIOB, GPIO_Pin_1); // led3片选
GPIO_SetBits(GPIOB, (GPIO_Pin_6 & (get_bit(number_3, 0) << 6)) | // a
(GPIO_Pin_8 & (get_bit(number_3, 1) << 8)) | // b
(GPIO_Pin_5 & (get_bit(number_3, 2) << 5)) | // c
(GPIO_Pin_3 & (get_bit(number_3, 4) << 3)) | // e
(GPIO_Pin_4 & (get_bit(number_3, 5) << 4)) | // f
(GPIO_Pin_7 & (get_bit(number_3, 6) << 7))); // g
GPIO_SetBits(GPIOA, GPIO_Pin_15 & (get_bit(number_3, 3) << 15)); // d
delay_ms(1);
GPIO_ResetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_5 |
GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7);
GPIO_ResetBits(GPIOA, GPIO_Pin_15);
GPIO_SetBits(GPIOB, GPIO_Pin_1);
GPIO_ResetBits(GPIOB, GPIO_Pin_0); // led4片选
GPIO_SetBits(GPIOB, (GPIO_Pin_6 & (get_bit(number_4, 0) << 6)) | // a
(GPIO_Pin_8 & (get_bit(number_4, 1) << 8)) | // b
(GPIO_Pin_5 & (get_bit(number_4, 2) << 5)) | // c
(GPIO_Pin_3 & (get_bit(number_4, 4) << 3)) | // e
(GPIO_Pin_4 & (get_bit(number_4, 5) << 4)) | // f
(GPIO_Pin_7 & (get_bit(number_4, 6) << 7))); // g
GPIO_SetBits(GPIOA, GPIO_Pin_15 & (get_bit(number_4, 3) << 15)); // d
delay_ms(1);
GPIO_ResetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_5 |
GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7);
GPIO_ResetBits(GPIOA, GPIO_Pin_15);
GPIO_SetBits(GPIOB, GPIO_Pin_0);
}
main.c
/**********************
芯片:STM32F103C8T6
实现功能:
引脚:
***********************/
#include "stm32f10x.h"
#include "bsp_nixie_tube.h"
void delay_ms2(u16 time)
{
u16 i=0;
while(time--)
{
i=8000; //自己定义
while(i--) ;
}
}
int main()
{
uint16_t n = 0;
uint8_t a = 1;
uint8_t b = 0;
uint8_t c = 0;
uint8_t d = 0;
Nixie_Tube_GPIO_Config(); // 初始化数码管GPIO
while(1)
{
if(a>9)
{
a = 0;
b++;
}
if(b>9)
{
b = 0;
c++;
}
if(c>9)
{
c = 0;
d++;
}
if(d>9)
{
a = 0;
b = 0;
c = 0;
d = 0;
}
Show_Number(d, c, b, a);
n++;
if(n > 50)
{
n = 0;
a++;
}
}
}
优化后的
bsp_nixie_tube.h
#ifndef __BSP_NIXIE_TUBE_H
#define __BSP_NIXIE_TUBE_H
#include "stm32f10x.h"
#define get_bit(dat, bit) ((dat & (1<<bit))>>bit)
#define OPEN 1
#define CLOCK 0
// 关闭数码管
#define SET_TUBE_BLACK_B GPIO_ResetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_5 |\
GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7);
#define SET_TUBE_BLACK_A GPIO_ResetBits(GPIOA, GPIO_Pin_15);
// 数码管片选开关
#define CHIP_1(OPEN_OR_CLOCK) if(OPEN_OR_CLOCK) GPIO_ResetBits(GPIOB, GPIO_Pin_14);\
else GPIO_SetBits(GPIOB, GPIO_Pin_14)
#define CHIP_2(OPEN_OR_CLOCK) if(OPEN_OR_CLOCK) GPIO_ResetBits(GPIOB, GPIO_Pin_13);\
else GPIO_SetBits(GPIOB, GPIO_Pin_13)
#define CHIP_3(OPEN_OR_CLOCK) if(OPEN_OR_CLOCK) GPIO_ResetBits(GPIOB, GPIO_Pin_1);\
else GPIO_SetBits(GPIOB, GPIO_Pin_1)
#define CHIP_4(OPEN_OR_CLOCK) if(OPEN_OR_CLOCK) GPIO_ResetBits(GPIOB, GPIO_Pin_0);\
else GPIO_SetBits(GPIOB, GPIO_Pin_0)
// 点亮数码管
#define SET_TUBE_LIGHT(num) \
GPIO_SetBits(GPIOB, \
((get_bit(num, 0) << 6)) | /*GPIO_Pin_6 a*/ \
((get_bit(num, 1) << 8)) | /*GPIO_Pin_8 b*/ \
((get_bit(num, 2) << 5)) | /*GPIO_Pin_5 c*/ \
((get_bit(num, 4) << 3)) | /*GPIO_Pin_3 e*/ \
((get_bit(num, 5) << 4)) | /*GPIO_Pin_4 f*/ \
((get_bit(num, 6) << 7))); /*GPIO_Pin_7 g*/ \
GPIO_SetBits(GPIOA, (get_bit(num, 3) << 15)); // GPIO_Pin_15 d
void Nixie_Tube_GPIO_Config(void);
void Show_Number(uint8_t number1, uint8_t number2, uint8_t number3, uint8_t number4);
#endif // __BSP_NIXIE_TUBE_H
bsp_nixie_tube.c
#include "bsp_nixie_tube.h"
// 共阳数码管真值表 0 1 2 3 4 5 6 7 8 9 A b C d E F H L ^ U P
uint8_t smgduan[21] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,
0x88,0x83,0xC6,0xA1,0x86,0x8E,0x89, 0xC7, 0xC8, 0xC1,0x8C};
// 初始化GPIO引脚
void Nixie_Tube_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
// 配置5个按键的引脚
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_15 ;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_4 |
GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 |
GPIO_Pin_9 | GPIO_Pin_13 | GPIO_Pin_14;
GPIO_Init(GPIOB, &GPIO_InitStruct);
}
// 延时
void delay_ms(u16 time)
{
u16 i=0;
while(time--)
{
i=8000; //自己定义
while(i--) ;
}
}
// 点亮数码管
void SET_Tube_Light(uint8_t num)
{
SET_TUBE_LIGHT(num);
}
// 设置数码管
void SET_Tube(uint8_t num)
{
SET_Tube_Light(num);
delay_ms(1);
SET_TUBE_BLACK_B;
SET_TUBE_BLACK_A;
}
// 数码管上显示的数据
void Show_Number(uint8_t number1, uint8_t number2, uint8_t number3, uint8_t number4)
{
// 取数值
uint8_t number_1 = smgduan[number1];
uint8_t number_2 = smgduan[number2];
uint8_t number_3 = smgduan[number3];
uint8_t number_4 = smgduan[number4];
CHIP_1(OPEN);
SET_Tube(number_1);
CHIP_1(CLOCK);
CHIP_2(OPEN);
SET_Tube(number_2);
CHIP_2(CLOCK);
CHIP_3(OPEN);
SET_Tube(number_3);
CHIP_3(CLOCK);
CHIP_4(OPEN);
SET_Tube(number_4);
CHIP_4(CLOCK);
}