bsp_nixie_tube.h

  1. #ifndef __BSP_NIXIE_TUBE_H
  2. #define __BSP_NIXIE_TUBE_H
  3. #include "stm32f10x.h"
  4. #define LED_SHOW 1
  5. #define get_bit(dat, bit) ((dat & (1<<bit))>>bit)
  6. void Nixie_Tube_GPIO_Config(void);
  7. void Show_Number(uint8_t number1, uint8_t number2, uint8_t number3, uint8_t number4);
  8. #endif // __BSP_NIXIE_TUBE_H

bsp_nixie_tube.c

  1. #include "bsp_nixie_tube.h"
  2. // 共阳数码管真值表 0 1 2 3 4 5 6 7 8 9 A b C d E F H L ^ U P
  3. uint8_t smgduan[21] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,
  4. 0x88,0x83,0xC6,0xA1,0x86,0x8E,0x89, 0xC7, 0xC8, 0xC1,0x8C};
  5. // 初始化GPIO引脚
  6. void Nixie_Tube_GPIO_Config(void)
  7. {
  8. GPIO_InitTypeDef GPIO_InitStruct;
  9. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
  10. // 配置5个按键的引脚
  11. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_15 ;
  12. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  13. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
  14. GPIO_Init(GPIOA, &GPIO_InitStruct);
  15. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_4 |
  16. GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 |
  17. GPIO_Pin_9 | GPIO_Pin_13 | GPIO_Pin_14;
  18. GPIO_Init(GPIOB, &GPIO_InitStruct);
  19. }
  20. // 延迟
  21. void delay_ms(u16 time)
  22. {
  23. u16 i=0;
  24. while(time--)
  25. {
  26. i=8000; //自己定义
  27. while(i--) ;
  28. }
  29. }
  30. // 数码管上显示的数据
  31. void Show_Number(uint8_t number1, uint8_t number2, uint8_t number3, uint8_t number4)
  32. {
  33. // 取数值
  34. uint8_t number_1 = smgduan[number1];
  35. uint8_t number_2 = smgduan[number2];
  36. uint8_t number_3 = smgduan[number3];
  37. uint8_t number_4 = smgduan[number4];
  38. GPIO_ResetBits(GPIOB, GPIO_Pin_14); // led1片选
  39. // 点亮对应的数码管led灯
  40. GPIO_SetBits(GPIOB, (GPIO_Pin_6 & (get_bit(number_1, 0) << 6)) | // a
  41. (GPIO_Pin_8 & (get_bit(number_1, 1) << 8)) | // b
  42. (GPIO_Pin_5 & (get_bit(number_1, 2) << 5)) | // c
  43. (GPIO_Pin_3 & (get_bit(number_1, 4) << 3)) | // e
  44. (GPIO_Pin_4 & (get_bit(number_1, 5) << 4)) | // f
  45. (GPIO_Pin_7 & (get_bit(number_1, 6) << 7))); // g
  46. GPIO_SetBits(GPIOA, GPIO_Pin_15 & (get_bit(number_1, 3) << 15)); // d
  47. // GPIO_SetBits(GPIOB, GPIO_Pin_6 & (get_bit(number_1, 0) << 6)); // a
  48. // GPIO_SetBits(GPIOB, GPIO_Pin_8 & (get_bit(number_1, 1) << 8)); // b
  49. // GPIO_SetBits(GPIOB, GPIO_Pin_5 & (get_bit(number_1, 2) << 5)); // c
  50. // GPIO_SetBits(GPIOA, GPIO_Pin_15 & (get_bit(number_1, 3) << 15)); // d 不能点亮
  51. // GPIO_SetBits(GPIOB, GPIO_Pin_3 & (get_bit(number_1, 4) << 3)); // e 不能点亮
  52. // GPIO_SetBits(GPIOB, GPIO_Pin_4 & (get_bit(number_1, 5) << 4)); // f 不能点亮
  53. // GPIO_SetBits(GPIOB, GPIO_Pin_7 & (get_bit(number_1, 6) << 7)); // g
  54. delay_ms(1);
  55. GPIO_ResetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_5 |
  56. GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7);
  57. GPIO_ResetBits(GPIOA, GPIO_Pin_15);
  58. GPIO_SetBits(GPIOB, GPIO_Pin_14); //关闭
  59. GPIO_ResetBits(GPIOB, GPIO_Pin_13); // led2片选
  60. GPIO_SetBits(GPIOB, (GPIO_Pin_6 & (get_bit(number_2, 0) << 6)) | // a
  61. (GPIO_Pin_8 & (get_bit(number_2, 1) << 8)) | // c
  62. (GPIO_Pin_5 & (get_bit(number_2, 2) << 5)) | // e
  63. (GPIO_Pin_3 & (get_bit(number_2, 4) << 3)) | // f
  64. (GPIO_Pin_4 & (get_bit(number_2, 5) << 4)) | // g
  65. (GPIO_Pin_7 & (get_bit(number_2, 6) << 7))); // a
  66. GPIO_SetBits(GPIOA, GPIO_Pin_15 & (get_bit(number_2, 3) << 15)); // d
  67. delay_ms(1);
  68. GPIO_ResetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_5 |
  69. GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7);
  70. GPIO_ResetBits(GPIOA, GPIO_Pin_15);
  71. GPIO_SetBits(GPIOB, GPIO_Pin_13); // led2片选
  72. GPIO_ResetBits(GPIOB, GPIO_Pin_1); // led3片选
  73. GPIO_SetBits(GPIOB, (GPIO_Pin_6 & (get_bit(number_3, 0) << 6)) | // a
  74. (GPIO_Pin_8 & (get_bit(number_3, 1) << 8)) | // b
  75. (GPIO_Pin_5 & (get_bit(number_3, 2) << 5)) | // c
  76. (GPIO_Pin_3 & (get_bit(number_3, 4) << 3)) | // e
  77. (GPIO_Pin_4 & (get_bit(number_3, 5) << 4)) | // f
  78. (GPIO_Pin_7 & (get_bit(number_3, 6) << 7))); // g
  79. GPIO_SetBits(GPIOA, GPIO_Pin_15 & (get_bit(number_3, 3) << 15)); // d
  80. delay_ms(1);
  81. GPIO_ResetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_5 |
  82. GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7);
  83. GPIO_ResetBits(GPIOA, GPIO_Pin_15);
  84. GPIO_SetBits(GPIOB, GPIO_Pin_1);
  85. GPIO_ResetBits(GPIOB, GPIO_Pin_0); // led4片选
  86. GPIO_SetBits(GPIOB, (GPIO_Pin_6 & (get_bit(number_4, 0) << 6)) | // a
  87. (GPIO_Pin_8 & (get_bit(number_4, 1) << 8)) | // b
  88. (GPIO_Pin_5 & (get_bit(number_4, 2) << 5)) | // c
  89. (GPIO_Pin_3 & (get_bit(number_4, 4) << 3)) | // e
  90. (GPIO_Pin_4 & (get_bit(number_4, 5) << 4)) | // f
  91. (GPIO_Pin_7 & (get_bit(number_4, 6) << 7))); // g
  92. GPIO_SetBits(GPIOA, GPIO_Pin_15 & (get_bit(number_4, 3) << 15)); // d
  93. delay_ms(1);
  94. GPIO_ResetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_5 |
  95. GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7);
  96. GPIO_ResetBits(GPIOA, GPIO_Pin_15);
  97. GPIO_SetBits(GPIOB, GPIO_Pin_0);
  98. }

main.c

  1. /**********************
  2. 芯片:STM32F103C8T6
  3. 实现功能:
  4. 引脚:
  5. ***********************/
  6. #include "stm32f10x.h"
  7. #include "bsp_nixie_tube.h"
  8. void delay_ms2(u16 time)
  9. {
  10. u16 i=0;
  11. while(time--)
  12. {
  13. i=8000; //自己定义
  14. while(i--) ;
  15. }
  16. }
  17. int main()
  18. {
  19. uint16_t n = 0;
  20. uint8_t a = 1;
  21. uint8_t b = 0;
  22. uint8_t c = 0;
  23. uint8_t d = 0;
  24. Nixie_Tube_GPIO_Config(); // 初始化数码管GPIO
  25. while(1)
  26. {
  27. if(a>9)
  28. {
  29. a = 0;
  30. b++;
  31. }
  32. if(b>9)
  33. {
  34. b = 0;
  35. c++;
  36. }
  37. if(c>9)
  38. {
  39. c = 0;
  40. d++;
  41. }
  42. if(d>9)
  43. {
  44. a = 0;
  45. b = 0;
  46. c = 0;
  47. d = 0;
  48. }
  49. Show_Number(d, c, b, a);
  50. n++;
  51. if(n > 50)
  52. {
  53. n = 0;
  54. a++;
  55. }
  56. }
  57. }

优化后的

bsp_nixie_tube.h

  1. #ifndef __BSP_NIXIE_TUBE_H
  2. #define __BSP_NIXIE_TUBE_H
  3. #include "stm32f10x.h"
  4. #define get_bit(dat, bit) ((dat & (1<<bit))>>bit)
  5. #define OPEN 1
  6. #define CLOCK 0
  7. // 关闭数码管
  8. #define SET_TUBE_BLACK_B GPIO_ResetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_5 |\
  9. GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7);
  10. #define SET_TUBE_BLACK_A GPIO_ResetBits(GPIOA, GPIO_Pin_15);
  11. // 数码管片选开关
  12. #define CHIP_1(OPEN_OR_CLOCK) if(OPEN_OR_CLOCK) GPIO_ResetBits(GPIOB, GPIO_Pin_14);\
  13. else GPIO_SetBits(GPIOB, GPIO_Pin_14)
  14. #define CHIP_2(OPEN_OR_CLOCK) if(OPEN_OR_CLOCK) GPIO_ResetBits(GPIOB, GPIO_Pin_13);\
  15. else GPIO_SetBits(GPIOB, GPIO_Pin_13)
  16. #define CHIP_3(OPEN_OR_CLOCK) if(OPEN_OR_CLOCK) GPIO_ResetBits(GPIOB, GPIO_Pin_1);\
  17. else GPIO_SetBits(GPIOB, GPIO_Pin_1)
  18. #define CHIP_4(OPEN_OR_CLOCK) if(OPEN_OR_CLOCK) GPIO_ResetBits(GPIOB, GPIO_Pin_0);\
  19. else GPIO_SetBits(GPIOB, GPIO_Pin_0)
  20. // 点亮数码管
  21. #define SET_TUBE_LIGHT(num) \
  22. GPIO_SetBits(GPIOB, \
  23. ((get_bit(num, 0) << 6)) | /*GPIO_Pin_6 a*/ \
  24. ((get_bit(num, 1) << 8)) | /*GPIO_Pin_8 b*/ \
  25. ((get_bit(num, 2) << 5)) | /*GPIO_Pin_5 c*/ \
  26. ((get_bit(num, 4) << 3)) | /*GPIO_Pin_3 e*/ \
  27. ((get_bit(num, 5) << 4)) | /*GPIO_Pin_4 f*/ \
  28. ((get_bit(num, 6) << 7))); /*GPIO_Pin_7 g*/ \
  29. GPIO_SetBits(GPIOA, (get_bit(num, 3) << 15)); // GPIO_Pin_15 d
  30. void Nixie_Tube_GPIO_Config(void);
  31. void Show_Number(uint8_t number1, uint8_t number2, uint8_t number3, uint8_t number4);
  32. #endif // __BSP_NIXIE_TUBE_H

bsp_nixie_tube.c

  1. #include "bsp_nixie_tube.h"
  2. // 共阳数码管真值表 0 1 2 3 4 5 6 7 8 9 A b C d E F H L ^ U P
  3. uint8_t smgduan[21] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,
  4. 0x88,0x83,0xC6,0xA1,0x86,0x8E,0x89, 0xC7, 0xC8, 0xC1,0x8C};
  5. // 初始化GPIO引脚
  6. void Nixie_Tube_GPIO_Config(void)
  7. {
  8. GPIO_InitTypeDef GPIO_InitStruct;
  9. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
  10. // 配置5个按键的引脚
  11. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_15 ;
  12. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  13. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
  14. GPIO_Init(GPIOA, &GPIO_InitStruct);
  15. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_4 |
  16. GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 |
  17. GPIO_Pin_9 | GPIO_Pin_13 | GPIO_Pin_14;
  18. GPIO_Init(GPIOB, &GPIO_InitStruct);
  19. }
  20. // 延时
  21. void delay_ms(u16 time)
  22. {
  23. u16 i=0;
  24. while(time--)
  25. {
  26. i=8000; //自己定义
  27. while(i--) ;
  28. }
  29. }
  30. // 点亮数码管
  31. void SET_Tube_Light(uint8_t num)
  32. {
  33. SET_TUBE_LIGHT(num);
  34. }
  35. // 设置数码管
  36. void SET_Tube(uint8_t num)
  37. {
  38. SET_Tube_Light(num);
  39. delay_ms(1);
  40. SET_TUBE_BLACK_B;
  41. SET_TUBE_BLACK_A;
  42. }
  43. // 数码管上显示的数据
  44. void Show_Number(uint8_t number1, uint8_t number2, uint8_t number3, uint8_t number4)
  45. {
  46. // 取数值
  47. uint8_t number_1 = smgduan[number1];
  48. uint8_t number_2 = smgduan[number2];
  49. uint8_t number_3 = smgduan[number3];
  50. uint8_t number_4 = smgduan[number4];
  51. CHIP_1(OPEN);
  52. SET_Tube(number_1);
  53. CHIP_1(CLOCK);
  54. CHIP_2(OPEN);
  55. SET_Tube(number_2);
  56. CHIP_2(CLOCK);
  57. CHIP_3(OPEN);
  58. SET_Tube(number_3);
  59. CHIP_3(CLOCK);
  60. CHIP_4(OPEN);
  61. SET_Tube(number_4);
  62. CHIP_4(CLOCK);
  63. }