360度旋转编码器
unsigned char cur_num = 0;
void EXTI_Init(void)
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_GPIOE_CLK_ENABLE();
GPIO_Initure.Pin=GPIO_PIN_2;
GPIO_Initure.Mode=GPIO_MODE_IT_FALLING;
GPIO_Initure.Pull=GPIO_PULLUP;
HAL_GPIO_Init(GPIOE,&GPIO_Initure);
HAL_NVIC_SetPriority(EXTI2_IRQn,2,1);
HAL_NVIC_EnableIRQ(EXTI2_IRQn);
}
void EXTI2_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2);
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_2)
{
if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_2) == HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3))
cur_num++;
else
cur_num--;
}
}
重点是先后顺时针旋转和逆时针旋转中断的引脚先后顺序不同。
摇杆
void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 3;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_4;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_5;
sConfig.Rank = 2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_6;
sConfig.Rank = 3;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(adcHandle->Instance==ADC1)
{
__HAL_RCC_ADC1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
hdma_adc1.Instance = DMA2_Stream0;
hdma_adc1.Init.Channel = DMA_CHANNEL_0;
hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_adc1.Init.Mode = DMA_CIRCULAR;
hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(ADC_IRQn);
}
}
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
{
if(adcHandle->Instance==ADC1)
{
__HAL_RCC_ADC1_CLK_DISABLE();
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6);
HAL_DMA_DeInit(adcHandle->DMA_Handle);
HAL_NVIC_DisableIRQ(ADC_IRQn);
}
}
void ADC1_Poll(void)
{
double value_ch1;
double value_ch2;
double value_ch3;
for(int i = 0; i < 36; i+=3)
{
value_ch1 += (float)(adc_dma_buf[i + 0]/4096.0*3.3);
value_ch2 += (float)(adc_dma_buf[i + 1]/4096.0*3.3);
value_ch3 += (float)(adc_dma_buf[i + 2]/4096.0*3.3);
}
value_ch1 /= 12.0;
value_ch2 /= 12.0;
value_ch3 /= 12.0;
if((value_ch1>=1.0)&&(value_ch1<=2.8) && (value_ch2>=3.0)&&(value_ch2<=3.3))
{
printf("\r\n***前进****\r\n");
}
if((value_ch1>=0) &&(value_ch1<=0.5) && (value_ch2>=1.0) && (value_ch2<=2.8))
{
printf("\r\n***向左****\r\n");
}
if((value_ch1>=3.0)&&(value_ch1<=3.3) && (value_ch2>=1.0)&&(value_ch2<=2.8))
{
printf("\r\n***向右****\r\n");
}
if((value_ch1>=1.0)&&(value_ch1<=2.8) && (value_ch2>=0)&&(value_ch2<=0.5))
{
printf("\r\n***后退****\r\n");
}
if((value_ch3<=0.02))
{
printf("\r\n***按键按下****\r\n");
}
}
就是一个简单的模拟量输出判断,打开printf可以实时看到adc采集的端口电压就能明白原理。
|