大家好,我是一名使用STM32F030K6片子的新人,在用它的PWM输出功能,现在看了书,书上都是提到了怎么输出PWM,我现在想在PWM能否进入中断,在中断中我想找到高电平状态,在高电平状态下做别的工作,我的例子如下,希望大家帮忙分析
static void TIM1PwmInit(FunctionalState flag)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, flag/*ENABLE*/);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, flag/*ENABLE*/); //使能定时器1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);// 第2 功能TIM1
//**************************************************************************
// 定时器1设置: 2分频,24KHz频率,
//**************************************************************************
TIM_TimeBaseStructure.TIM_Period = 65535; // 设置自动重装周期值
TIM_TimeBaseStructure.TIM_Prescaler = 9;//1; //2分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//设置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;// 向上计数
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);//初始化定时1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;//TIM_OCMode_PWM2;//;//PWM2模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//TIM_OCPolarity_High;//;//输出高
TIM_OCInitStructure.TIM_Pulse = 480;//50; // 300-1 30%占空比
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);//
//TIM_OC1PreloadConfig(TIM1, ENABLE);
/* TIM1 enable counter */
TIM_Cmd(TIM1, flag/*ENABLE*/);
//TIM_CtrlPWMOutputs(TIM1,flag/*ENABLE*/);
TIM_ITConfig(TIM1,TIM_IT_CC1,flag/*ENABLE*/);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelCmd = flag;//ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM1_CC_IRQHandler(void)
{
//TIM1_CH1 输出频率2k
if (TIM_GetITStatus(TIM1, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1 );
capture = TIM_GetCapture1(TIM3);
if(cc1_sig)
{
TIM_SetCompare1(TIM1, capture + 480 ); //不知道这里是不是高电平状态下
cc1_sig =0;
}
else
{
TIM_SetCompare1(TIM1, capture + 1920 );
cc1_sig =1;
}
}
} |