AnsweredAssumed Answered

Cannot enter low power mode STM8L052C6

Question asked by Billy Bob on Apr 18, 2018
Latest reply on May 9, 2018 by Billy Bob



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


  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

  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 16

  while(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("JRA next\n");}