Experimental purpose
Flash light Program in embedded learning like "Hello world." "It's just as Classic in C + + language learning. It guides countless embedded enthusiasts in a simple way. With this section you can learn a basic understanding of STM32 's Gpio and the use of basic timers.
Hardware description
This routine requires a timer and an LED, where the LED is the red led on the expansion board on the PD3 and the positive is connected to the high level, the timer selects the basic Timer 7.
1. STM32 Gpio Introduction
gpio Main Features
Output status selectable push-pull, open-drain, pull-up or pull-down
Speed can be selected for each I/O
Input status optional then floating, pull/pull-down, analog
Multiplexing function for each I/O pin
Bit operation on each output pin
Each gpio of the STM32 has 4 32-bit configuration registers: Mode selection register, output type configuration register, output speed configuration register, pull/pull-down resistor configuration register, 2 32-bit data registers: Data input register, data output register , a 32-bit lock register and 2 32-bit multiplexing feature selection registers. Whether you choose an I/O as input or output, you can choose whether to use pull-up or pull-down resistors depending on your needs. In general, each I/O has 8 modes to choose from: input dangling, with pull-up input, drop-down input, pull-up or down-drain output, pull-up or pull-pull output, analog input, push-pull, and multiplexing with pull-up or pull-down, open-drain, and pull-up or pull-down multiplexing functions.
1.1 I/O mode selection
Each I/O pin has 4 usage modes to choose from. Gpiox_moder (x = A). I) is a 32-bit register that configures one pin per two bits, bit [1:0] configuration pin 0 and so on. The values and meanings are shown in table 1.1.
Table 1.1 I/O usage mode settings
MODER[1:0] Description
B00 Input mode (initial value)
B01 General Output mode
B10 multiplexing Function mode
B11 Analog Signal Mode
1.2 Output Type selection
Depending on the output requirements, you can set the push-pull or open-drain output in the gpiox_otyper. This register is only valid for low 16 bits, and the value and definition are shown in table 1.2.
Table 1.2 Output Type settings
Ot[0] Description
B0 push-Pull output (initial value)
B1 Open-Drain output
1.3 Output Speed setting
Table 1.3 Port Output Speed settings
OSPEEDER[1:0] Description
B00 2MHZ Low Speed
B01 25MHZ Medium speed
B10 50MHZ Fast
B11 100MHZ High Speed
1.5 data input and output
When Gpio is set to universal input, read Register GPI OX_IDR) (x = A). I) The input state of the port can be obtained and the register is read-only; GPIOX_ODR (x = A). I) is a read/write register that writes data to this register to control the port output level, which reads data from this register to determine the output status of the port.
1.6 Multiplexing Feature Selection
There are 16 multiplexing functions in the STM32, and one pin will correspond to several of them. A total of two sent
GPIOX_AFRL and GPIOX_AFRH are used to set the multiplexing function of the pin, where each
4 bits corresponds to one pin.
Table 1.6 Re-use function configuration
AFR[3:0] Description
0x1 AF1 (TIM1/TIM2)
0X2 AF2 (TIM3 ... 5)
0X4 AF4 (i2c1 ... 3)
0XD AF14 (DCMI)
2. Introduction to STM32 Basic timers
STM32 Timer is very powerful, according to the function can be divided into advanced control timer, general timer, basic timing, wherein the timer 6, 7 is the basic timer. Here we mainly give a brief introduction to the basic timer. It has a 16-bit automatic overload addition counter and a 16-bit programmable prescaler. The prescaler divides the input clock and supplies it to the counter as a counting clock. The STM32 automatic reload Register (Timx_arr) physically corresponds to two registers, one that we can read and write casually, and the other can only be read by the timer. This register that we can't manipulate is called the Shadow Register. The value in the shadow register can be changed immediately when the Arpe bit in the TIMX_CR1 equals 0 o'clock to change the value of the Timx_arr. When the value of Arpe equals 1 o'clock Timx_arr is cached, the new value is transferred to the shadow register only after the update event occurs.
The input clock needs to be turned on before the operation of the timer, and then the re-prescaler register (TIMX_PSC) and the automatic load register can be set. Because we want the timer to produce an update interrupt, it is necessary to enable the Uie bit in the Timx_dier and set the Nvic correlation register. Set TIMX_CR1 's cen bit after initialization is complete to turn on the timer.
Experimental principle and program structure
Experimental design
Using STM32 's basic timer to generate an update interrupt, the interrupt handler function controls the level of the LED pin, giving everyone a flashing light effect. This routine involves initialization of the GPIO_D03 and basic Timer 7 and a Timer 7 interrupt service routine, and so on.
SOURCE Program Description
Let's take a look at how BLINK.C is implemented.
LED initialization
Code: SELECT/* BLINK.C */
void Led_init (void)
{
Gpio_inittypedef gpio_initstructure;
The initialization of LEDs is simple, select the PIN and set the PIN usage mode to output. It is important to note that the clock for each peripheral of the STM32 can be individually controlled, the clock needs to be opened before operation and the STM32 user's manual must be confirmed clearly, which bus (AHB1, APB1, etc.) you want to use to mount the device.
LED control
Code: SELECT/* BLINK.C */
void led_on (void)
{
/* Set PD3 to Low */
Gpio_resetbits (Gpiod, gpio_pin_3);
}
void Led_off (void)
{
/* Set PD3 to High level */
Gpio_setbits (Gpiod, gpio_pin_3);
}
These three led control functions use bit operation, because the LED's positive is connected to the high level, the led_on is to let the PD3 output low level to light the LED. The led_toggle will be called in the interrupt service routine of the timer, which switches the pin between the high and low levels to achieve the flashing light.
Timer initialization
Code: SELECT/* BLINK.C */
void Tim7_configuration (void)
{
Nvic_inittypedef nvic_initstructure;
Tim_timebaseinittypedef tim_timebaseinitstruct;
/* Enable input clock before operation timer */
Rcc_apb1periphclockcmd (RCC_APB1PERIPH_TIM7, ENABLE);
/**** Time Base Configuration ****/
/* Set automatic reload cycle, count to 5000 to 1000ms */
Tim_timebaseinitstruct.tim_period = 5000;
/* Set the Prescaler value, i.e. the timer count frequency is 10Khz */
Tim_timebaseinitstruct.tim_prescaler = (8400-1);
tim_timebaseinitstruct.tim_clockdivision = 0;
/* Up counting Mode */
Tim_timebaseinitstruct.tim_countermode = tim_countermode_up;
/* Initialize the TIM7 's base unit based on the parameters specified in Tim_timebaseinitstruct */
Tim_timebaseinit (TIM7, &tim_timebaseinitstruct);
/* Enable TIM7 update interrupt */
Tim_itconfig (TIM7, Tim_it_update, ENABLE);
Here is the simplest basic timer, which can be activated by opening the input clock, setting the Prescaler, and counting the overloaded values after configuring the relevant interrupt register. The stm32f4xx_it of the St library code. All interrupt portals are defined in C, and we need to add the following code inside:
Timer 7 Interrupt Service Routine
Code: SELECT/* stm32f4xx_it. C */
void Tim7_irqhandler (void)
{
if (Tim_getitstatus (TIM7, tim_it_update)! = RESET)
{
/* Clear the Interrupt flag */
Tim_clearitpendingbit (TIM7, tim_it_update);
/* Toggle I/O status */
Led_toggle ();
}
}
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.