I think the OP is very impatient. Don't do anything now. Read the article "Don't be an impatient embedded engineer" several times. Think it
through before you start.
I made an example to light up the LED without using the ST library to answer your question .
My KeilMDK 3.5 and
my STM32 board are the Struggle version. The IC is STM32F103VET6.
The debugging tool is JLINK V8.
The LED is connected to PB5 and lights up at a high level.
Since the OP said that he must know C language, how much do you know about the following questions without checking Baidu and relying on yourself? And how much do you know after checking Baidu?
(I) Create a new Keil project, select ST's STM32F103VE for IC, Keil prompts whether to copy the startup file, select yes. I
have a question for the OP.
Have you read this startup header file? 51 is also the same startup file. Have you read the startup file of 51? Do you know
what is done in the header file? Does C language really start from the main function? What is the runtime library?
Where do I know this information? The behavior of the Keil compiler?
(If you say that the header file is assembled and there is no need to read it, then I will pretend that I didn’t say anything)
For example, there is such a sentence in the startup file. My question is where is the label __main implemented? Note that this is definitely not the main function.
Where does it jump to? Another question: What does [WEAK] mean here? What is its use? ? ? ?
Reset_Handler PROC
EXPORT Reset_Handler [WEAK]
IMPORT __main
LDR R0, =__main
BX R0
(II) Create a new main.c and write a main function, which does nothing. This is the same as 51.
void main(void)
{
while (1)
{
}
}
Then because I need to debug, I set up the Jlink debugger. In the project properties, select the Debug tab, Use J-LINK/J-TRACE, and then go to
the Utilities tab. Also select J-LINK /J-TRACK and select the Setting button. The Programming Algorithm
is still empty, indicating that Keil does not know what the target is. I add a STM32F10X High-density Flash. The question is, why is it
High-density? What is the basis? ? ?
All confirmations return.
At this time, it can be compiled, the development board is powered on, and the simulation can be downloaded, although the program has not been written.
(III) Since the hardware, emulator, and debugging are all ready, then start writing the program.
I always recommend that novices spend money to buy learning boards and emulators, because they can eliminate hardware problems and let beginners concentrate on writing programs without doubting that
there are problems with the hardware. This is very important.
This stage is mainly about reading books, understanding the architecture of this IC, understanding the instruction set, and understanding the registers (don't tell me you can't find these materials? .....)
Cortex-M3 Authoritative Guide CnR2 (e-book).pdf
STM3210x Reference Manual.pdf
Learning Board Schematic Diagram
Blogs, forums and other posts, it is necessary to have a preliminary understanding of the entire IC. This process is a bit painful, but it is worth spending this time.
(IV) Start writing LED
Since we want to operate the IO port, of course we have to read the knowledge related to the IO port. Open the STM3210x Reference Manual.pdf, my purpose is only to operate GPIO,
so I only need to read Chapter 5. There are many chapters, I am too lazy to read them, based on general experience (OP, you lack experience, right?), I won’t say much
Just AVR and PIC. There are generally two steps to operate IO. First, operate the IO control register and set IO to output, and the second is to send data.
Then it is obvious that only GPIOx_CRL, GPIOx_CRH, and GPIOx_ODR registers are needed.
After reading the introduction of these registers carefully, we know that GPIOx_CRL controls the properties of PIN 0-7, GPIOx_CRH controls PIN 8-15, and ODR register
is of course for output data. Just send the data here.
Then, what are the addresses of these registers? First, look at stm32f103ve.pdf, which is the official datasheet. Look at Chapter 4.
Why do you read this chapter for Mmeory Mapping? Anyone who knows English can guess it, right? Look at the address of PORTB, which is 0x40010C00 - 0x40010FFF, which is the base address. The base address
plus the offset can find the specific register.
For example, I need to operate the offset of GPIOB_CRL to 00H, (see STM3210x reference manual.pdf) The offset of ODR register is 0CH,
so it is natural to get
GPIOB_CRL = 0x40010C00
GPIOB_ODR = 0x40010C0C
How to verify that my conclusion is correct? First look at the header file KeilARMINCSTSTM32F10xstm32f10x_map.h given by Keil
#define PERIPH_BASE ((u32)0x40000000)
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
#define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00)
In this way, 0x40010C00 can be calculated, right? ? The same goes for the ODR register.
To light up the LED, I need to set PB5 (GPIOB5) as output and write 1 to the corresponding bit of ODR. From the data, I can see that MODE5 is
controlled by bits 20 and 21, and CNF5 is controlled by bits 22 and 23.
MODE5 should be set to 10 (0x2) to select 2MHZ output, CNF5 should select 00 (0x0), general push-pull mode, so write this value into
(*volatile unsigned long)0x40010C00 = (2<<20) | (0<<22); // For simplicity, ignore other bits
Hello, can you understand this C language? ? What does volatile mean and what is it used for? What is the essence of a pointer? Why can it be used like this? What does 2<<20
mean, and why can it be used like this? I'm really not trying to embarrass you, embedded systems are written like this, and ST's header files are also defined like this
Similarly, set the ODR register
*(volatile unsigned long *)0x40010C0C = 1<<5;
*(volatile unsigned long *)0x40010C0C = 0;
STM32 has no SFR, no bit, and no concept of sbit. Is it not as good as 51?
Download and run, it still doesn't work, because the CLK of GPIOB is not enabled. In fact, GPIOB cannot work at this time. This is a special feature of STM32. When powered on, the
clocks of peripherals are all turned off by default. It is forgivable for beginners not to pay attention to this. Just read more books, practice more, and ask more questions.
After finding the cause of the problem, set RCC_APB2ENR, where BIT 3 is IOPBEN, which is the clock enable bit. As above, first find
the address of RCC_APB2ENR
#define PERIPH_BASE ((u32)0x40000000)
#define AHBPERIPH_BASE (PERIPH_BASE + 0x20000)
#define RCC_BASE (AHBPERIPH_BASE + 0x1000)
The offset of RCC_APB2ENR is 18H, so the final address is 0x40021018. The operation method is the same as above
*(volatile unsigned long *)0x40021018 |= 1<<3;
The final program to turn on the LED is completed.
void main(void)
{
*(volatile unsigned long *)0x40021018 |= 1<<3;
*(volatile unsigned long *)0x40010C00 = (2<<20) | (0<<22);
*(volatile unsigned long *)0x40010C0C = 1<<5;
while (1)
{
}
}
If a register is defined, the program becomes as follows
#define RCC_APB2ENR *(volatile unsigned long *)0x40021018
#define GPIOB_CRL *(volatile unsigned long *)0x40010C00
#define GPIOB_ODR *(volatile unsigned long *)0x40010C0C
void main(void)
{
RCC_APB2ENR |= 1<<3;
GPIOB_CRL = (2<<20) | (0<<22);
GPIOB_ODR = 1<<5;
while (1)
{
}
}
RCC_APB2ENR RCC is the clock register, APB2 is peripheral 2, ENR can be understood as enable
GPIOB_CRL GPIO B control control register
GPIOB_ODR GPIO (general purpose input output) B output data register output data register
They are all meaningful names, why is it difficult to remember? ? And the names are all from ST's official datasheet. I really
don't see much difference between this program and the one you wrote with 51...
Add the GPIOB register just now and see how ST's official library defines it.
LibrariesCMSISCM3DeviceSupportSTSTM32F10xstm32f10x.h
Open it with UltraEdit and search for GPIOB
#define PERIPH_BASE ((uint32_t)0x40000000)
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
#define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00)
Yes, it is exactly the same as in Keil.
typedef struct
{
__IO uint32_t CRL;
__IO uint32_t CRH;
__IO uint32_t IDR;
__IO uint32_t ODR;
__IO uint32_t BSRR;
__IO uint32_t BRR;
__IO uint32_t LCKR;
} GPIO_TypeDef;
where __IO is defined in LibrariesCMSISCM3CoreSupportcore_cm3.h Why do I know it is in this file, because I can
use source insight ...
#define __IO volatile
__IO uint32_t CRL is actually volatile uint32_t CRL
Why use a structure? Because the address allocation of the members of the structure (in RAM) is continuous (I don't know if the original poster understands this, this is still a C language problem),
and the function registers of a module of STM32 are continuous, and each register is equivalent to the base address plus the offset, which is consistent with the above theory. So
there is the use of structure pointers.
Track the source code of the library function. For example, the GPIO initialization function
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
passes the IO port GPIO_TypeDef* GPIOx in the form of a structure pointer
to access the CRL register. The member form GPIOx->CRL is used;
there is no need to worry about the efficiency of doing so, because they are all addresses, that is, pointers, and the final efficiency is direct register operation, which is very efficient. If
you can't understand the library function, it all comes down to your poor C language skills. Don't think that you understand C language after writing a few lines of 51, it's still a long way to go.
Also, when you download the STM library, there are many examples included. There are very detailed introductions on how to use the library functions in the examples. You don't need to write a few lines of code. You can
just copy the examples and do experiments. It's also very easy.
To summarize the author's questions
: 1. ARM does not have SFR, nor does it need it. SFR is a keyword of 51. There is no reason why 51 must have ARM. For example, ARM does not have ACC, but has
R0-R15. These are the architectures (architecture differences).
2. The registers of STM32 have all been defined in the official header files, which have been explained above. (Just because you don't understand it doesn't mean it doesn't exist, right?)
3. The LED program without library functions has been implemented.
The only way to make progress is to read more books, look at more code, write more, think more, and talk less. The author is too impetuous. Reflect on it.
Previous article:Common problems and solutions in the debugging process of STM32
Next article:stm32f10x_conf.h and stm32f10x.h
Recommended ReadingLatest update time:2024-11-15 17:50
- Popular Resources
- Popular amplifiers
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
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Unboxing of Materials - STM32F767 & ESP32
- Prize Review: 50 Rapid IoT Kits from NXP to Proof of Concept in Minutes
- Download Keysight Technologies' e-book "X-Apps Treasure Map: Essential Measurement Apps for Signal Analyzers That Accelerate Tests" and receive a gift!
- [Evaluation of EC-01F-Kit, the EC-01F NB-IoT development board] Power-on and serial port test
- ELEXCON 2022 Shenzhen International Electronics Exhibition will open on November 6 (new schedule), hurry up and get your tickets! There are also many gifts waiting for you!
- Registration reminder: In the last few hours, 100 sets of Pingtouge RISC-V ecological development boards worth 390 yuan will be given away for free~
- Visit Avnet New Energy Technology Innovation Cloud Exhibition and win great gifts!
- Python Learning Handbook (4th Edition)
- Small Automotive Front-End 16W Power Supply Reference Design
- What is stray capacitance