forked from whchoi/Modbus-Slave-for-PIC18F
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interrupts.c
68 lines (63 loc) · 2.28 KB
/
interrupts.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
/******************************************************************************/
/*Files to Include */
/******************************************************************************/
#include <p18cxxx.h> /* C18 General Include File */
#include "user.h"
#include "modbus.h"
#include "system.h"
/******************************************************************************/
/* Global Variables */
/******************************************************************************/
volatile char endOfMessage,newMessage = 1;
volatile char timerCount,messageLength,modbusMessage,z = 0;
volatile unsigned char response[125]; //Enough to return all holding-r's
volatile unsigned char received[125]; //Enough to write all holding-r's
/******************************************************************************/
/* Interrupt Routines */
/******************************************************************************/
// start ISR code
#pragma code isr = 0x08 // store the below code at address 0x08
#pragma interrupt isr // let the compiler know that the function isr() is int
void isr(void)
{
if(ReceiveFlag1){ // USART receive interrupt flag has been set
if((!endOfMessage)&&(!newMessage)){
if(TransmitFlag1){ // check if the TXREG is empty
received[z] = ReceiveBuffer;
z++;
timerCount = 0;
}
}
if(newMessage){
OpenTmr0();
if(TransmitFlag1){ // check if the TXREG is empty
received[z] = ReceiveBuffer;
z++;
newMessage = 0;
endOfMessage = 0;
messageLength = 0;
modbusMessage = 0;
timerCount = 0;
return;
}
}
}
else if(Timer0Flag){ //TMR0 interrupt flag
modbusDelay(); //Resets timer for 1.04ms
timerCount++;
if(timerCount > 4){
endOfMessage = 1;
newMessage = 1;
messageLength = z;
modbusMessage = 1;
for(z=(messageLength);z!=125;z++){ //Clear rest of message
received[z] = 0;
}
z=0;
T0CONbits.TMR0ON = 0; //Close timer0
timerCount = 0;
}
Timer0Flag = 0; // Clear flag
}
}
#pragma code // return to the default code section