实验原理:
STM32内部集成三个12位ADC,iCore1S的所有电源经过
电阻分压或者直接接入STM32的ADC的输出通道内,输入电流
经过高端电流检测芯片ZXCT1009F输入到ADC的输入通道内,
从而实现电源监控功能。
实验现象:
iCore1S双核心板红色LED常亮,串口向终端输出电源监
控的数据。
核心代码:
int main(void) { /* USER CODE BEGIN 1 */ int i; /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init();//ÍâÉè³õʼ»¯ /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ADC3_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ LED_RED_ON; usart1.printf("\x0c"); usart1.printf("\033[1;32;40m"); usart1.printf("\r\n\r\nhello! I am iCore1S!\r\n\r\n\r\n"); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ for(i = 0;i < 100000;i++); //ADC采集 for(i = 0;i < 5;i++){ adc.read(i); } usart1.printf(" [V] %4.2fV, ",adc.value[0] * 6); usart1.printf("[I] %3.0fmA , ",adc.value[1] / 2* 1000.); usart1.printf("[1.2V] %4.2fV, ",adc.value[2]); usart1.printf("[3.3V] %4.2fV, ",adc.value[3] * 2); usart1.printf("[2.5V] %4.2fV\r",adc.value[4] * 2); /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }
static int read_adc(int channel) { int i,k; unsigned long int temp[20] = {0}; unsigned long int value; unsigned short int data[100]; ADC_ChannelConfTypeDef channel_config; unsigned char channel_remap[5] = {ADC_CHANNEL_4,ADC_CHANNEL_5,ADC_CHANNEL_6,ADC_CHANNEL_7,ADC_CHANNEL_8}; channel_config.Channel = channel_remap[channel]; channel_config.Rank = 1; channel_config.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; for(k = 0;k < 20;k++){ for(i = 0;i < 100;i++){ HAL_ADC_ConfigChannel(&hadc3,&channel_config); HAL_ADC_Start(&hadc3); while(!__HAL_ADC_GET_FLAG(&hadc3,ADC_FLAG_EOC)); data[i] = HAL_ADC_GetValue(&hadc3); } 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; adc.value[channel] = value * ADC_REF / 4096; return value; }
源代码下载链接:
链接:http://pan.baidu.com/s/1eRScqK6 密码:0tmn