mirror of
https://github.com/IcedRooibos/py32f0-template.git
synced 2025-10-29 08:52:04 -07:00
feat: ll example - adc read internal temperature
This commit is contained in:
parent
2d498d808b
commit
64a8c2a28f
104
Examples/LL/ADC/ADC_InternalTemperature/main.c
Normal file
104
Examples/LL/ADC/ADC_InternalTemperature/main.c
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
/**
|
||||||
|
* Demo of reading internal temperature sensor
|
||||||
|
* - Works on QFN32 packaging
|
||||||
|
* - Doesn't work on TSSOP20 packaging, always read 0xFFF
|
||||||
|
*/
|
||||||
|
#include "main.h"
|
||||||
|
#include "py32f0xx_bsp_clock.h"
|
||||||
|
#include "py32f0xx_bsp_printf.h"
|
||||||
|
|
||||||
|
#define VDDA_APPLI ((uint32_t)3300)
|
||||||
|
|
||||||
|
uint16_t convert_result = 0, temperature = 0;
|
||||||
|
|
||||||
|
static void APP_ADCConfig(void);
|
||||||
|
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// Set system clock to 48MHz
|
||||||
|
BSP_HSI_PLL_48MConfig();
|
||||||
|
// Initialize UART on PA2:TX PA3:RX
|
||||||
|
BSP_USART_Config(115200);
|
||||||
|
printf("PY32F0 Internal Temperature Sensor Demo\r\nClock: %ld\r\n", SystemCoreClock);
|
||||||
|
|
||||||
|
APP_ADCConfig();
|
||||||
|
// Start ADC regular conversion
|
||||||
|
LL_ADC_REG_StartConversion(ADC1);
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
if(LL_ADC_IsActiveFlag_EOS(ADC1))
|
||||||
|
{
|
||||||
|
LL_ADC_ClearFlag_EOS(ADC1);
|
||||||
|
// Read regular conversion data, 12-bit
|
||||||
|
convert_result = LL_ADC_REG_ReadConversionData12(ADC1);
|
||||||
|
// Convert it to temperature
|
||||||
|
temperature = __LL_ADC_CALC_TEMPERATURE(VDDA_APPLI, convert_result, LL_ADC_RESOLUTION_12B);
|
||||||
|
printf("Temperature:%d DR:%d \r\n", temperature, convert_result);
|
||||||
|
LL_mDelay(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void APP_ADCConfig(void)
|
||||||
|
{
|
||||||
|
__IO uint32_t wait_loop_index = 0;
|
||||||
|
|
||||||
|
LL_ADC_InitTypeDef ADC_Init;
|
||||||
|
LL_ADC_REG_InitTypeDef LL_ADC_REG_InitType;
|
||||||
|
|
||||||
|
LL_ADC_Reset(ADC1);
|
||||||
|
|
||||||
|
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_ADC1);
|
||||||
|
|
||||||
|
// Calibrate start
|
||||||
|
if (LL_ADC_IsEnabled(ADC1) == 0)
|
||||||
|
{
|
||||||
|
LL_ADC_StartCalibration(ADC1);
|
||||||
|
while (LL_ADC_IsCalibrationOnGoing(ADC1) != 0);
|
||||||
|
/* Delay 1ms(>= 4 ADC clocks) before re-enable ADC */
|
||||||
|
LL_mDelay(1);
|
||||||
|
}
|
||||||
|
// Calibrate end
|
||||||
|
|
||||||
|
ADC_Init.Clock = LL_ADC_CLOCK_SYNC_PCLK_DIV64;
|
||||||
|
ADC_Init.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
|
||||||
|
ADC_Init.LowPowerMode = LL_ADC_LP_MODE_NONE;
|
||||||
|
ADC_Init.Resolution = LL_ADC_RESOLUTION_12B;
|
||||||
|
LL_ADC_Init(ADC1, &ADC_Init);
|
||||||
|
LL_ADC_SetSamplingTimeCommonChannels(ADC1, LL_ADC_SAMPLINGTIME_239CYCLES_5);
|
||||||
|
|
||||||
|
// Regular ADC config
|
||||||
|
LL_ADC_REG_InitType.ContinuousMode = LL_ADC_REG_CONV_CONTINUOUS;
|
||||||
|
LL_ADC_REG_InitType.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
|
||||||
|
LL_ADC_REG_InitType.Overrun = LL_ADC_REG_OVR_DATA_OVERWRITTEN;
|
||||||
|
LL_ADC_REG_InitType.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
|
||||||
|
LL_ADC_REG_InitType.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
|
||||||
|
LL_ADC_REG_Init(ADC1, &LL_ADC_REG_InitType);
|
||||||
|
// Set common path and internal channel
|
||||||
|
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(ADC1), LL_ADC_PATH_INTERNAL_TEMPSENSOR);
|
||||||
|
// Delay to ensure temperature sensor becomes stable
|
||||||
|
wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US * (SystemCoreClock / (100000 * 2))) / 10);
|
||||||
|
while(wait_loop_index != 0)
|
||||||
|
{
|
||||||
|
wait_loop_index--;
|
||||||
|
}
|
||||||
|
// Select temperature sensor
|
||||||
|
LL_ADC_REG_SetSequencerChannels(ADC1, LL_ADC_CHANNEL_TEMPSENSOR);
|
||||||
|
// Enable ADC
|
||||||
|
LL_ADC_Enable(ADC1);
|
||||||
|
LL_mDelay(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void APP_ErrorHandler(void)
|
||||||
|
{
|
||||||
|
while (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef USE_FULL_ASSERT
|
||||||
|
void assert_failed(uint8_t *file, uint32_t line)
|
||||||
|
{
|
||||||
|
while (1);
|
||||||
|
}
|
||||||
|
#endif /* USE_FULL_ASSERT */
|
||||||
27
Examples/LL/ADC/ADC_InternalTemperature/main.h
Normal file
27
Examples/LL/ADC/ADC_InternalTemperature/main.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#ifndef __MAIN_H
|
||||||
|
#define __MAIN_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "py32f0xx_ll_adc.h"
|
||||||
|
#include "py32f0xx_ll_bus.h"
|
||||||
|
#include "py32f0xx_ll_cortex.h"
|
||||||
|
#include "py32f0xx_ll_dma.h"
|
||||||
|
#include "py32f0xx_ll_exti.h"
|
||||||
|
#include "py32f0xx_ll_gpio.h"
|
||||||
|
#include "py32f0xx_ll_pwr.h"
|
||||||
|
#include "py32f0xx_ll_rcc.h"
|
||||||
|
#include "py32f0xx_ll_system.h"
|
||||||
|
#include "py32f0xx_ll_tim.h"
|
||||||
|
#include "py32f0xx_ll_utils.h"
|
||||||
|
|
||||||
|
|
||||||
|
void APP_ErrorHandler(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __MAIN_H */
|
||||||
42
Examples/LL/ADC/ADC_InternalTemperature/py32f0xx_it.c
Normal file
42
Examples/LL/ADC/ADC_InternalTemperature/py32f0xx_it.c
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#include "main.h"
|
||||||
|
#include "py32f0xx_it.h"
|
||||||
|
|
||||||
|
extern void APP_TransferCompleteCallback(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief This function handles Non maskable interrupt.
|
||||||
|
*/
|
||||||
|
void NMI_Handler(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief This function handles Hard fault interrupt.
|
||||||
|
*/
|
||||||
|
void HardFault_Handler(void)
|
||||||
|
{
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief This function handles System service call via SWI instruction.
|
||||||
|
*/
|
||||||
|
void SVC_Handler(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief This function handles Pendable request for system service.
|
||||||
|
*/
|
||||||
|
void PendSV_Handler(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief This function handles System tick timer.
|
||||||
|
*/
|
||||||
|
void SysTick_Handler(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
18
Examples/LL/ADC/ADC_InternalTemperature/py32f0xx_it.h
Normal file
18
Examples/LL/ADC/ADC_InternalTemperature/py32f0xx_it.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#ifndef __PY32F0XX_IT_H
|
||||||
|
#define __PY32F0XX_IT_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void NMI_Handler(void);
|
||||||
|
void HardFault_Handler(void);
|
||||||
|
void SVC_Handler(void);
|
||||||
|
void PendSV_Handler(void);
|
||||||
|
void SysTick_Handler(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __PY32F0XX_IT_H */
|
||||||
Loading…
x
Reference in New Issue
Block a user