Hi,
We use stm32f103c8t6 microcontroller. I need a output for Analog watchdog,adc read continuously background. I access analog value to activate sleep mode and wake up mode. I access the POT to Low threshold(0<100) to activate sleep mode and access a high threshold (>700) to activate MCU wake up mode. But I need to ADC still read for these two conditions. So I'm already try this method to develop my code.But my output was not clear to me. I attach my code and I made a mistake pls correct me.
#include "main.h"
/* USER CODE BEGIN PV */
volatile uint8_t watchDogTrig=0;
uint16_t adc_data;
int i=0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void adc_watchDog_Init(uint16_t low_threshold, uint16_t high_threshold)
{
/*Enable clock access to GPIOA*/
RCC->APB2ENR|=RCC_APB2ENR_IOPAEN;
RCC->CFGR |= RCC_CFGR_ADCPRE_DIV6;
// GPIOA->CRL= 0x00000000; ///GPIO_MODER_MODE0; /*Set PA0 as analog mode*/
GPIOA->CRL |= GPIO_CRL_CNF0_1;
GPIOA->CRL &= ~(GPIO_CRL_CNF0_0);
ADC1->SMPR2 |= ADC_SMPR2_SMP0 | ADC_SMPR2_SMP1 | ADC_SMPR2_SMP2;
ADC1->SQR1 &= ~ ADC_SQR1_L;
ADC1->SQR3 &= ~ADC_SQR3_SQ1_0;
RCC->APB2ENR|=RCC_APB2ENR_ADC1EN; /*Enable clock access to ADC1*/
ADC1->CR1|= ADC_CR1_EOCIE; //Interrupt enable for EOC
ADC1->CR2|=ADC_CR2_CONT; /*Enable continuous conversion*/
ADC1->CR1|=ADC_CR1_AWDSGL; /*Enable watch dog on single channel*/
ADC1->CR1|=ADC_CR1_AWDIE; /*Enable watch dog interrupt*/
ADC1->LTR=0; /*Set the low threshold*/
ADC1->LTR=low_threshold;
ADC1->HTR=0; /*Set the high threshold*/
ADC1->HTR=high_threshold;
ADC1->CR1&=~(0x1F<<ADC_CR1_AWDCH_0); /*Set watch dog channel channel to be channel 0*/
ADC1->CR1|=ADC_CR1_AWDEN; /*Enable analog watchdog on regular channel*/
ADC1->CR2|=ADC_CR2_ADON; /*Enable the ADC module*/
NVIC_EnableIRQ(ADC1_2_IRQn); /*Enable ADC1 interrupt in NVIC*/
// ADC1->CR2|=ADC_CR2_SWSTART; /*Start the conversion*/
ADC1->CR2|=ADC_CR2_ADON; /*Enable the ADC module*/
}
/* USER CODE END 0 */
void sleep_mode(void)
{
GPIOC->ODR |= 1<<13;
HAL_Delay(5000);
HAL_SuspendTick();
GPIOC->BRR = 1<<13; // Just to indicate sleep mode is activated
HAL_PWR_EnableSleepOnExit();
// Enter the sleep MODE NOW....
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFE); // interrupt for wake up
}
void sleep_on_wake(void)
{
HAL_ResumeTick();
HAL_PWR_DisableSleepOnExit();
GPIOC->BSRR |= 1<<13;
HAL_Delay(150);
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
adc_watchDog_Init(100,4000);
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
GPIOC->CRH = 0x00300000;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if (watchDogTrig == 1)
{
watchDogTrig = 0;
if (adc_data <100)
{
i=1;
}
if (adc_data > 100)
{
i=0;
}
}
if(i==1)
{
sleep_mode();
}
if(i==0)
{
sleep_on_wake();
}
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void ADC1_2_IRQHandler(void)
{
if((ADC1->SR & ADC_SR_EOC) == ADC_SR_EOC)
{
adc_data = ADC1->DR;
/* Start conversion of regular Channels */
ADC1->CR2|= ADC_CR2_SWSTART;
}
if((ADC1->SR & ADC_SR_AWD) != 0)
{
watchDogTrig=1;
ADC1->SR &=~ADC_SR_AWD;
}
}