WOSEN LIGHTING TECHNOLOGY LIMITED

WOSEN LIGHTING TECHNOLOGY LIMITED

Casa> Blog> Use the timer to control the Led light to flash

Use the timer to control the Led light to flash

June 11, 2023

Purpose

The flash program is as classic as "Hello World!" in embedded learning in C/C++ language learning. It guides countless embedded enthusiasts in a simple way. Through this section you can get a basic understanding of the STM32 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 connected to PD3 and the positive pole is connected to the high level. The timer selects the basic timer 7.

1. Introduction to STM32 GPIO

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 is optional, floating, pull-up/pull-down, simulation

Each I/O pin has a multiplexing function

Bit operation for each output pin

Each GPIO of STM32 has four 32-bit configuration registers: mode select register, output type configuration register, output speed configuration register, pull-up/pull-down resistor configuration register; two 32-bit data registers: data input register, data output register ; 1 32-bit lock register and 2 32-bit multiplexed function select registers. Whether you choose an I/O as an input or an output, you can choose whether to use pull-up or pull-down resistors as needed. In general, each I/O has 8 modes to choose from: input floating, with pull-up input, pull-down input, pull-up or pull-down open-drain output, pull-up or pull-down push-pull output, analog Input, push-pull and multiplex function with pull-up or pull-down, open-drain and multiplex function with pull-up or pull-down.

1.1 I / O mode selection

There are 4 usage modes available for each I/O pin. GPIOx_MODER (x = A..I) is a 32-bit register with one pin per two bits, pin 0 in bits [1:0], and so on. Its value and meaning 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 alternate function mode

B11 analog signal mode

1.2 Output Type Selection

You can set the push-pull or open-drain output in GPIOx_OTYPER depending on the output requirements. This register is valid only for the lower 16 bits. The values ​​and definitions 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.4 Pull-up pull-down resistor setting

Table 1.4 Pull-up pull-down resistor settings

PUPDR[1:0] Description

B00 no pull-up or pull-down resistor

B01 pull-up resistor connection

B10 pull-down resistor connection

B11 reservation

1.5 data input and output

When GPIO is set to general-purpose input, the register GPI Ox_IDR) is read (x = A..I) to get the input state of the port and this register is read-only; GPIOx_ODR (x = A..I) is a readable and writable Register, write data to this register to control the port output level, read data from this register to determine the port's output status.

1.6 Multiplexing function selection

There are 16 kinds of multiplexing functions in STM32, one pin will correspond to several. There are two mailings

The registers GPIOx_AFRL and GPIOx_AFRH are used to set the alternate function of the pins, each of which

4 bits correspond to one pin.

Table 1.6 Multiplexing 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 timer

The timer of the STM32 is very powerful. It can be divided into advanced control timers, general-purpose timers, and basic timing according to functions. Timers 6 and 7 are basic timers. Here we mainly give a brief introduction to the basic timer. It has a 16-bit auto-reload addition counter and a 16-bit programmable prescaler. The prescaler divides the input clock and provides it to the counter for use as a count clock. The STM32's auto-reload register (TIMx_ARR) physically corresponds to two registers, one is that we can read and write at will, and the other can only be read by the timer. This register that we cannot operate is called a shadow register. Changing the value of TIMx_ARR when the ARPE bit in TIMx_CR1 is equal to 0 changes the value in the shadow register immediately. When the ARPE is equal to 1, the value of TIMx_ARR is buffered, and the new value is transferred to the shadow register only after the update event occurs.

The input clock needs to be turned on before operating the timer, and then the re-prescaler coefficient register (TIMx_PSC) and the auto-load register can be set. Because we want the timer to generate an update interrupt, we must enable the UIE bit in TIMx_DIER and set the NVIC related registers. The timer can be turned on by setting the CEN bit of TIMx_CR1 after initialization.

Experimental principle and program structure

experimental design

  The STM32's basic timer is used to generate the update interrupt, and the level of the LED pin is controlled in the interrupt handler function to give you a flash effect. This routine will refer to GPIO_D03 and the initialization of Basic Timer 7 and a Timer 7 Interrupt Service Routine.

Source description

Let's take a look at how blink.c is implemented.

LED initialization

/* blink.c */

Void led_init(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

/* Enable GPIOD clock*/

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

/* Configure GPIO_LED pin*/

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOD, &GPIO_InitStructure);

}

LED initialization is relatively simple, select the pin and set the pin usage mode to the output. It should be noted that the clock of each peripheral of STM32 can be controlled separately. It is necessary to turn on the clock before operation and confirm from the STM32 user manual. Which peripheral you want to use is specifically mounted on which bus (AHB1) , APB1, etc.).

LED control

/* blink.c */

Void led_on(void)

{

/* Set PD3 to low level*/

GPIO_ResetBits(GPIOD, GPIO_Pin_3);

}

Void led_off(void)

{

/* Set PD3 to high level*/

GPIO_SetBits(GPIOD, GPIO_Pin_3);

}

Void led_toggle(void)

{

/* Switch PD3 level status*/

GPIO_ToggleBits(GPIOD, GPIO_Pin_3);

}

These three LED control functions all use the bit operation. Since the positive pole of the LED is connected to the high level, led_on causes the PD3 to output a low level to illuminate the LED. Led_toggle will be called in the timer's interrupt service routine, which will switch the pin between high and low levels to the result of the flash.

Timer initialization

/* blink.c */

Void TIM7_Configuration(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

/* Enable the input clock before operating the timer*/

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);

/**** Time base configuration ****/

/* Set the auto-reload cycle to count to 5000 for 1000ms */

TIM_TimeBaseInitStruct.TIM_Period = 5000;

/* Set the prescaler value, ie 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;

/* Initializes the time base unit of TIM7 according to the parameters specified in TIM_TimeBaseInitStruct*/

TIM_TimeBaseInit(TIM7, &TIM_TimeBaseInitStruct);

/* Enable TIM7 update interrupt*/

TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE);

/* Configure the NVIC Preemption Priority Bits */

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

/* Enable the TIM7 global Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

/* Start timer*/

TIM_Cmd(TIM7, ENABLE);

}

Here is the simplest basic timer. After the input clock is turned on, the prescaler is set, and the reload value is set, the relevant interrupt registers are configured and the timer can be started. All interrupt entries are defined in stm32f4xx_it.C of the ST library code, we need to add the following code inside:

Timer 7 interrupt service routine

/* stm32f4xx_it.C */

Void TIM7_IRQHandler(void)

{

If (TIM_GetITStatus(TIM7, TIM_IT_Update) != RESET)

{

/* Clear interrupt flag */

TIM_ClearITPendingBit(TIM7, TIM_IT_Update );

/* Switch I/O status*/

Led_toggle();

}

}


0 times
Window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "2", "bdMiniList": false, "bdPic": "", "bdStyle": " 0", "bdSize": "24" }, "share": {}, "image": { "viewList": ["qzone", "tsina", "tqq", "renren", "weixin"], "viewText": "Share to:", "viewSize": "16" }, "selectShare": { "bdContainerClass": null, "bdSelectMiniList": ["qzone", "tsina", "tqq", "renren" , "weixin"] } }; with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share. Baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)];
Contattaci

Author:

Ms. Mandy

Phone/WhatsApp:

+8613425434349

Prodotti popolari
You may also like
Related Categories

Mail a questo fornitore

Oggetto:
E-mail:
messaggio:

Your message must be betwwen 20-8000 characters

  • Invia domanda

Copyright © 2024 WOSEN LIGHTING TECHNOLOGY LIMITEDTutti i diritti riservati

We will contact you immediately

Fill in more information so that we can get in touch with you faster

Privacy statement: Your privacy is very important to Us. Our company promises not to disclose your personal information to any external company with out your explicit permission.

Invia