原创 有人试过STM32用另一种方式控制数码管么?

2023-4-22 07:34 1543 16 4 分类: MCU/ 嵌入式 文集: 单片机

近期搞了个单板,客户定制的,核心STMCU。熟悉又陌生的东西啊,依稀记得那时候自己还是个年轻小伙。功能简单,花了一天的时间把原理图和PCB搞完就投产了。等待几天PCB回来了,在等待几天PCBA也回来了。准备调试。。。。。。。。。

嗯。。。。。。。。。。调那个呢?功能要求有数码管,要不先搞点东西显示下。。。。。。

找到以前的DEMO,改改引脚,应该很快。这样想着。结果上天不做美啊。DEMO上数码管段选脚是放在同一GPIO的,而我的,七零八落的,哎。。。。。。。。。。。。。。。。这可咋办?

思索良久之后,是不是可以通过将段码值通过移位赋值的方式直接将相应值直接给GPIO

共阳极段码表是这个

  1. unsigned char LEDSEG[] =
  2. {
  3. 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xFF
  4. };

如果可以像这样给段码赋值就好了

  1. SMG_A=((LEDSEG[要显示的数字]>>0)&0x01);
  2. SMG_B=((LEDSEG[要显示的数字]>>1)&0x01);
  3. SMG_C=((LEDSEG[要显示的数字]>>2)&0x01);
  4. SMG_D=((LEDSEG[要显示的数字]>>3)&0x01);
  5. SMG_E=((LEDSEG[要显示的数字]>>4)&0x01);
  6. SMG_F=((LEDSEG[要显示的数字]>>5)&0x01);
  7. SMG_G=((LEDSEG[要显示的数字]>>6)&0x01);

思路有了,接下来就是看看ST库里面有木有像51单片机那样可以直接给个引脚赋值就能控制高低电平的源码了,搜索良久,发现并没有,要不只能通过GPIO_SetBits或者GPIO_ResetBits控制单个GPIO,要不WRITE_DATA控制一组引脚。

哎,我又沉思了,后来网上找一遍,结果还真找到了

  1. #define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
  2. #define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
  3. #define BIT_ADDR(addr, bitnum) MEM_ADDR( BITBAND(addr, bitnum) )
  4. #define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
  5. #define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
  6. #define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
  7. #define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
  8. #define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
  9. #define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
  10. #define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
  11. #define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
  12. #define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
  13. #define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
  14. //-----------------------------------------------------
  15. #define PA0 BIT_ADDR(GPIOA_ODR_Addr, 0) //输出
  16. #define PA1 BIT_ADDR(GPIOA_ODR_Addr, 1) //输出
  17. #define PA2 BIT_ADDR(GPIOA_ODR_Addr, 2) //输出
  18. #define PA3 BIT_ADDR(GPIOA_ODR_Addr, 3) //输出
  19. #define PA4 BIT_ADDR(GPIOA_ODR_Addr, 4) //输出
  20. #define PA5 BIT_ADDR(GPIOA_ODR_Addr, 5) //输出
  21. #define PA6 BIT_ADDR(GPIOA_ODR_Addr, 6) //输出
  22. #define PA7 BIT_ADDR(GPIOA_ODR_Addr, 7) //输出
  23. #define PA8 BIT_ADDR(GPIOA_ODR_Addr, 8) //输出
  24. #define PA9 BIT_ADDR(GPIOA_ODR_Addr, 9) //输出
  25. #define PA10 BIT_ADDR(GPIOA_ODR_Addr, 10) //输出
  26. #define PA11 BIT_ADDR(GPIOA_ODR_Addr, 11) //输出
  27. #define PA12 BIT_ADDR(GPIOA_ODR_Addr, 12) //输出
  28. #define PA13 BIT_ADDR(GPIOA_ODR_Addr, 13) //输出
  29. #define PA14 BIT_ADDR(GPIOA_ODR_Addr, 14) //输出
  30. #define PA15 BIT_ADDR(GPIOA_ODR_Addr, 15) //输出
  31. #define PA0in BIT_ADDR(GPIOA_IDR_Addr, 0) //??
  32. #define PA1in BIT_ADDR(GPIOA_IDR_Addr, 1) //??
  33. #define PA2in BIT_ADDR(GPIOA_IDR_Addr, 2) //??
  34. #define PA3in BIT_ADDR(GPIOA_IDR_Addr, 3) //??
  35. #define PA4in BIT_ADDR(GPIOA_IDR_Addr, 4) //??
  36. #define PA5in BIT_ADDR(GPIOA_IDR_Addr, 5) //??
  37. #define PA6in BIT_ADDR(GPIOA_IDR_Addr, 6) //??
  38. #define PA7in BIT_ADDR(GPIOA_IDR_Addr, 7) //??
  39. #define PA8in BIT_ADDR(GPIOA_IDR_Addr, 8) //??
  40. #define PA9in BIT_ADDR(GPIOA_IDR_Addr, 9) //??
  41. #define PA10in BIT_ADDR(GPIOA_IDR_Addr, 10) //??
  42. #define PA11in BIT_ADDR(GPIOA_IDR_Addr, 11) //??
  43. #define PA12in BIT_ADDR(GPIOA_IDR_Addr, 12) //??
  44. #define PA13in BIT_ADDR(GPIOA_IDR_Addr, 13) //??
  45. #define PA14in BIT_ADDR(GPIOA_IDR_Addr, 14) //??
  46. #define PA15in BIT_ADDR(GPIOA_IDR_Addr, 15) //??
  47. //-----------------------------------------------------
  48. #define PB0 BIT_ADDR(GPIOB_ODR_Addr, 0) //输出
  49. #define PB1 BIT_ADDR(GPIOB_ODR_Addr, 1) //输出
  50. #define PB2 BIT_ADDR(GPIOB_ODR_Addr, 2) //输出
  51. #define PB3 BIT_ADDR(GPIOB_ODR_Addr, 3) //输出
  52. #define PB4 BIT_ADDR(GPIOB_ODR_Addr, 4) //输出
  53. #define PB5 BIT_ADDR(GPIOB_ODR_Addr, 5) //输出
  54. #define PB6 BIT_ADDR(GPIOB_ODR_Addr, 6) //输出
  55. #define PB7 BIT_ADDR(GPIOB_ODR_Addr, 7) //输出
  56. #define PB8 BIT_ADDR(GPIOB_ODR_Addr, 8) //输出
  57. #define PB9 BIT_ADDR(GPIOB_ODR_Addr, 9) //输出
  58. #define PB10 BIT_ADDR(GPIOB_ODR_Addr, 10) //输出
  59. #define PB11 BIT_ADDR(GPIOB_ODR_Addr, 11) //输出
  60. #define PB12 BIT_ADDR(GPIOB_ODR_Addr, 12) //输出
  61. #define PB13 BIT_ADDR(GPIOB_ODR_Addr, 13) //输出
  62. #define PB14 BIT_ADDR(GPIOB_ODR_Addr, 14) //输出
  63. #define PB15 BIT_ADDR(GPIOB_ODR_Addr, 15) //输出
  64. #define PB0in BIT_ADDR(GPIOB_IDR_Addr, 0) //??
  65. #define PB1in BIT_ADDR(GPIOB_IDR_Addr, 1) //??
  66. #define PB2in BIT_ADDR(GPIOB_IDR_Addr, 2) //??
  67. #define PB3in BIT_ADDR(GPIOB_IDR_Addr, 3) //??
  68. #define PB4in BIT_ADDR(GPIOB_IDR_Addr, 4) //??
  69. #define PB5in BIT_ADDR(GPIOB_IDR_Addr, 5) //??
  70. #define PB6in BIT_ADDR(GPIOB_IDR_Addr, 6) //??
  71. #define PB7in BIT_ADDR(GPIOB_IDR_Addr, 7) //??
  72. #define PB8in BIT_ADDR(GPIOB_IDR_Addr, 8) //??
  73. #define PB9in BIT_ADDR(GPIOB_IDR_Addr, 9) //??
  74. #define PB10in BIT_ADDR(GPIOB_IDR_Addr, 10) //??
  75. #define PB11in BIT_ADDR(GPIOB_IDR_Addr, 11) //??
  76. #define PB12in BIT_ADDR(GPIOB_IDR_Addr, 12) //??
  77. #define PB13in BIT_ADDR(GPIOB_IDR_Addr, 13) //??
  78. #define PB14in BIT_ADDR(GPIOB_IDR_Addr, 14) //??
  79. #define PB15in BIT_ADDR(GPIOB_IDR_Addr, 15) //??
  80. //----------------------------------------------------
  81. #define PC0 BIT_ADDR(GPIOC_ODR_Addr, 0) //输出
  82. #define PC1 BIT_ADDR(GPIOC_ODR_Addr, 1) //输出
  83. #define PC2 BIT_ADDR(GPIOC_ODR_Addr, 2) //输出
  84. #define PC3 BIT_ADDR(GPIOC_ODR_Addr, 3) //输出
  85. #define PC4 BIT_ADDR(GPIOC_ODR_Addr, 4) //输出
  86. #define PC5 BIT_ADDR(GPIOC_ODR_Addr, 5) //输出
  87. #define PC6 BIT_ADDR(GPIOC_ODR_Addr, 6) //输出
  88. #define PC7 BIT_ADDR(GPIOC_ODR_Addr, 7) //输出
  89. #define PC8 BIT_ADDR(GPIOC_ODR_Addr, 8) //输出
  90. #define PC9 BIT_ADDR(GPIOC_ODR_Addr, 9) //输出
  91. #define PC10 BIT_ADDR(GPIOC_ODR_Addr, 10) //输出
  92. #define PC11 BIT_ADDR(GPIOC_ODR_Addr, 11) //输出
  93. #define PC12 BIT_ADDR(GPIOC_ODR_Addr, 12) //输出
  94. #define PC13 BIT_ADDR(GPIOC_ODR_Addr, 13) //输出
  95. #define PC14 BIT_ADDR(GPIOC_ODR_Addr, 14) //输出
  96. #define PC15 BIT_ADDR(GPIOC_ODR_Addr, 15) //输出
  97. #define PC0in BIT_ADDR(GPIOC_IDR_Addr, 0) //??
  98. #define PC1in BIT_ADDR(GPIOC_IDR_Addr, 1) //??
  99. #define PC2in BIT_ADDR(GPIOC_IDR_Addr, 2) //??
  100. #define PC3in BIT_ADDR(GPIOC_IDR_Addr, 3) //??
  101. #define PC4in BIT_ADDR(GPIOC_IDR_Addr, 4) //??
  102. #define PC5in BIT_ADDR(GPIOC_IDR_Addr, 5) //??
  103. #define PC6in BIT_ADDR(GPIOC_IDR_Addr, 6) //??
  104. #define PC7in BIT_ADDR(GPIOC_IDR_Addr, 7) //??
  105. #define PC8in BIT_ADDR(GPIOC_IDR_Addr, 8) //??
  106. #define PC9in BIT_ADDR(GPIOC_IDR_Addr, 9) //??
  107. #define PC10in BIT_ADDR(GPIOC_IDR_Addr, 10) //??
  108. #define PC11in BIT_ADDR(GPIOC_IDR_Addr, 11) //??
  109. #define PC12in BIT_ADDR(GPIOC_IDR_Addr, 12) //??
  110. #define PC13in BIT_ADDR(GPIOC_IDR_Addr, 13) //??
  111. #define PC14in BIT_ADDR(GPIOC_IDR_Addr, 14) //??
  112. #define PC15in BIT_ADDR(GPIOC_IDR_Addr, 15) //??
  113. //----------------------------------------------------
  114. #define PD0 BIT_ADDR(GPIOD_ODR_Addr, 0) //输出
  115. #define PD1 BIT_ADDR(GPIOD_ODR_Addr, 1) //输出
  116. #define PD2 BIT_ADDR(GPIOD_ODR_Addr, 2) //输出
  117. #define PD3 BIT_ADDR(GPIOD_ODR_Addr, 3) //输出
  118. #define PD4 BIT_ADDR(GPIOD_ODR_Addr, 4) //输出
  119. #define PD5 BIT_ADDR(GPIOD_ODR_Addr, 5) //输出
  120. #define PD6 BIT_ADDR(GPIOD_ODR_Addr, 6) //输出
  121. #define PD7 BIT_ADDR(GPIOD_ODR_Addr, 7) //输出
  122. #define PD8 BIT_ADDR(GPIOD_ODR_Addr, 8) //输出
  123. #define PD9 BIT_ADDR(GPIOD_ODR_Addr, 9) //输出
  124. #define PD10 BIT_ADDR(GPIOD_ODR_Addr, 10) //输出
  125. #define PD11 BIT_ADDR(GPIOD_ODR_Addr, 11) //输出
  126. #define PD12 BIT_ADDR(GPIOD_ODR_Addr, 12) //输出
  127. #define PD13 BIT_ADDR(GPIOD_ODR_Addr, 13) //输出
  128. #define PD14 BIT_ADDR(GPIOD_ODR_Addr, 14) //输出
  129. #define PD15 BIT_ADDR(GPIOD_ODR_Addr, 15) //输出
  130. #define PD0in BIT_ADDR(GPIOD_IDR_Addr, 0) //??
  131. #define PD1in BIT_ADDR(GPIOD_IDR_Addr, 1) //??
  132. #define PD2in BIT_ADDR(GPIOD_IDR_Addr, 2) //??
  133. #define PD3in BIT_ADDR(GPIOD_IDR_Addr, 3) //??
  134. #define PD4in BIT_ADDR(GPIOD_IDR_Addr, 4) //??
  135. #define PD5in BIT_ADDR(GPIOD_IDR_Addr, 5) //??
  136. #define PD6in BIT_ADDR(GPIOD_IDR_Addr, 6) //??
  137. #define PD7in BIT_ADDR(GPIOD_IDR_Addr, 7) //??
  138. #define PD8in BIT_ADDR(GPIOD_IDR_Addr, 8) //??
  139. #define PD9in BIT_ADDR(GPIOD_IDR_Addr, 9) //??
  140. #define PD10in BIT_ADDR(GPIOD_IDR_Addr, 10) //??
  141. #define PD11in BIT_ADDR(GPIOD_IDR_Addr, 11) //??
  142. #define PD12in BIT_ADDR(GPIOD_IDR_Addr, 12) //??
  143. #define PD13in BIT_ADDR(GPIOD_IDR_Addr, 13) //??
  144. #define PD14in BIT_ADDR(GPIOD_IDR_Addr, 14) //??
  145. #define PD15in BIT_ADDR(GPIOD_IDR_Addr, 15) //??
  146. //----------------------------------------------------
  147. #define PE0 BIT_ADDR(GPIOE_ODR_Addr, 0) //??
  148. #define PE1 BIT_ADDR(GPIOE_ODR_Addr, 1) //??
  149. #define PE2 BIT_ADDR(GPIOE_ODR_Addr, 2) //??
  150. #define PE3 BIT_ADDR(GPIOE_ODR_Addr, 3) //??
  151. #define PE4 BIT_ADDR(GPIOE_ODR_Addr, 4) //??
  152. #define PE5 BIT_ADDR(GPIOE_ODR_Addr, 5) //??
  153. #define PE6 BIT_ADDR(GPIOE_ODR_Addr, 6) //??
  154. #define PE7 BIT_ADDR(GPIOE_ODR_Addr, 7) //??
  155. #define PE8 BIT_ADDR(GPIOE_ODR_Addr, 8) //??
  156. #define PE9 BIT_ADDR(GPIOE_ODR_Addr, 9) //??
  157. #define PE10 BIT_ADDR(GPIOE_ODR_Addr, 10) //??
  158. #define PE11 BIT_ADDR(GPIOE_ODR_Addr, 11) //??
  159. #define PE12 BIT_ADDR(GPIOE_ODR_Addr, 12) //??
  160. #define PE13 BIT_ADDR(GPIOE_ODR_Addr, 13) //??
  161. #define PE14 BIT_ADDR(GPIOE_ODR_Addr, 14) //??
  162. #define PE15 BIT_ADDR(GPIOE_ODR_Addr, 15) //??
  163. #define PE0in BIT_ADDR(GPIOE_IDR_Addr, 0) //??
  164. #define PE1in BIT_ADDR(GPIOE_IDR_Addr, 1) //??
  165. #define PE2in BIT_ADDR(GPIOE_IDR_Addr, 2) //??
  166. #define PE3in BIT_ADDR(GPIOE_IDR_Addr, 3) //??
  167. #define PE4in BIT_ADDR(GPIOE_IDR_Addr, 4) //??
  168. #define PE5in BIT_ADDR(GPIOE_IDR_Addr, 5) //??
  169. #define PE6in BIT_ADDR(GPIOE_IDR_Addr, 6) //??
  170. #define PE7in BIT_ADDR(GPIOE_IDR_Addr, 7) //??
  171. #define PE8in BIT_ADDR(GPIOE_IDR_Addr, 8) //??
  172. #define PE9in BIT_ADDR(GPIOE_IDR_Addr, 9) //??
  173. #define PE10in BIT_ADDR(GPIOE_IDR_Addr, 10) //??
  174. #define PE11in BIT_ADDR(GPIOE_IDR_Addr, 11) //??
  175. #define PE12in BIT_ADDR(GPIOE_IDR_Addr, 12) //??
  176. #define PE13in BIT_ADDR(GPIOE_IDR_Addr, 13) //??
  177. #define PE14in BIT_ADDR(GPIOE_IDR_Addr, 14) //??
  178. #define PE15in BIT_ADDR(GPIOE_IDR_Addr, 15) //??

有木有一种豁然开朗的感觉,这时候才意识到自己基础知识得有多差了。

接下来数码管就可以好好玩了,不管硬件电路控制数码管的引脚是一组的还是乱七八糟的,我们都有一战之力了。

看了上面的源码,有些人就会有个疑问,只定义了此GPIO的输入输出,并没有指出输出的类型,如果是输入,也有很多属性没有赋值。其实不用考虑那么多,直接使用库函数啊

 

  1. GPIO_InitTypeDef GPIO_InitStructure;</p>
  2. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
  3. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
  4. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15|GPIO_Pin_0;
  5. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  6. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  7. GPIO_Init(GPIOB, &GPIO_InitStructure);
  8. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
  9. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
  10. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  11. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  12. GPIO_Init(GPIOA, &GPIO_InitStructure);
接下来就跟之前控制方式一样了,这里粘贴部分代码

作者: 二月半, 来源:面包板社区

链接: https://mbb.eet-china.com/blog/uid-me-1862109.html

版权声明:本文为博主原创,未经本人允许,禁止转载!

文章评论0条评论)

登录后参与讨论
我要评论
0
16
关闭 站长推荐上一条 /2 下一条