-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.c
More file actions
70 lines (55 loc) · 2.5 KB
/
system.c
File metadata and controls
70 lines (55 loc) · 2.5 KB
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
/******************************************************************************/
/* Files to Include */
/******************************************************************************/
/* Device header file */
#if defined(__XC16__)
#include <xc.h>
#elif defined(__C30__)
#if defined(__PIC24E__)
#include <p24Exxxx.h>
#elif defined (__PIC24F__)||defined (__PIC24FK__)
#include <p24Fxxxx.h>
#elif defined(__PIC24H__)
#include <p24Hxxxx.h>
#endif
#endif
#include <stdint.h> /* For uint32_t definition */
#include <stdbool.h> /* For true/false definition */
#include "system.h" /* variables/params used by system.c */
#include <libpic30.h> /* includes delay_us and delay_ms */
/* System Level Functions */
/* */
/* Custom oscillator configuration funtions, reset source evaluation */
/* functions, and other non-peripheral microcontroller initialization */
/* functions get placed in system.c */
/* */
/* */
/* Refer to the device Family Reference Manual Oscillator section for
information about available oscillator configurations. Typically
this would involve configuring the oscillator tuning register or clock
switching useing the compiler's __builtin_write_OSCCON functions.
Refer to the C Compiler for PIC24 MCUs and dsPIC DSCs User Guide in the
compiler installation directory /doc folder for documentation on the
__builtin functions. */
/* TODO Add clock switching code if appropriate. An example stub is below. */
void ConfigureOscillator(void) {
#if 1
/* Disable Watch Dog Timer */
RCONbits.SWDTEN = 0;
//CLKDIV = 0x0000;
CLKDIVbits.FRCDIV = 0x00;
CLKDIVbits.PLLPOST = 0x01; // 1 -> N1 = 4
CLKDIVbits.PLLPRE = 0x00; // 0 -> N2 = 2
PLLFBDbits.PLLDIV = 30;
OSCTUNbits.TUN = 0;
/* When clock switch occurs switch to Prim Osc (HS, XT, EC)with PLL */
__builtin_write_OSCCONH(0x03); /* Set OSCCONH for clock switch */
__builtin_write_OSCCONL(0x01); /* Start clock switching */
while (OSCCONbits.COSC != 0b011);
/* Wait for Clock switch to occur */
/* Wait for PLL to lock, if PLL is used */
while (OSCCONbits.LOCK != 1) {
};
return;
#endif
}