预期功能:用TIM3产生PWM,并且通过串口发送数据实现PWM可调,
问题:调用了库函数TIM_SetCompare1(TIM3,CCR1_Val);后就不能对PWM进行初始化了,芯片一上电相应的PWM通道直接被拉高,初始化语句不起作用,但是通过串口发数据能调节,就是初始化无法完成,下面是代码:
//001
#include "pwm_output.h"
uint16_t CCR1_Val=500;//????????????±???????±???
void TIM3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*GPIOA and GPIOB clock enable*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
/*TIM3 clock enable*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //PLCK1??±????ó×÷??TIM3???±????????72MHz
/*GPIOA Configuration:TIM3 channel 1 and 2 as alternate function push-pull*/
/*TIM3 channel 1--PA6, TIM3 channel 2---PA7*/
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); //???á???????????????à?????????÷
/*GPIOB Configuration :TIM3 channel 3 and 4 as alternate function push-pull*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
// GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; // ???????ì????
// GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;//?±?ù???????????á?????????????±????
TIM_OCInitTypeDef TIM_OCInitStructure; //?¨???¨?±?÷???????????????á?????????????±????
/*time base configuration*/
TIM_TimeBaseInitStructure.TIM_Period=999; //?¨?±?÷??0??999???????¨?±?????¨??1000?©
TIM_TimeBaseInitStructure.TIM_Prescaler=0; //?è???¤·????÷?????¤·?????????72MHz
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; //?è???±??·???????????·???
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //?ò??????????
/*Initializes the TIMx Time Base Unit peripheral according to the specified parameters
* in the TIM_TimeBaseInitStruct.
*/
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; //??????PWM1????
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //????
TIM_OCInitStructure.TIM_Pulse=CCR1_Val;//?è????±??????±?????÷?????????????±??????·??ú??±?
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //?±?¨?±?÷????????CCR1_Val?±????????
TIM_OC1Init(TIM3,&TIM_OCInitStructure);//?????¨??1
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);//?????¤×°???????÷(CCR1)
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR1_Val; //?è???¨??2????±??????ú?ú?í????????±???PWM
TIM_OC2Init(TIM3,&TIM_OCInitStructure);//?????¨??2
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR1_Val;
TIM_OC3Init(TIM3,&TIM_OCInitStructure);//?????¨??3
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
/* PWM1Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR1_Val;
TIM_OC4Init(TIM3,&TIM_OCInitStructure);//?????¨??4
TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3,ENABLE); //?????????????÷ARR
TIM_Cmd(TIM3,ENABLE); //?????¨?±?÷3
}
/*
*????????TIM3_Config
*????:TIM3 ????PWM?????????????????÷??????????
* TIM3???????¨?????á??PWM????????
*????: ??
*????????
*?÷?????????÷??
*/
void TIM3_PWM_Init()
{
TIM3_GPIO_Config();
TIM3_Mode_Config();
}
void ZKB(char a0,char a1,char a2) //Mark-Space Ratio:????±?
{
uint16_t bai,shi,ge;
bai=((uint16_t)a0)-48;
shi=((uint16_t)a1)-48;
ge=((uint16_t)a2)-48;
CCR1_Val=bai*100+shi*10+ge;
TIM_SetCompare1(TIM3,CCR1_Val);//?¨??1
TIM_SetCompare2(TIM3,CCR1_Val);//?¨??2
TIM_SetCompare3(TIM3,CCR1_Val);//?¨??3
TIM_SetCompare4(TIM3,CCR1_Val);//?¨??4
}
//以下是串口中断
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
RxBuffer[RxCounter++] = USART_ReceiveData(USART1);//???????ó????×???????±ê????
if(RxCounter==4)
{
printf("%s\n",RxBuffer);
printf("%c\n",RxBuffer[0]);
printf("%c\n",RxBuffer[1]);
printf("%c\n",RxBuffer[2]);
printf("%c\n",RxBuffer[3]);
ZKB(RxBuffer[0],RxBuffer[1],RxBuffer[2]); //????±?(????)?÷??
RxCounter=0;//
}
}
}
求大神指导,纠结好几天了
|