DSP280049C的ECAP配置,STM32F103C8T6作为信号发生端
实现目标:通过串口助手输入目标频率,STM32串口接收后生成相近频率的PWM波,DSP的ECAP接口与STM32的PWM输出接口相接,然后通过ECAP识别PWM波频率,进而达到调控电机目标速度。
一、DSP280049C的ECAP配置
void CAP_int(void)
{
EALLOW;
ECap1Regs.ECCTL0.bit.INPUTSEL = 0;
ECap1Regs.ECCTL1.bit.CAP1POL = EC_RISING;
ECap1Regs.ECCTL1.bit.CAP2POL = EC_FALLING;
ECap1Regs.ECCTL1.bit.CAP3POL = EC_RISING;
ECap1Regs.ECCTL1.bit.CAP4POL = EC_FALLING;
ECap1Regs.ECCTL1.bit.CTRRST1 = 0;
ECap1Regs.ECCTL1.bit.CTRRST2 = 0;
ECap1Regs.ECCTL1.bit.CTRRST3 = 0;
ECap1Regs.ECCTL1.bit.CTRRST4 = 1;
ECap1Regs.ECCTL1.bit.CAPLDEN = EC_ENABLE;
ECap1Regs.ECCTL1.bit.PRESCALE = EC_DIV1;
ECap1Regs.ECCTL2.bit.CAP_APWM = EC_CAP_MODE;
ECap1Regs.ECCTL2.bit.CONT_ONESHT = EC_CONTINUOUS;
ECap1Regs.ECCTL2.bit.SYNCI_EN = EC_DISABLE;
ECap1Regs.ECCTL2.bit.SYNCO_SEL = EC_SYNCO_DIS;
ECap1Regs.ECCTL2.bit.TSCTRSTOP = EC_RUN;
EDIS;
EALLOW;
InputXbarRegs.INPUT1SELECT = 6;
GpioCtrlRegs.GPADIR.bit.GPIO6 = 0;
GpioCtrlRegs.GPAPUD.bit.GPIO6 = 0;
GpioCtrlRegs.GPAQSEL1.bit.GPIO6 = 3;
EDIS;
}
uint16_t Get_cap(void)
{
uint16_t cap1cnt;
uint16_t cap2cnt;
uint16_t cap3cnt;
uint16_t cap4cnt;
uint32_t Period1;
uint16_t speed;
cap1cnt = ECap1Regs.CAP1;
cap2cnt = ECap1Regs.CAP2;
cap3cnt = ECap1Regs.CAP3;
cap4cnt = ECap1Regs.CAP4;
Period1 = cap3cnt - cap1cnt;
speed = (100000)/Period1;
return speed;
}
while (1)
{
if (GPIO_ReadPin(10) ==0)
{
a=0;
b=0;
}
else
{
a = Get_cap();
}
SPIA_sendtwo(1234,a);
DELAY_US(2000L);
}
二、STM32F103C8T6的PWM配置
void MX_TIM2_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = 6;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 7;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 4;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim2);
}
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
speed_tar =0;
select_speed (speed_tar);
void select_speed (uint16_t speed_tar)
{
uint32_t arr;
if (speed_tar == 0)
{
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_RESET);
}
else
{
arr = 10286 / speed_tar;
__HAL_TIM_SET_AUTORELOAD(&htim2, arr-1);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, arr/2);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET);
}
}
三、STM32F103C8T6的串口配置(开启接收中断)
HAL_UART_Receive_IT(&huart1, (uint8_t *)&rx_bit, 1);
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
static int i=0;
int size;
rx_data[rx_cnt] = rx_bit;
rx_cnt=rx_cnt+1;
if (rx_data[rx_cnt-1] == 0x0A && rx_data[rx_cnt-2] == 0x0D)
{
size = sizeof(rx_data);
speed_tar = (rx_data[1]-48)*1000 + (rx_data[2]-48)*100 + (rx_data[3]-48)*10 + (rx_data[4]-48);
if (speed_tar>1600)
{
speed_tar = 0;
}
select_speed (speed_tar);
printf ("speed_tar = %d\r\n",speed_tar);
rx_cnt =0;
rx_data[0]=0;
rx_data[1]=0;
rx_data[2]=0;
rx_data[3]=0;
rx_data[4]=0;
}
HAL_UART_Receive_IT(&huart1, (uint8_t *)&rx_bit, 1);
}