FreeRTOS is a realtime kernel for many small CPU's.

Demo for ATmega168

New Demo project, including a the command line interpreter from Peter Fleury (be carefull with the license):

I am developing this code for my theses with the title:
"Optimization of a security related stepper motor control"

An example project for ATmega168 can be downloaded here:

It expects:

That's it.

Look into main.c, there are two #defines in line 79 and 80. Chose one of them, because not all tests can run at the same time.

With 1 kBytes of RAM I was able to run 5 Tasks + Idle Task or 4 Tasks + many CoRoutines.

I tested this program with 3,3 Volt power supply.

You might have to adapt the OSCAL value in line 160 to adjust the RC oscillator to 8 Mhz +- 2 %, to avoid problems with serial communication.

(It is set to 38 kBaud.)

Reducing the power to a minimum


  1. use powersave mode most of the time
  2. use Timer2 with 32 kHz clock quarz, to wakeup the cpu every 1/512 sec
  3. use internal 8 Mhz oscillator, when active
  4. for RS232 communication, don't use powersave mode, but idle mode

To reduce the power supply current to about 200 µA, something similar to the following code can be used:

I use PC3 to measure the time when IRQ's are disabled, and PC2 to see when the CPU is active.

Don't forget to enable both hooks in FreeRTOSconfig.h.

unsigned portBASE_TYPE cmd_bSleep;
unsigned portBASE_TYPE cmd_bGotoSleep;

  // at the beginning of main()
  // don't sleep for 100 tick counts
  // use powersave mode; can be disabled for debugging
  // check the datasheet, which peripherals you need and which can be disabled
  PRR = 0xAD;

void vApplicationIdleHook( void );
void vApplicationIdleHook( void )
   unsigned portCHAR ucQueuedBytes;     
   static unsigned portCHAR ucLastQueuedBytes = 0;      
   // put cpu into sleep state
   // set IRQ-Disabled output
   PORTC |= (1 << PC3);
   // start sleep timer for two ticks when queue reaches empty state 
   if ((ucQueuedBytes==0) && (ucLastQueuedBytes > 0)
        && (cmd_bGotoSleep < 2)){
   // any condition can be testet here atomically
   if ((cmd_bSleep > 0) && (ucQueuedBytes==0)
      && (cmd_bGotoSleep==0)) {
      // enter power down mode
   } else {
          // enter idle mode
   // clear SLEEPQ Ausgang
   PORTC &= ~(1 << PC2);
   // clear IRQ-Disabled output
   PORTC &= ~(1 << PC3);
   // set SLEEPQ output
   PORTC |= (1 << PC2); 
   // end of sleep

// this interrupt is triggert, when the RX-LINE changes,
// just to wakeup the processor

void vApplicationTickHook(void);
void vApplicationTickHook(void){
        if (cmd_bGotoSleep) cmd_bGotoSleep--;

License: Gnu Public License with exception (same as FreeRTOS)

Back to AVR-GCC

KielTech: FreeRTOS (last edited 2008-06-07 15:13:51 by localhost)