【Nucleo+BLUENRG心得】+BLUENRG 控制小车详解2
要求:L053输出PWM信号
对PWM使用到的端口初始化
/**
* @brief Configures PA2,PA3,PB10,PB11为电机驱动IO
* @param
* @retval None
*/
void BSP_Motor_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable the GPIO Clock */
__GPIOA_CLK_ENABLE();
__GPIOB_CLK_ENABLE();
/* Configure the GPIOA pin */
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Configure the GPIOB pin */
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET);
}
//PWM初始化
//头文件宏:stm32l0xx_hal_conf.h #define HAL_TIM_MODULE_ENABLED
//硬件初始化:stm32l0xx_hal_msp.c void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
//TIM2 初始化
void init_pwm(void)
{
/* 计算分频系数使 TIM2 计数时钟, SystemCoreClock=32000000HZ */
uwPrescalerValue = (HAL_RCC_GetHCLKFreq()/ 1000) - 1;//31999
/*##-1- 配置 TIM 外设 #######################################*/
/* Initialize TIMx peripheral as follow:
+ Prescaler = (SystemCoreClock)/16000000
+ Period = 1600 (to have an output frequency equal to 10 KHz)
+ ClockDivision = 0
+ Counter direction = Up
*/
TimHandle.Instance = TIM2;
TimHandle.Init.Prescaler= 0;//uwPrescalerValue;
TimHandle.Init.Period = 0xFF;//PERIOD_VALUE;//1600
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- 配置 PWM 通道 #########################################*/
/* 所有通道的通用配置 */
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
// 设置通道3脉宽值
sConfig.Pulse = 0;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
{
// Configuration Error
Error_Handler();
}
// 设置通道4脉宽值
sConfig.Pulse = 0;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4) != HAL_OK)
{
// Configuration Error
Error_Handler();
}
/*##-3- PWM信号生成启动 #######################################*/
// 开始通道3
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
// Starting Error
Error_Handler();
}
// 开始通道4
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_4) != HAL_OK)
{
// Starting Error
Error_Handler();
}
}
//APP接收,L053输出PWM及IO
void Attribute_Modified_CB(tHalUint16 handle, tHalUint8 data_length, tHalUint8 *att_data)
{
uint8 newVal;
uint8 rgbwVal[LIGHTSPROFILE_RGBW_LEN];
uint8 l_out=0,r_out=0;
if (handle == TXCharHandle + 1) //11 UUID=0xFFB1 ATT_CHAR_USER_DESC_UUID=Red 接收状态下 //if(att_data[0] == 0x01) notification_enabled = TRUE;
{
receiveData(att_data, data_length);//接收10字节
}
//判断最后状态字节
//
Remote.key =(re_data[0]<<8)+re_data[1]; //按键值
//
Remote.pwm[0]=re_data[2]; //PWM值
Remote.pwm[1]=re_data[3]; //PWM值
Remote.pwm[2]=re_data[4]; //PWM值
Remote.pwm[3]=re_data[5]; //PWM值
Remote.Acc[0]=re_data[6]; //加速度值
Remote.Acc[1]=re_data[7]; //加速度值
Remote.Acc[2]=re_data[8]; //加速度值
Remote.state=re_data[9]; //状态
if((Remote.state & 0x02)==0x02)//状态bit 1=1?是否有效
{
//------------------------------------------------------------
if((Remote.state & 0x10)==0x10) //开启 左 模拟调整功
{
//动力大小
//
if(Remote.pwm[1]>128)//往前方向
{
Moto[0].pwm=Remote.pwm[1]-128;
Moto[0].pwm=Moto[0].pwm*2;
L_dir=1,R_dir=1;//L,R前进
}
else //往后方向
{
Moto[0].pwm=128 - Remote.pwm[1];
Moto[0].pwm=Moto[0].pwm*2;
L_dir=2,R_dir=2;//L,R后退
}
//左右大小
//
if(Remote.pwm[0]>128)//往右方向,右减dir
{
Moto[0].dir=Remote.pwm[0]-128; //0~128
k = (float)Moto[0].dir / 128;//比例系数
r_out= Moto[0].pwm * k;//动力的比例
//-------------------------------------------------
//右减
pwm_out(Moto[0].pwm,(Moto[0].pwm - (unsigned char)r_out)); //PWM输出
}
else
{
Moto[0].dir = 128 - Remote.pwm[0]; //往左方向
k = (float)Moto[0].dir / 128; //比例系数
l_out= (float)Moto[0].pwm * (float)k; //动力的比例
//-------------------------------------------------
//左减,
pwm_out((Moto[0].pwm - (unsigned char)l_out),Moto[0].pwm); //PWM输出
}
}
else
{
BSP_Motor_Init(); //驱动端口初始化
switch(Remote.key&0x000F)//通过按键控制
{
case 0x0001:
BSP_diver_Motor_L(1);
BSP_diver_Motor_R(1);
break;//向上
case 0x0002:
BSP_diver_Motor_L(2);
BSP_diver_Motor_R(2);
break;//向下
case 0x0004:
BSP_diver_Motor_L(1);
BSP_diver_Motor_R(2);
break;//向右
case 0x0008:
BSP_diver_Motor_L(2);
BSP_diver_Motor_R(1);
break;//向左
default:
BSP_diver_Motor_L(3);
BSP_diver_Motor_R(3);
break;
}
}
//------------------------------------------------------------
if((Remote.state & 0x08)==0x08) //开"启"右模拟调整功能
{;
}
else
{
switch(Remote.key)//通过按键控制
{
case 0x0010:BSP_LED_Toggle(LED2);break;//Y
case 0x0020:BSP_LED_Toggle(LED2);break;//A
case 0x0040:BSP_LED_Toggle(LED2);break;//B
case 0x0100:BSP_LED_Toggle(LED2);break;//X
}
}
//---------------------------------------------------------------
switch(Remote.key)//通过按键控制
{
case 0x0200:BSP_LED_Toggle(LED2);break;//L1
case 0x0400:BSP_LED_Toggle(LED2);break;//L2
case 0x1000:BSP_LED_Toggle(LED2);break;//R2
case 0x0800:BSP_LED_Toggle(LED2);break;//R1
case 0x000C:BSP_LED_Toggle(LED2);break;//SELCET
case 0x0003:BSP_LED_Toggle(LED2);break;//START
}
}
else
{
//信号有误,关闭输出
}
/* if(HAL_UART_Transmit(&UartHandle, (uint8_t*)att_data, data_length, 5000)!= HAL_OK)
{
Error_Handler();
}*/
}