STM32 Embedded Development Study Notes (a)

In this paper, we will introduce the use of embedded development tools Keil uVision5, using the C language for embedded microprocessor STM32F103C8 development.

Developed using C language, C language first need to create a new file, it will set the main entry function, therefore, this file is named main.c

After configuring the hardware and software environment dependent, type the following in this file, try to compile, if we can successfully compile configuration was successful environment.

Stm32f10x.h library function is to provide for the definition and implementation of all functions stm32f10x series development board, so to add that in the header file.

#include <stdio.h>
#include <stm32f10x.h>
int main(){

    return 0;
}

Experiment 1: Let the little lights lit

Check STM32 technical manuals, we can see a small pin A1-4 is used to control lights, however, to manipulate pins A1-4, we must first clock signal can GPIOA of

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

Then, we define a GPIO structure for handling small lights

GPIO_InitTypeDef GPIO_LED_INIT;

According to technical manuals, GPIO_InitStruct type structure has the following three members:

typedef struct
{
    u16 GPIO_Pin;
    GPIOSpeed_TypeDef GPIO_Speed;
    GPIOMode_TypeDef GPIO_Mode;
} GPIO_InitTypeDef; 

Represent control which of the pins, signal rate and working conditions.

Wherein the operating state is divided into input and output, the output state is divided into (multiplexed) open drain output (multiplexed) push-pull output.

In this experiment we only the first control led lights, so only the control pin under the control of clock GPIOA 1, the signal rate is set to a push-pull output 2MHz, the operation mode.

GPIO_InitTypeDef GPIO_LED_INIT;
GPIO_LED_INIT.GPIO_Pin=GPIO_Pin_1;
GPIO_LED_INIT.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_LED_INIT.GPIO_Mode=GPIO_Mode_Out_PP;

Then we initialize this set according to the above structure, and a set GPIO function, so that output pin, so that the small lights.

GPIO_Init(GPIOA,&GPIO_LED_INIT);
GPIO_SetBits(GPIOA,GPIO_Pin_1);

Experiment 2: Let the four small lights shining at the same time

At the same time I want to manipulate multiple small light, just to GPIO_Pin parameter bit or up.

GPIO_Pin is an unsigned 16-bit integer, in fact, each bit of which indicates a next clock controlling the opening and closing pins 16, 0 represents no signal, the signal represents.

GPIO_Pin_x macro definition, in fact, is a 16-bit integer, a bit = 1, other bits are 0

GPIO_LED_INIT.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;

Let little light shining, blinking his time there must be a certain delay, the human eye can see. You can not use the Sleep () function in embedded development, because the Sleep () function depends on the operating system clock.

Therefore, to run a handwriting function blocking development board processor.

void Delay(int time){
    for(int i=0;i<time;i++)
        for(int j=0;j<100000;j++);
}

Let the small light when the light off delay, this blocking function can be called.

while(1){
    GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4); 
    Delay(500); 
    GPIO_ResetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4); 
    Delay(500); 
}

Experiment 3: Light water

Light water is to make small lights turn lights, flashing lights slightly modify the procedures.

    while(1){
        GPIO_SetBits(GPIOA,GPIO_Pin_1);
        Delay(500);
        GPIO_ResetBits(GPIOA,GPIO_Pin_1);
        GPIO_SetBits(GPIOA,GPIO_Pin_2);
        Delay(500);
        GPIO_ResetBits(GPIOA,GPIO_Pin_2);
        GPIO_SetBits(GPIOA,GPIO_Pin_3);
        Delay(500);
        GPIO_ResetBits(GPIOA,GPIO_Pin_3);
        GPIO_SetBits(GPIOA,GPIO_Pin_4);
        Delay(500);
        GPIO_ResetBits(GPIOA,GPIO_Pin_4);
    }

In this study, the complete code:

#include <stdio.h>
#include <stm32f10x.h>
void Delay(int time){
    for(int i=0;i<time;i++)
        for(int j=0;j<1000;j++);
}
void blingbling(){
    while(1){
        GPIO_SetBits(GPIOA,GPIO_Pin_1);
        Delay(500);
        GPIO_ResetBits(GPIOA,GPIO_Pin_1);
        GPIO_SetBits(GPIOA,GPIO_Pin_2);
        Delay(500);
        GPIO_ResetBits(GPIOA,GPIO_Pin_2);
        GPIO_SetBits(GPIOA,GPIO_Pin_3);
        Delay(500);
        GPIO_ResetBits(GPIOA,GPIO_Pin_3);
        GPIO_SetBits(GPIOA,GPIO_Pin_4);
        Delay(500);
        GPIO_ResetBits(GPIOA,GPIO_Pin_4);
    }
}
void hualahuala(){
    while(1){
            GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4); 
            Delay(500); 
            GPIO_ResetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4); 
            Delay(500); 
    }
}
int main(){
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
    
    GPIO_InitTypeDef GPIO_LED_INIT;
    GPIO_LED_INIT.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
    GPIO_LED_INIT.GPIO_Speed=GPIO_Speed_2MHz;
    GPIO_LED_INIT.GPIO_Mode=GPIO_Mode_Out_PP;
    
    GPIO_Init(GPIOA,&GPIO_LED_INIT);
    blingbling();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/isakovsky/p/11420073.html