【GD32F310G-START】ADC obtains the on-chip temperature and displays it
[Copy link]
【GD32F310G-START】HELLO WORLD - GD32 MCU - Electronic Engineering World - Forum (eeworld.com.cn)
【GD32F310G-START】OLED HELLO EEWORLD——Hardware I2C - GD32 MCU - Electronic Engineering World-Forum
【GD32F310G-START】SPI driver ST7735 - GD32 MCU - Electronic Engineering World - Forum (eeworld.com.cn)
【GD32F310G-START】Hardware SPI driver for ST7735 - GD32 MCU - Electronic Engineering World - Forum (eeworld.com.cn)
ADC is one of the commonly used peripherals. Today, we will get the temperature value inside the chip according to the routine and display it on the LCD screen:
1. Initialize ADC:
/*!
\brief 初始化ADC
\param[in] none
\param[out] none
\retval none
*/
void my_adc_init(void)
{
/* 开启时ADC时钟 */
rcu_periph_clock_enable(RCU_ADC);
/* config ADC clock */
rcu_adc_clock_config(RCU_ADCCK_APB2_DIV6);
/* ADC channel length config */
adc_channel_length_config(ADC_INSERTED_CHANNEL, 2U);
/* ADC temperature sensor channel config */
adc_inserted_channel_config(0U, ADC_CHANNEL_16, ADC_SAMPLETIME_239POINT5);
/* ADC internal reference voltage channel config */
adc_inserted_channel_config(1U, ADC_CHANNEL_17, ADC_SAMPLETIME_239POINT5);
/* ADC trigger config */
adc_external_trigger_source_config(ADC_INSERTED_CHANNEL, ADC_EXTTRIG_INSERTED_NONE);
/* ADC data alignment config */
adc_data_alignment_config(ADC_DATAALIGN_RIGHT);
/* ADC SCAN function enable */
adc_special_function_config(ADC_SCAN_MODE, ENABLE);
/* ADC temperature and Vrefint enable */
adc_tempsensor_vrefint_enable();
adc_external_trigger_config(ADC_INSERTED_CHANNEL, ENABLE);
/* enable ADC interface */
adc_enable();
delay_1ms(1U);
/* ADC calibration and reset calibration */
adc_calibration_enable();
}
2. Get temperature:
float get_gd32f310g_temper(void)
{
float temperature;
adc_software_trigger_enable(ADC_INSERTED_CHANNEL);
/* value convert */
temperature = (1.45f - ADC_IDATA0 * 3.3f / 4096) * 1000 / 4.3f + 25;
return temperature;
}
3. Get the temperature in the main loop and scan it on the LCD screen:
main.c
#include "gd32f3x0.h"
#include "gd32f310g_eval.h"
#include "my_i2c.h"
#include "systick.h"
#include "ssd1306.h"
#include "lcd.h"
#include "lcd_init.h"
#include "adc.h"
uint8_t i2c_transmitter[16];
/*!
\brief main function
\param[in] none
\param[out none
\retval none
*/
int main(void)
{
float temper_val;
uint8_t str_temper[10];
systick_config();
// my_i2c0_init();
// oled_display_test();
LCD_Init();//LCD初始化
LCD_Fill(0,0,LCD_W,LCD_H,BLUE);
LCD_ShowString(12,0,"GD32F320G",WHITE,BLUE,32,0);
LCD_ShowString(18,36,"EEWORDL",WHITE,BLUE,32,0);
LCD_ShowString(16,72,"2022-5-7",BLACK,BLUE,32,0);
LCD_ShowString(12,100,"gd32f310g_adc_demo",RED,BLUE,16,0);
my_adc_init();
/* infinite loop */
while(1)
{
temper_val = get_gd32f310g_temper();
sprintf(str_temper,"T:%.2f ",temper_val);
LCD_ShowString(18,36,str_temper,WHITE,BLUE,32,0);
delay_1ms(500);
}
}
Display effect:
【Summary】This can be easily accomplished through the ADC routine, but the on-chip temperature only reflects the temperature of the MCU, which is not proportional to the temperature of the external environment, so an external sensor is required to obtain the temperature.
|