1. Gossip
When developing recently, I used the STM32F030F4P6 microcontroller, which has only 20 pins and is very cheap, but it has complete functions; timers, external interrupts, serial ports, IIC, SPI, DMA and WWDG, etc., are all available, making it very suitable for small devices. But there is a problem, it is Cortex-M0 core, unlike M3 and M4 cores, it can support bit-band operation (that is, one bit operation, like 80C51 microcontroller), which brings a little trouble to program transplantation or development, so I use the bit segment operation of C language structure to realize the bit-band operation, but the efficiency may be slightly inferior to the real bit-band operation, but the code is compatible and can be applied to basically any processor. I hope it can help you. The original address of this article: http://www.cnblogs.com/endlesscoding/p/7429743.html, please indicate the source for reprinting.
2. Basic knowledge of bit band operation
There are a lot of information about the real bit-band operation on the Internet, and they are written in great detail. Here I will just briefly talk about my understanding. In addition, if you do not understand the real bit-band operation, it will not affect your understanding of this article, because this article has nothing to do with the bit-band operation. It is just an imitation and should not be taken seriously. If you do not want to understand the real bit-band operation, you can directly ignore this section.
If we do not use bit-banding operations, we have to move 32 bits (STM32 is 32 bits) when we operate a piece of data. To make an inappropriate analogy, this is equivalent to sitting on a train with 32 carriages, but the train has only one door. If we want to check the information of the passengers in the train, or if the passengers want to get off, they must enter and exit from that door, as shown in Figure 1.
Figure 1 A 32-car train with only one door
If we have bit-band operation, it is equivalent to installing 32 doors on this 32-carriage train. In this way, if you want to check the information of a passenger or get off the train, you can quickly get off from the designated door. As shown in Figure 2 below.
Figure 2 A 32-car train with 32 doors
With 32 gates, the speed is much faster, but the hardware cost will definitely increase. This is why the STM32F030 series does not have bit-banding operations, because its cost is low.
3. C language structure and body segment operation
This section mainly describes the basic knowledge of C language structures. If you are an expert in C language, please ignore this section. In C language, there is a bit field in the declaration of a structure, which can control how many bits the members in this structure occupy. Regarding its use, there is the following code:
1 typedef struct _16_Bits_Struct
2 {
3 u16 bit0 : 1; // occupies one byte
4 u16 bit1 : 1;
5 u16 bit2 : 1;
6 u16 bit3 : 1;
7 u16 bit4 : 1;
8 u16 bit5 : 1;
9 u16 bit6 : 1;
10 u16 bit7 : 1;
11 u16 bit8 : 1;
12 u16 bit9 : 1;
13 u16 bit10 : 1;
14 u16 bit11 : 2; // occupies two bytes
15 u16 bit12 : 3; // occupies three bytes
16 } _16_Bits_Struct;
The _16_Bits_Struct structure type above occupies 2 bytes, or 16 bits, but the number of bits occupied by its 13 member variables is not all the same. The number after the ":" determines how many bits it occupies. The code is as follows, which operates a bit of this structure type.
1 _16_Bits_Struct _16_bits;
2 unsigned short _16bits_data;
3 memset(&_16_bits, 0, sizeof(_16_Bits_Struct)); // clear its memory to 0
4
5 _16_bits.bit2 = 1;
6 _16_bits.bit5 = 1;
7 _16_bits.bit8 = 5;
8
9 _16bits_data = *((unsigned short*)(&_16_bits));
10
11 printf("_16bits_data = %0xH\n", _16bits_data);
The output is:
From the result, we can see that in the structure, bit0~bit12 are from low to high. In line 7 of the above code, although 5 is written to bit8, because it only occupies one bit, only the lowest bit of 5(D)=0101(B) is taken, which is 1. Therefore, the final result is 124H, and its memory structure is shown in Figure 3 below.
Figure 3 Structure memory structure diagram
4. STM32F030 emulation operation
With the foundation of the above structure bit segment operation, it is very close to realize the bit band operation of STM32F030. I plan to make a simple one to realize the operation of a certain GPIO pin to achieve the function of turning on and off the LED.
From the reference manual of STM32F030, find the GPIO output register ODR and see its basic information as shown in Figure 4. This register is readable and writable (RW), so as long as we write 1 to one of the bits in this register, the pin will output 1, and write 0 to output 0 (of course, the prerequisite is that you configure it to output mode and enable its clock).
Figure 4 GPIO ODR register structure diagram
How do I operate this register one bit at a time? Let's look at the code below to explain.
1 typedef struct _16_Bits_Struct
2 {
3 u16 bit0 : 1;
4 u16 bit1 : 1;
5 u16 bit2 : 1;
6 u16 bit3 : 1;
7 u16 bit4 : 1;
8 u16 bit5 : 1;
9 u16 bit6 : 1;
10 u16 bit7 : 1;
11 u16 bit8 : 1;
12 u16 bit9 : 1;
13 u16 bit10 : 1;
14 u16 bit11 : 1;
15 u16 bit12 : 1;
16 u16 bit13 : 1;
17 u16 bit14 : 1;
18 u16 bit15 : 1;
19 } Bits_16_TypeDef;
20 #define LED_GPIO_CLK RCC_AHBPeriph_GPIOA
21 #define LED_PORT GPIOA
22 #define LED_PIN GPIO_Pin_4
23 //Use the bit segment operation of the structure, compatible with the bit band operation of Cortex-M3.
24 #define LED_PORT_OUT ((Bits_16_TypeDef *)(&(LED_PORT->ODR)))
25 #define LED (LED_PORT_OUT->bit4)
My hardware connection is: LED is connected to pin 4 of GPIOA. Lines 1 to 19 have been explained in the previous structure knowledge. Lines 20 to 22 are just some macro definitions for better code porting, which are unnecessary. Line 24 is more critical: first take out the address of GPIOA->ODR, and then force it to be converted to Bits_16_TypeDef * type (note, it is a pointer type). After converting to this type, ODR has the characteristics of a bit field, so it can be operated on. Line 25 defines the LED connected to PA.4 as the 4th bit of GPIOA->ODR.
With this operation, it is easy to turn our LED on and off. The code is as follows.
1 LED = 0;//LED亮
2 LED = 1;//LED is off
Due to different hardware connections, the effect may be the opposite. After reading this, do you think the operation is very simple?
The complete code is as follows:
1 /*------------------------------------------------------------------------------
2 Fan Monitoring System (August 12, 2017 12:22:38)
3 Functional description:
4 LED switch function, mainly used for status display
5
6 Resources used: GPIOA changes as the board is not used
7
8 File Description: None
9 Author: Endless Email: endless@139.com Time: August 10, 2017 21:35:38
10 Modification: None Time:
11 ------------------------------------------------------------------------------*/
12 #include "led.h"
13 #include "stm32f0xx.h"
14
15 /*-----------------------------------------------------------------------------
16 Function: LED initialization
17 Function parameters: None
18 Function returns: None
19 Function Description: Before calling this function, you need to modify the macro definition of LED pin in LED.h
20 Author: Endless
21 -----------------------------------------------------------------------------*/
22 void LED_Init(void)
23 {
24 GPIO_InitTypeDef GPIO_InitStructure;
25
26 RCC_AHBPeriphClockCmd(LED_GPIO_CLK, ENABLE);
27
28 GPIO_InitStructure.GPIO_Pin = LED_PIN;
29 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
30 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
31 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
32 GPIO_Init(LED_PORT, &GPIO_InitStructure);
33 }
led.c
1 #ifndef __led_H
2 #define __led_H
3
4 #include "stm32f0xx.h"
5 #include "mytype.h"
6
7 #define LED_GPIO_CLK RCC_AHBPeriph_GPIOA
8 #define LED_PORT GPIOA
9 #define LED_PIN GPIO_Pin_4
10
11 //Use the bit segment operation of the structure, compatible with the bit band operation of Cortex-M3.
12 #define LED_PORT_OUT ((Bits_16_TypeDef *)(&(LED_PORT->ODR)))
13 #define LED (LED_PORT_OUT->bit4)
14
15 void LED_Init(void);
16
17 #endif
led.h
1 #ifndef __MYTYPE_H
2 #define __MYTYPE_H
3 #include "stm32f0xx.h"
4
5 #ifndef BIT
6 #define BIT(x) (1 << (x))
7 #endif
8
9 #ifndef u8
10 #define u8 uint8_t
11 #endif
12
13 #ifndef u16
14 #define u16 uint16_t
15 #endif
16
17 #ifndef u32
18 #define u32 uint32_t
19 #endif
20
21 #ifndef NULL
22 #define NULL 0
23 #endif
24
25 /*------------------------------------------------------------------------------
26 User defined variables
27 Function description: Use the bit segment operation of the structure to realize bit operation
28 Author: Endless 2017-08-13 18:32:37
29 Modification: None Time:
30 ------------------------------------------------------------------------------*/
31 typedef struct _16_Bits_Struct
32 {
33 u16 bit0 : 1;
34 u16 bit1 : 1;
35 u16 bit2 : 1;
36 u16 bit3 : 1;
37 u16 bit4 : 1;
38 u16 bit5 : 1;
39 u16 bit6 : 1;
40 u16 bit7 : 1;
41 u16 bit8 : 1;
42 u16 bit9 : 1;
43 u16 bit10 : 1;
44 u16 bit11 : 1;
45 u16 bit12 : 1;
46 u16 bit13 : 1;
47 u16 bit14 : 1;
48 u16 bit15 : 1;
49 } Bits_16_TypeDef;
mytype.h
If you want to perform more bit operations, just define it a few more times, it's very easy. That's almost the end. I hope it can help you. If you have any questions, please contact me or leave a message below.
Summarize
It is not easy to do technology, I hope we can all stick to it together!!
Previous article:STM32 Getting Started Learning ADC (STM32F030F4P6 based on CooCox IDE)
Next article:STM32 Getting Started with DMA (STM32F030F4P6 based on CooCox IDE)
- Learn ARM development(14)
- Learn ARM development(15)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- CyCubeTouch
- Digi-Key Empowers the Internet of Things: Unboxing
- National Technology N32G032K6Q7_STB Development Board Hardware Usage Guide
- "Linux Shell Programming from Beginner to Mastery"——Embedded Linux learning resources are free!
- Design of intelligent charging lighting control system based on MSP430 single chip microcomputer
- A circuit for sampling voltage with common optocoupler isolation
- C2000F2802x cyclic query button lights up LED
- GD32E231 DIY Contest (2) GD32E231C Development Environment Getting Started DEMO Test
- [NXP Rapid IoT Review] A Bluetooth BLE communication app designed for NXP IoT in the third week
- Introduction to the system reset method of Atria AT32F403A/407