//功能要求:防盜器報警變頻聲音,在定時中斷里修改pwm頻率,如何實現(xiàn)這功能? 現(xiàn)在這程序是報錯的.
#include "debug.h"
u16 led0pwmval=0;
u8 dir=1;
//-----------------------------------
void TIM2_Int_Init(u16 arr, u16 psc)
{
? ? TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
? ? NVIC_InitTypeDef NVIC_InitStructure;
? ? RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能TIM2時鐘
? ? TIM_TimeBaseStructure.TIM_Period = arr;? ?//指定下次更新事件時要加載到活動自動重新加載寄存器中的周期值。
? ? TIM_TimeBaseStructure.TIM_Prescaler = psc; //指定用于劃分TIM時鐘的預(yù)分頻器值。
? ? TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;? ? ?//時鐘分頻因子
? ? TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM計數(shù)模式,向上計數(shù)模式
? ? TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根據(jù)指定的參數(shù)初始化TIMx的時間基數(shù)單位
? ? TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //使能TIM2中斷,允許更新中斷
? ? //初始化TIM NVIC,設(shè)置中斷優(yōu)先級分組
? ? NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;? ? ? ? ? ?//TIM2中斷
? ? NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //設(shè)置搶占優(yōu)先級0
? ? NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;? ? ? ? //設(shè)置響應(yīng)優(yōu)先級1
? ? NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;? ? ? ? ? ?//使能通道1中斷
? ? NVIC_Init(&NVIC_InitStructure); //初始化NVIC
? ? TIM_Cmd(TIM2, ENABLE); //TIM2使能
}
//---------------------------------
void TIM2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));? //80us int
void TIM2_IRQHandler(void)
{
?//? ?static u8 i = 0;
? ? if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //檢查TIM2中斷是否發(fā)生。
? ? {
? ? ? ? TIM_ClearITPendingBit(TIM2, TIM_IT_Update);? ? //清除TIM2的中斷掛起位。
? ? ? ? //80us中斷一次,改變頻參數(shù)
? ? ? ? if(dir)
? ? ? ? ? ? led0pwmval++;
? ? ? ? else
? ? ? ? ? ? led0pwmval--;
? ? ? ? if(led0pwmval>50)
? ? ? ? ? ? dir=0;
? ? ? ? if(led0pwmval<30)
? ? ? ? ? ? dir=1;
? ? ? ??
? ? ? ? //這里不能通過編譯 ???????????????????
? ? ? ? TIM1_PWMOut_Init( 100, led0pwmval, 50 );? //修改pwm頻率
? ? ? ? //??????????????
? ? }
}
/*********************************************************************
?* @fn? ? ? TIM1_OutCompare_Init
?* @brief? ?Initializes TIM1 output compare.
?* @param? ?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={0};
? ? TIM_OCInitTypeDef TIM_OCInitStructure={0};
? ? TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};
? ? RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD | RCC_APB2Periph_TIM1, ENABLE );
? ? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
? ? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
? ? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
? ? GPIO_Init( GPIOD, &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_CtrlPWMOutputs(TIM1, ENABLE );
? ? TIM_OC1PreloadConfig( TIM1, TIM_OCPreload_Disable );
? ? TIM_ARRPreloadConfig( TIM1, ENABLE );
? ? TIM_Cmd( TIM1, ENABLE );
}
/*********************************************************************
?* @fn? ? ? main
?* @brief? ?Main program.
?* @return? none
?*/
int main(void)
{
? ? NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
? ? SystemCoreClockUpdate();
? ? Delay_Init();
? ? TIM2_Int_Init(80,46);? //80us中斷一次
?//? ?u8 dir=1;
?//? ?u16 led0pwmval=0;//在這里運行正常
? ? while(1)
? ? {
? ? ? ? Delay_Ms(10);
? /*
? //在這里運行正常
? ? ? ? if(dir)
? ? ? ? ? ? led0pwmval++;
? ? ? ? else
? ? ? ? ? ? led0pwmval--;
? ? ? ? if(led0pwmval>50)
? ? ? ? ? ? dir=0;
? ? ? ? if(led0pwmval<30)
? ? ? ? ? ? dir=1;
? ? ? ? TIM1_PWMOut_Init( 100, led0pwmval, 50 ); //修改pwm頻率
*/
? ? }
}