Skip to content

Instantly share code, notes, and snippets.

@spe-ciellt
Created May 27, 2015 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spe-ciellt/38977de1fba5338d47bf to your computer and use it in GitHub Desktop.
Save spe-ciellt/38977de1fba5338d47bf to your computer and use it in GitHub Desktop.
How to change from Timer1 (16-bit) to Timer0 (8-bit) in AVR Arduino port and set timer tick to 1 ms.
diff --git a/ports/avr/atomport.c b/ports/avr/atomport.c
index 9ae5555..4107a1d 100644
--- a/ports/avr/atomport.c
+++ b/ports/avr/atomport.c
@@ -277,21 +277,21 @@ void archThreadContextInit (ATOM_TCB *tcb_ptr, void *stack_top, void (*entry_poi
*/
void avrInitSystemTickTimer ( void )
{
- /* Set timer 1 compare match value for configured system tick,
- * with a prescaler of 256. We will get a compare match 1A
+ /* Set timer 0 compare match value for configured system tick,
+ * with a prescaler of 64. We will get a compare match 1A
* interrupt on every system tick, in which we must call the
* OS's system tick handler. */
- OCR1A = (AVR_CPU_HZ / 256 / SYSTEM_TICKS_PER_SEC);
+ OCR0A = (AVR_CPU_HZ / 64 / SYSTEM_TICKS_PER_SEC);
/* Enable compare match 1A interrupt */
#ifdef TIMSK
- TIMSK = _BV(OCIE1A);
+ TIMSK = _BV(OCIE0A);
#else
- TIMSK1 = _BV(OCIE1A);
+ TIMSK0 = _BV(OCIE0A);
#endif
- /* Set prescaler 256 */
- TCCR1B = _BV(CS12) | _BV(WGM12);
+ /* Set prescaler 64 */
+ TCCR0B = _BV(CS01) |_BV(CS00) | _BV(WGM02);
}
@@ -324,7 +324,7 @@ void avrInitSystemTickTimer ( void )
*
* @return None
*/
-ISR (TIMER1_COMPA_vect)
+ISR (TIMER0_COMPA_vect)
{
/* Call the interrupt entry routine */
atomIntEnter();
diff --git a/ports/avr/atomport.h b/ports/avr/atomport.h
index 875bd15..12ff3ae 100644
--- a/ports/avr/atomport.h
+++ b/ports/avr/atomport.h
@@ -40,8 +40,8 @@
/* Definition of NULL is available from stddef.h on this platform */
#include <stddef.h>
-/* Required number of system ticks per second (normally 100 for 10ms tick) */
-#define SYSTEM_TICKS_PER_SEC 100
+/* Required number of system ticks per second (normally 1000 for 1ms tick) */
+#define SYSTEM_TICKS_PER_SEC 1000
/* Size of each stack entry / stack alignment size (8 bits on AVR) */
#define STACK_ALIGN_SIZE sizeof(uint8_t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment