ADC LL 初始化
void MX_ADC_Init(void)
{
LL_ADC_InitTypeDef ADC_InitStruct = {0};
LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Peripheral clock enable */
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_ADC1);
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
/**ADC GPIO Configuration
PA0 ------> ADC_IN0
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/** Configure Regular Channel
*/
LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_0);//
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
ADC_InitStruct.Clock = LL_ADC_CLOCK_SYNC_PCLK_DIV2;//
// ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_12B;
// ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
// ADC_InitStruct.LowPowerMode = LL_ADC_LP_MODE_NONE;
LL_ADC_Init(ADC1, &ADC_InitStruct);
ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;//
// ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE;//
// ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_LIMITED;
ADC_REG_InitStruct.Overrun = LL_ADC_REG_OVR_DATA_OVERWRITTEN;//
LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);
// LL_ADC_REG_SetSequencerScanDirection(ADC1, LL_ADC_REG_SEQ_SCAN_DIR_FORWARD);
LL_ADC_SetSamplingTimeCommonChannels(ADC1, LL_ADC_SAMPLINGTIME_239CYCLES_5);//
}
先校准ADC,后打开ADC,顺序不能错
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_ADC_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
LL_ADC_StartCalibration(ADC1); //开始校准
while (LL_ADC_IsCalibrationOnGoing(ADC1)){}; //等待校准完成
LL_ADC_Enable(ADC1);
while (LL_ADC_IsActiveFlag_ADRDY(ADC1)==0){};
while (1)
{
Send();
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
读取ADC数据,串口发送到山外多功能调试助手
void UART1Send(char byData)
{
LL_USART_TransmitData8(USART1, byData);
while(!LL_USART_IsActiveFlag_TC(USART1)){};
}
uint16_t GetADC1_0(void)
{
LL_ADC_REG_StartConversion(ADC1);
while(LL_ADC_IsActiveFlag_EOC(ADC1)==0){};
LL_ADC_ClearFlag_EOC(ADC1);
return LL_ADC_REG_ReadConversionData12(ADC1);
}
void Send(void)
{
uint32_t dwTimer=0;
uint16_t wADC=GetADC1_0();
UART1Send(0x03);
UART1Send(0xfc);
UART1Send(wADC&0xff);
UART1Send((wADC>>8)&0xff);
UART1Send(0xfc);
UART1Send(0x03);
while(dwTimer<10000)
{
dwTimer++;
}
}