-
Notifications
You must be signed in to change notification settings - Fork 0
/
osi.c
90 lines (76 loc) · 2.76 KB
/
osi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <c8051F020.h>
#include "osi.h"
//-----------------------------------------------------------------------------
// SYSCLK_Init ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This routine initializes the system clock to use the internal 16 MHz
// oscillator as its clock source. Also enables missing clock detector reset.
//
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
OSCICN |= 0x03; // Configure internal oscillator for
// its highest frequency (16 MHz)
OSCICN |= 0x80; // Enable missing clock detector
}
//-----------------------------------------------------------------------------
// ExtCrystalOsc_Init ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This routine initializes for and switches to the External Crystal
// Oscillator.
//
//-----------------------------------------------------------------------------
void ExtCrystalOsc_Init (void)
{
// Set the appropriate XFCN bits for the crystal frequency
//
// XFCN Crystal (XOSCMD = 11x)
// 000 f <= 32 kHz
// 001 32 kHz < f <= 84 kHz
// 010 84 kHz < f <= 225 kHz
// 011 225 kHz < f <= 590 kHz
// 100 590 kHz < f <= 1.5 MHz
// 101 1.5 MHz < f <= 4 MHz
// 110 4 MHz < f <= 10 MHz
// 111 10 MHz < f <= 30 MHz
#if (Crystal_Clock <= 32000)
#define XFCN 0
#elif (Crystal_Clock <= 84000L)
#define XFCN 1
#elif (Crystal_Clock <= 225000L)
#define XFCN 2
#elif (Crystal_Clock <= 590000L)
#define XFCN 3
#elif (Crystal_Clock <= 1500000L)
#define XFCN 4
#elif (Crystal_Clock <= 4000000L)
#define XFCN 5
#elif (Crystal_Clock <= 10000000L)
#define XFCN 6
#elif (Crystal_Clock <= 30000000L)
#define XFCN 7
#else
#error "Defined Crystal Frequency outside allowable range!"
#define XFCN 0
#endif
unsigned int i;
// Step 1. Enable the external oscillator.
OSCXCN = 0x60; // External Oscillator is an external
// crystal (no divide by 2 stage)
OSCXCN |= XFCN;
// Step 2. Wait at least 1 ms.
for (i = 9000; i > 0; i--); // at 16 MHz, 1 ms = 16000 SYSCLKs
// DJNZ = 2 SYSCLKs
// Step 3. Poll for XTLVLD => ??
while ((OSCXCN & 0x80) != 0x80);
// Step 4. Switch the system clock to the external oscillator.
OSCICN |= 0x08;
}