- static void MX_ADC1_Init(void)
- {
- ADC_ChannelConfTypeDef ConfigChannel = {0};
- hadc1.Instance = ADC1;
- hadc1.Init.ConversionType = ADC_CONVERSION_WITH_DS;
- hadc1.Init.SequenceLength = 2;
- hadc1.Init.SamplingMode = ADC_SAMPLING_AT_START;
- hadc1.Init.SampleRate = ADC_SAMPLE_RATE_16;
- hadc1.Init.InvertOutputMode = ADC_DATA_INVERT_NONE;
- hadc1.Init.Overrun = ADC_NEW_DATA_IS_LOST;
- hadc1.Init.ContinuousConvMode = DISABLE;
- hadc1.Init.DownSamplerConfig.DataWidth = ADC_DS_DATA_WIDTH_12_BIT;
- hadc1.Init.DownSamplerConfig.DataRatio = ADC_DS_RATIO_1;
- if (HAL_ADC_Init(&hadc1) != HAL_OK)
- {
- Error_Handler();
- }
- ConfigChannel.Channel = ADC_CHANNEL_TEMPSENSOR;
- ConfigChannel.Rank = ADC_RANK_1;
- ConfigChannel.VoltRange = ADC_VIN_RANGE_1V2;
- ConfigChannel.CalibrationPoint.Number = ADC_CALIB_POINT_1;
- ConfigChannel.CalibrationPoint.Gain = 0x0;
- ConfigChannel.CalibrationPoint.Offset = 0x0;
- if (HAL_ADC_ConfigChannel(&hadc1, &ConfigChannel) != HAL_OK)
- {
- Error_Handler();
- }
- ConfigChannel.Channel = ADC_CHANNEL_VINP0;
- ConfigChannel.Rank = ADC_RANK_2;
- ConfigChannel.CalibrationPoint.Number = ADC_CALIB_NONE;
- ConfigChannel.CalibrationPoint.Gain = 0x00;
- ConfigChannel.CalibrationPoint.Offset = 0x00;
- if (HAL_ADC_ConfigChannel(&hadc1, &ConfigChannel) != HAL_OK)
- {
- Error_Handler();
- }
- }
由于该例程没有配置串口,因此对A/D的检测值是无法打印输出的。为此,需要配合相应的显示器件来显示。
前面曾介绍过彩色LCD屏的显示驱动,由于在A/D检测中会与LCD的引脚产生冲突,故改变引脚后的高低电平输出语句定义为:
#define OLED_SCLK_Set HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET) // CLK
#define OLED_SCLK_Clr HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET)
#define OLED_SDIN_Set HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET) // DIN
#define OLED_SDIN_Clr HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET)
#define OLED_RST_Set HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET) // RES
#define OLED_RST_Clr HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET)
#define OLED_DC_Set HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET) // DC
#define OLED_DC_Clr HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET)
#define OLED_CS_Set HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET) //CS
#define OLED_CS_Clr HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_RESET)
相应的引脚功能配置函数为:
- static void LCD_GPIO(void)
- {
- __HAL_RCC_GPIOB_CLK_ENABLE();
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Pin = GPIO_PIN_6;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = GPIO_PIN_7;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = GPIO_PIN_3;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- __HAL_RCC_GPIOA_CLK_ENABLE();
- GPIO_InitStruct.Pin = GPIO_PIN_8;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = GPIO_PIN_11;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = GPIO_PIN_9;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
用于2个通道检测与显示的主程序为:
- int main(void)
- {
- uint32_t u,V;
- uint32_t tmp_index;
- for (tmp_index = 0; tmp_index < ADC_CONVERTED_DATA_BUFFER_SIZE; tmp_index++)
- {
- uhADCxConvertedData[tmp_index] = VAR_CONVERTED_DATA_INIT_VALUE;
- }
- HAL_Init();
- SystemClock_Config();
- PeriphCommonClock_Config();
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_ADC1_Init();
- BSP_LED_Init(LD1);
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Pin = LD1_PIN;
- HAL_GPIO_Init(LD1_GPIO_PORT, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = LD2_PIN;
- HAL_GPIO_Init(LD2_GPIO_PORT, &GPIO_InitStruct);
- LCD_GPIO();
- Lcd_Init();
- LCD_Clear(RED);
- LCD_ShowString(10,10,"STM32WB09KE",YELLOW);
- LCD_ShowString(10,30,"jinglixixi",YELLOW);
- HAL_Delay(1000);
- LCD_Clear(RED);
- LCD_ShowString(10,10,"Tempe: ",YELLOW);
- LCD_ShowString(10,30,"ADC: ",YELLOW);
- if (HAL_ADC_Start_DMA(&hadc1,
- (uint32_t *)uhADCxConvertedData,
- ADC_CONVERTED_DATA_BUFFER_SIZE
- ) != HAL_OK)
- {
- Error_Handler();
- }
- BSP_LED_On(LD1);
- HAL_Delay(LED_BLINK_SLOW);
- BSP_LED_Off(LD1);
- HAL_Delay(LED_BLINK_SLOW);
- while (1)
- {
- if (HAL_ADC_Start(&hadc1) != HAL_OK)
- {
- Error_Handler();
- }
- BSP_LED_On(LD1);
- HAL_Delay(LED_BLINK_SLOW);
- BSP_LED_Off(LD1);
- HAL_Delay(LED_BLINK_SLOW);
- if(ubDmaTransferStatus == 1)
- {
- uhADCxConvertedData_VoltageGPIO_mVolt = __LL_ADC_CALC_DATA_TO_VOLTAGE(LL_ADC_VIN_RANGE_3V6, uhADCxConvertedData[1], LL_ADC_DS_DATA_WIDTH_12_BIT);
- hADCxConvertedData_Temperature_DegreeCelsius = __LL_ADC_CALC_TEMPERATURE( uhADCxConvertedData[0], LL_ADC_DS_DATA_WIDTH_12_BIT);
- LCD_ShowNum(66,10,hADCxConvertedData_Temperature_DegreeCelsius,3,YELLOW);
- LCD_ShowNum(66,30,uhADCxConvertedData_VoltageGPIO_mVolt,4,YELLOW);
- ubDmaTransferStatus = 0;
- tmp_index = 4*2;
- while(tmp_index != 0)
- {
- BSP_LED_Toggle(LD1);
- HAL_Delay(LED_BLINK_FAST);
- tmp_index--;
- }
- HAL_Delay(500);
- }
- }
- }
经程序的编译与下载,其检测效果如图1和图2所示,说明检测有效。
图1 连接与显示
图2 显示效果
在搭建外部NTC温度检测电路的情况下,需在主程序中添加相应的NTC温度变换处理程序,其具体的内容如下:
- LCD_ShowString(10,10,"Tempe1: ",YELLOW);
- LCD_ShowString(10,30,"Tempe2: ",YELLOW);
- u=uhADCxConvertedData_VoltageGPIO_mVolt = __LL_ADC_CALC_DATA_TO_VOLTAGE(LL_ADC_VIN_RANGE_3V6, uhADCxConvertedData[1], LL_ADC_DS_DATA_WIDTH_12_BIT);
- u=u*33/36;
- ...
- if((u<=1644) && (u>1268))
- V=10+(1644-u)/37;
- if((u<=1268) && (u>947))
- V=20+(1268-u)/32;
- if((u<=947) && (u>692))
- V=30+(947-u)/25;
- ...
- LCD_ShowNum(76,30,V,4,YELLOW);
这样,在图3和图4的情况下即可进行室温和升温的测试。
图3 室温检测
图4 触摸升温