|
我的pwm配置程序如下
u16 time_cnt = 3600;
/*
* º¯ÊýÃû£ºTIM2_GPIO_Config
* ÃèÊö £ºÅäÖÃTIM2¸´ÓÃÊä³öPWMʱÓõ½µÄI/O
* ÊäÈë £ºÎÞ
* Êä³ö £ºÎÞ
* µ÷Óà £ºÄÚ²¿µ÷ÓÃ
*/
static void TIM2_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM2 clock enable */
//PCLK1¾¹ý2±¶Æµºó×÷ΪTIM3µÄʱÖÓÔ´µÈÓÚ72MHz
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
// NVIC_InitTypeDef NVIC_InitStructure;
//ÖжÏÓÅÏȼ¶NVICÉèÖÃ
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM3ÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÏÈÕ¼ÓÅÏȼ¶0¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //´ÓÓÅÏȼ¶0¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀ±»Ê¹ÄÜ
NVIC_Init(&NVIC_InitStructure); //³õʼ»¯NVIC¼Ä´æÆ÷
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
//ÖжÏÓÅÏȼ¶NVICÉèÖÃ
/*GPIOA Configuration: TIM2 channel 1 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
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_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_2;
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_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*
* º¯ÊýÃû£ºTIM2_Mode_Config
* ÃèÊö £ºÅäÖÃTIM2Êä³öµÄPWMÐźŵÄģʽ£¬ÈçÖÜÆÚ¡¢¼«ÐÔ¡¢Õ¼¿Õ±È
* ÊäÈë £ºÎÞ
* Êä³ö £ºÎÞ
* µ÷Óà £ºÄÚ²¿µ÷ÓÃ
*/
static void TIM2_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWMÐźŵçƽÌø±äÖµ */
u16 CCR_Val = 899;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period =time_cnt-1; //Êý72´ÎΪ1us£¬ÖÜÆÚΪ25us£¬¾ÍÊÇ40£Ë£È£ú
TIM_TimeBaseStructure.TIM_Prescaler = 0; //ÉèÖÃÔ¤·ÖƵ£º²»·ÖƵ£¬¼´Îª72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //ÉèÖÃʱÖÓ·ÖƵϵÊý£º²»·ÖƵ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //ʹÄÜÖ¸¶¨µÄTIM3ÖжÏ,ÔÊÐí¸üÐÂÖжÏ
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ÅäÖÃΪPWMģʽ1
TIM_OCInitStructure.TIM_Pulse = CCR_Val; //ÉèÖÃÌø±äÖµ£¬µ±¼ÆÊýÆ÷¼ÆÊýµ½Õâ¸öֵʱ£¬µçƽ·¢ÉúÌø±ä
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //µ±¶¨Ê±Æ÷¼ÆÊýֵСÓÚCCR1_ValʱΪµÍµçƽ
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;// ¿ÕÏÐ״̬ÏµĹ¤×÷״̬
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC2Init(TIM2, &TIM_OCInitStructure); //ʹÄÜͨµÀ2
TIM_OC1Init(TIM2, &TIM_OCInitStructure); //ʹÄÜͨµÀ1
TIM_OC3Init(TIM2, &TIM_OCInitStructure); //ʹÄÜͨµÀ3
TIM_OC4Init(TIM2, &TIM_OCInitStructure); //ʹÄÜͨµÀ4
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE); //ʹÄܶ¨Ê±Æ÷2
}
void TIM2_IRQHandler(void) //TIM2ÖжÏ
{
static s16 Ta,Tb,Tc,Td;
float angle = 0;
// Ta = 0;
// Tb = 0;
// Tc = 0;
// Td = 0;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //¼ì²éTIM3¸üÐÂÖжϷ¢ÉúÓë·ñ
{
// TIM_SetCompare1(TIM2,SinDataA[Counter_sine]/pa);
// TIM_SetCompare2(TIM2,SinDataB[Counter_sine]/pa);
// TIM_SetCompare3(TIM2,SinDataC[Counter_sine]/pa);
// TIM_SetCompare4(TIM2,SinDataD[Counter_sine]/pa);
// Counter_sine-=1;
// Counter_sine = (Counter_sine+1024) % 1024;
stepCounter++;
if((stepCounter%(8 * 2300))==0)
{
stepCounter++;
LED_FLASH(1);
}
TIM_SetCompare1(TIM2,Ta);
TIM_SetCompare2(TIM2,Tb);
TIM_SetCompare3(TIM2,Tc);
TIM_SetCompare4(TIM2,Td);
angle = 2.0 * PI * Counter_sine/ 1024;
t1 = time_cnt * sin(angle)+0.5;
t2 = time_cnt * sin(angle + PI * 0.5)+0.5;
if(t1>=0)
{
Ta = t1;
Tb = 0;
}
else
{
Tb = abs(t1);
Ta = 0;
}
if(t2>=0)
{
Tc = t2;
Td = 0;
}
else
{
Td = abs(t2);
Tc = 0;
}
Counter_sine+=step;
Counter_sine = Counter_sine % 1024;
TIM2->SR = (uint16_t)~TIM_IT_Update; //Çå³ýTIM2¸üÐÂÖжϱêÖ¾
}
/*ΪÁ˲úÉúÒ»¸öÂö³å,¼ìÑéÊÇ·ñ½øÈëÖжÏ*/
}
|
|