-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChangeClk.c
36 lines (34 loc) · 824 Bytes
/
ChangeClk.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
#include "xc.h"
#include "ChangeClk.h"
//clkval = 8 for 8MHz;
//clkval = 500 for 500kHz;
//clkval = 32 for 32kHz;
void NewClk(unsigned int clkval)
{
char COSCNOSC;
if (clkval == 8) //8MHz
{
COSCNOSC = 0x00;
}
else if (clkval == 500) // 500 kHz
{
COSCNOSC = 0x66;
}
else if (clkval== 32) //32 kHz
{
COSCNOSC = 0x55;
}
else // default 32 kHz
{
COSCNOSC = 0x55;
}
// Switch clock to 500 kHz
SRbits.IPL = 7; //Disable interrupts
CLKDIVbits.RCDIV = 0; // CLK division = 0
__builtin_write_OSCCONH(COSCNOSC); // (0x00) for 8MHz; (0x66) for 500kHz; (0x55) for 32kHz;
__builtin_write_OSCCONL(0x01);
OSCCONbits.OSWEN=1;
while(OSCCONbits.OSWEN==1)
{}
SRbits.IPL = 0; //enable interrupts
}