-
Notifications
You must be signed in to change notification settings - Fork 3
/
system.c
125 lines (97 loc) · 4.41 KB
/
system.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/******************************************************************************/
/* Files to Include */
/******************************************************************************/
/* Device header file */
#if defined(__XC16__)
#include <xc.h>
#elif defined(__C30__)
#if defined(__dsPIC33E__)
#include <p33Exxxx.h>
#elif defined(__dsPIC33F__)
#include <p33Fxxxx.h>
#endif
#endif
#include <stdint.h> /* For uint16_t definition */
#include <stdbool.h> /* For true/false definition */
#include "system.h" /* variables/params used by system.c */
#define BAUDRATE 9600
//#define BAUDRATE 1200
#define BRGVAL ((FCY/BAUDRATE)/16)-1
#define DELAY_105uS asm volatile ("REPEAT, #4201"); Nop(); // 105uS delay
/******************************************************************************/
/* 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)
{
//Ver Oscillator.pdf pagina 26 (FIN = 7.37)
// Configure PLL prescaler, PLL postscaler, PLL divisor
//PLLFBD = 63; // M = 65 para 60 MIPS (63 = 65-2)
PLLFBD = 74; // M = 76 para 70 MIPS (74 = 76-2)
CLKDIVbits.PLLPOST=0; // N2 = 2
//CLKDIVbits.PLLPRE=1; // N1 = 3
CLKDIVbits.PLLPRE=0; // N1 = 2
// Initiate Clock Switch to FRC oscillator with PLL (NOSC=0b001)
__builtin_write_OSCCONH(0x01);
__builtin_write_OSCCONL(OSCCON | 0x01);
// Wait for Clock switch to occur
while (OSCCONbits.COSC != 0b001);
// Wait for PLL to lock
while(OSCCONbits.LOCK != 1);
#if 0
/* Disable Watch Dog Timer */
RCONbits.SWDTEN = 0;
/* When clock switch occurs switch to Primary Osc (HS, XT, EC) */
__builtin_write_OSCCONH(0x02); /* 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, only if PLL is needed */
/* while(OSCCONbits.LOCK != 1); */
#endif
}
void configureUART(void)
{
//*************************************************************
// Unlock Registers
//*************************************************************
__builtin_write_OSCCONL(OSCCON & ~(1<<6));
// Assign U1Rx To Pin RP36
//***************************
RPINR18bits.U1RXR = 0x24;
// Assign U1Tx To Pin RP37
//***************************
RPOR1bits.RP37R = 1;
//*************************************************************
// Lock Registers
//*************************************************************
__builtin_write_OSCCONL(OSCCON | (1<<6));
U1MODE = 0;
U1MODEbits.STSEL = 0; // 1-Stop bit
U1MODEbits.PDSEL = 0; // No Parity, 8-Data bits
U1MODEbits.ABAUD = 0; // Auto-Baud disabled
U1MODEbits.BRGH = 0; // Standard-Speed mode
U1BRG = BRGVAL; // Baud Rate setting for 9600
//U1BRG = 1562;
//U1STAbits.UTXISEL0 = 0; // Interrupt after one TX character is transmitted
//U1STAbits.UTXISEL1 = 0;
//IEC0bits.U1TXIE = 1; // Enable UART TX interrupt
U1STA = 0; //clear status register
U1MODEbits.UARTEN = 1; // Enable UART
U1STAbits.UTXEN = 1; // Enable UART TX
//IFS0bits.U1RXIF = 0; //clear the receive flag
DELAY_105uS
//U1TXREG = 'a';
}