The simplest STM32 Getting started tutorial----flashing LEDs

Source: Internet
Author: User

This article is about how to start from scratch, using Keil to build a simple STM32 project, and flashing LED lights to give small white look.

0th Step , of course, first you have to have a STM32 board, its IO port is connected with an LED ...

The first step is to create a folder 0.0

Step Two , open the Keil, set up the project

In the pop-up dialog box, select the STM32 chip you are using.

Select Yes in the next pop-up dialog, so that Keil helps us build the startup file.

The third step is to create a new main.c file and add it to the project.

Click the New button to create a text file.

Enter the main function in C in the created text file

Click Save

After saving, add the file to the project

Fourth Step , click Compile

You can see Keil has an error.

The error message is: There is no defined symbol Systeminit, because there is a call to the Systeminit function in the boot file, but we do not define it, such as:

For the time being, you can eliminate this error by simply adding the function in main.c to the meaning of the assembly in the above startup file.

Modified and then compiled, the program did not error. At this point, a STM32 project was established.

Fifth Step , copy and paste the following code

#define PERIPH_BASE ((unsigned int) 0x40000000) #define Apb2periph_base (periph_base + 0x10000) #define Gpioa            _base (apb2periph_base + 0x0800) #define Gpiob_base (apb2periph_base + 0x0c00) #define Gpioc_base (Apb2periph_base + 0x1000) #define Gpiod_base (apb2periph_base + 0x1400) #define GPIOE_BASE (AP B2periph_base + 0x1800) #define Gpiof_base (apb2periph_base + 0x1c00) #define GPIOG_BASE (Apb2periph_b  ASE + 0x2000) #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 GPI OE_ODR_ADDR (gpioe_base+12)//0x4001180c #define GPIOF_ODR_ADDR (gpiof_base+12)//0x40011a0c #define GPIOG_ODR_Ad Dr (gpiog_base+12)//0x40011e0c #define Bitband (addr, Bitnum) ((Addr & 0xf0000000) +0x2000000+ ((addr &0xfffff) & LT;&LT;5) + (bitnum<< 2)) #define MEM_ADDR (ADDR) * ((volatile unsigned long *) (ADDR)) #define LED0 mem_addr (Bitband (gpioa_odr_addr,8))//#defi  NE LED0 * ((volatile unsigned long *) (0X422101A0))//pa8typedef struct{volatile unsigned int CR;  volatile unsigned int CFGR;  volatile unsigned int CIR;  volatile unsigned int apb2rstr;  volatile unsigned int apb1rstr;  volatile unsigned int ahbenr;  volatile unsigned int apb2enr;  volatile unsigned int apb1enr;  volatile unsigned int bdcr; volatile unsigned int CSR;} rcc_typedef; #define RCC ((RCC_TYPEDEF *) 0x40021000) TypeDef struct{volatile unsigned int CRL; volatile unsigned int CRH; VO latile unsigned int IDR; volatile unsigned int ODR; volatile unsigned int bsrr; volatile unsigned int BRR; volatile unsigned int lckr; } gpio_typedef; #define GPIOA ((GPIO_TYPEDEF *) gpioa_base) void Ledinit (void) {rcc->apb2enr|=1<<2;//GPIOA Clock Open gpioa->crh&=0xfffffff0; gpioa->crh|=0x00000003; }//coarse delay void delay_ms (volatile unsigned int t) {unsigned int i,n;for (n=0;n<t;n++) for (i=0;i<800;i++);} int main (void) {ledinit (); while (1) {Led0=0;delay_ms (500); Led0=1;delay_ms (500);}} void Systeminit (void) {}

  

The following paragraph is a brief explanation of the code, but not too deep.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////

The complete flow of the STM32 Gpio output high and low levels is as follows:

Configure clock → Configure IO port →io port output Highland level

The 1,stm32 clock is capable of PLL. But I am here to simplify the operation, it is not configured to use only the default clock----internal 8MZH oscillator.

2, on the hardware, my LED light is received by PA8. So, how do I control PA8?

STM32 can not be like a single-chip microcomputer, the ability to operate the IO port. To operate the STM32 IO port, first configure the IO port.

void Ledinit (void)

{

rcc->apb2enr|=1<<2;

gpioa->crh&=0xfffffff0;

gpioa->crh|=0x00000003;

}

where rcc->apb2enr|=1<<2; Is the clock that enables the GPIOA to be made. If you want to make GPIOB clock, it's rcc->apb2enr|=1<<3; . Other and so on.

gpioa->crh&=0xfffffff0;

gpioa->crh|=0x00000003;

is to configure the PA8 for push-pull output, 50MHZ.

To set the PA9 is:

gpioa->crh&=0xffffff0f;

gpioa->crh|=0x00000030;

Other and so on, if the pa0~pa7 will change CRH to the CRL on the line.

If you want to configure other GPIO ports, change the Gpioa to GPIOB,GPIOC ... On the line. ,

3, we know that 51 single-chip microcomputer can control each IO port separately, STM32 can also do, this mechanism is called Bit-bond.

Refer to the cortex-m3 authoritative guide to know, as long as you find the PA8 output register at Bit-bond address, you can operate PA8 output. The method of calculating the address is as follows:

the cortex-m3 authoritative guide gives the method of the C language macro definition, which we can use directly.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////

After entering the above code, click the Configure option and configure the following:

Tick the Create HEX File

Select the J-link download tool

Build again, then download and you're done.

As can be seen, if the PLL is not configured STM32, then relative to the 51 microcontroller, STM32 flashing LED light is only one more step----Configure the Gpio port as output.

The simplest STM32 Getting started tutorial----flashing LEDs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.