使用正点原子的串口初始程序,然后tim8在pwm2模式下运行,无法输出波形,同时tim1,也在工作,正常输出,就是tim8不行,求解?代码如下
u16 TIM1_FLAG,TIM8_FLAG;
void stepper_motor57or42_init_l(u32 PulseFrequency)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 nPDTemp ;
nPDTemp = 72000UL/PulseFrequency;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1|RCC_APB2Periph_GPIOA, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; /*?¨?±?÷????????*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; /*??????????0*/
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; /*?ì????????1*/
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; /*???í????*/
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
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_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //???ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_BaseInitStructure.TIM_Period = nPDTemp-1;
TIM_BaseInitStructure.TIM_Prescaler = 999;
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0; /*?????¨?±?÷????????*/
TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = nPDTemp>>1;//50%
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM1, DISABLE); //????TIMx???è
TIM_CtrlPWMOutputs(TIM1,DISABLE);
}
///////////////////////////////////////////////////////////////////////////////////
void stepper_motor57or42_init_r(u32 PulseFrequency)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 nPDTemp ;
nPDTemp = 72000UL/PulseFrequency;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8|RCC_APB2Periph_GPIOC, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_IRQn; /*?¨?±?÷????????*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; /*??????????0*/
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; /*?ì????????1*/
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; /*???í????*/
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //???ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //???ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
TIM_BaseInitStructure.TIM_Period = nPDTemp-1;
TIM_BaseInitStructure.TIM_Prescaler = 999;
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0; /*?????¨?±?÷????????*/
TIM_TimeBaseInit(TIM8, &TIM_BaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = nPDTemp>>1;//50%
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
TIM_ClearFlag(TIM8, TIM_FLAG_Update);
TIM_ITConfig(TIM8, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM8, DISABLE); //????TIMx???è
TIM_CtrlPWMOutputs(TIM8,DISABLE);
}
/////////////////////////////////////////////////////////
void TIM1_UP_IRQHandler(void)
{
if(TIM1_FLAG>0)
{
TIM1->RCR=255;
TIM1_FLAG--;
}
else if(TIM1_FLAG==0)
{
TIM_Cmd(TIM1, DISABLE); //????TIMx???è
TIM_CtrlPWMOutputs(TIM1,DISABLE);
}
TIM_ClearITPendingBit(TIM1, TIM_FLAG_Update);
}
//////////////////////////////////////////////////////////////
void TIM8_UP_IRQHandler(void)
{
if(TIM8_FLAG>0)
{
TIM8->RCR=255;
TIM8_FLAG--;
}
else if(TIM1_FLAG==0)
{
TIM_Cmd(TIM8, DISABLE);
TIM_CtrlPWMOutputs(TIM8,DISABLE);
}
TIM_ClearITPendingBit(TIM8, TIM_FLAG_Update);
}
//////////////////////////////////////////////////////////
void revolution_l(u16 angle,u8 direction)
{
u8 bb;
while(TIM1_FLAG);
TIM1_FLAG=angle/256;
bb=(angle%256)-1;
cw_l=direction;
TIM1->RCR=bb;
TIM_Cmd(TIM1, ENABLE); //????TIMx???è
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
void revolution_r(u16 angle,u8 direction)
{
u8 bb;
while(TIM8_FLAG);
TIM8_FLAG=angle/256;
bb=(angle%256)-1;
cw_r=direction;
TIM8->RCR=bb;
TIM_Cmd(TIM8, ENABLE);
TIM_CtrlPWMOutputs(TIM8,ENABLE);
}
int angle(float angle)
{
float pulse=(float)angle*subsection/1.8;
if((pulse-(int)pulse)>=0.5)
{
pulse+=1;
return pulse;
}
else
{
return pulse;
}
}
///////////////////////////主函数/////////////////////////////////////////////
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
//#include "lcd.h"
#include "57or42.h"
int main(void)
{
SystemInit();
uart_init(9600);
//LED_Init();
//LCD_Init();
//LCD_Clear(BLACK);
//POINT_COLOR=WHITE;
//BACK_COLOR=BLACK;
stepper_motor57or42_init_l(1000);
stepper_motor57or42_init_r(1000);
while(1)
{
revolution_r(angle(50),forward);
revolution_l(angle(60),forward);
delay_ms(2500);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
我遇到过uart1和tim1的初始化顺序调换 才能正常使用tim1 的情况
一周热门 更多>