//功能要求:防盗器报警变频声音,在定时中断里修改pwm频率,如何实现这功能? 现在这程序是报错的.
#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时钟的预分频器值。
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频因子
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM计数模式,向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //使能TIM2中断,允许更新中断
//初始化TIM NVIC,设置中断优先级分组
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM2中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //设置抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //设置响应优先级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中断是否发生。
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //清除TIM2的中断挂起位。
//80us中断一次,改变频参数
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频率
*/
}
}