初中生
最后登录1970-1-1
在线时间 小时
注册时间2015-8-12
|
预期功能:用TIM3产生PWM,并且通过串口发送数据实现PWM可调,
问题:调用了库函数TIM_SetCompare1(TIM3,CCR1_Val);后就不能对PWM进行初始化了,芯片一上电相应的PWM通道直接被拉高,初始化语句不起作用,但是通过串口发数据能调节,就是初始化无法完成,下面是代码:
//001
#include "pwm_output.h"
uint16_t CCR1_Val=500;//对PWM占空比进行初始化
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¶t±¶Æμoó×÷ÎaTIM3μÄê±ÖóÔ′μèóú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; // ¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); //½«½á11ìåμÄ3õÖμ¸3¸øÏàó|μļÄ′æÆ÷
/*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; // ¸′óÃíÆíìêä3ö
// GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;//ê±»ù3õê¼»ˉóé¸Ã½á11ìåμļ¸¸ö3éÔ±ÅäÖÃ
TIM_OCInitTypeDef TIM_OCInitStructure; //í¨óö¨ê±Æ÷μÄêä3öÄ£ê½óé¸Ã½á11ìåμļ¸¸ö3éÔ±ÅäÖÃ
/*time base configuration*/
TIM_TimeBaseInitStructure.TIM_Period=999; //¶¨ê±Æ÷′ó0μ½999Îaò»¸ö¶¨ê±ÖüÆú£¨¼′1000£©
TIM_TimeBaseInitStructure.TIM_Prescaler=0; //éèÖÃÔ¤·ÖÆμÆ÷£¬2»Ô¤·ÖÆ죬¼′Îa72MHz
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; //éèÖÃê±Öó·ÖÆμÏμêy£¬2»·ÖÆμ
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
/*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; //ÅäÖÃÎaPWM1Ä£ê½
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //ê1Äü
TIM_OCInitStructure.TIM_Pulse=CCR1_Val;//éèÖÃìø±äÖμ£¬μ±¼ÆêyÆ÷¼Æêyμ½Õa¸öÖμê±£¬μçƽ·¢éúìø±ä
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //챶¨ê±Æ÷μÄÖμD¡óúCCR1_Valê±Îa¸ßμçƽ
TIM_OC1Init(TIM3,&TIM_OCInitStructure);//ê1Äüí¨μà1
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);//ê1ÄüԤװÔؼÄ′æÆ÷(CCR1)
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR1_Val; //éèÖÃí¨μà2μÄìø±äÖ죬2úéúáíò»¸öÕ¼¿Õ±èμÄWM
TIM_OC2Init(TIM3,&TIM_OCInitStructure);//ê1Äüí¨μà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);//ê1Äüí¨μà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);//ê1Äüí¨μà4
TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3,ENABLE); //ê1ÄüÖØÔؼÄ′æÆ÷ARR
TIM_Cmd(TIM3,ENABLE); //ê1Äü¶¨ê±Æ÷3
}
/*
*oˉêyÃû£oTIM3_Config
*1|Äü:TIM3 êä3öWMDÅoÅ3õê¼»ˉ£¬Ö»òaμ÷óÃÕa¸öoˉêy
* TIM3μÄËĸöí¨μà¾í»áóDPWMDÅoÅêä3ö
*êäèë: ÎT
*êä3ö£oÎT
*μ÷óãoía2¿μ÷óÃ
*/
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);//¶áêy¾Yoóó2¼t×Ô¶ˉÇå3y±ê־λ
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]); //Õ¼¿Õ±è(3μËù)μ÷½ú
RxCounter=0;//
}
}
}
求大神指导,纠结好几天了
|
|