2023-06-05 04:01 AM
/* MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/
#include "stm8s.h"
#include "stm8s_clk.h"
#include "stm8s_adc1.h"
void Clk_Config(void);
void ADC_Config(void);
void ADC_Read(uint16_t *MV);
main()
{
unsigned short MV;
Clk_Config();
ADC_Config();
while (1)
{
ADC_Read(&MV);
}
}
void Clk_Config()
{
CLK_DeInit();
CLK_HSECmd(DISABLE);
CLK_LSICmd(DISABLE);
CLK_HSICmd(ENABLE);
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);
CLK_ClockSwitchCmd(ENABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV2);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV4);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI,
DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, ENABLE);
}
void ADC_Config()
{
ADC1_DeInit();
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS,
ADC1_CHANNEL_5,
ADC1_PRESSEL_FCPU_D2,
ADC1_EXTTRIG_GPIO,
DISABLE,
ADC1_ALIGN_RIGHT,
ADC1_SCHMITTTRIG_CHANNEL5,
DISABLE);
ADC1_Cmd(ENABLE);
ADC1_ITConfig(ADC1_IT_EOCIE, ENABLE);
}
void ADC_Read(uint16_t *MV)
{
ADC1_StartConversion();
while (!ADC1_GetFlagStatus(ADC1_FLAG_EOC))
{
// Wait for conversion to complete
}
*MV= ADC1_GetConversionValue();
}