完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
用开发板提供的PWM例程修改的tiM3 PWM无输出,TIM1就可以
/********************************** (C) COPYRIGHT ******************************* * File Name : main.c * Author : WCH * Version : V1.0.0 * Date : 2020/04/30 * Description : Main program body. *******************************************************************************/ /* *@Note PWM输出例程: TIM1_CH1(PA8) 本例程演示 TIM_CH1(PA8) 引脚在 PWM模式1和PWM模式2 下,输出 PWM。 */ #include "debug.h" /* PWM Output Mode Definition */ #define PWM_MODE1 0 #define PWM_MODE2 1 /* PWM Output Mode Selection */ //#define PWM_MODE PWM_MODE1 #define PWM_MODE PWM_MODE2 /******************************************************************************* * Function Name : TIM1_PWMOut_Init * Description : Initializes TIM1 PWM output. * Input : arr: the period value. * psc: the prescaler value. * ccp: the pulse value. * Return : None *******************************************************************************/ void TIM1_PWMOut_Init( u16 arr, u16 psc, u16 ccp ) { GPIO_InitTypeDef GPIO_InitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_TIM1, ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOA, &GPIO_InitStructure ); TIM_TimeBaseInitStructure.TIM_Period = arr; TIM_TimeBaseInitStructure.TIM_Prescaler = psc; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure); #if (PWM_MODE == PWM_MODE1) TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; #elif (PWM_MODE == PWM_MODE2) TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; #endif TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = ccp; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init( TIM1, &TIM_OCInitStructure ); TIM_OC4Init(TIM1,&TIM_OCInitStructure); TIM_CtrlPWMOutputs(TIM1, ENABLE ); TIM_OC1PreloadConfig( TIM1, TIM_OCPreload_Enable ); TIM_OC4PreloadConfig( TIM1, TIM_OCPreload_Enable ); TIM_ARRPreloadConfig( TIM1, ENABLE ); TIM_Cmd( TIM1, ENABLE ); } void TIM3_PWMOut_Init( u16 arr, u16 psc, u16 ccp ) { GPIO_InitTypeDef GPIO_InitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_InternalClockConfig(TIM3); //选择时钟为内部时钟 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB1Periph_TIM3, ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOA, &GPIO_InitStructure ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; GPIO_Init( GPIOB, &GPIO_InitStructure ); TIM_TimeBaseInitStructure.TIM_Period = arr; TIM_TimeBaseInitStructure.TIM_Prescaler = psc; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit( TIM3, &TIM_TimeBaseInitStructure); #if (PWM_MODE == PWM_MODE1) TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; #elif (PWM_MODE == PWM_MODE2) TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; #endif TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = ccp; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init( TIM3, &TIM_OCInitStructure ); TIM_OC2Init(TIM3,&TIM_OCInitStructure); TIM_OC3Init(TIM3,&TIM_OCInitStructure); TIM_OC4Init(TIM3,&TIM_OCInitStructure); TIM_CtrlPWMOutputs(TIM3, ENABLE ); TIM_OC1PreloadConfig( TIM3, TIM_OCPreload_Enable ); TIM_OC2PreloadConfig( TIM3, TIM_OCPreload_Enable ); TIM_OC3PreloadConfig( TIM3, TIM_OCPreload_Enable ); TIM_OC4PreloadConfig( TIM3, TIM_OCPreload_Enable ); //TIM_ARRPreloadConfig( TIM3, ENABLE ); TIM_Cmd( TIM3, ENABLE ); } /******************************************************************************* * Function Name : main * Description : Main program. * Input : None * Return : None *******************************************************************************/ int main(void) { u16 pwmval=0; u8 a=1; Delay_Init(); USART_Printf_Init(115200); printf("SystemClk:%drn",SystemCoreClock); TIM1_PWMOut_Init( 899, 0, 1000 ); TIM3_PWMOut_Init( 899, 0, 500 ); while(1) { Delay_Ms(1); if(a) pwmval++; else pwmval--; if(pwmval >500) a =0; if(pwmval==0) a =1; TIM_SetCompare1(TIM1, pwmval); TIM_SetCompare4(TIM1, pwmval); TIM_SetCompare1(TIM3, pwmval); TIM_SetCompare2(TIM3, pwmval); TIM_SetCompare3(TIM3, pwmval); TIM_SetCompare4(TIM3, pwmval); } } |
|
相关推荐
1个回答
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
463 浏览 1 评论
CH579M+RT-Thread,RTC从Sleep模式唤醒失败是什么原因?
2868 浏览 2 评论
2357 浏览 1 评论
810浏览 2评论
CH569通过HSPI实现USB3.0和FPGA高速双向通讯
633浏览 1评论
494浏览 1评论
CH32F103C8T6使用当前官网上的CDC例程会出现设备描述符请求失败
358浏览 1评论
632浏览 1评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 13:44 , Processed in 0.963689 second(s), Total 76, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号