(06) —Systick timer and ADC of STM32

Directory

【1】Systick timer

concept

working principle

clock reference

【2】HAL_Delay function analysis

【3】Timer

basic concept

Timer classification

Timer Composition

1. Counter

2. Automatically reload registers

3. Prescaler

Timer Counting Principle

experiment

2.PWM

definition

parameter

working principle

application

Exercise: Adjust the brightness of LED light through PWM signal

Exercise: Realize the Breathing Light Effect

3. Buzzer

Introduction:

Classification:

Buzzer sound experiment

【4】ADC

1. Definition

2. Introduction to ADCs

3. ADC characteristics

4. ADC clock

5. Working mode

6. Single channel single conversion experiment


【1】Systick timer

concept:

SysTick is also called tick timer. It is a timing device, which is located in the Cortex-M0 core and bundled with NVIC. It generates SysTick exception (IRQ exception number 15) to count the input clock. The system timer is generally used in the operating system to generate the time base and maintain the operation. The heartbeat of the system.

How it works

working principle:

The tick timer is a 24-bit down timer, that is, it can count up to 2^24 (0xFFFFFF).

After the initial value of SysTick is set and enabled, the count value will be decremented by 1 every time a clock signal comes.

When the count reaches 0, an interrupt is triggered, and the SysTick counter automatically reloads the initial value and continues to decrease by one, and the cycle continues.

The tick timer is enabled by default.

Clock Base

Exploration: How often does the systick trigger an exception?

First look at what Systick does in the exception handler every time it triggers an exception

Every time the systick triggers an interrupt, uwTick will increase by 1

The value of this uwTick is used as the clock reference of our system.

Then let’s study again, how long will uwTick be increased by 1.

In main.c, the first function HAL_Init() executed by the main function will initialize the systick.

Initialize Tick by calling the HAL_InitTick function in HAL_Init

Pass parameters as:

TICK_INT_PRIORITY = 0 as the interrupt priority of systick

Call the HAL_SYSTICK_Config function to configure the systick time base

SystemCoreClock / (1000U /(uint32_t)uwTickFreq)

uint32_t SystemCoreClock = 16000000UL;

16000000 / 1000 / 1 = 16000

Passed 16000 as a parameter to the HAL_SYSTICK_Config function

The Systic_Config function configures the reload value, initial value, priority and calibration value of Systick.

if ((ticks – 1UL) > SysTick_LOAD_RELOAD_Msk) //If the initial value of systick is greater than the maximum value 0xFFFFFF

{

return (1UL); //return 1 to indicate configuration failure

}

SysTick->LOAD = (uint32_t)(ticks – 1UL); /* reload count value */ 16000 – 1

NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Systick Interrupt */

SysTick->VAL = 0UL; /* Load the SysTick Counter Value */

SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |

SysTick_CTRL_TICKINT_Msk |

SysTick_CTRL_ENABLE_Msk;

return (0UL);

First look at the value of each reload

SysTick->LOAD = (uint32_t)(ticks – 1UL); //ie 16000 – 1

Decrement from 16000 – 1 to 0, a total of 16000 counts are required

The current clock frequency is 16Mhz, indicating that it takes 1/16M s to count a number

It takes time to count 16000 numbers 16000 * 1/16000000 s = 1/1000 s = 1ms

So Systick will trigger an exception every 1ms.

[Exercise] Use Systick exception to print “helloworld” in 1 second

Let the flag increase by 1 every time the Systick triggers an exception

Judging in the main function, when the flag is added to 1000, it means that the systick exception has been triggered 1000 times, and it just takes 1s at this time.

print “helloworld”

【2】HAL_Delay function analysis

__weak void HAL_Delay(uint32_t Delay)
{
  uint32_t tickstart = HAL_GetTick();//Get the current time
  uint32_t wait = Delay;//Time to delay
  /* Add a freq to guarantee minimum wait */
  if (wait < HAL_MAX_DELAY)// caused an error of 1ms, no matter how long the delay time is, it will add 1
  {
    wait += (uint32_t)(uwTickFreq);
  }
  while ((HAL_GetTick() - tickstart) < wait)
  // Get the latest time minus the time when it came in
  {
  }
}

The GetTick function returns the value of the current system uwTick

uwTick will increase by 1 every 1ms

Therefore, the implementation of HAL_Delay relies on Systick to achieve the delay effect in milliseconds.

Clock review:

Configured to use an external high-speed clock

Four clock sources HSI LSI HSE LSE

【3】Timer

Basic concept

A timer is essentially a counter that can count the input clock and trigger an interrupt when the count value reaches the set value. When the input of this counter is an accurate and reliable reference clock, the process of counting the reference clock is timing the process of.

Timer classification

The basic structure of the timer is universal, and many module circuits can be used, so STM32 timing has expanded a lot of functions. According to the complexity and application scenarios, it is divided into three types: advanced timer, general timer and basic timer. .

Timer composition

1. Counter

Up-counting mode: The counter starts counting from 0. When it reaches the value in the auto-load register (TIMx_ARR), it is automatically cleared and an overflow event is generated, and then counts up from 0.

Down-counting mode: The counter starts counting down from the value in the auto-load register (TIMx_ARR). When the count value reaches 0, a timer overflow event is generated, and the initial value is reloaded to continue counting down.

Central alignment mode: also known as up/down counting, the counter increments from 0 to the value of ARR to generate a timer overflow event, and then decrements from the value of ARR to 0 to generate a timer overflow event.

2. Auto-reload register

3. Prescaler

Timer Counting Principle

The clock frequency is configured as 48Mhz, how to make the timer generate 1s interrupt?

Writing 0 for the frequency division value is equivalent to no frequency division 48/1 => 48

First divide the main frequency by 48 to get the frequency of 1Mhz, then the frequency division value is 48-1

48Mhz / 48 => 1Mhz

The clock frequency of 1Mhz is equivalent to 1/1000000 seconds to count a number.

So if you want to get a 1s interrupt, you need to count from 0 to 1000000-1, that is, it takes 1 second to count 1000000 numbers.

1000000 numbers * 1/1000000s = 1s

Frequency is the reciprocal of time 1Mhz = 1/1000000s

Experiment

Use the timer interrupt to print a “helloword” in 1s

Rewrite the timer overflow interrupt callback function in main.c

Start timer to count in interrupt mode

2.PWM

Define

PWM (Pulse Width Modulation), short for pulse width modulation, is a very effective technology that uses the digital output of a microprocessor to control an analog circuit.

parameter

Cycle

The time required for high and low level changes, unit: ms

T=1/f T is the period and f is the frequency.

Frequency

Within 1 second, the number of times the signal goes from high level to low level and back to high level, that is to say, how many cycles of PWM there are in one second, and the unit is Hz.

For example: If the frequency is 50Hz, which means one cycle is 20ms, then there are 50 PWM cycles in one second.

Duty Cycle

In a pulse cycle, the high level time accounts for the proportion of the entire cycle time, and the unit is % (0%-100%).

How it works

Counter Register (TIMx_CNT)

Autoload Register (TIMx_ARR)

Capture/Compare Register (TIMx_CCRx)

Count up mode:

Output process:

During the period from 0 to t1, the value of CNT in the counter register is less than CCR, and the output is high.

During the period of t1-t2, the value of CNT in the counter register is greater than CCR and less than ARR, and the output is low.

When the value of CNT reaches the value in ARR, an overflow event is generated, and the auto-clear starts counting up from 0 again.

apply

LED lighting dimming, motor speed control, steering gear steering control, buzzer control, etc.

Exercise: Adjust the brightness of LED light by PWM signal

Frequency: 1/T = 1/1ms = 1/0.001s = 1000HZ

Cycle: 1ms

Duty cycle: 50%

Find the PWM signal generation function in TIM:

call in main.c

Exercise: Realize the effect of breathing light

3. Buzzer

Introduction:

The buzzer is an electronic sounder powered by DC voltage.

Category:

Active buzzer

There is a vibration source inside, it can vibrate and make a sound when it is powered on, and it is easier to drive.

Because it is an internally integrated oscillator circuit, the frequency is fixed.

Passive buzzer

There is no oscillating source inside, and DC cannot be driven, so a square wave signal is used for driving.

The price is cheap and the frequency is controllable. It is necessary to control the tone and loudness through programming, and the driver is a little troublesome.

Buzzer Sound Experiment

Buzzer module circuit diagram

Drive the buzzer to sound

Method 1: Let the microcontroller output high and low levels alternately through the D1 port

Method 2: Use the timer to output PWM signal to control the buzzer

Thinking: What determines the pitch and loudness of a voice?

ARR -> period -> 1/frequency

The larger the period, the smaller the frequency and the lower the tone.

The smaller the period, the higher the frequency and the higher the pitch.

CCR -> Pulse – Duty Cycle

The higher the duty cycle, the longer the power-on time, and the louder it will be.

The lower the duty cycle, the shorter the power-on time and the less loudness.

[4] ADC

1. Definition:

An analog-to-digital converter, or A/D converter, or ADC for short, usually refers to an electronic component that converts an analog signal into a digital signal.

Analog signals: voltage, temperature, light, pressure…. (sensors can convert non-electrical quantities into electrical quantities)

The most intuitive embodiment is that the analog signal is a continuously changing curve, while the digital quantity is a discontinuous discrete point.

2. Introduction to ADC

The 12-bit ADC is a successive approximation analog-to-digital converter.

It has up to 19 channels and can measure 16 external channels (16 channel analog input connected from external GPIO port)

3 internal signal sources, namely internal temperature sensing (VSENSE) input, internal reference voltage (VREFINT) input, external battery VBAT supply pin input

A/D conversion for each channel can be performed in one-shot, continuous, sweep or intermittent mode.

The ADC result can be stored in the 16-bit data registers in left-justified or right-justified fashion.

3.ADC Features

Range: Measurable voltage range 0 ~ 3.6V

Resolution: The resolution of the ADC is usually represented by the number of digits of the output binary number. The more digits, the higher the resolution. Generally speaking, the higher the resolution, the longer the conversion time.

Configurable conversion precision: 6-bit, 8-bit, 10-bit, 12-bit

Conversion time: The time required for the analog input voltage to obtain a stable digital output from the start of the conversion within the maximum allowable variation range is called the conversion time

4.ADC clock

SYSCLK system clock

HSI 16Mhz high speed internal clock

PLL phase-locked loop frequency multiplier

5. Working Mode

EOC: channel conversion end signal

EOS: end of sequence conversion signal

Single conversion mode: ADC performs only one conversion;

Continuous conversion mode: start a new conversion immediately after the conversion is completed;

Tips:

ADC single-shot mode and continuous mode. The concepts of these two modes are corresponding. The one-shot mode here does not refer to a channel. Suppose you open the four channels ch0, ch1, ch4, and ch5 at the same time. In single mode conversion mode, these four channels will be collected once and then stopped. The continuous mode is that the four channels are converted and then cycled to start from ch0.

Scan Mode: The ADC scans all channels selected, performing a single conversion on each channel of each group. At the end of each conversion, the next channel in this group is automatically converted. If the CONT bit is set (continuous conversion mode is on), the conversion does not stop on the last channel of the selected group, but continues from the first channel of the selected group again.

Intermittent mode: trigger once, convert a channel, trigger, convert. Cycle through the selected conversion channel, start a new round of conversion by the trigger signal, until the conversion is completed.

ADC single channel:

Single conversion: only one ADC conversion is performed: configured as “single conversion mode”, scan mode is off. After the ADC channel converts once, it stops converting. It will re-convert after waiting to be enabled again

Continuous conversion: Perform continuous ADC conversion: configure as “continuous conversion mode”, scan mode is off. After the ADC channel converts once, the next conversion is performed continuously.

ADC multi-channel:

Single conversion: only one ADC conversion: configured as “single conversion mode”, scan mode enabled. After multiple channels of the ADC are converted once in the order of configuration, the conversion stops. It will re-convert after waiting to be enabled again

Continuous conversion: Perform continuous ADC conversion: configure as “continuous conversion mode”, scan mode enabled. The multiple channels of the ADC are converted once in sequence according to the order of configuration, and then the next conversion is performed continuously.

6. Single channel single conversion experiment

Collect light/flame/combustible gas sensor values

Open the corresponding channel of ADC

enable serial port

programming

1. Start the ADC

HAL_StatusTypeDef HAL_ADC_Sstart (ADC_HandleTypeDef * hadc)

Function: start ADC to start conversion

Parameter: ADC_HandleTypeDef * hadc handle

Return value: Status

2. Wait for the conversion to end

HAL_StatusTypeDef HAL_ADC_PollForConversion (ADC_HandleTypeDef * hadc, uint32_t Timeout)

Function: Wait for conversion to complete

Parameters: ADC_HandleTypeDef * hadc handle

uint32_t Timeout timeout time

Good return value: transition status

3. Get the conversion result

uint32_t HAL_ADC_GetValue (ADC_HandleTypeDef * hadc)

Function: get conversion result

Parameter: ADC_HandleTypeDef * hadc handle

Return value: conversion result

4. Stop ADC

HAL_StatusTypeDef HAL_ADC_Stop (ADC_HandleTypeDef * hadc)

Function: Stop ADC

Parameter: ADC_HandleTypeDef * hadc handle

Return value: Status

5. Serial port output conversion result

Redirect printf to the serial port, and then output the conversion result through printf

Exercise: Realize light control lights.

When the light intensity is high, the three LED lights are off, and when the light intensity is low, the three LED lights are on.

syntaxbug.com © 2021 All Rights Reserved.