1 | //-----------------------------------------------------------------------------
|
2 | // Includes
|
3 | //-----------------------------------------------------------------------------
|
4 | #include <c8051f310.h> // SFR declarations
|
5 |
|
6 | unsigned long ms;
|
7 |
|
8 | //-----------------------------------------------------------------------------
|
9 | // 16-bit SFR Definitions for 'F33x
|
10 | //-----------------------------------------------------------------------------
|
11 |
|
12 | sfr16 TMR2RL = 0xca; // Timer2 reload value
|
13 | sfr16 TMR2 = 0xcc; // Timer2 counter
|
14 | //-----------------------------------------------------------------------------
|
15 | // Global CONSTANTS
|
16 | //-----------------------------------------------------------------------------
|
17 |
|
18 | #define SYSCLK 24500000 // SYSCLK frequency in Hz
|
19 |
|
20 | sbit LED = P0^3; // LED='1' means ON
|
21 |
|
22 | //-----------------------------------------------------------------------------
|
23 | // Function PROTOTYPES
|
24 | //-----------------------------------------------------------------------------
|
25 | void SYSCLK_Init (void);
|
26 | void PORT_Init (void);
|
27 | void Timer2_Init (void);
|
28 | void Timer2_ISR (void);
|
29 | void ADC0_Init (void);
|
30 | void ADC0_ISR (void);
|
31 |
|
32 | //-----------------------------------------------------------------------------
|
33 | // MAIN Routine
|
34 | //-----------------------------------------------------------------------------
|
35 | void main (void) {
|
36 |
|
37 | unsigned long wait;
|
38 |
|
39 | ms=0;
|
40 | wait=0;
|
41 |
|
42 | // disable watchdog timer
|
43 | PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
|
44 | // enable)
|
45 |
|
46 | SYSCLK_Init (); // Initialize system clock to
|
47 | // 24.5MHz
|
48 | PORT_Init (); // Initialize crossbar and GPIO
|
49 | Timer2_Init (); // Init Timer2
|
50 |
|
51 | EA = 1; // enable global interrupts
|
52 | while (1) { // spin forever
|
53 | if( (ms-wait) < 1000 ) {
|
54 | LED=~LED;
|
55 | wait=ms;
|
56 | }
|
57 |
|
58 | }
|
59 | }
|
60 |
|
61 | //-----------------------------------------------------------------------------
|
62 | // SYSCLK_Init
|
63 | //-----------------------------------------------------------------------------
|
64 | //
|
65 | // This routine initializes the system clock to use the internal 24.5MHz
|
66 | // oscillator as its clock source. Also enables missing clock detector reset.
|
67 | //
|
68 | void SYSCLK_Init (void)
|
69 | {
|
70 |
|
71 | OSCICN = 0xC3; // configure internal oscillator for
|
72 |
|
73 | RSTSRC = 0x04; // enable missing clock detector
|
74 | }
|
75 |
|
76 | //-----------------------------------------------------------------------------
|
77 | // PORT_Init
|
78 | //-----------------------------------------------------------------------------
|
79 | //
|
80 | // Configure the Crossbar and GPIO ports.
|
81 | // P0.3 - LED (push-pull)
|
82 | //
|
83 | void PORT_Init (void)
|
84 | {
|
85 |
|
86 | XBR0 = 0x00; // no digital peripherals selected
|
87 | XBR1 = 0x40; // Enable crossbar and weak pull-ups
|
88 | P0MDOUT |= 0x08; // enable LED as a push-pull output
|
89 | }
|
90 |
|
91 | //-----------------------------------------------------------------------------
|
92 | // Timer2_Init
|
93 | //-----------------------------------------------------------------------------
|
94 | //
|
95 | // Configure Timer2 to 16-bit auto-reload and generate an interrupt
|
96 | //
|
97 | void Timer2_Init (void)
|
98 | {
|
99 | TMR2CN = 0x00; // Stop Timer2; Clear TF2;
|
100 | // use SYSCLK/12 as timebase
|
101 | CKCON &= ~0x20; // Timer2 clocked based on T2XCLK;
|
102 |
|
103 | TMR2RL = -SYSCLK/12/1000; // Init reload values
|
104 | TMR2 = 0xffff; // set to reload immediately
|
105 | ET2 = 1; // enable Timer2 interrupts
|
106 | TR2 = 1; // start Timer2
|
107 | }
|
108 |
|
109 |
|
110 | //-----------------------------------------------------------------------------
|
111 | // Interrupt Service Routines
|
112 | //-----------------------------------------------------------------------------
|
113 |
|
114 | //-----------------------------------------------------------------------------
|
115 | // Timer2_ISR
|
116 | //-----------------------------------------------------------------------------
|
117 | // This routine changes the state of the LED whenever Timer2 overflows.
|
118 | //
|
119 | void Timer2_ISR (void) interrupt 5
|
120 | {
|
121 | TF2H = 0; // clear Timer2 interrupt flag
|
122 | ms++;
|
123 | }
|