Why learn STM32?
STM32 is a 32-bit microcontroller but only costs the price of an eight-bit microcontroller, and the speed is several times that of an eight-bit microcontroller.
More importantly, it is easier to grasp as an ARM entry-level chip, and there are many online materials, and many people are using it.
The IO port of STM32 has 7 registers to control, but we usually use 4 CRL CRH IDR ODR.
Port configuration low register (GPIOx_CRL)
Port configuration high register (GPIOx_CRH)
Port input data register (GPIOx_IDR)
Port output data register (GPIOx_ODR)
The CRL controls the high 8 bits of the IO CRH and the low 8 bits are essentially the same.
Compared with our AVR, GPIOx_CRL is equivalent to DDRx, GPIOx_ODR is equivalent to PORTx, GPIOx_ODR is equivalent to PINx STM32 IO port can be configured by software into software configuration into software configuration into 8 modes: modes:
1. Enter floating
2. Input pull-up
3. Input drop-down
4. Analog input
5. Open drain output
6. Push-pull output
7. Push-pull multiplexing function
8. Open drain multiplexing function
Simply put, the CRL register of STM32 can set the input and output modes, and can also set the maximum output rate.
Input floating: It is neither a pull-up nor a pull-down input.
Open-drain mode and push-pull mode: weak pull-up and pull-down resistors are disabled. In open-drain mode, the I/O port status can be obtained when reading the input data register, and in push-pull mode, the last write can be obtained when reading the output data register The value of the analog input configuration. The weak pull-up and pull-down resistors are disabled and the value is '0' when reading the input data register.
Okay, let's talk about the next control method that is often heard. Direct manipulation of registers and library functions means direct manipulation of registers is to directly write values ​​to CRL and other registers. The library function is ST official, all the controls are written, we just need to call it. Direct manipulation register
GPIOA-CRH=0XFFFFFFF0;
GPIOA-CRH|=0X00000003;/PA8 push-pull output
GPIOA-ODR|=18; /PA8 output high
Library Functions
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed ​​=GPIO_Speed_Level_3;
GPIO_Init (GPIOA, GPIO_InitStruct);
GPIO_SetBits (GPIOA, GPIO_Pin_11 | GPIO_Pin_12 );
Now write the initialization of an LED
Direct manipulation register
/LED IO initialization
void LED_Init(void)
{
RCC-APB2ENR|=12; /Enable PORTA clock
GPIOA-CRH=0XFFFFFFF0; /PA8 push-pull output
GPIOA-CRH|=0X00000003;/rate
GPIOA-ODR|=18; /PA8 output high, high light off at the beginning
}
Note: First enable the PORTA clock
Library Functions
void LED_Init(void)
{
GPIO_InitTypeDefGPIO_InitStructure; / Declare structure ST library
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);/Enable PA port clock
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;/LED0--PA.8 port configuration
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /Push-pull output
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; /speed
GPIO_Init (GPIOA, GPIO_InitStructure); / structure initialization
GPIO_SetBits (GPIOA, GPIO_Pin_8);/PA.8 output high
}
OK to the initialization of these two methods are OK
main function
int main(void)
{
SystemInit(); /The system clock is initialized to 72M SYSCLK_FREQ_72MHz
delay_init(72); /delay function initialization
NVIC_Configuration(); /Set NVIC interrupt group 2: 2-bit preemption priority, 2-bit response priority
LED_Init(); /LED port initialization
while(1)
{
LED0=0;/You can also use (library) GPIO_ResetBits (GPIOA, GPIO_Pin_8);
/Register GPIOA-ODR=~(18);
delay_ms(300);
LED0=1; / GPIO_SetBits (GPIOA, GPIO_Pin_8) can also be used;
/Register GPIOA-ODR|=18;
delay_ms(300);
}
}
Enabling the PORTA clock, here is a big difference between the clock tree of STM32 and AVR51, etc. Each peripheral has its own clock. To use a peripheral, you must turn on the clock of the peripheral first. This also reduces power consumption to a certain extent.
DC Capacitor For Electric Furnace
DC Capacitor For Electric Furnace
DC Capacitor For Electric Furnace,DCMJ DC Filter Capacitors,DCMJ Pulse Capacitors,DCMJ DC Support Capacitors
YANGZHOU POSITIONING TECH CO., LTD. , https://www.cnpositioning.com