| 147 |
Kevin |
1 |
#include "defines.h"
|
| 127 |
Kevin |
2 |
#include "interrupts.h"
|
| 119 |
Kevin |
3 |
#include "uart.h"
|
| 120 |
Kevin |
4 |
#include "i2c.h"
|
| 121 |
Kevin |
5 |
#include "spi.h"
|
| 123 |
Kevin |
6 |
#include "adc.h"
|
| 147 |
Kevin |
7 |
#include "timers.h"
|
| 119 |
Kevin |
8 |
|
|
|
9 |
//----------------------------------------------------------------------------
|
|
|
10 |
// Note: This code for processing interrupts is configured to allow for high and
|
|
|
11 |
// low priority interrupts. The high priority interrupt can interrupt the
|
|
|
12 |
// the processing of a low priority interrupt. However, only one of each type
|
|
|
13 |
// can be processed at the same time. It is possible to enable nesting of low
|
|
|
14 |
// priority interrupts, but this code is not setup for that and this nesting
|
|
|
15 |
// is not enabled.
|
|
|
16 |
|
| 151 |
Kevin |
17 |
void Interrupt_Init() {
|
| 119 |
Kevin |
18 |
// Peripheral interrupts can have their priority set to high or low
|
|
|
19 |
// Decide on the priority of the enabled peripheral interrupts (0 is low, 1 is high)
|
|
|
20 |
|
|
|
21 |
// High priority interrupts
|
|
|
22 |
IPR1bits.RC1IP = 1; // USART1 RX interrupt
|
|
|
23 |
IPR1bits.TX1IP = 1; // USART1 TX interrupt
|
| 121 |
Kevin |
24 |
// IPR3bits.RC2IP = 1; // USART2 RX interrupt
|
|
|
25 |
IPR1bits.SSPIP = 1; // I2C interrupt
|
|
|
26 |
IPR3bits.SSP2IP = 1; // MSSP2 (SPI2) interrupt
|
| 119 |
Kevin |
27 |
|
|
|
28 |
// Low priority interrupts
|
|
|
29 |
// INTCON2bits.TMR0IP = 0; // Timer0 interrupt
|
| 147 |
Kevin |
30 |
IPR1bits.TMR1IP = 0; // Timer1 interrupt
|
| 119 |
Kevin |
31 |
// IPR2bits.TMR3IP = 0; // Timer 3 interrupt
|
| 147 |
Kevin |
32 |
// IPR1bits.ADIP = 0; // ADC interupt
|
| 119 |
Kevin |
33 |
// INTCON2bits.RBIP = 0; // Port B interrupt
|
|
|
34 |
// INTCON3bits.INT1IP = 0; // INT1 interrupt
|
|
|
35 |
|
|
|
36 |
// Enable Port B interrupt
|
|
|
37 |
// INTCONbits.RBIE = 1;
|
|
|
38 |
// Enable interrupt for INT1
|
|
|
39 |
// INTCON3bits.INT1IE = 1;
|
|
|
40 |
}
|
|
|
41 |
|
| 151 |
Kevin |
42 |
void Interrupt_Enable() {
|
| 119 |
Kevin |
43 |
// Peripheral interrupts can have their priority set to high or low.
|
|
|
44 |
// Enable both high-priority interrupts and low-priority interrupts
|
|
|
45 |
RCONbits.IPEN = 1;
|
|
|
46 |
INTCONbits.GIEH = 1;
|
|
|
47 |
INTCONbits.GIEL = 1;
|
|
|
48 |
}
|
|
|
49 |
|
| 151 |
Kevin |
50 |
void Interrupt_Disable() {
|
|
|
51 |
RCONbits.IPEN = 0;
|
|
|
52 |
INTCONbits.GIEH = 0;
|
|
|
53 |
INTCONbits.GIEL = 0;
|
|
|
54 |
}
|
| 119 |
Kevin |
55 |
|
|
|
56 |
// Set up the interrupt vectors
|
|
|
57 |
void InterruptHandlerHigh();
|
|
|
58 |
void InterruptHandlerLow();
|
|
|
59 |
|
|
|
60 |
#pragma code InterruptVectorLow = 0x18
|
|
|
61 |
|
|
|
62 |
void InterruptVectorLow(void) {
|
|
|
63 |
_asm
|
|
|
64 |
goto InterruptHandlerLow //jump to interrupt routine
|
|
|
65 |
_endasm
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
#pragma code InterruptVectorHigh = 0x08
|
|
|
69 |
|
|
|
70 |
void InterruptVectorHigh(void) {
|
|
|
71 |
_asm
|
|
|
72 |
goto InterruptHandlerHigh //jump to interrupt routine
|
|
|
73 |
_endasm
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
//----------------------------------------------------------------------------
|
|
|
77 |
// High priority interrupt routine
|
|
|
78 |
// this parcels out interrupts to individual handlers
|
|
|
79 |
|
|
|
80 |
#pragma code
|
|
|
81 |
#pragma interrupt InterruptHandlerHigh
|
|
|
82 |
|
|
|
83 |
void InterruptHandlerHigh() {
|
|
|
84 |
// We need to check the interrupt flag of each enabled high-priority interrupt to
|
|
|
85 |
// see which device generated this interrupt. Then we can call the correct handler.
|
|
|
86 |
|
| 147 |
Kevin |
87 |
// // Check to see if we have an SPI2 interrupt
|
|
|
88 |
// if (PIR3bits.SSP2IF) {
|
|
|
89 |
// // Call the handler
|
|
|
90 |
// SPI2_Recv_Interrupt_Handler();
|
|
|
91 |
//
|
|
|
92 |
// // Clear the interrupt flag
|
|
|
93 |
// PIR3bits.SSP2IF = 0;
|
|
|
94 |
//
|
|
|
95 |
// return;
|
|
|
96 |
// }
|
| 121 |
Kevin |
97 |
|
|
|
98 |
// Check to see if we have an I2C interrupt
|
|
|
99 |
if (PIR1bits.SSPIF) {
|
|
|
100 |
// Call the handler
|
|
|
101 |
I2C_Interrupt_Handler();
|
|
|
102 |
|
|
|
103 |
// Clear the interrupt flag
|
|
|
104 |
PIR1bits.SSPIF = 0;
|
|
|
105 |
|
|
|
106 |
return;
|
|
|
107 |
}
|
|
|
108 |
|
| 119 |
Kevin |
109 |
// Check to see if we have an interrupt on USART1 RX
|
|
|
110 |
if (PIR1bits.RC1IF) {
|
|
|
111 |
// Call the interrupt handler
|
|
|
112 |
UART1_Recv_Interrupt_Handler();
|
|
|
113 |
|
|
|
114 |
// Clear the interrupt flag
|
|
|
115 |
PIR1bits.RC1IF = 0;
|
|
|
116 |
|
|
|
117 |
return;
|
|
|
118 |
}
|
|
|
119 |
|
| 121 |
Kevin |
120 |
#ifndef _DEBUG // Disable UART1 TX interrupt for debug mode (using printf)
|
| 119 |
Kevin |
121 |
// Check to see if we have an interrupt on USART1 TX
|
|
|
122 |
if (PIR1bits.TX1IF) {
|
|
|
123 |
// Call the interrupt handler
|
|
|
124 |
UART1_Send_Interrupt_Handler();
|
|
|
125 |
|
|
|
126 |
// Clear the interrupt flag
|
|
|
127 |
PIR1bits.TX1IF = 0;
|
|
|
128 |
|
|
|
129 |
return;
|
|
|
130 |
}
|
| 121 |
Kevin |
131 |
#endif
|
| 119 |
Kevin |
132 |
|
|
|
133 |
// // Check to see if we have an interrupt on USART2 RX
|
|
|
134 |
// if (PIR3bits.RC2IF) {
|
|
|
135 |
// DBG_PRINT_INT("INT: UART2 RX\r\n");
|
|
|
136 |
// // Call the interrupt handler
|
|
|
137 |
// uart_2_recv_interrupt_handler();
|
|
|
138 |
//
|
|
|
139 |
// // Clear the interrupt flag
|
|
|
140 |
// PIR3bits.RC2IF = 0;
|
|
|
141 |
// }
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
//----------------------------------------------------------------------------
|
|
|
145 |
// Low priority interrupt routine
|
|
|
146 |
// this parcels out interrupts to individual handlers
|
|
|
147 |
#pragma code
|
|
|
148 |
#pragma interruptlow InterruptHandlerLow
|
|
|
149 |
// This works the same way as the "High" interrupt handler
|
|
|
150 |
|
|
|
151 |
void InterruptHandlerLow() {
|
|
|
152 |
// // Check to see if we have an interrupt on INT1
|
|
|
153 |
// if (INTCON3bits.INT1IF) {
|
|
|
154 |
// DBG_PRINT_INT("INT: INT1\r\n");
|
|
|
155 |
// int1_interrupt_handler();
|
|
|
156 |
//
|
|
|
157 |
// INTCON3bits.INT1IF = 0;
|
|
|
158 |
// }
|
|
|
159 |
|
|
|
160 |
// // Check to see if we have an interrupt on any port B inputs <4:7>
|
|
|
161 |
// if (INTCONbits.RBIF) {
|
|
|
162 |
// DBG_PRINT_INT("INT: Port B\r\n");
|
|
|
163 |
// port_b_int_interrupt_handler();
|
|
|
164 |
//
|
|
|
165 |
// INTCONbits.RBIF = 0;
|
|
|
166 |
// }
|
|
|
167 |
|
|
|
168 |
// // Check to see if we have an interrupt on timer 0
|
|
|
169 |
// if (INTCONbits.TMR0IF) {
|
|
|
170 |
// DBG_PRINT_INT("INT: Timer 0\r\n");
|
|
|
171 |
// // Call the handler
|
|
|
172 |
// timer0_interrupt_handler();
|
|
|
173 |
//
|
|
|
174 |
// // Clear this interrupt flag
|
|
|
175 |
// INTCONbits.TMR0IF = 0;
|
|
|
176 |
// }
|
|
|
177 |
|
| 147 |
Kevin |
178 |
// Check to see if we have an interrupt on timer 1
|
|
|
179 |
if (PIR1bits.TMR1IF) {
|
|
|
180 |
// Call the interrupt handler
|
|
|
181 |
Timer1_Interrupt_Handler();
|
| 119 |
Kevin |
182 |
|
| 147 |
Kevin |
183 |
// Clear the interrupt flag
|
|
|
184 |
PIR1bits.TMR1IF = 0;
|
|
|
185 |
}
|
|
|
186 |
|
| 119 |
Kevin |
187 |
// // Check to see if we have an interrupt on timer 3
|
|
|
188 |
// if (PIR2bits.TMR3IF) {
|
|
|
189 |
// DBG_PRINT_INT("INT: Timer 3\r\n");
|
|
|
190 |
// timer3_interrupt_handler();
|
|
|
191 |
//
|
|
|
192 |
// PIR2bits.TMR3IF = 0;
|
|
|
193 |
// }
|
|
|
194 |
|
| 147 |
Kevin |
195 |
// // Check to see if we have an interrupt on ADC
|
|
|
196 |
// if (PIR1bits.ADIF) {
|
|
|
197 |
// // Call the interrupt handler
|
|
|
198 |
// ADC_Interrupt_Handler();
|
|
|
199 |
//
|
|
|
200 |
// // Clear the interrupt flag
|
|
|
201 |
// PIR1bits.ADIF = 0;
|
|
|
202 |
// }
|
| 119 |
Kevin |
203 |
}
|
|
|
204 |
|