stm32学习笔记---PWM实现控制呼吸灯

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42471952/article/details/81175687

 

 

 


void tim3_init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;
/***************************¶¨Ê±Æ÷»ù±¾¹¦ÄÜ************************************/	
	//ʹÄÜʱÖÓ
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode   = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period        = 255;   
	TIM_TimeBaseInitStruct.TIM_Prescaler     = 719;
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
/***************************¶¨Ê±Æ÷»ù±¾¹¦ÄÜ************************************/	

/***************************PWM¹¦ÄÜ************************************/
	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
	TIM_OCInitStruct.TIM_Pulse    = 0;
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
	
	//PWMµÄͨµÀ
	TIM_OC1Init(TIM3, &TIM_OCInitStruct);
	TIM_OC2Init(TIM3, &TIM_OCInitStruct);
	TIM_OC3Init(TIM3, &TIM_OCInitStruct);	
	
//void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
//void TIM_OC2PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
//void TIM_OC3PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);	
	
	TIM_ARRPreloadConfig(TIM3, ENABLE);   
/***************************PWM¹¦ÄÜ************************************/	

	//¶¨Ê±Æ÷ʹÄÜ
	TIM_Cmd(TIM3,ENABLE);
}


 

int main(void)
{
	rcc_init();
	systick_init();
	led_init();
	uart1_init();   //Uart1³õʼ»¯
	uart1_NVIC_init();
	tim2_init();
	tim2_NVIC_init();
	tim3_init();
	

	led_control(LED_GREEN,LED_ON);
	

	while (1)
	{
		breath_led();
	}

}
void breath_led(void)
{
	int i;
	
	for(i = 0; i < 51; i++)
	{
		delay_ms(20);
		TIM3->CCR2 = i * 5;
	}
	
	for(i = 51; i > 0; i--)
	{
		delay_ms(20);
		TIM3->CCR2 = i * 5;
	}
}

 重定义时,需要启用复用时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);


 

展开阅读全文

没有更多推荐了,返回首页