银杏科技有限公司旗下技术文档发布平台 | |||
技术支持电话 | 0379-69926675-801 | ||
技术支持邮件 | Gingko@vip.163.com | ||
版本 | 日期 | 作者 | 修改内容 |
---|---|---|---|
V1.0 | 2020-11-25 | gingko | 初次建立 |
1.在主界面选择File–>New Project或者直接点击ACCEE TO MCU SELECTOR 2.出现芯片型号选择,搜索自己芯片的型号,双击型号,或者点击Start Project进入配置在搜索栏的下面,提供的各 种查找方式,可以选择芯片内核,型号等等,可以帮助你查找芯片。本实验选取的芯片型号为:STM32F429IGHx。 3.配置RCC,使用外部时钟源 4.Debug选择Serial Wire,时基源选择SysTick 5.将PI3、PI4、PH14设置为GPIO_Output 6.引脚模式配置 7.设置串口 8.在NVIC Settings一栏使能接收中断 9.配置ADC 10.时钟源设置,选择外部高速时钟源,配置为最大主频 11.工程文件的设置, 这里就是工程的各种配置我们只用到有限几个,其他的默认即可IDE我们使用的是 MDK V5.27 12.点击Code Generator,进行进一步配置
ADC主要参数有以下几点:
ADC 接口内置的其它逻辑功能允许:
STM32内部集成三个12位ADC,iCore3L的所有电源经过电阻分压或者直接接入STM32的ADC的输出通道内,输入电流经过高端电流检测芯片TP1001S3输入到ADC的输入通道内,从而实现电源监控功能。电压监控硬件连接示意图如下图所示: 由上图可知:
故知:
其它电源监控同理可得:
通过R1的电流:
带入R2=10K,R1=0.02K:
1.主函数
int main(void) { int i; HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART2_UART_Init(); MX_ADC1_Init(); MX_ADC3_Init(); /* USER CODE BEGIN 2 */ while (1) { if(systick._500ms_flag == 1) { LED_RED_ON; systick._500ms_flag = 0; for(i=0;i<5;i++) { my_adc.read(i); } usart1.initialize(115200); usart1.printf("\x0c"); usart1.printf("\033[1;32;40m"); usart1.printf("Hello,I am iCore3L!\r\n\r\n"); usart1.printf("[1.2V] %4.2fV,",my_adc.value[0]); //打印输入电源电压 usart1.printf("[1.8V] %4.2fV,",my_adc.value[1]); usart1.printf("[3.3V] %4.2fV,",my_adc.value[2]*2); usart1.printf("[V] %4.2fV,",my_adc.value[3] * 6); usart1.printf("[I] %3.0fmA\r\n",my_adc.value[4]/2*1000.); LED_RED_OFF; } } }
2.ADC初始化函数
void MX_ADC1_Init(void) { ADC_ChannelConfTypeDef sConfig = {0}; hadc1.Instance = ADC1; hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8; hadc1.Init.Resolution = ADC_RESOLUTION_12B; //ADC转换分辨率12位 hadc1.Init.ScanConvMode = DISABLE; //非扫描模式 hadc1.Init.ContinuousConvMode = DISABLE; //关闭连续转换 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 = 1; hadc1.Init.DMAContinuousRequests = DISABLE; hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; //关闭EOC中断 if (HAL_ADC_Init(&hadc1) != HAL_OK) { Error_Handler(); } sConfig.Channel = ADC_CHANNEL_15; //通道15 sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; //采样时间 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } } void MX_ADC3_Init(void) { ADC_ChannelConfTypeDef sConfig = {0}; hadc3.Instance = ADC3; hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8; hadc3.Init.Resolution = ADC_RESOLUTION_12B; //ADC转换分辨率12位 hadc3.Init.ScanConvMode = DISABLE; //非扫描模式 hadc3.Init.ContinuousConvMode = DISABLE; //关闭连续转换 hadc3.Init.DiscontinuousConvMode = DISABLE; //禁止不连续采样模式 hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; //禁止触发检测 hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START; //软件触发 hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc3.Init.NbrOfConversion = 1; hadc3.Init.DMAContinuousRequests = DISABLE; hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV; //关闭EOC中断 if(HAL_ADC_Init(&hadc3) != HAL_OK) { Error_Handler(); } sConfig.Channel = ADC_CHANNEL_6; //通道6 sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; //采样时间 if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { Error_Handler(); } }
3.ADC读取函数
int read(int channel) { int i,k; unsigned long int temp[20] = {0}; unsigned short int data[100]; ADC_ChannelConfTypeDef channel_config; int channel_remap[5] = {ADC_CHANNEL_13,ADC_CHANNEL_6,ADC_CHANNEL_12,ADC_CHANNEL_7,ADC_CHANNEL_6}; channel_config.Channel = channel_remap[channel]; channel_config.Rank = 1; channel_config.SamplingTime = ADC_SAMPLETIME_3CYCLES; if(channel == 1 || channel == 3) { HAL_ADC_ConfigChannel(&hadc3,&channel_config); } else { HAL_ADC_ConfigChannel(&hadc1,&channel_config); } for(k = 0;k < 20;k ++) { for(i = 0;i < 100;i ++) { if(channel == 1 || channel == 3) { HAL_ADC_Start(&hadc3); while(!__HAL_ADC_GET_FLAG(&hadc3,ADC_FLAG_EOC));//等待转换结束 data[i] = HAL_ADC_GetValue(&hadc3); //将结果保存 } else { HAL_ADC_Start(&hadc1); while(!__HAL_ADC_GET_FLAG(&hadc1,ADC_FLAG_EOC));//等待转换结束 data[i] = HAL_ADC_GetValue(&hadc1); //将结果保存 } } sort(data,100); for(i = 40;i < 60;i++) { temp[k] += data[i]; } } temp[k] = temp[k] / 20; value = 0; for(k = 0;k < 20;k ++) { value += temp[k]; } value /= 20; my_adc.value[channel] = value * ADC_REF / 4096; return value; }