Executing code at diferent speeds, using one timer, and no extra RAM

cpictimer

I was wondering, will this actualy work? (when timer / prescaler set) It saves RAM by not using extra variables. Are the better ways of doing this?

// Microchip PIC10F222

while (1) {  //main loop
   while ((TMR0 ^ TMR0-1)& 4) {  // execute @ 1/4 speed of TMR0 (whenever TMR0 Bit 3 has just toggled)
   // high speed stuff
   }

   while ((TMR0 ^ TMR0-1)& 128) {   // execute @ 1/128 speed of TMR0
   // low speed stuff (toggle blinking led's) 
   }
}

Next idea:

I Don't like the idea of waisting processor time , or being dependent on exact timing

I'm not sure about the TMR0-2. I asume If TMR0=0 TMR0-2 will be 254.

// Microchip PIC10F222

While(1) { //main loop
    // Full speed 
    if (TMR0&1) {  //skip, eccept for TMR0 odd numbers.
       // 1/1 timer speed. (Not nececairily used)
       // this code may run in one pass with either one of the loops below. 
       if ((TMR0 ^ TMR0-2)& 64) {    
          // 1/32 timer speed,(state machines, one step each pass.)   
          // do not exceed TMR0 period
          }
       if ((TMR0==255) {     
          // 1/128 timer speed. (toggle blinking led's, wachdogg)
          // do not exceed TMR0 period
          }
    TMR0++; //TMR0 will be even, preventing passing trough this loop
            //until the timer increments itself, 
            //(Doubeling timer speed in the process)
    }
}

Best Answer

The PIC10F222 has no interrupts, only 2 levels of call stack, and very little memory. Techniques that are appropriate for more powerful chips may be inefficient or not work at all. To avoid 'wasting' cycles while waiting for a bit toggle you can inline code to do other things while you wait (eg. do high speed stuff while for waiting for low speed bit toggle).

To detect when a bit changes you need to wait until the bit is in one state, then wait again for it to change to the other state. The following code executes high speed stuff whenever TMR0 bit 1 changes from low to high, and executes low speed stuff whenever bit 6 changes from low to high. No RAM is used.

If all your 'stuff' completes within the current high speed time slot then it will be perfectly synchronized with the timer, with no wasted cycles. If it takes too long and the bit change is missed then it will have to wait for the next one. If some 'Stuff' is taking too long then you may be able to split it up into two or more parts, and do each part in a different time slot.

// Microchip PIC10F222
#include <pic.h>

main ()
{
   while (1) {  
      while (TMR0 & 64) {   // wait until bit 6 goes low. 
         while (TMR0 & 2) { // wait until bit 1 goes low 
         }
         while (!(TMR0 & 2)){ // wait until bit 1 goes high
         }
         // while waiting for bit 6, do high speed stuff
      }
      while (!(TMR0 & 64)){ // wait until bit 6 goes high
         while (TMR0 & 2) { // wait until bit 1 goes low
         }
         while (!(TMR0 & 2)){ // wait until bit 1 goes high
         }
         // while waiting for bit 6, do high speed stuff again
      }
      // do low speed stuff
   }
}