STM32 beginner led no library detailed analysis

Publisher:自由梦想Latest update time:2015-12-28 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I was just passing by, so I wrote it in more detail.

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.
Keywords:STM32 Reference address:STM32 beginner led no library detailed analysis

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

Run the first STM32 program using Keil MDK
1.1.1 Run the first STM32F10X program using Keil MDK In the previous section, we have introduced in detail the process of creating a project using Keil MDK and the standard peripheral library. The following section will introduce how to write a small program based on this project. Through this program, we can have a p
[Microcontroller]
Run the first STM32 program using Keil MDK
Circuit protection is an important part of LED drive circuit design
    In order to accelerate the development of the LED industry, the Fujian Provincial Government issued in May this year the "Notice of the General Office of the Fujian People's Government on the Promotion and Application of LED Lighting Products in Fujian Province", planning to achieve the universal use of LED lighti
[Power Management]
How to generate hex file in IAR using stm32
1In the project options, 2 Select the output converter option. 3 Check the Generate additional output option 4 Select the Intel extended option in Output format 5. Check Override default under Output file to complete. Then there will be ×××.hex in the Exe folder. The above steps are as shown below  
[Microcontroller]
How to generate hex file in IAR using stm32
STM32 ADC
How many bits does the ADC have? 12-bit How many ADCs are there? 1, 2 or up to 3, depending on the device; each with multiple channels. About the channel: 10.3.3   Channel Selection              There are 16 multiplexed channels. Conversions can be divided into two groups: regular and injected. A serie
[Microcontroller]
STM32 debugging support (DBG)
The STM32F10xxx uses the Cortex™-M3 core, which contains a hardware debug module that supports complex debugging operations. The hardware debug module allows the core to stop when fetching instructions (instruction breakpoints) or accessing data (data breakpoints). When the core stops, the internal state of the core
[Microcontroller]
STM32 debugging support (DBG)
A collection of questions about STM32 ADC/DAC
1. How does the STM32 DAC conversion start? Q: How does the STM32 DAC conversion start? How to use the DAC to output a single pulse with controlled pulse width?  A: The DAC is started by writing to the DAC output register. In addition, if you want a pulse, use the TIM function. 2. STM32 DAC output voltage Q:
[Microcontroller]
Why does STM32 remap boot from Flash address 0x08000000
When I first wrote an STM32 program, I encountered a confusion. The STM32 Flash was set to start at address 0x0800 0000 in MDK, but the CM3 manual stipulates that the interrupt vector should be taken from address 0x0000 0000 when the chip is reset. So how does the STM32 execute the code? Address remapping? Or is there
[Microcontroller]
Micro2440 driver analysis 1——LED driver
micro2440 uses the S3C2440 processor (which is not much different from the S3C2410). In its Linux source code, the code related to this platform is mainly in arch/arm/mach-s3c2410 and include/asm-arm/arch-s3c2410, and the related drivers are in the drivers directory. (1) DM9000 network card driver kernel-2.6.13/dri
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号