银杏科技有限公司旗下技术文档发布平台 | |||
技术支持电话 | 0379-69926675-801 | ||
技术支持邮件 | Gingko@vip.163.com | ||
版本 | 日期 | 作者 | 修改内容 |
---|---|---|---|
V1.0 | 2020-07-28 | gingko | 初次建立 |
1.在主界面选择File–>New Project 或者直接点击ACCEE TO MCU SELECTOR 2.出现芯片型号选择,搜索自己芯片的型号,双击型号,或者点击Start Project进入配置 在搜索栏的下面,提供的各 种查找方式,可以选择芯片内核,型号,等等,可以帮助你查找芯片。本实验选取的芯片型号为:STM32H750IBKx。 3.配置RCC,使用外部时钟源 4.时基源选择SysTick 5.将PA10,PI1,PI2,PI3设置为GPIO_Output 6.引脚模式配置 7.设置串口 8.在NVIC Settings一栏使能接收中断 9.配置ADC 10.时钟源设置,选择外部高速时钟源,配置为最大主频 11.工程文件的设置, 这里就是工程的各种配置 我们只用到有限几个,其他的默认即可 IDE我们使用的是 MDK V5.27 12.点击Code Generator,进行进一步配置
13.然后点击GENERATE CODE 创建工程 创建成功,打开工程。
ADC主要参数有以下几点:
int main(void) { HAL_Init(); SystemClock_Config(); i2c.initialize(); axp152.initialize(); axp152.set_dcdc1(3500);//[ARM & FPGA] axp152.set_dcdc2(1200);//[FPGA INT] axp152.set_dcdc3(3300);//[DCOUT3] axp152.set_dcdc4(3300);//[DCOUT4] axp152.set_aldo1(3300);//[BK3] axp152.set_aldo2(3300);//[ALDOOUT2] axp152.set_dldo1(3300);//[BK0] axp152.set_dldo2(3300);//[BK1] HAL_Delay(200); MX_GPIO_Init(); MX_USART2_UART_Init(); MX_ADC1_Init(); MX_ADC3_Init(); usart2.initialize(115200); while (1) { if(systick._500ms_flag == 1){ systick._500ms_flag = 0; LED_ON; my_adc.read(0); my_adc.read_mux(); usart2.printf("\x0c");//清屏 usart2.printf("\033[1;32;40m");//设置终端字体为绿色 usart2.printf("Hello,I am iCore4TX!\r\n\r\n"); //打印ADC的值 usart2.printf("[V ] %4.2fV\r\n",my_adc.value[0] * 6); usart2.printf("[I ] %3.0fmA\r\n",my_adc.value[5] / 2* 1000.); usart2.printf("[3.3V ] %4.2fV\r\n",my_adc.value[7] * 2); usart2.printf("[2.5V ] %4.2fV\r\n",my_adc.value[4] * 2); usart2.printf("[1.2V ] %4.2fV\r\n",my_adc.value[6]); usart2.printf("[BK3 ] %4.2fV\r\n",my_adc.value[2] * 2); usart2.printf("[BK4 ] %4.2fV\r\n",my_adc.value[1] * 2); usart2.printf("[BK5 ] %4.2fV\r\n",my_adc.value[3] * 2); usart2.printf("[BK7 ] %4.2fV\r\n",my_adc.value[8] * 2); LED_OFF; } } }
//定义74HC4051片选管脚 #define SEL_A_ON HAL_GPIO_WritePin(GPIOI, SEL_A_Pin, GPIO_PIN_SET) #define SEL_A_OFF HAL_GPIO_WritePin(GPIOI, SEL_A_Pin, GPIO_PIN_RESET) #define SEL_B_ON HAL_GPIO_WritePin(GPIOI, SEL_B_Pin, GPIO_PIN_SET) #define SEL_B_OFF HAL_GPIO_WritePin(GPIOI, SEL_B_Pin, GPIO_PIN_RESET) #define SEL_C_ON HAL_GPIO_WritePin(GPIOI, SEL_C_Pin, GPIO_PIN_SET) #define SEL_C_OFF HAL_GPIO_WritePin(GPIOI, SEL_C_Pin, GPIO_PIN_RESET) //选择测量BK4时片选脚状态 #define CHANNEL_0_ON SEL_C_OFF;\ SEL_B_OFF;\ SEL_A_OFF //选择测量BK3时片选脚状态 #define CHANNEL_1_ON SEL_C_OFF;\ SEL_B_OFF;\ SEL_A_ON //选择测量BK5时片选脚状态 #define CHANNEL_2_ON SEL_C_OFF;\ SEL_B_ON;\ SEL_A_OFF //选择测量2.5V时片选脚状态 #define CHANNEL_3_ON SEL_C_OFF;\ SEL_B_ON;\ SEL_A_ON //选择测量输入电流时片选脚状态 #define CHANNEL_4_ON SEL_C_ON;\ SEL_B_OFF;\ SEL_A_OFF //选择测量1.2V时片选脚状态 #define CHANNEL_5_ON SEL_C_ON;\ SEL_B_OFF;\ SEL_A_ON //选择测量3.3V时片选脚状态 #define CHANNEL_6_ON SEL_C_ON;\ SEL_B_ON;\ SEL_A_OFF //选择测量BK7时片选脚状态 #define CHANNEL_7_ON SEL_C_ON;\ SEL_B_ON;\
void MX_ADC1_Init(void) { ADC_MultiModeTypeDef multimode = {0}; ADC_ChannelConfTypeDef sConfig = {0}; hadc1.Instance = ADC1; hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV16; //16分频 hadc1.Init.Resolution = ADC_RESOLUTION_16B; //ADC转换分辨率16位 hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; //非扫描模式 hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; //关闭 EOC 中断 hadc1.Init.LowPowerAutoWait = DISABLE; //自动低功耗关闭 hadc1.Init.ContinuousConvMode = DISABLE; //关闭连续转换 hadc1.Init.NbrOfConversion = 1; //1个转换在规则序列中 hadc1.Init.DiscontinuousConvMode = DISABLE; //禁止不连续采样模式 hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; //软件触发 hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; //禁止触发检测 hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR; //存到DR寄存器 hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; //溢出保留上次转换数据 hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE; //位数不左移 hadc1.Init.OversamplingMode = DISABLE; //关闭过采样 if (HAL_ADC_Init(&hadc1) != HAL_OK) { Error_Handler(); } multimode.Mode = ADC_MODE_INDEPENDENT; //独立模式 if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) { Error_Handler(); } sConfig.Channel = ADC_CHANNEL_16; //通道16 sConfig.Rank = ADC_REGULAR_RANK_1; //第 1 个序列 sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; //采样时间 sConfig.SingleDiff = ADC_SINGLE_ENDED; //单端输入 sConfig.OffsetNumber = ADC_OFFSET_NONE; //不选择偏移序号 sConfig.Offset = 0; 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_ASYNC_DIV16; hadc3.Init.Resolution = ADC_RESOLUTION_16B; hadc3.Init.ScanConvMode = ADC_SCAN_DISABLE; hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV; hadc3.Init.LowPowerAutoWait = DISABLE; hadc3.Init.ContinuousConvMode = DISABLE; hadc3.Init.NbrOfConversion = 1; hadc3.Init.DiscontinuousConvMode = DISABLE; hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hadc3.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR; hadc3.Init.Overrun = ADC_OVR_DATA_PRESERVED; hadc3.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE; hadc3.Init.OversamplingMode = DISABLE; if (HAL_ADC_Init(&hadc3) != HAL_OK) { Error_Handler(); } sConfig.Channel = ADC_CHANNEL_1; sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; sConfig.SingleDiff = ADC_SINGLE_ENDED; sConfig.OffsetNumber = ADC_OFFSET_NONE; sConfig.Offset = 0; if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { Error_Handler(); } }
void sort(unsigned short int a[], int n) { int i, j, t; //元素从小到大排列 for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (a[j] > a[j + 1]) { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } } int read(int channel) { int i; unsigned long int temp = 0; unsigned short int data[50]; ADC_ChannelConfTypeDef channel_config; int channel_remap[2] = {ADC_CHANNEL_16,ADC_CHANNEL_1};//ADC1,16通道 channel_config.Channel = channel_remap[channel]; //通道选择 channel_config.Offset = 0; //偏移量为0 channel_config.Rank = ADC_REGULAR_RANK_1; //第一个序列 channel_config.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; //采样时间 channel_config.SingleDiff = ADC_SINGLE_ENDED; //单端输入 channel_config.OffsetNumber = ADC_OFFSET_NONE; //不选择偏移序号 for(i = 0;i < 50;i ++){ if(channel == 0){ //如果通道为0,则使能ADC1 HAL_ADC_ConfigChannel(&hadc1,&channel_config); HAL_ADC_Start(&hadc1); while(!__HAL_ADC_GET_FLAG(&hadc1,ADC_FLAG_EOC)); data[i] = HAL_ADC_GetValue(&hadc1); }else if(channel == 1){ //如果通道为1,则使能ADC3 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,50); for(i = 20;i < 30;i++){ //取ADC排序后的中间10位数值 temp += data[i]; } temp = temp / 10; //取ADC平均值 if(channel == 0){ //读取ADC1的值 my_adc.value[channel] = temp * ADC_REF / 65536; } return temp; } static int read_mux(void) { //打开通道并读取ADC的值 CHANNEL_0_ON; my_adc.value[1] = my_adc.read(1) * ADC_REF / 65536; CHANNEL_1_ON; my_adc.value[2] = my_adc.read(1) * ADC_REF / 65536; CHANNEL_2_ON; my_adc.value[3] = my_adc.read(1) * ADC_REF / 65536; CHANNEL_3_ON; my_adc.value[4] = my_adc.read(1) * ADC_REF / 65536; CHANNEL_4_ON; my_adc.value[5] = my_adc.read(1) * ADC_REF / 65536; CHANNEL_5_ON; my_adc.value[6] = my_adc.read(1) * ADC_REF / 65536; CHANNEL_6_ON; my_adc.value[7] = my_adc.read(1) * ADC_REF / 65536; CHANNEL_7_ON; my_adc.value[8] = my_adc.read(1) * ADC_REF / 65536; return 0; }