-
Notifications
You must be signed in to change notification settings - Fork 0
/
Configuration.c
159 lines (119 loc) · 3.88 KB
/
Configuration.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* File: Configuration.c
* Author: Fededc88
*
* Created on 29 de julio de 2018, 18:06
*/
#include "Configuration.h"
#include "HardwareDef.h"
void Clock_Init(void){
//Set new oscillator selection
CLKDIVbits.RCDIV = 0b000;
DELAY_105uS;
asm volatile(
"MOV #OSCCONH, w1\n"
"MOV.b #0x01, w0\n"
"MOV #0x78, w2\n"
"MOV #0x9A, w3\n"
"MOV.b w2, [w1]\n"
"MOV.b w3, [w1]\n"
"MOV.b w0, [w1]\n"
"MOV #OSCCONL,w1\n"
"MOV.b #0x01, w0\n"
"MOV #0x46, w2\n"
"MOV #0x57, w3\n"
"MOV.b w2, [w1]\n"
"MOV.b w3, [w1]\n"
"MOV.b w0, [w1]\n"
);
Nop();
DELAY_105uS;
while(OSCCONbits.OSWEN != 0){}
while(OSCCONbits.COSC != 0b001){}
}
void Interrupts(void){
//INTCON1bits.NSTDIS = 1;
SRbits.IPL = 1; //CPU Interrupt priority level is 1
}
void Timer1_Init(long f_Hz){
//Timer1_f Timer 1 frecuency on Hz
//
// PR1 = 0x3E80;
// T1CONbits.TCKPS = 0; // 1:1 Clock prescaler
//
if((Fcy/f_Hz)<=65536){
PR1 = (int) (Fcy/f_Hz);
T1CONbits.TCKPS = 0; // 1:1 Clock prescaler
}
else if (Fcy/f_Hz/8 <= 65536 ){
PR1 = (int) ((Fcy/8)/f_Hz);
T1CONbits.TCKPS = 1; // 1:8 Clock prescaler
}
else if (Fcy/f_Hz/64 <= 65536 ){
PR1 = (int) ((Fcy/64)/f_Hz);
T1CONbits.TCKPS = 2; // 1:64 Clock prescaler
}
else if (Fcy/f_Hz/256 <= 65536 ) {
PR1 = (int) ((Fcy/256)/f_Hz);
T1CONbits.TCKPS = 3; // 1:64 Clock prescaler
}
T1CONbits.TON = 1; //Enable Timer 1
IFS0bits.T1IF = 0; // Clear Interrupts T1 flag
IEC0bits.T1IE = 1; //Timer1 Interrupt Enable
}
void Timer2_Init(long f_Hz){
//Timer1_f Timer 1 frecuency on Hz
//
// PR1 = 0x3E80;
// T1CONbits.TCKPS = 0; // 1:1 Clock prescaler
//
if((Fcy/f_Hz)<=65536){
PR2 = (int) (Fcy/f_Hz);
T2CONbits.TCKPS = 0; // 1:1 Clock prescaler
}
else if (Fcy/f_Hz/8 <= 65536 ){
PR2 = (int) ((Fcy/8)/f_Hz);
T2CONbits.TCKPS = 1; // 1:8 Clock prescaler
}
else if (Fcy/f_Hz/64 <= 65536 ){
PR2 = (int) ((Fcy/64)/f_Hz);
T2CONbits.TCKPS = 2; // 1:64 Clock prescaler
}
else if (Fcy/f_Hz/256 <= 65536 ) {
PR2 = (int) ((Fcy/256)/f_Hz);
T2CONbits.TCKPS = 3; // 1:64 Clock prescaler
}
T2CONbits.TON = 1; //Enable Timer 2
IPC1bits.T2IP = 6; // Setup Output Compare 1 interrupt for
IFS0bits.T2IF = 0; // Clear Output Compare 1 interrupt flag
IEC0bits.T2IE = 1; // Enable Output Compare 1 interrupts
return;
}
void Timer3_Init(long f_Hz){
//Timer1_f Timer 1 frecuency on Hz
//
// PR1 = 0x3E80;
// T1CONbits.TCKPS = 0; // 1:1 Clock prescaler
//
if((Fcy/f_Hz)<=65536){
PR3 = (int) (Fcy/f_Hz);
T3CONbits.TCKPS = 0; // 1:1 Clock prescaler
}
else if (Fcy/f_Hz/8 <= 65536 ){
PR3 = (int) ((Fcy/8)/f_Hz);
T3CONbits.TCKPS = 1; // 1:8 Clock prescaler
}
else if (Fcy/f_Hz/64 <= 65536 ){
PR3 = (int) ((Fcy/64)/f_Hz);
T3CONbits.TCKPS = 2; // 1:64 Clock prescaler
}
else if (Fcy/f_Hz/256 <= 65536 ) {
PR3 = (int) ((Fcy/256)/f_Hz);
T3CONbits.TCKPS = 3; // 1:64 Clock prescaler
}
T3CONbits.TON = 1; //Enable Timer 2
IPC2bits.T3IP = 7; // Setup Timer3 interrupt for
IFS0bits.T3IF = 0; // Clear Timer3 interrupt flag
IEC0bits.T3IE = 1; // Disable Timer3 interrupts
return;
}