初级会员
- 积分
- 75
- 金钱
- 75
- 注册时间
- 2018-4-2
- 在线时间
- 8 小时
|
5金钱
小弟的毕设是用STM32F030做一个小四轴,这两天刚把硬件做完,准备调试程序,可是却碰到了一个怪问题,就是不接电机的时候,可以通过遥控器去控制四路PWM的占空比大小,可是一接上电机,电机转起来后,2.4G就不通信了,2.4G芯片我用的是NRF24L01,稳压芯片用的是AMS1117。求各路大神帮我分析下原因,万分感谢。
我先说一下,我的几个思路,给各位抛砖引玉。
硬件问题 1、一开始,我怀疑是电池电压不足,就换了个充满电的电池,电池电压达4.2V,可是问题依旧。
2、怕电池还是不能说明问题,我就找来了直流源,直接上5V,最大电流给到2A,当电机工作时,我去测各模块的电压,都能够维持在3.3V左右。甚至,我直接用电流源输出3.3V到2.4模块两端,可是问题还是存在。
3、布线可能有问题,这个不好验证,我准备一会儿用开发板连短线试一下。这个我上传图片,请大家帮我看一下,右侧双四脚直插封装是2.4G。
软件问题 硬件暂时没有找到问题,我就去想程序或许有问题。但是,在不接电机的时候,我通过示波器可以看到,遥控器的确是可以控制PWM的输出占空比的。而且在网上查,好多人说,把2.4G的自动应答关掉,我也关掉了,可是并没有起作用。
定时器配置代码,TIM3用来做定时,TIM1产生四路PWM
#include "time.h"
__IO uint16_t CCR3_Val = 60000;//定时10ms
__IO uint16_t CCR4_Val = 6000; //定时1ms
__IO uint16_t temp1=0;
__IO uint16_t temp2=0;
uint16_t PrescalerValue = 0;
uint16_t TimerPeriod = 0;
uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;
extern uint16_t motor_out1,motor_out2,motor_out3,motor_out4;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
void TIM_INT_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM_OUT_Config(void)
{
/* 计算预分频值 */
PrescalerValue = (uint16_t) (SystemCoreClock / 6000000) - 1;
/* Time 定时器基础设置 */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* 预分频器配置 */
TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);
/* 输出比较时序模式配置设置 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* 输出比较时序模式配置: 频道3*/
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* 输出比较时序模式配置: 频道4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* TIM 中断使能 */
TIM_ITConfig(TIM3, TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
/* TIM3 使能 */
TIM_Cmd(TIM3, ENABLE);
}
void TIM_PWM_Config(void)
{
TimerPeriod = (SystemCoreClock / 5000 ) - 1; //20kHz
Channel1Pulse = (uint16_t) (((uint32_t) motor_out1 * (TimerPeriod - 1)) / 200);//50%占空比
Channel2Pulse = (uint16_t) (((uint32_t) motor_out2 * (TimerPeriod - 1)) / 200);
Channel3Pulse = (uint16_t) (((uint32_t) motor_out3 * (TimerPeriod - 1)) / 200);
Channel4Pulse = (uint16_t) (((uint32_t) motor_out4 * (TimerPeriod- 1)) / 200);
/* TIM1 时钟使能 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
/* Time 定时基础设置*/
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; /* Time 定时设置为上升沿计算模式*/
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* 频道1,2,3,4的PWM 模式设置 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;//使能频道1配置
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;//使能频道2配置
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;//使能频道3配置
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;//使能频道4配置
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
/* TIM1 计算器使能*/
TIM_Cmd(TIM1, ENABLE);
/* TIM1 主输出使能 */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
void TIM_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能GPIO时钟 */
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
/* 配置GPIO管脚复用*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
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_DOWN ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_2);
}
主函数代码,不带姿态反馈
void Init(void)
{
SystemInit();
Systick_Init();
LED_Init();
KEY_Init();
TIM_Config();
TIM_INT_Config();
TIM_OUT_Config();
USART_Configuration();
MPU_Init();
sensorsBiasInit(&gyroBias);
NRF24L01_Init();
}
/**********************************************/
/* 函数功能;主函数 */
/* 入口参数:无 */
/**********************************************/
int main(void)
{
Init();
Delay_ms(1000);
while(1)
{
while(NRF24L01_Check())
{printf("NRF2401 ERROR\n");}
NRF24L01_RX_Mode();
while(1)
{
NRF24L01_RxPacket(temp_buf);
Start_Word=temp_buf[0];
j=countof(temp_buf)-1;
for(i=1;i<=j;i++)
{
At_expect_buf=temp_buf-51;
printf("%d: %d ",i,At_expect_buf);
}
printf("\n");
motor[0]=At_expect_buf[0] + 70 - At_expect_buf[1] + At_expect_buf[2] - At_expect_buf[3];
motor[1]=At_expect_buf[0] + 70 - At_expect_buf[1] - At_expect_buf[2] + At_expect_buf[3];
motor[2]=At_expect_buf[0] + 70 + At_expect_buf[1] + At_expect_buf[2] + At_expect_buf[3];
motor[3]=At_expect_buf[0] + 70 + At_expect_buf[1] - At_expect_buf[2] - At_expect_buf[3];
motor_out1=motor[0];
motor_out2=motor[1];
motor_out3=motor[2];
motor_out4=motor[3];
if(Start_Word==10){TIM_PWM_Config();} //通过遥控器控制电机启动
|
|