【晒心得】STM32F03--TIM定时器
STM32F03一共有7个16位的定时器,其中TIM6是基本定时器,TIM3、TIM14/15/16/17是通用定时器,TIM1是高级定时器。这些定时器使STM32具有定时、频率采样、PWM输出等功能。
一:基本定时器TIM6
基本定时器TIM6随时钟脉冲计数的值超过预定值时,能触发中断或者DMA请求。TIM6的时钟源是TIMx_CLK,时钟源经过PSC预分频器输入至脉冲计数器TIMx_CNT,基本定时器的工作模式是向上计数的。预定值保存在重载寄存器TIMx_APP中。
当脉冲计数器TIMx_CNT的计数值等于重载寄存器TIMx_APP中设定的值时,会产生溢出事件或中断,然后TIMx_CNT的值重新被置为0,重新向上计数。
二、通用定时器
通用定时器除了基本的定时,还有频率采样、PWM输出等功能,还有编码器功能。
下面程序是配置PWM1模式输出:
- #include "stm32f0xx.h"
-
- /*TIM3基本配置*/
- void TIM3_Config()
- {
- uint16_t CCR1_Val = 500;
- uint16_t CCR2_Val = 300;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
- TIM_TimeBaseInitStructure.TIM_Period = 999;//从0~999计数,定时周期为1000次
- TIM_TimeBaseInitStructure.TIM_Prescaler = 0x0000;//不预分频,48mhz
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
-
- /*PWM配置*/
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//使能通道1
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC1Init(TIM3, &TIM_OCInitStructure);
- TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
-
- //TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//使能通道1
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
- //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC2Init(TIM3, &TIM_OCInitStructure);
- TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
-
- //TIM_ARRPreloadConfig(TIM3, ENABLE);
- TIM_Cmd(TIM3, ENABLE);
- }
- void TIM_GPIO_Config()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_1);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- int main(void)
- {
- TIM_GPIO_Config();
- TIM3_Config();
- while(1)
- {
- }
- }
复制代码
--------------------------------华丽的分割线--------------------------------
问题来了,在
KEIL下软件仿真,输入信号
输入PORTA,却提示无效是怎么回事呢?
请问在keil中仿真如何知道仿真信号的名称,如PORTA
是不是有些不支持的呀?
[
本帖最后由 qinkaiabc 于 2013-11-22 14:56 编辑 ]