初中生
最后登录1970-1-1
在线时间 小时
注册时间2017-11-27
|
我用5V电源给MG996R舵机单独供电,然后STM32F429的定时器输出周期为20ms,正脉宽在0.5ms~2.5ms之间变化的PWM波,作为控制信号直接给舵机,舵机一点反应都没有。是哪里出现了问题呢?https://www.firebbs.cn/forum.php?mod=attachment&aid=MTY5OTZ8YjUyZTgzOGUxMzQwYjU1ZTBlYzc1YzQwNTZkNzgyZTN8MTczMjMzNjU2Nw%3D%3D&request=yes&_f=.rar
#include "stm32f4xx.h"
#include "./tim/bsp_advance_tim.h"
#include "./key/bsp_key.h"
#include "./usart/bsp_usart.h"
extern __IO uint16_t ChannelPulse;
/**
* @brief Ö÷oˉêy
* @param ÎT
* @retval ÎT
*/
int main(void)
{
__IO uint8_t dir=1;
char ch;
Key_GPIO_Config();
TIMx_Configuration();
USARTx_Config();
while(1)
{ if(dir)
ChannelPulse++;
else
ChannelPulse--;
if(ChannelPulse>2500)dir=0;
if(ChannelPulse==500)dir=1;
TIM_SetCompare1(ADVANCE_TIM,ChannelPulse);
}
}
#include "./tim/bsp_advance_tim.h"
__IO uint16_t ChannelPulse = 1500;
/**
* @brief ÅäÖÃTIM¸′óÃêä3öWMê±óÃμ½μÄI/O
* @param ÎT
* @retval ÎT
*/
static void TIMx_GPIO_Config(void)
{
/*¶¨òåò»¸öGPIO_InitTypeDefààDíμĽá11ìå*/
GPIO_InitTypeDef GPIO_InitStructure;
/*¿aÆô¶¨ê±Æ÷Ïà1ØμÄGPIOíaéèê±Öó*/
RCC_AHB1PeriphClockCmd (ADVANCE_OCPWM_GPIO_CLK | ADVANCE_OCNPWM_GPIO_CLK| ADVANCE_BKIN_GPIO_CLK, ENABLE);
/* Ö¸¶¨òy½Å¸′óÃ1|Äü */
GPIO_PinAFConfig(ADVANCE_OCPWM_GPIO_PORT,ADVANCE_OCPWM_PINSOURCE, ADVANCE_OCPWM_AF);
GPIO_PinAFConfig(ADVANCE_OCNPWM_GPIO_PORT,ADVANCE_OCNPWM_PINSOURCE,ADVANCE_OCNPWM_AF);
GPIO_PinAFConfig(ADVANCE_BKIN_GPIO_PORT,ADVANCE_BKIN_PINSOURCE,ADVANCE_BKIN_AF);
/* ¶¨ê±Æ÷1|Äüòy½Å3õê¼»ˉ */
GPIO_InitStructure.GPIO_Pin = ADVANCE_OCPWM_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(ADVANCE_OCPWM_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ADVANCE_OCNPWM_PIN;
GPIO_Init(ADVANCE_OCNPWM_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ADVANCE_BKIN_PIN;
GPIO_Init(ADVANCE_BKIN_GPIO_PORT, &GPIO_InitStructure);
}
/*
* ×¢òa£oTIM_TimeBaseInitTypeDef½á11ìåàïÃæóD5¸ö3éÔ±£¬TIM6oíTIM7μļÄ′æÆ÷àïÃæÖ»óD
* TIM_PrescaleroíTIM_Period£¬ËùòÔê1óÃTIM6oíTIM7μÄê±oòÖ»Dè3õê¼»ˉÕaὸö3éÔ±¼′¿é£¬
* áííaèy¸ö3éÔ±êÇí¨óö¨ê±Æ÷oí¸ß¼¶¶¨ê±Æ÷2ÅóD.
*-----------------------------------------------------------------------------
* TIM_Prescaler ¶¼óD
* TIM_CounterMode TIMx,x[6,7]ûóD£¬ÆäËû¶¼óD£¨»ù±¾¶¨ê±Æ÷£©
* TIM_Period ¶¼óD
* TIM_ClockDivision TIMx,x[6,7]ûóD£¬ÆäËû¶¼óD(»ù±¾¶¨ê±Æ÷)
* TIM_RepetitionCounter TIMx,x[1,8]2ÅóD(¸ß¼¶¶¨ê±Æ÷)
*-----------------------------------------------------------------------------
*/
static void TIM_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
// ¿aÆôTIMx_CLK,x[1,8]
RCC_APB2PeriphClockCmd(ADVANCE_TIM_CLK, ENABLE);
/* àÛ¼Æ TIM_Period¸öoó2úéúò»¸ö¸üD»òÕßÖD¶Ï*/
//챶¨ê±Æ÷′ó0¼Æêyμ½1023£¬¼′Îa1024′Σ¬Îaò»¸ö¶¨ê±ÖüÆú
TIM_TimeBaseStructure.TIM_Period = 19999-1;
// ¸ß¼¶¿ØÖƶ¨ê±Æ÷ê±ÖóÔ′TIMxCLK = HCLK=180MHz
// é趨¶¨ê±Æ÷ÆμÂêÎa=TIMxCLK/(TIM_Prescaler+1)=100000Hz
TIM_TimeBaseStructure.TIM_Prescaler = 180-1;
// 2éÑùê±Öó·ÖÆμ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
// ¼Æêy·½ê½
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
// Öظ′¼ÆêyÆ÷
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
// 3õê¼»ˉ¶¨ê±Æ÷TIMx, x[1,8]
TIM_TimeBaseInit(ADVANCE_TIM, &TIM_TimeBaseStructure);
/*PWMÄ£ê½ÅäÖÃ*/
//ÅäÖÃÎaPWMÄ£ê½1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = ChannelPulse;
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_OCNIdleState_Reset;
//ê1Äüí¨μà1
TIM_OC1Init(ADVANCE_TIM, &TIM_OCInitStructure);
/* ê1Äüí¨μà1ÖØÔØ */
TIM_OC1PreloadConfig(ADVANCE_TIM, TIM_OCPreload_Enable);
/* ×Ô¶ˉêä3öê1Äü£¬¶Ï·¡¢ËàÇøê±¼äoíËø¶¨ÅäÖà */
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM_BDTRInitStructure.TIM_DeadTime = 11;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(ADVANCE_TIM, &TIM_BDTRInitStructure);
// ê1Äü¶¨ê±Æ÷
TIM_Cmd(ADVANCE_TIM, ENABLE);
/* Ö÷¶ˉêä3öê1Äü */
TIM_CtrlPWMOutputs(ADVANCE_TIM, ENABLE);
}
/**
* @brief 3õê¼»ˉ¸ß¼¶¿ØÖƶ¨ê±Æ÷
* @param ÎT
* @retval ÎT
*/
void TIMx_Configuration(void)
{
TIMx_GPIO_Config();
TIM_Mode_Config();
}
/*********************************************END OF FILE**********************/
|
|