这片STM320C31的ADC具有过采样功能,这次评测就采用过采样128位,右移3位的ADC模式,这样12位ADC就能够得到16位ADC的分辨率。CubeMX里的ADC配置如下:
配置ADC时发现STM32C031不像其他型号一样可以从下拉选项使能ADC,而是要从引脚图中直接点击相应引脚使能ADC。
主要代码
- void MX_ADC1_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig = {0};
- hadc1.Instance = ADC1;
- hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV8;
- hadc1.Init.Resolution = ADC_RESOLUTION_12B;
- 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 = ENABLE;
- hadc1.Init.NbrOfConversion = 1;
- hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
- hadc1.Init.DMAContinuousRequests = DISABLE;
- hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
- hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_79CYCLES_5;
- hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_160CYCLES_5;
- hadc1.Init.OversamplingMode = ENABLE;
- hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_128;
- hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_3;
- hadc1.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
- hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
- if (HAL_ADC_Init(&hadc1) != HAL_OK)
- {
- Error_Handler();
- }
- sConfig.Channel = ADC_CHANNEL_6;
- sConfig.Rank = ADC_REGULAR_RANK_1;
- sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_2;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- }
- int main(void)
- {
- HAL_Init();
- SystemClock_Config();
- MX_GPIO_Init();
- MX_ADC1_Init();
- LCD_Init();
- LCD_Print(16, 8,"OVERSAMPLING",TYPE16X16,TYPE8X16);
- LCD_Print(48, 24,"ADC",TYPE16X16,TYPE8X16);
- HAL_ADCEx_Calibration_Start(&hadc1) ;
- HAL_ADC_Start_IT(&hadc1);
- while (1)
- {
- uwInputVoltage = uwConvertedValue * 3300;
- uwInputVoltage = uwInputVoltage / 0xFFF0;
- To_buffer(uwInputVoltage);
- LCD_Print(30, 48,Buff,1,1);
- HAL_Delay(200);
- }
-
- }
- void ADC1_IRQHandler(void)
- {
- uwConvertedValue= LL_ADC_REG_ReadConversionData32(ADC1);
- HAL_ADC_IRQHandler(&hadc1);
- }
复制代码
ADC通道6连接可调电阻输入0-3.3V电压,结合上一贴的OLED显示,将电压值在屏幕显示。
STM320C31的过采样ADC大大提高了STM320C31ADC转换分辨率,是STM8S系列无法相对比的。它是替代STM8S的首选。
本次 STM320C31试用评测完成。
|
不好意思,原来评测要求发帖是在“STM32团队版块”,我不知道要进入“文章”板块。下次会注意。