|
这个程序貌似智能控制左电机的方向和速度,控制不了右电机的方向和速度!各位大神帮忙查找下是什么原因啊
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#include "platform_config.h"
#include "stm32f10x_tim.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ErrorStatus HSEStartUpStatus;
u8 count=0;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 TimerPeriod = 0;
u16 Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void Delay(vu32 nCount);
/* Private functions ---------------------------------------------------------*/
void CarSpeed(u32 frequency,u32 ch1,u32 ch2,u32 ch3,u32 ch4)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM1, GPIOA, GPIOB, GPIOE and AFIO clocks enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOE|
RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE);
/* GPIOA Configuration: Channel 1, 2 and 3 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* GPIOB Configuration: Channel 1N, 2N and 3N as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Compute the value to be set in ARR regiter to generate signal frequency at 17.57 Khz */
TimerPeriod = (72000000 / frequency ) - 1;
/* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */
Channel1Pulse = (u16) (((u32) ch1 * (TimerPeriod - 1)) / 1000);
/* Compute CCR2 value to generate a duty cycle at 37.5% for channel 2 and 2N */
Channel2Pulse = (u16) (((u32) ch2 * (TimerPeriod - 1)) / 1000);
/* Compute CCR3 value to generate a duty cycle at 25% for channel 3 and 3N */
Channel3Pulse = (u16) (((u32) ch3 * (TimerPeriod - 1)) / 1000);
/* Compute CCR4 value to generate a duty cycle at 12.5% for channel 4 */
Channel4Pulse = (u16) (((u32) ch4 * (TimerPeriod- 1)) / 1000);
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
void CarDirect(u8 LeftDirect,u8 RightDirect)
{
GPIO_InitTypeDef GPIO_InitStructure;
//配置时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
//配置引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
if(LeftDirect==1) GPIO_SetBits(GPIOF, GPIO_Pin_0);
else if(LeftDirect==0) GPIO_ResetBits(GPIOF, GPIO_Pin_0);
if(RightDirect==1) GPIO_SetBits(GPIOF, GPIO_Pin_1);
else if(RightDirect==0) GPIO_ResetBits(GPIOF, GPIO_Pin_1);
}
void CarStop(u8 LeftStop,u8 RightStop)
{
GPIO_InitTypeDef GPIO_InitStructure;
//配置时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
//配置引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
//配置时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//配置引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |GPIO_Pin_9 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
if(LeftStop==1)
{
GPIO_SetBits(GPIOF, GPIO_Pin_0);
GPIO_SetBits(GPIOA, GPIO_Pin_8);
}
if(RightStop==1)
{
GPIO_SetBits(GPIOF, GPIO_Pin_1);
GPIO_SetBits(GPIOA, GPIO_Pin_9);
}
}
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
u32 T= 0x000AFFFF+0x000AFFFF;
u32 SPEED= 0x000AFFFF;
u32 i;
u32 LuCheng=100;
/* System Clocks Configuration **********************************************/
RCC_Configuration();
/* NVIC Configuration *******************************************************/
NVIC_Configuration();
///////////////////
CarDirect(1,0);
CarSpeed(100,500,500,350,150);
for(i=0;i<100;i++)
Delay(0xAFFFF);
CarStop(1,1);
/////////////////////////////////
CarStop(1,1);
for(i=0;i<100;i++)
Delay(0xAFFFF);
//////////////////////////////////
CarDirect(1,0);
CarSpeed(100,150,500,350,150);
for(i=0;i<100;i++)
Delay(0xAFFFF);
CarStop(1,1);
//////////////////////////
while (1)
{}
}
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|