1, Introduction to STM32F103C8T6
STM32F103C8T6 is a 32-bit microcontroller based on Cortex-M3 core launched by St. the hardware is packaged with LQFP48 and belongs to STM32 series of microcontrollers of ST company.
2, Initialize GPIO and turn on the LED
The functions of peripherals are completely different, but the initialization is similar.
Lighting is the first skill that all people who learn single chip microcomputer should learn. Only in this way can they get started.
The lighting of 51 single chip microcomputer is to pull the off chip pin (we call it IO port) low and high through the control register, and output high and low levels to control the LED on and off.
Its process: MCU gives instruction - > control register - > gives IO port level - > controls LED on and off
The lighting of stm32 is to enable the peripheral GPIO clock and send instructions to the peripheral GPIO. After receiving the instructions, the peripheral GPIO starts to configure its own registers, and then give the IO port mode to realize various functions.
Process: CPU gives instruction - > GPIO receives instruction - > configure internal register - > configure IO port mode (note mode) - > control LED on and off.
3, STM32 minimum system core board (STM32F103C8T6) + panel board + 3 red, green and blue LEDs are used to build the circuit. The three ports of GPIOB, GPIOC and GPIOD are used to control the LED lights (the maximum clock is 2Mhz) and flash in turn, with an interval of 1 second.
stay Punctual atom openedv data download address Download the rct6 data of mini board and develop the routines of the board.
(1) Establish project template
1. Create project
2. Select chip
3. Add groups and files required for the project
4. Create the name of Groups and click the Add Files... Button on the right to add the corresponding files
5. Configure Options for Target 'Target 1'
Click Target, you can see that the STM chip is STM32F103C8, and modify the crystal oscillator frequency value to 8
Click Output, where select folder for objects is the directory where the generated HEX is stored. Here, select and store it in the established OBJ folder. Create HEX File is used to generate executable code file (the HEX format file of MCU chip can be written by programmer, and the file extension is. HEX)
Then click the C/C + + option and set Define to USE_STDPERIPH_DRIVER,STM32F10X_MD, and then click the Add button at the bottom right to add the path of Include Paths
(2) Configure GPIO ports
There are three steps to initialize and set the GPIO port:
- Clock configuration
- Input / output mode setting
- Maximum rate setting
1. Configure clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //Turn on GPIOB port clock
2. Initialize structure
// @file stm32f10x_gpio.h typedef struct { uint16_t GPIO_Pin; /*!< Select the GPIO pin to configure */ GPIOSpeed_TypeDef GPIO_Speed; /*!< Select the rate of the GPIO pin */ GPIOMode_TypeDef GPIO_Mode; /*!< Select the operating mode of GPIO pin */ }GPIO_InitTypeDef;
3. Configure input / output mode
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP; //The output mode is universal push-pull output GPIO_InitStruct.GPIO_Pin=GPIO_Pin_4 ; //The selected output port is GPIO_ Pin_ four GPIO_InitStruct.GPIO_Speed=GPIO_Speed_2MHz; //The output speed is 2M GPIO_Init(GPIOA,&GPIO_InitStruct);
(3) Main function
1.led.h
#ifndef _LED_H #define _LED_H #include "stm32f10x.h" void LED_R_TOGGLE(void); void LED_G_TOGGLE(void); void LED_Y_TOGGLE(void); void LED_Init(void); #endif
2.led.c
#include "led.h" #include "delay.h" void LED_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE); //Turn on the clock of the peripheral GPIOB GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP; //The output mode is universal push-pull output GPIO_InitStruct.GPIO_Pin=GPIO_Pin_4 ; //The selected port is GPIO_ Pin_ four GPIO_InitStruct.GPIO_Speed=GPIO_Speed_2MHz; //The output speed is 2M GPIO_Init(GPIOA,&GPIO_InitStruct); GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP; //The output mode is universal push-pull output GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10 ; //The selected port is GPIO_ Pin_ one GPIO_InitStruct.GPIO_Speed=GPIO_Speed_2MHz; //The output speed is 2M GPIO_Init(GPIOB,&GPIO_InitStruct); GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP; //The output mode is universal push-pull output GPIO_InitStruct.GPIO_Pin=GPIO_Pin_14 ; //The selected port is GPIO_ Pin_ fourteen GPIO_InitStruct.GPIO_Speed=GPIO_Speed_2MHz; //The output speed is 2M GPIO_Init(GPIOC,&GPIO_InitStruct); } void LED_R_TOGGLE(void) { GPIO_SetBits(GPIOA, GPIO_Pin_4); delay_ms(500); GPIO_ResetBits(GPIOA,GPIO_Pin_4); } void LED_G_TOGGLE(void) { GPIO_SetBits(GPIOB, GPIO_Pin_10); delay_ms(500); GPIO_ResetBits(GPIOB,GPIO_Pin_10); } void LED_Y_TOGGLE(void) { GPIO_SetBits(GPIOC, GPIO_Pin_14); delay_ms(500); GPIO_ResetBits(GPIOC,GPIO_Pin_14); }
3.delay.h
#ifndef __DELAY_H #define __DELAY_H #include "sys.h" void delay_init(void); void delay_ms(u16 nms); void delay_us(u32 nus); #endif
4.delay.c
#include "delay.h" // //If you need to use OS, include the following header file #if SYSTEM_SUPPORT_OS #include "includes.h" // ucos usage #endif static u8 fac_us=0; //Delay multiplier static u16 fac_ms=0; //MS delay multiplier, in ucos, represents the number of ms per beat #if SYSTEM_SUPPORT_OS // If system_ SUPPORT_ The OS is defined, indicating that the OS is to be supported (not limited to UCOS) #ifdef OS_CRITICAL_METHOD // OS_CRITICAL_METHOD is defined to support UCOSII #define delay_osrunning OSRunning // Whether the OS is running flag, 0, not running; 1. In operation #define delay_ostickspersec OS_TICKS_PER_SEC // OS clock beat, i.e. scheduling times per second #define delay_osintnesting OSIntNesting // Break nesting level, that is, the number of times to break nesting #endif //Support UCOSIII #ifdef CPU_CFG_CRITICAL_METHOD // CPU_CFG_CRITICAL_METHOD is defined to support UCOSIII #define delay_osrunning OSRunning // Whether the OS is running flag, 0, not running; 1. In operation #define delay_ostickspersec OSCfg_TickRate_Hz // OS clock beat, i.e. scheduling times per second #define delay_osintnesting OSIntNestingCtr // Break nesting level, that is, the number of times to break nesting #endif //In case of us level delay, turn off task scheduling (to prevent interrupting us level delay) void delay_osschedlock(void) { #ifdef CPU_CFG_CRITICAL_METHOD // Use UCOSIII OS_ERR err; OSSchedLock(&err); //In ucosiiii mode, scheduling is prohibited to prevent interruption and delay #else // Otherwise, UCOSII OSSchedLock(); //In UCOSII mode, scheduling is prohibited to prevent interruption and delay #endif } //Resume task scheduling in case of us level delay void delay_osschedunlock(void) { #ifdef CPU_CFG_CRITICAL_METHOD // Use UCOSIII OS_ERR err; OSSchedUnlock(&err); //UCOSIII mode, resume scheduling #else // Otherwise, UCOSII OSSchedUnlock(); //UCOSII mode, resume scheduling #endif } //Call the delay function of the OS //ticks: delayed beats void delay_ostimedly(u32 ticks) { #ifdef CPU_CFG_CRITICAL_METHOD OS_ERR err; OSTimeDly(ticks,OS_OPT_TIME_PERIODIC,&err); //Ucosiiii delay adopts periodic mode #else OSTimeDly(ticks); //UCOSII delay #endif } //systick interrupt service function, which is used when using ucos void SysTick_Handler(void) { if(delay_osrunning==1) //When the OS starts running, it performs normal scheduling processing { OSIntEnter(); //Entry interrupt OSTimeTick(); //Call ucos clock service program OSIntExit(); //Trigger task switching soft interrupt } } #endif //Initialization delay function //When using OS, this function initializes the clock beat of OS //SYSTICK's clock is fixed to 1 / 8 of HCLK's clock //SYSCLK: system clock void delay_init() { #if SYSTEM_SUPPORT_OS // If necessary, support OS u32 reload; #endif SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //Select external clock HCLK/8 fac_us=SystemCoreClock/8000000; //Is 1 / 8 of the system clock #if SYSTEM_SUPPORT_OS // If necessary, support OS reload=SystemCoreClock/8000000; //The number of counts per second is in M reload*=1000000/delay_ostickspersec; //According to delay_ostickspersec sets the overflow time //reload is a 24 bit register with a maximum value of 16777216. At 72M, it is about 1.86s fac_ms=1000/delay_ostickspersec; //Represents the minimum unit that the OS can delay SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk; //Enable SYSTICK interrupt SysTick->LOAD=reload; //Every 1/delay_ostickspersec is interrupted once per second SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk; //Turn on SYSTICK #else fac_ms=(u16)fac_us*1000; //Under non OS, it represents the number of systick clocks required for each ms #endif } #if SYSTEM_SUPPORT_OS // If necessary, support OS //Delay nus //nus is the number of us to delay void delay_us(u32 nus) { u32 ticks; u32 told,tnow,tcnt=0; u32 reload=SysTick->LOAD; //Value of LOAD ticks=nus*fac_us; //Number of beats required tcnt=0; delay_osschedlock(); //Prevent OS scheduling and interrupt us delay told=SysTick->VAL; //Counter value when entering while(1) { tnow=SysTick->VAL; if(tnow!=told) { if(tnow<told)tcnt+=told-tnow; //Note that SYSTICK is a decrement counter else tcnt+=reload-tnow+told; told=tnow; if(tcnt>=ticks)break; //If the time exceeds / equals the time to be delayed, exit } }; delay_osschedunlock(); //Resume OS scheduling } //Delay nms //nms: number of ms to delay void delay_ms(u16 nms) { if(delay_osrunning&&delay_osintnesting==0) //If the OS is already running and is not in an interrupt (task scheduling cannot be performed in an interrupt) { if(nms>=fac_ms) //The delay time is greater than the minimum time period of the OS { delay_ostimedly(nms/fac_ms); //OS delay } nms%=fac_ms; //The OS can no longer provide such a small delay, so it adopts the ordinary delay mode } delay_us((u32)(nms*1000)); //Common mode delay } #else / / when OS is not used //Delay nus //nus is the number of us to delay void delay_us(u32 nus) { u32 temp; SysTick->LOAD=nus*fac_us; //Time loading SysTick->VAL=0x00; //Clear counter SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //Start counting down do { temp=SysTick->CTRL; }while((temp&0x01)&&!(temp&(1<<16))); //Waiting time arrives SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //Turn off counter SysTick->VAL =0X00; //Clear counter } //Delay nms //Note the range of nms //Systick - > load is a 24 bit register, so the maximum delay is: //nms<=0xffffff*8*1000/SYSCLK //SYSCLK is in Hz and NMS is in ms //For 72M, NMS < = 1864 void delay_ms(u16 nms) { u32 temp; SysTick->LOAD=(u32)nms*fac_ms; //Time loading (systick - > load is 24bit) SysTick->VAL =0x00; //Clear counter SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //Start counting down do { temp=SysTick->CTRL; }while((temp&0x01)&&!(temp&(1<<16))); //Waiting time arrives SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //Turn off counter SysTick->VAL =0X00; //Clear counter } #endif
5.main.c
#include "stm32f10x.h" #include "delay.h" #include "led.h" int main(void) { LED_Init(); delay_init(); //Use the system tick timer and delay initialization while(1) //Cycle on { LED_R_TOGGLE(); delay_ms(500); //Delay 1s after the red light is on LED_G_TOGGLE(); delay_ms(500); //Delay 1 s after the green light is on LED_Y_TOGGLE(); delay_ms(500); //Delay 1s after the yellow light is on } }
6. Generate. HEX file
(4) Build circuit
GND — GND
3v3 — 3v3
TXD — A10
RXD — A9
1. Download the program through serial port
To use the serial port, you must first install the USB to serial port driver - CH340 version in the computer
2. Open the mcuisp software and configure it as follows:
① Search the serial port and set the baud rate to 115200 (try not to set it too high)
② Select the HEX file to download
③ Execute after verification and programming
④ DTR low level resets and RTS high level enters bootloader
⑤ Start programming. If it is always connected, press the reset key of the development board
3. Success
(5) References
(18 messages) STM32F103 register mode turns on the LED water flow lamp_ Favorite blog - CSDN blog