实验原理:
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 ()
{
/* USER CODE END WHILE */
for(i = ;i < ;i++); //ADC采集
for(i = ;i < ;i++){
adc.read(i);
} usart1.printf(" [V] %4.2fV, ",adc.value[] * );
usart1.printf("[I] %3.0fmA , ",adc.value[] / * .);
usart1.printf("[1.2V] %4.2fV, ",adc.value[]);
usart1.printf("[3.3V] %4.2fV, ",adc.value[] * );
usart1.printf("[2.5V] %4.2fV\r",adc.value[] * ); /* USER CODE BEGIN 3 */ }
/* USER CODE END 3 */ }
static int read_adc(int channel)
{
int i,k;
unsigned long int temp[] = {};
unsigned long int value;
unsigned short int data[];
ADC_ChannelConfTypeDef channel_config;
unsigned char channel_remap[] = {ADC_CHANNEL_4,ADC_CHANNEL_5,ADC_CHANNEL_6,ADC_CHANNEL_7,ADC_CHANNEL_8}; channel_config.Channel = channel_remap[channel];
channel_config.Rank = ;
channel_config.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; for(k = ;k < ;k++){
for(i = ;i < ;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,); for(i = ;i < ;i++){
temp[k] += data[i];
} temp[k] = temp[k] / ;
} value = ;
for(k = ;k < ;k++){
value += temp[k];
}
value /= ; adc.value[channel] = value * ADC_REF / ; return value;
}
源代码下载链接:
链接:http://pan.baidu.com/s/1eRScqK6 密码:0tmn