stm32C8t6 通用定时器输出PWM+定时中断控制板载LED

stm32f103c8t6只有基本定时器和通用定时器,没有高级定时器

通过定时器3使能中断1s一次控制板载LED 基本步骤 *开启定时器时钟 *定时器初始化结构体配置 *定时器中断开启 *定时器使能

void led_init(void)
{
          
   
  GPIO_InitTypeDef   GPIO_InitStruct;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOC,&GPIO_InitStruct);

}
void tim_init(void)
{
          
   
  TIM_TimeBaseInitTypeDef  TIM_InitStruct;
	NVIC_InitTypeDef   NVIC_InitStruct;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	TIM_InitStruct.TIM_Prescaler = 7200 - 1 ;  //预分频      72M/7200 = 10000 
	TIM_InitStruct.TIM_Period =  10000 - 1;   //自动重装载值  10000 / 10000 = 1Hz   1/1 = 1s 
	TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_InitStruct.TIM_ClockDivision = 0;
	TIM_TimeBaseInit(TIM3,&TIM_InitStruct);
	TIM_ClearFlag(TIM3, TIM_FLAG_Update);
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
	TIM_Cmd(TIM3,ENABLE);
	
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
}

int num = 0;
void TIM3_IRQHandler(void)   //TIM3中断 1s一次
{
          
    
	if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 
		{
          
   
			num++;
			TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  //清除TIMx的中断待处理位:TIM 中断源 
		}
}

定时器4控制输出pwm 实现led呼吸灯 /* 打开GPIO时钟 PA0 – CH1 打开定时器时钟 初始化定时器 初始化输出比较 */

void TIM5_init(void)
{
          
   
  GPIO_InitTypeDef   GPIO_InitStruct;
	TIM_TimeBaseInitTypeDef  TIM_InitStruct;
	TIM_OCInitTypeDef   TIM_OCInitStruct;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init(GPIOB,&GPIO_InitStruct);
	 
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
	TIM_InitStruct.TIM_Prescaler = 720 - 1 ;  // 预分频 PSC 72M/(720) =  100000  
	TIM_InitStruct.TIM_CounterMode =  TIM_CounterMode_Up;
	TIM_InitStruct.TIM_Period = 100 - 1;  //自动重装载值 ARR 100/100000 =  0.001S  输出频率 = 72M/720/100 = 1KHZ
	TIM_InitStruct.TIM_ClockDivision = 0;
	TIM_TimeBaseInit(TIM4,&TIM_InitStruct);

	
	
	TIM_OCStructInit(&TIM_OCInitStruct);
  TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OCInitStruct.TIM_OutputState =  TIM_OutputState_Enable;
	TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OCInitStruct.TIM_Pulse = 50;  //占空比 CCR  占空比 = CCR/(ARR+1) = 50% ,可以通过TIM_SetCompare2来改变,这里只是一个初始化
	TIM_OC2Init(TIM4,&TIM_OCInitStruct); //输出比较通道2初始化
	TIM_Cmd(TIM4,ENABLE);
//	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
	
}

占空比控制:

for(i = 0;i<100;i++)
{
          
   
TIM_SetCompare2(TIM4,i);  //输出比较通道2 这个函数是改变定时器的CCR寄存器的值,也就是可以改变占空比
	delay_ms(10);
}
for(i = 100;i> 0;i--)
{
          
   
TIM_SetCompare2(TIM4,i);
	delay_ms(10);
}

pwm输出的占空比,频率,分辨率计算

经验分享 程序员 微信小程序 职场和发展