stm32外设使用
- 1. adc使用
- 2. uart使用
1. adc使用
参考文章
static void MX_Adc_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Channel1_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
}
static int MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
// ------------------DMA Init 结构体参数 初始化--------------------------
MX_Adc_DMA_Init(); // 必须在前面
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_8B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.LowPowerAutoPowerOff = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_1CYCLE_5;
hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_1CYCLE_5;
hadc1.Init.OversamplingMode = DISABLE;
hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
HAL_ADCEx_Calibration_Start(&hadc1);
/* USER CODE END ADC1_Init 2 */
// HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&adc_value, sizeof(adc_value) / 2 - 8);
return 0;
}
/**
* @brief ADC 中断回调函数
* @param None
* @retval None
*/
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
{
HAL_ADC_Stop_DMA(AdcHandle);
// HAL_ADC_Start_DMA(AdcHandle, (uint32_t*)&adc_val, 1);
// uint16_t val = HAL_ADC_GetValue(AdcHandle);
// printf("adc -> %d\n",adc_val[0]);
ecbm_modbus_cmd_write_reg(LED_ADC, adc_val[0]);
}
2. uart使用
uart配饰初始化
/* DMA 必须在前初始化 */
__HAL_RCC_DMA1_CLK_ENABLE();
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
/* usart init */
dev->uart.Init.BaudRate = baudrate;
dev->uart.Init.WordLength = UART_WORDLENGTH_8B;
dev->uart.Init.StopBits = UART_STOPBITS_1;
dev->uart.Init.Parity = UART_PARITY_NONE;
dev->uart.Init.Mode = UART_MODE_TX_RX;
dev->uart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
dev->uart.Init.OverSampling = UART_OVERSAMPLING_16;
dev->uart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
dev->uart.Init.ClockPrescaler = UART_PRESCALER_DIV1;
dev->uart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&dev->uart) != HAL_OK)
{
}
msp 中初始化
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/* USER CODE END USART1_MspInit 0 */
/* Peripheral clock enable */
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/* Peripheral clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART1_RX Init */
hdma_usart1_rx.Instance = DMA1_Channel1;
hdma_usart1_rx.Init.Request = DMA_REQUEST_USART1_RX;
hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart1_rx.Init.Mode = DMA_CIRCULAR;
hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(huart,hdmarx,hdma_usart1_rx);
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
中断服务函数
void USART1_IRQHandler(void)
{
uint32_t tmp_flag = 0;
uint32_t temp;
tmp_flag =__HAL_UART_GET_FLAG(&modbus_dev.uart, UART_FLAG_IDLE); // 获取IDLE标志使
if((tmp_flag != RESET)) // idle标志被置使
{
__HAL_UART_CLEAR_IDLEFLAG(&modbus_dev.uart); // 清除标志使
HAL_UART_DMAStop(&modbus_dev.uart);
temp = __HAL_DMA_GET_COUNTER(&hdma_usart1_rx); // 获取DMA中未传输的数据个敿
modbus_dev.r_len = sizeof(modbus_dev.r_buf) - temp; // 总计数减去未传输的数据个数,得到已经接收的数据个敿
rt_sem_release(&modbus_dev.rece_sem);
//HAL_UART_Transmit(&uart2.uart, uart2.r_buf, uart2.r_len, 1000);
}
//HAL_UART_Receive_DMA(&uart2.uart, (uint8_t*)uart2.r_buf, sizeof(uart2.r_buf));
HAL_UART_IRQHandler(&modbus_dev.uart);
}