原创
有人试过STM32用另一种方式控制数码管么?
近期搞了个单板,客户定制的,核心ST的MCU。熟悉又陌生的东西啊,依稀记得那时候自己还是个年轻小伙。功能简单,花了一天的时间把原理图和PCB搞完就投产了。等待几天PCB回来了,在等待几天PCBA也回来了。准备调试。。。。。。。。。
嗯。。。。。。。。。。调那个呢?功能要求有数码管,要不先搞点东西显示下。。。。。。
找到以前的DEMO,改改引脚,应该很快。这样想着。结果上天不做美啊。DEMO上数码管段选脚是放在同一GPIO的,而我的,七零八落的,哎。。。。。。。。。。。。。。。。这可咋办?
思索良久之后,是不是可以通过将段码值通过移位赋值的方式直接将相应值直接给GPIO呢
共阳极段码表是这个
- unsigned char LEDSEG[] =
- {
- 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xFF
- };
-
如果可以像这样给段码赋值就好了
- SMG_A=((LEDSEG[要显示的数字]>>0)&0x01);
- SMG_B=((LEDSEG[要显示的数字]>>1)&0x01);
- SMG_C=((LEDSEG[要显示的数字]>>2)&0x01);
- SMG_D=((LEDSEG[要显示的数字]>>3)&0x01);
- SMG_E=((LEDSEG[要显示的数字]>>4)&0x01);
- SMG_F=((LEDSEG[要显示的数字]>>5)&0x01);
- SMG_G=((LEDSEG[要显示的数字]>>6)&0x01);
思路有了,接下来就是看看ST库里面有木有像51单片机那样可以直接给个引脚赋值就能控制高低电平的源码了,搜索良久,发现并没有,要不只能通过GPIO_SetBits或者GPIO_ResetBits控制单个GPIO,要不WRITE_DATA控制一组引脚。
哎,我又沉思了,后来网上找一遍,结果还真找到了
- #define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
- #define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
- #define BIT_ADDR(addr, bitnum) MEM_ADDR( BITBAND(addr, bitnum) )
- #define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
- #define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
- #define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
- #define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
- #define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
- #define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
- #define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
- #define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
- #define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
- #define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
- //-----------------------------------------------------
- #define PA0 BIT_ADDR(GPIOA_ODR_Addr, 0) //输出
- #define PA1 BIT_ADDR(GPIOA_ODR_Addr, 1) //输出
- #define PA2 BIT_ADDR(GPIOA_ODR_Addr, 2) //输出
- #define PA3 BIT_ADDR(GPIOA_ODR_Addr, 3) //输出
- #define PA4 BIT_ADDR(GPIOA_ODR_Addr, 4) //输出
- #define PA5 BIT_ADDR(GPIOA_ODR_Addr, 5) //输出
- #define PA6 BIT_ADDR(GPIOA_ODR_Addr, 6) //输出
- #define PA7 BIT_ADDR(GPIOA_ODR_Addr, 7) //输出
- #define PA8 BIT_ADDR(GPIOA_ODR_Addr, 8) //输出
- #define PA9 BIT_ADDR(GPIOA_ODR_Addr, 9) //输出
- #define PA10 BIT_ADDR(GPIOA_ODR_Addr, 10) //输出
- #define PA11 BIT_ADDR(GPIOA_ODR_Addr, 11) //输出
- #define PA12 BIT_ADDR(GPIOA_ODR_Addr, 12) //输出
- #define PA13 BIT_ADDR(GPIOA_ODR_Addr, 13) //输出
- #define PA14 BIT_ADDR(GPIOA_ODR_Addr, 14) //输出
- #define PA15 BIT_ADDR(GPIOA_ODR_Addr, 15) //输出
-
- #define PA0in BIT_ADDR(GPIOA_IDR_Addr, 0) //??
- #define PA1in BIT_ADDR(GPIOA_IDR_Addr, 1) //??
- #define PA2in BIT_ADDR(GPIOA_IDR_Addr, 2) //??
- #define PA3in BIT_ADDR(GPIOA_IDR_Addr, 3) //??
- #define PA4in BIT_ADDR(GPIOA_IDR_Addr, 4) //??
- #define PA5in BIT_ADDR(GPIOA_IDR_Addr, 5) //??
- #define PA6in BIT_ADDR(GPIOA_IDR_Addr, 6) //??
- #define PA7in BIT_ADDR(GPIOA_IDR_Addr, 7) //??
- #define PA8in BIT_ADDR(GPIOA_IDR_Addr, 8) //??
- #define PA9in BIT_ADDR(GPIOA_IDR_Addr, 9) //??
- #define PA10in BIT_ADDR(GPIOA_IDR_Addr, 10) //??
- #define PA11in BIT_ADDR(GPIOA_IDR_Addr, 11) //??
- #define PA12in BIT_ADDR(GPIOA_IDR_Addr, 12) //??
- #define PA13in BIT_ADDR(GPIOA_IDR_Addr, 13) //??
- #define PA14in BIT_ADDR(GPIOA_IDR_Addr, 14) //??
- #define PA15in BIT_ADDR(GPIOA_IDR_Addr, 15) //??
-
-
- //-----------------------------------------------------
- #define PB0 BIT_ADDR(GPIOB_ODR_Addr, 0) //输出
- #define PB1 BIT_ADDR(GPIOB_ODR_Addr, 1) //输出
- #define PB2 BIT_ADDR(GPIOB_ODR_Addr, 2) //输出
- #define PB3 BIT_ADDR(GPIOB_ODR_Addr, 3) //输出
- #define PB4 BIT_ADDR(GPIOB_ODR_Addr, 4) //输出
- #define PB5 BIT_ADDR(GPIOB_ODR_Addr, 5) //输出
- #define PB6 BIT_ADDR(GPIOB_ODR_Addr, 6) //输出
- #define PB7 BIT_ADDR(GPIOB_ODR_Addr, 7) //输出
- #define PB8 BIT_ADDR(GPIOB_ODR_Addr, 8) //输出
- #define PB9 BIT_ADDR(GPIOB_ODR_Addr, 9) //输出
- #define PB10 BIT_ADDR(GPIOB_ODR_Addr, 10) //输出
- #define PB11 BIT_ADDR(GPIOB_ODR_Addr, 11) //输出
- #define PB12 BIT_ADDR(GPIOB_ODR_Addr, 12) //输出
- #define PB13 BIT_ADDR(GPIOB_ODR_Addr, 13) //输出
- #define PB14 BIT_ADDR(GPIOB_ODR_Addr, 14) //输出
- #define PB15 BIT_ADDR(GPIOB_ODR_Addr, 15) //输出
-
-
- #define PB0in BIT_ADDR(GPIOB_IDR_Addr, 0) //??
- #define PB1in BIT_ADDR(GPIOB_IDR_Addr, 1) //??
- #define PB2in BIT_ADDR(GPIOB_IDR_Addr, 2) //??
- #define PB3in BIT_ADDR(GPIOB_IDR_Addr, 3) //??
- #define PB4in BIT_ADDR(GPIOB_IDR_Addr, 4) //??
- #define PB5in BIT_ADDR(GPIOB_IDR_Addr, 5) //??
- #define PB6in BIT_ADDR(GPIOB_IDR_Addr, 6) //??
- #define PB7in BIT_ADDR(GPIOB_IDR_Addr, 7) //??
- #define PB8in BIT_ADDR(GPIOB_IDR_Addr, 8) //??
- #define PB9in BIT_ADDR(GPIOB_IDR_Addr, 9) //??
- #define PB10in BIT_ADDR(GPIOB_IDR_Addr, 10) //??
- #define PB11in BIT_ADDR(GPIOB_IDR_Addr, 11) //??
- #define PB12in BIT_ADDR(GPIOB_IDR_Addr, 12) //??
- #define PB13in BIT_ADDR(GPIOB_IDR_Addr, 13) //??
- #define PB14in BIT_ADDR(GPIOB_IDR_Addr, 14) //??
- #define PB15in BIT_ADDR(GPIOB_IDR_Addr, 15) //??
- //----------------------------------------------------
- #define PC0 BIT_ADDR(GPIOC_ODR_Addr, 0) //输出
- #define PC1 BIT_ADDR(GPIOC_ODR_Addr, 1) //输出
- #define PC2 BIT_ADDR(GPIOC_ODR_Addr, 2) //输出
- #define PC3 BIT_ADDR(GPIOC_ODR_Addr, 3) //输出
- #define PC4 BIT_ADDR(GPIOC_ODR_Addr, 4) //输出
- #define PC5 BIT_ADDR(GPIOC_ODR_Addr, 5) //输出
- #define PC6 BIT_ADDR(GPIOC_ODR_Addr, 6) //输出
- #define PC7 BIT_ADDR(GPIOC_ODR_Addr, 7) //输出
- #define PC8 BIT_ADDR(GPIOC_ODR_Addr, 8) //输出
- #define PC9 BIT_ADDR(GPIOC_ODR_Addr, 9) //输出
- #define PC10 BIT_ADDR(GPIOC_ODR_Addr, 10) //输出
- #define PC11 BIT_ADDR(GPIOC_ODR_Addr, 11) //输出
- #define PC12 BIT_ADDR(GPIOC_ODR_Addr, 12) //输出
- #define PC13 BIT_ADDR(GPIOC_ODR_Addr, 13) //输出
- #define PC14 BIT_ADDR(GPIOC_ODR_Addr, 14) //输出
- #define PC15 BIT_ADDR(GPIOC_ODR_Addr, 15) //输出
-
-
- #define PC0in BIT_ADDR(GPIOC_IDR_Addr, 0) //??
- #define PC1in BIT_ADDR(GPIOC_IDR_Addr, 1) //??
- #define PC2in BIT_ADDR(GPIOC_IDR_Addr, 2) //??
- #define PC3in BIT_ADDR(GPIOC_IDR_Addr, 3) //??
- #define PC4in BIT_ADDR(GPIOC_IDR_Addr, 4) //??
- #define PC5in BIT_ADDR(GPIOC_IDR_Addr, 5) //??
- #define PC6in BIT_ADDR(GPIOC_IDR_Addr, 6) //??
- #define PC7in BIT_ADDR(GPIOC_IDR_Addr, 7) //??
- #define PC8in BIT_ADDR(GPIOC_IDR_Addr, 8) //??
- #define PC9in BIT_ADDR(GPIOC_IDR_Addr, 9) //??
- #define PC10in BIT_ADDR(GPIOC_IDR_Addr, 10) //??
- #define PC11in BIT_ADDR(GPIOC_IDR_Addr, 11) //??
- #define PC12in BIT_ADDR(GPIOC_IDR_Addr, 12) //??
- #define PC13in BIT_ADDR(GPIOC_IDR_Addr, 13) //??
- #define PC14in BIT_ADDR(GPIOC_IDR_Addr, 14) //??
- #define PC15in BIT_ADDR(GPIOC_IDR_Addr, 15) //??
-
-
- //----------------------------------------------------
- #define PD0 BIT_ADDR(GPIOD_ODR_Addr, 0) //输出
- #define PD1 BIT_ADDR(GPIOD_ODR_Addr, 1) //输出
- #define PD2 BIT_ADDR(GPIOD_ODR_Addr, 2) //输出
- #define PD3 BIT_ADDR(GPIOD_ODR_Addr, 3) //输出
- #define PD4 BIT_ADDR(GPIOD_ODR_Addr, 4) //输出
- #define PD5 BIT_ADDR(GPIOD_ODR_Addr, 5) //输出
- #define PD6 BIT_ADDR(GPIOD_ODR_Addr, 6) //输出
- #define PD7 BIT_ADDR(GPIOD_ODR_Addr, 7) //输出
- #define PD8 BIT_ADDR(GPIOD_ODR_Addr, 8) //输出
- #define PD9 BIT_ADDR(GPIOD_ODR_Addr, 9) //输出
- #define PD10 BIT_ADDR(GPIOD_ODR_Addr, 10) //输出
- #define PD11 BIT_ADDR(GPIOD_ODR_Addr, 11) //输出
- #define PD12 BIT_ADDR(GPIOD_ODR_Addr, 12) //输出
- #define PD13 BIT_ADDR(GPIOD_ODR_Addr, 13) //输出
- #define PD14 BIT_ADDR(GPIOD_ODR_Addr, 14) //输出
- #define PD15 BIT_ADDR(GPIOD_ODR_Addr, 15) //输出
-
-
- #define PD0in BIT_ADDR(GPIOD_IDR_Addr, 0) //??
- #define PD1in BIT_ADDR(GPIOD_IDR_Addr, 1) //??
- #define PD2in BIT_ADDR(GPIOD_IDR_Addr, 2) //??
- #define PD3in BIT_ADDR(GPIOD_IDR_Addr, 3) //??
- #define PD4in BIT_ADDR(GPIOD_IDR_Addr, 4) //??
- #define PD5in BIT_ADDR(GPIOD_IDR_Addr, 5) //??
- #define PD6in BIT_ADDR(GPIOD_IDR_Addr, 6) //??
- #define PD7in BIT_ADDR(GPIOD_IDR_Addr, 7) //??
- #define PD8in BIT_ADDR(GPIOD_IDR_Addr, 8) //??
- #define PD9in BIT_ADDR(GPIOD_IDR_Addr, 9) //??
- #define PD10in BIT_ADDR(GPIOD_IDR_Addr, 10) //??
- #define PD11in BIT_ADDR(GPIOD_IDR_Addr, 11) //??
- #define PD12in BIT_ADDR(GPIOD_IDR_Addr, 12) //??
- #define PD13in BIT_ADDR(GPIOD_IDR_Addr, 13) //??
- #define PD14in BIT_ADDR(GPIOD_IDR_Addr, 14) //??
- #define PD15in BIT_ADDR(GPIOD_IDR_Addr, 15) //??
-
-
- //----------------------------------------------------
- #define PE0 BIT_ADDR(GPIOE_ODR_Addr, 0) //??
- #define PE1 BIT_ADDR(GPIOE_ODR_Addr, 1) //??
- #define PE2 BIT_ADDR(GPIOE_ODR_Addr, 2) //??
- #define PE3 BIT_ADDR(GPIOE_ODR_Addr, 3) //??
- #define PE4 BIT_ADDR(GPIOE_ODR_Addr, 4) //??
- #define PE5 BIT_ADDR(GPIOE_ODR_Addr, 5) //??
- #define PE6 BIT_ADDR(GPIOE_ODR_Addr, 6) //??
- #define PE7 BIT_ADDR(GPIOE_ODR_Addr, 7) //??
- #define PE8 BIT_ADDR(GPIOE_ODR_Addr, 8) //??
- #define PE9 BIT_ADDR(GPIOE_ODR_Addr, 9) //??
- #define PE10 BIT_ADDR(GPIOE_ODR_Addr, 10) //??
- #define PE11 BIT_ADDR(GPIOE_ODR_Addr, 11) //??
- #define PE12 BIT_ADDR(GPIOE_ODR_Addr, 12) //??
- #define PE13 BIT_ADDR(GPIOE_ODR_Addr, 13) //??
- #define PE14 BIT_ADDR(GPIOE_ODR_Addr, 14) //??
- #define PE15 BIT_ADDR(GPIOE_ODR_Addr, 15) //??
-
-
- #define PE0in BIT_ADDR(GPIOE_IDR_Addr, 0) //??
- #define PE1in BIT_ADDR(GPIOE_IDR_Addr, 1) //??
- #define PE2in BIT_ADDR(GPIOE_IDR_Addr, 2) //??
- #define PE3in BIT_ADDR(GPIOE_IDR_Addr, 3) //??
- #define PE4in BIT_ADDR(GPIOE_IDR_Addr, 4) //??
- #define PE5in BIT_ADDR(GPIOE_IDR_Addr, 5) //??
- #define PE6in BIT_ADDR(GPIOE_IDR_Addr, 6) //??
- #define PE7in BIT_ADDR(GPIOE_IDR_Addr, 7) //??
- #define PE8in BIT_ADDR(GPIOE_IDR_Addr, 8) //??
- #define PE9in BIT_ADDR(GPIOE_IDR_Addr, 9) //??
- #define PE10in BIT_ADDR(GPIOE_IDR_Addr, 10) //??
- #define PE11in BIT_ADDR(GPIOE_IDR_Addr, 11) //??
- #define PE12in BIT_ADDR(GPIOE_IDR_Addr, 12) //??
- #define PE13in BIT_ADDR(GPIOE_IDR_Addr, 13) //??
- #define PE14in BIT_ADDR(GPIOE_IDR_Addr, 14) //??
- #define PE15in BIT_ADDR(GPIOE_IDR_Addr, 15) //??
有木有一种豁然开朗的感觉,这时候才意识到自己基础知识得有多差了。
接下来数码管就可以好好玩了,不管硬件电路控制数码管的引脚是一组的还是乱七八糟的,我们都有一战之力了。
看了上面的源码,有些人就会有个疑问,只定义了此GPIO的输入输出,并没有指出输出的类型,如果是输入,也有很多属性没有赋值。其实不用考虑那么多,直接使用库函数啊
- GPIO_InitTypeDef GPIO_InitStructure;</p>
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
-
- 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;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
接下来就跟之前控制方式一样了,这里粘贴部分代码
作者: 二月半, 来源:面包板社区
链接: https://mbb.eet-china.com/blog/uid-me-1862109.html
版权声明:本文为博主原创,未经本人允许,禁止转载!
文章评论(0条评论)
登录后参与讨论