diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/test-timers.cpp | 136 |
1 files changed, 133 insertions, 3 deletions
diff --git a/examples/test-timers.cpp b/examples/test-timers.cpp index 374b903..9b1efc6 100644 --- a/examples/test-timers.cpp +++ b/examples/test-timers.cpp @@ -11,6 +11,9 @@ void handler2(void); void handler3(void); void handler4(void); +void handler3b(void); +void handler4b(void); + int toggle = 0; int t; @@ -34,7 +37,7 @@ void setup() /* Set up the LED to blink */ pinMode(LED_PIN, OUTPUT); - pinMode(38, INPUT); + pinMode(38, INPUT_PULLUP); /* Send a message out USART2 */ //SerialUSB.begin(9600); @@ -45,15 +48,133 @@ void setup() Timers[t].setChannel3Mode(TIMER_OUTPUTCOMPARE); Timers[t].setChannel4Mode(TIMER_OUTPUTCOMPARE); } + + // Wait for user to attach... + delay(2000); } void loop() { + SerialUSB.println("-----------------------------------------------------"); + SerialUSB.println("Testing Pause/Resume; button roughly controls Timer4"); + count3 = 0; + count4 = 0; + Timer3.setChannel1Mode(TIMER_OUTPUTCOMPARE); + Timer4.setChannel1Mode(TIMER_OUTPUTCOMPARE); + Timer3.pause(); + Timer4.pause(); + Timer3.setCount(0); + Timer4.setCount(0); + Timer3.setOverflow(30000); + Timer4.setOverflow(30000); + Timer3.setCompare1(1000); + Timer4.setCompare1(1000); + Timer3.attachCompare1Interrupt(handler3b); + Timer4.attachCompare1Interrupt(handler4b); + Timer3.resume(); + Timer4.resume(); + SerialUSB.println("~4 seconds..."); + for(int i = 0; i<4000; i++) { + if(digitalRead(38)) { + Timer4.pause(); + } else { + Timer4.resume(); + } + delay(1); + } + Timer3.setChannel1Mode(TIMER_DISABLED); + Timer4.setChannel1Mode(TIMER_DISABLED); + SerialUSB.print("Count3: "); SerialUSB.println(count3); + SerialUSB.print("Count4: "); SerialUSB.println(count4); + + SerialUSB.println("-----------------------------------------------------"); + SerialUSB.println("Testing setPeriod"); + Timer4.setChannel1Mode(TIMER_OUTPUTCOMPARE); + Timer4.setCompare1(1); + Timer4.setPeriod(10); + Timer4.pause(); + Timer4.setCount(0); + Timer4.attachCompare1Interrupt(handler4b); + SerialUSB.println("Period 10ms, wait 2 seconds..."); + count4 = 0; + Timer4.resume(); + delay(2000); + Timer4.pause(); + Timer4.setChannel1Mode(TIMER_DISABLED); + SerialUSB.print("Count4: "); SerialUSB.println(count4); + SerialUSB.println("(should be around 2sec/10ms = 200000)"); + Timer4.setChannel1Mode(TIMER_OUTPUTCOMPARE); + Timer4.setCompare1(1); + Timer4.pause(); + Timer4.setPeriod(30000); + Timer4.setCount(0); + Timer4.attachCompare1Interrupt(handler4b); + SerialUSB.println("Period 30000ms, wait 2 seconds..."); + count4 = 0; + Timer4.resume(); + delay(2000); + Timer4.pause(); + Timer4.setChannel1Mode(TIMER_DISABLED); + SerialUSB.print("Count4: "); SerialUSB.println(count4); + SerialUSB.println("(should be around 2sec/30000ms ~ 67)"); + + Timer4.setChannel1Mode(TIMER_OUTPUTCOMPARE); + Timer4.setPeriod(300000); + Timer4.setCompare1(1); + Timer4.pause(); + Timer4.setCount(0); + Timer4.attachCompare1Interrupt(handler4b); + SerialUSB.println("Period 300000ms, wait 2 seconds..."); + count4 = 0; + Timer4.resume(); + delay(2000); + Timer4.pause(); + Timer4.setChannel1Mode(TIMER_DISABLED); + SerialUSB.print("Count4: "); SerialUSB.println(count4); + SerialUSB.println("(should be around 2sec/300000ms ~ 6.7)"); + + Timer4.setChannel1Mode(TIMER_OUTPUTCOMPARE); + Timer4.setPrescaleFactor(33); + Timer4.setOverflow(65454); + Timer4.pause(); + Timer4.setCount(0); + Timer4.setCompare1(1); + Timer4.attachCompare1Interrupt(handler4b); + SerialUSB.println("Period 30000ms, wait 2 seconds..."); + count4 = 0; + Timer4.resume(); + delay(2000); + Timer4.pause(); + Timer4.setChannel1Mode(TIMER_DISABLED); + SerialUSB.print("Count4: "); SerialUSB.println(count4); + SerialUSB.println("(should be around 2sec/30000ms ~ 67)"); + + Timer4.setChannel1Mode(TIMER_OUTPUTCOMPARE); + Timer4.setCompare1(1); + Timer4.setPeriod(30000); + Timer4.pause(); + Timer4.setCount(0); + Timer4.attachCompare1Interrupt(handler4b); + SerialUSB.println("Period 30000ms, wait 2 seconds..."); + count4 = 0; + Timer4.resume(); + delay(2000); + Timer4.pause(); + Timer4.setChannel1Mode(TIMER_DISABLED); + SerialUSB.print("Count4: "); SerialUSB.println(count4); + SerialUSB.println("(should be around 2sec/30000ms ~ 67)"); + for(t=0; t<4; t++) { toggle ^= 1; digitalWrite(LED_PIN, toggle); - delay(1000); + delay(100); SerialUSB.println("-----------------------------------------------------"); SerialUSB.print("Testing Timer "); SerialUSB.println(t+1); count1 = count2 = count3 = count4 = 0; + Timers[t].setOverflow(0xFFFF); + Timers[t].setPrescaleFactor(1); + Timers[t].setCompare1(65535); + Timers[t].setCompare2(65535); + Timers[t].setCompare3(65535); + Timers[t].setCompare4(65535); Timers[t].setChannel1Mode(TIMER_OUTPUTCOMPARE); Timers[t].setChannel2Mode(TIMER_OUTPUTCOMPARE); Timers[t].setChannel3Mode(TIMER_OUTPUTCOMPARE); @@ -62,7 +183,8 @@ void loop() { Timers[t].attachCompare2Interrupt(handler2); Timers[t].attachCompare3Interrupt(handler3); Timers[t].attachCompare4Interrupt(handler4); - delay(5000); + Timers[t].resume(); + delay(3000); Timers[t].setChannel1Mode(TIMER_DISABLED); Timers[t].setChannel2Mode(TIMER_DISABLED); Timers[t].setChannel3Mode(TIMER_DISABLED); @@ -72,6 +194,7 @@ void loop() { SerialUSB.print("Count3: "); SerialUSB.println(count3); SerialUSB.print("Count4: "); SerialUSB.println(count4); } + } void handler1(void) { @@ -95,6 +218,13 @@ void handler4(void) { count4++; } +void handler3b(void) { + count3++; +} +void handler4b(void) { + count4++; +} + int main(void) { init(); |