Commits

Anonymous committed c3a5bfd

Added timers example.

Updated fx2ints.h to include macros to enable and clear
the timer interrupts.

Comments (0)

Files changed (6)

 
 serial:
  A simple terminal echo firmware.  Echos whatever you type back to the serial terminal.
+
+timers:
+ Demonstrates setting up timers t0, t1, and t2 to be 16 bit counters and also demonstrates
+ installing interrupts to process the timer overlow.
+

examples/lights/lights.c

  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  **/
 
-#define ALLOCATE_EXTERN
 #include <fx2regs.h>
 
 #include <lights.h>

examples/timers/.gitignore

+lights

examples/timers/Makefile

+FX2LIBDIR=../..
+BASENAME = timers
+SOURCES=timers.c
+
+include $(FX2LIBDIR)/lib/fx2.mk
+
+DSCR_AREA=
+INT2JT=

examples/timers/timers.c

+/**
+ * Copyright (C) 2010 Ubixum, Inc. 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ **/
+
+#include <fx2regs.h>
+#include <fx2macros.h>
+#include <fx2ints.h>
+#include <lights.h>
+
+volatile bit d2;
+volatile bit d3;
+volatile bit d4;
+volatile bit d5;
+
+void timer0_isr() interrupt TF0_ISR {
+ d2 = !d2;
+ if (d2) { d2on(); } else { d2off(); }
+}
+
+void timer1_isr() interrupt TF1_ISR {
+ d3 = !d3;
+ if (d3) { d3on(); } else { d3off(); }
+}
+
+void timer2_isr() interrupt TF2_ISR {
+ d4 = !d4;
+ if (d4) { d4on(); } else { d4off(); }
+
+ CLEAR_TIMER2(); // This one is not done automatically!
+}
+
+
+void main(void)
+{
+    WORD counter=0;
+
+    SETCPUFREQ(CLK_12M);
+
+    // enable timer 0 and timer 1 to be 16 bit counters
+    TMOD = 0x11; 
+
+    // enable timer 2 to also be a 16 bit counter
+    T2CON = 0;
+    RCAP2L = 0; // reload values for t2
+    RCAP2H = 0; 
+
+    EA=1; // enable interrupts
+    ENABLE_TIMER0();
+    ENABLE_TIMER1();
+    ENABLE_TIMER2();
+    TR0=1; // start t0
+    TR1=1; // start t1 
+    TR2=1; // start t2
+
+    // and blink d5
+    while (TRUE) {
+        ++counter;
+        if (!counter) {
+            d5 = !d5;
+            if (d5) { d5off(); } else { d5on(); }
+        }
+    }
+}
+
+

include/fx2ints.h

 
 
 /**
+ * \brief Enable the timer 0 interrupt.
+ *
+ * There is not CLEAR_TIMER0 because the timer interrupt flag 
+ * is automatically cleared when the isr is called.
+ **/
+#define ENABLE_TIMER0() ET0=1
+
+/**
+ * \brief Enable timer 1 interrupt
+ * There is no CLEAR_TIMER1 because the timer interrupt flag
+ * is automatically cleared when the isr is called.
+ **/
+#define ENABLE_TIMER1() ET1=1
+
+
+/**
+ * \brief Enable timer 2 interrupt
+ *
+ * This is the same interrupt whether timer 2 overflowed or 
+ * for the external EXF2 flag.
+ **/
+#define ENABLE_TIMER2() ET2=1
+/**
+ *  \brief Clear timer 2 interrupt
+ *
+ *  Clears both the TF2 AND EXF2 flag
+ **/
+#define CLEAR_TIMER2() TF2=0;EXF2=0;
+
+/**
  * \brief Enable the Resume Interrupt.  Requires EA=1 separately.
  **/
 #define ENABLE_RESUME() ERESI = 1