實驗原理:
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