77#include "gpio.h"
88#include "timers.h"
99
10- #define PIN_LED 20
10+ #define PIN_LED 28
1111#define my_timer TIMER0
1212
1313void setup_led ()
@@ -19,40 +19,66 @@ void setup_led()
1919void setup_timer ()
2020{
2121 // reset timer
22- TIMER_TASK_STOP (my_timer ) = 1 ;
22+ TIMER_TASK_STOP (my_timer ) = 1 ;
2323 TIMER_TASK_CLEAR (my_timer ) = 1 ;
2424
2525 // configure 16MHz crystal frequency as clock source
2626 CLOCK_XTALFREQ = 0xFF ;
2727
2828 // start high frequency clock
2929 // (is that necessary?)
30- CLOCK_HFCLKSTAT = 1 ;
30+ CLOCK_TASK_HFCLKSTART = 1 ;
31+ while (!CLOCK_EVENT_HFCLKSTARTED )
32+ asm("nop" );
3133
3234 // configure timer frequency
3335 TIMER_PRESCALER (my_timer ) = 9 ;
3436
3537 // configure timer mode
36- TIMER_MODE (my_timer ) = TIMER_MODE_TIMER ;
37- TIMER_BITMODE (my_timer ) = TIMER_BITMODE_24BIT ;
38+ // TIMER_MODE(my_timer) = TIMER_MODE_TIMER;
39+ // TIMER_BITMODE(my_timer) = TIMER_BITMODE_32BIT ;
3840
3941 // configure timer interrupt
40- TIMER_CC (my_timer )[0 ] = 0 ;
41- TIMER_INTENSET (my_timer ) = TIMER_INTERRUPT_UPON_COMPARE (0 );
42+ TIMER_CC (my_timer )[0 ] = 0xffffffff ;
43+ TIMER_INTENSET (my_timer ) | = TIMER_INTERRUPT_UPON_COMPARE (0 );
4244 interrupt_enable (TIMER0_INTERRUPT );
4345
4446 // configure shortcut
47+ /*
4548 TIMER_SHORTCUTS(my_timer) =
4649 TIMER_SHORTCUT_COMPARE_CLEAR(0)
4750 | TIMER_SHORTCUT_COMPARE_STOP(0);
51+ */
4852}
4953
54+ bool led_on = false;
55+ uint32_t counter = 0 ;
56+
5057void TIMER0_Handler ()
5158{
52- // XOR with the mask for the chosen LED pin to toggle corresponding bit
53- GPIO_OUT = GPIO_OUT ^ (1 << PIN_LED );
59+ TIMER_TASK_STOP (my_timer ) = 1 ;
60+ TIMER_TASK_CLEAR (my_timer ) = 1 ;
61+ TIMER_EVENT_COMPARE (my_timer )[0 ] = 0 ;
62+
63+ counter ++ ;
64+ if (counter >= 50000 )
65+ {
66+ counter = 0 ;
67+
68+ if (led_on )
69+ {
70+ GPIO_OUT &= ~(1 << PIN_LED );
71+ led_on = false;
72+ }
73+ else
74+ {
75+ GPIO_OUT |= 1 << PIN_LED ;
76+ led_on = true;
77+ }
78+ }
5479
5580 // restart timer
81+ // TIMER_CC(my_timer)[0] = 0x10000;
5682 TIMER_TASK_START (my_timer ) = 1 ;
5783}
5884
0 commit comments