Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 27, 2024, 06:01:05 06:01


Login with username, password and session length


Pages: [1]
Print
Author Topic: Prescaler reassignment TMR0/WDT  (Read 4981 times)
0 Members and 1 Guest are viewing this topic.
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2420

Thank You
-Given: 862
-Receive: 678


Top Topic Starter


« on: March 20, 2013, 11:52:52 23:52 »

Trying to think loud here, I am using PIC16F886 and trying to take it into sleep for about 60 seconds. I have implemented RC5 using RB0 and TMR0 interrupts. I read the datasheet of this PIC, and it seems I had to follow a specific sequence when I change the prescaler assignment between WDT and TMR0. Any way, I did what the datasheet says, and I still get random sleep periods.

The real problem behind this behavior is that we have an interrupt at RB0 that causes random wakeups. This is something I can’t control, because if I disable RB0 interrupt, I will no longer be able to receive RC5 as you know.

This is how things work:

There are 2 prescalers: one controlled from OPTION_REG and the other is for WDT. The sleep period is calculated as follows:

Period = (1/31000) x WDT_PRESCALER x OPTION_REG_PRESCALER

The problem occurs when RB0 interrupt triggers, and the OPTION_REG_PRESCALER is assigned to TMR0, RB0 interrupt can trigger on random light spikes other than RC5, it depends on the light inside the room, and the electricity. It is not possible to WDT value before OPTION_REG_PRESCALER is assigned from WDT to TMR0, and this is where I am stuck. Hence, the actual sleep period depends on the random RB0 trigger, because once OPTION_REG_PRESCALER is re-assigned to WDT, its value will be 0, when you look at how it is calculated, then this is a real problem. Imagine that 40 seconds elapsed and RB0 triggers by a non RC-5 signal, when PIC goes to sleep again, the sleep period will repeat again, adding 50~60 seconds to the 40 seconds, this is a real disaster. This is how I am putting PIC into sleep:

Code:
    // WDT enable, SLEEP enable
    if (sflag && !RC5_Code.valid) {
      GIE = 0;
      MOTOR_PLUS_OFF();
      MOTOR_MINUS_OFF();
      LED_ON();
      // Sequence p.74
      CLRWDT();
      TMR0 = 0;
      PSA = 1;
      CLRWDT();
      OPTION_REG &= 0xF8;
      OPTION_REG |= 0x05;
      WDTCON = 0x10;
      SWDTEN = 1;
      GIE = 1;
      SLEEP();
      SWDTEN = 0;
    }

In RC5 code, this is how I reassign OPTION_REG_PRESCALER to TMR0, just the important snippet:
Code:
void interrupt isr_vector(void) {
  if (INTF) {
    switch (Decoder.State) {
      case START_S:
       CLRWDT();
       OPTION_REG &= 0xF0;
       OPTION_REG |= 0x05;
      
        TMR0 = 0;
        T0IF = 0;
        T0IE = 1;
        Decoder.bit_counter = 0;
        Decoder.rc_data = 0;
        Decoder.State = CALIBRATE_S;
        sflag = 0;
        break;

I spent more than 8 hours yesterday troubleshooting to understand what happens inside the PIC, the problem is inherent. I must first avoid using OPTION_REG_PRESCALER for the sleep function. Second, I must figure out a way to keep the actual sleep period regardless of how many times RB0 interrupt is triggered regardless of the cause, whether it is RC5 or a light spike… I tried using a software counter along with WDT_PRESCALER, but when I implemented the new code, I noticed that RC5 reception became very slow.

Suggestions are welcome, I am have been stuck in this for the last 2 (free time) days. I am reluctant to using Timer1 for RC5, not even timer2.
Logged
Ichan
Hero Member
*****
Offline Offline

Posts: 833

Thank You
-Given: 312
-Receive: 392



WWW
« Reply #1 on: March 24, 2013, 05:37:42 17:37 »

Metal, i hasn't yet read the code - but an analogy come crossing to my mind.

Let say i want to sleep in my bed room, but there are randomly people knocking hard the door so i can not sleep as planned. I will then put a banner on the door "DON'T DISTURB!". But in my case, possibly there will another problem - the sleep period can then become too long...  Grin

Will try to look at your code later.

-ichan
Logged

There is Gray, not only Black or White.
Ichan
Hero Member
*****
Offline Offline

Posts: 833

Thank You
-Given: 312
-Receive: 392



WWW
« Reply #2 on: March 25, 2013, 12:00:27 00:00 »

Metal, my cheap suggestion is to use Timer2 instead of Timer0 for the RC5 decoding.

Difficult to go deeper without seeing the RC5 code, do you use modulated ir receiver module? I wonder why it is frequently triggering the interrupt, many 36khz light source around perhaps?

-ichan
Logged

There is Gray, not only Black or White.
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC