Cannot enter low power mode STM8L052C6
Hi,
I feel like I've been through the reference manual a million times, but I cant figure out how to get my project into low power mode.
I'm driving a LCD screen with a counter on it. I have two external switches to wake up count up and down. And I'm using timer4 to hopefully wake up periodically and do some work.
I need two low power modes, one with the LCD on and it just wakes up to handle a button press or a timer4 timeout.
The other low power mode will turn off the LCD and will only wake up on a button press. It needs to wake up and not reset because I don't want to lose count on the LCD.
To save power I'm running off LSI clock.
Code below. Any ideas would be greatly appreciated. I'm new to STM8
main()
{ CLK_CKDIVR = 0b0; //Div clock by 1 CLK_SWR = 0x02; //Sys clock is LSI (38kHz) CLK_SWCR |= 0x2; //Tell system to switch clocks to one defined in SWR while(CLK_SWCR & 1); CLK_ICKCR &= 0b11111110; //HSI Off CFG_GCR |= 0b10; //main context not restored after WFI/HALT lcd_init(); //Do this first or its really slow.PB_DDR = 0; //Set pins as Inputs. By default
//INPUTS PULLED UP PA_CR1 = 0x8C; PB_CR1 = 0xFF; PC_CR1 = 0xF7; PD_CR1 = 0x3C; PE_CR1 = 0xB6; PE_CR1 = 0x01; PB_CR2 = 0b110; //PB1 and PB2 interrupt enable EXTI_CR1 = 0b101000; //Port1 and 2 to falling edge only interrupt CLK_PCKENR1 |= 0x04; //Enable CLK to Timer4 TIM4_CR1 = 0b101; //Enable Timer4 TIM4_IER = 1; //Timer4 interrupt enable TIM4_PSCR = 1; //CLK/2(8) so CLK div 16while(CLK_SWCR & 1);
{_asm('rim\n');} //enable interrupts while (1){ if (readyToSleep == 1){ readyToSleep = 0; CLK_PCKENR1 = 0; //Disable clocks to perfs CLK_PCKENR2 = 0; CLK_PCKENR3 = 0; EXTI_SR1 = 0; //Clear interrupts EXTI_SR2 = 0; TIM4_SR1 = 0; {_asm('HALT\n');} } else{ {_asm('WFE\n');} {_asm('JRA next\n');} {_asm('next:\n');} //{_asm('WFI\n');} } }}#halt #sleep #wfe #stm8l052c6 #wfi #lcd #low-power #stm8
