Article directory
Timing comparison with 51 microcontroller
Delay of 51 microcontroller (software timing):
//由stc软件生成的51延时代码
void Delay500ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 4;
j = 129;
k = 119;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
Disadvantages: imprecise, occupying CPU resources
Working principle of stm32 timer:
Use an accurate time base to implement timing functions through hardware. The core of the timer is the counter.
Timer classification:
- Basic timer (TIM6~TIM7)
- General timer (TIM2~TIM5)
- Advanced timers (TIM1 and TIM8)
STM32F103C8T6 timer resources:
General timer introduction:
- 16-bit up, down, up/down autoload counter (TIMx_CNT).
16-bit programmable (can be modified in real time) prescaler (TIMx_PSC), the division coefficient of the counter clock frequency is any value between 1 and 65535 .- 4 independent channels (TIMx_CH1~4), these channels can be used as:
A. Input captureB
. Output comparison
C.PWM generation (edge or center aligned mode)
D. Single pulse mode output- The synchronization circuit of timers and timer interconnects (one timer can be used to control another timer) can be controlled using external signals (TIMx_ETR)
.- Interrupt/DMA is generated when the following events occur:
A. Update: counter overflows/underflows, counter initialization (via software or internal/external trigger)
B. Trigger events (counter start, stop, initialization or counting triggered by internal/external)
C. Input captureD
. Output comparison
E. Supports incremental (quadrature) encoder and Hall sensor circuits for positioning
F. Trigger input as external clock or cycle-by-cycle current management
Timer counting mode:
Timer clock source:
The timer overflow time calculation formula is:
(PSC is the frequency division coefficient) (ARR is the reload value) (Tout is the output time (required)) (Tclk is the clock frequency)
Known: Tclk = 72MHZ.
For example, if you want to time 1000ms, then: PSC=7199, ARR=9999 ,Tclk=72M
#############################################################################
PWM resources
- Advanced timer (TIM1): 7 channels
- General timer (TIM2~TIM4): 4 channels each
PWM output mode:
-
PWM mode 1: When counting up, once CNT < CCRx, the output is a valid level, otherwise it is an invalid level; when counting down, once
CNT > CCRx, the output is an invalid level, otherwise it is a valid level. -
PWM mode 2: When counting up, the output is an invalid level once CNT < CCRx, otherwise it is a valid level; when counting down, the
output is a valid level once CNT > CCRx, otherwise it is an invalid level.
PWM duty cycle: determined by the TIMx_CCRx register.
PWM configuration (Cube Max)
//启动tim2
HAL_TIM_Base_Start_IT(&htim2);
//配置占空比
//参数:句柄,通道,值
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, pwmVal);
PWM breathing light experiment
int main()
{
// 定义变量
uint16_t pwmVal=0; //调整PWM占空比
uint8_t flag=1; //设置改变方向。1:占空比越来越大;0:占空比越来越小
// 使能 Timer4 第3通道 PWM 输出
HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_3);
// while循环实现呼吸灯效果
while (1)
{
HAL_Delay(1);
if (flag)
pwmVal++;
else
pwmVal--;
if (pwmVal > 500)
flag = 0;
if (pwmVal == 0)
flag = 1;
//修改比较值,修改占空比
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, pwmVal);
}
}
Finish
If you have any questions, please feel free to raise them and make progress together.