本帖最后由 BinWin 于 2020-8-3 22:36 编辑
由于F091库存原因,收到了管理员发来的STM32G474,在此也感谢管理员的协调,有幸体验到G系列芯片。
这块板卡比较抢眼的可谓是板载的STLINK V3E调试器了,核心处理器是高性能系列stm32f723,是不是比G474更有意思呢
首次连接到MDK下载时会提示更新固件,选择是就好了。然后就是flash烧录算法要选dualxxx,这也是看了例程才知道的,用CUBEMX建好的工程默认会下载失败。转入正题,来看下配套的例程之ADC。ST提供了两套,一是使用增益补偿功能实现结果自动转换(mV电压),二是使用ADC失调补偿功能直接转换来自ADC范围到特定应用范围,无需后期计算。 例程一主要代码
static void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_MultiModeTypeDef multimode = {0};
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.GainCompensation = VDDA_APPLI * GAIN_COMPENSATION_X1_FACTOR / DIGITAL_SCALE_12BITS; //敲黑板
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_TRGO;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure the ADC multi-mode
*/
multimode.Mode = ADC_MODE_INDEPENDENT;
if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_6;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
ADC主要配置如以上代码,实则就是3300 * 1000 / 0xFFF,转换结果就是直接经过运算的mV电压值,自己以前没有用过,感觉挺方便。仿真效果如下
接下来看另一套例程,讲的是利用失调补偿来实现偏置设置,达到改变范围的效果,测试如下:
输入恒定为0~3.3V的情况下,可以将转换结果扩展到-2048---2047,也可以将0-1.65V定为2048-4095等,应用比较灵活,主要代码如下
modeIndex++;
if (modeIndex >= sizeof(modeString)/sizeof(modeString[0])) modeIndex = 0;
/* Reconfigure offset */
sConfig.Channel = ADC_CHANNEL_6;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = modeString[modeIndex].number;
sConfig.Offset = modeString[modeIndex].offset;
sConfig.OffsetSign = modeString[modeIndex].sign;
sConfig.OffsetSaturation = modeString[modeIndex].satur;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
//设置的参数表如下
<div class="blockcode"><blockquote>uint16_t modeIndex = 0;
const t_offsetcfg modeString[5] = {
{ADC_OFFSET_NONE, 0, ADC_OFFSET_SIGN_NEGATIVE, DISABLE, "Cfg #0: Offset disabled"},
{ADC_OFFSET_1, 0x800, ADC_OFFSET_SIGN_NEGATIVE, DISABLE, "Cfg #1: Offset enabled, sign is negative & saturation disabled"},
{ADC_OFFSET_1, 0x800, ADC_OFFSET_SIGN_POSITIVE, DISABLE, "Cfg #2: Offset enabled, sign is positive & saturation disabled"},
{ADC_OFFSET_1, 0x800, ADC_OFFSET_SIGN_NEGATIVE, ENABLE, "Cfg #3: Offset enabled, sign is negative & saturation enabled"},
{ADC_OFFSET_1, 0x800, ADC_OFFSET_SIGN_POSITIVE, ENABLE, "Cfg #4: Offset enabled, sign is positive & saturation enabled"},
};
第一次接触HAL库还是18年初的时候,经过这么久的迭代,包括CubeMX工具的更新以及CubeIDE的诞生,代码移植性越来越好,开发效率也提高了很多。基于CubeMX的HAL实现,在不同芯片间的代码复用率显著提升。手里这块G474更是应运而生,提升了F系列的性能,更具性价比。接下来继续体验Nucleo G4的其他优势功能。
|