在线时间1 小时
UID2021763
ST金币0
蝴蝶豆0
注册时间2013-6-28
新手上路
- 最后登录
- 2020-10-31
|
a0a.1 0b0c
TIM2是产生4路的PWM信号,改变TIM_OCInitStructure.TIM_Pulse的值(CCRx)即改变占空比
G_SetAxis本质是对I2C读写的操作(详细见附件L3GD20_driver.*,sourcecode实际是ST的库代码)。
奇怪的是:如果注释掉下面while中的语句1、2、3(即不操作I2C)或用其它延时、加减乘除等语句替换,运行程序,测得TIM2(PA02)输出的PWM波形如附件1_good.jpg,非常的完美。
如果不注释掉下面while中的语句1、2、3(即操作I2C),运行程序,测得TIM2(PA02)输出的PWM波形如附件2_error.jpg,波形用示波器看时总跳动(移动),非常糟糕,
不知道为什么?
请知者支招,多谢!
main()
{
.....
TIM2_Output_PWM_Init();
.....
while(1) /* only for test*/
{
Delay_Nms(20);
G_SetAxis(G_X_ENABLE | G_Y_ENABLE | G_Z_ENABLE); //-----1 I2C 操作
G_SetAxis(G_X_ENABLE | G_Y_ENABLE | G_Z_ENABLE); //-----2
G_SetAxis(G_X_ENABLE | G_Y_ENABLE | G_Z_ENABLE); //-----3
}
}
//-------------------------------4路PWM输出----------------------------------------------
void TIM2_Output_PWM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure ;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
// PA0(T2_CH1)--Motor1; PA1(T2_CH2)--Motor2; PA2(T2_CH3)--Motor3; PA3(T2_CH4)--Motor4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//**************************************************************************
// 定时器2设置: 720分频,频率50Hz,向上计数
//**************************************************************************
TIM_TimeBaseStructure.TIM_Period = 19999; //20ms
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// 主定时器T2为 PWM1 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 100; //default value, no meaning
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);//使能的预装载寄存器
TIM_OCInitStructure.TIM_Pulse = 100; //default value, no meaning
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);//使能的预装载寄存器
TIM_OCInitStructure.TIM_Pulse = 100; //default value, no meaning
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);//使能的预装载寄存器
TIM_OCInitStructure.TIM_Pulse = 100; //default value, no meaning
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);//使能的预装载寄存器
TIM_ARRPreloadConfig(TIM2, ENABLE);
//使能定时器
TIM_Cmd(TIM2, ENABLE);
}
TIM2是产生4路的PWM信号,改变TIM_OCInitStructure.TIM_Pulse的值(CCRx)即改变占空比
G_SetAxis本质是对I2C读写的操作(详细见附件L3GD20_driver.*,sourcecode实际是ST的库代码)。
奇怪的是:如果注释掉下面while中的语句1、2、3(即不操作I2C)或用其它延时、加减乘除等语句替换,运行程序,测得TIM2(PA02)输出的PWM波形如附件1_good.jpg,非常的完美。
如果不注释掉下面while中的语句1、2、3(即操作I2C),运行程序,测得TIM2(PA02)输出的PWM波形如附件2_error.jpg,波形用示波器看时总跳动(移动),非常糟糕,
不知道为什么?
谢谢!
main()
{
.....
TIM2_Output_PWM_Init();
.....
while(1) /* only for test*/
{
G_SetAxis(G_X_ENABLE | G_Y_ENABLE | G_Z_ENABLE); //-----1 I2C 操作
G_SetAxis(G_X_ENABLE | G_Y_ENABLE | G_Z_ENABLE); //-----2
G_SetAxis(G_X_ENABLE | G_Y_ENABLE | G_Z_ENABLE); //-----3
}
}
//-------------------------------4路PWM输出----------------------------------------------
void TIM2_Output_PWM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure ;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
// PA0(T2_CH1)--Motor1; PA1(T2_CH2)--Motor2; PA2(T2_CH3)--Motor3; PA3(T2_CH4)--Motor4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//**************************************************************************
// 定时器2设置: 720分频,频率50Hz,向上计数
//**************************************************************************
TIM_TimeBaseStructure.TIM_Period = 19999; //20ms
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// 主定时器T2为 PWM1 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 100; //default value, no meaning
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);//使能的预装载寄存器
TIM_OCInitStructure.TIM_Pulse = 100; //default value, no meaning
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);//使能的预装载寄存器
TIM_OCInitStructure.TIM_Pulse = 100; //default value, no meaning
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);//使能的预装载寄存器
TIM_OCInitStructure.TIM_Pulse = 100; //default value, no meaning
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);//使能的预装载寄存器
TIM_ARRPreloadConfig(TIM2, ENABLE);
//使能定时器
TIM_Cmd(TIM2, ENABLE);
}
|
-
|