ee1shradha
Newbie
Missing code tags
How to add ISR for the below code. I am using PIC18F56Q43 Microcontroller.
This program is for communication between PIC18F56Q43 Microcontroller to Arduino MKR1010 Wifi and Ethernet shield.
sensor data is sent to Arduino MKR1010 Wifi. So, the sensor data sometimes gets missed or gets interrupted while UART communication takes place so how to stop this happen and have continuous sensor data while communicating. Below is the code for UART communication so where to add the ISR in this below code. Please do help with this i tried using these steps below but not sure if it is working
This program is for communication between PIC18F56Q43 Microcontroller to Arduino MKR1010 Wifi and Ethernet shield.
sensor data is sent to Arduino MKR1010 Wifi. So, the sensor data sometimes gets missed or gets interrupted while UART communication takes place so how to stop this happen and have continuous sensor data while communicating. Below is the code for UART communication so where to add the ISR in this below code. Please do help with this i tried using these steps below but not sure if it is working
[Moderator action]
- Added Code/Syntax tags
Code C - [expand] 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 // Example function to retrieve next byte to send void Default_ISR(void) { NOP(); } void __interrupt_SW_ISR(void) { PIR0bits.SWIF = 0; // Clear the interrupt flag LATCbits.LATC0 ^= 1; // ISR code goes here } void __attribute__((__interrupt__, auto_psv)) _Uart1RXInterrupt(void) { if (PIR4bits.U1RXIF) // Check if the interrupt is from UART1 receive { Uart_Data = UART1_Read(); // Read received data } PIR4bits.U1RXIF = 0; // Cleared the UART receive interrupt flag } void INTERRUPT_Initialize (void) { INTCON0bits.GIEH = 1; // Enable high priority interrupts INTCON0bits.GIEL = 1; // Enable low priority interrupts INTCON0bits.IPEN = 1; // Enable interrupt priority PIE0bits.SWIE = 1; // Enable SW interrupt PIE0bits.HLVDIE = 1; // Enable HLVD interrupt IPR0bits.SWIP = 0; // Make Software sw interrupt low priority // Change IVTBASE if required IVTBASEU = 0x00; //Optional IVTBASEH = 0x30; //Default is 0x000008 IVTBASEL = 0x08; } till here are the steps i used to add and ISR *************************************************************************************************************** below is the main code where the ISR has to be added here comes the declarations void main() { Sys_config(); Delay_1ms(); Delay_1ms(); Delay_1ms(); Delay_1ms(); Delay_1ms(); Reset_Device(); Delay_1ms(); Delay_1ms(); Delay_1ms(); Delay_1ms(); Write_Reg_IIN0(); //Write to REGISTER Delay_1ms(); Delay_1ms(); Delay_1ms(); Delay_1ms(); Delay_1ms(); __delay_ms(1000); Write_Reg_IIN0(); //Write to REGISTER RX_RE_SetLow(); __delay_ms(1000); __delay_ms(1000); __delay_ms(1000); __delay_ms(1000); TX_DE_SetHigh(); for(uint8_t x = 0;Company_Name[x]!='\0' ; x++) { UART1_Write(Company_Name[x]); } __delay_ms(1); TX_DE_SetLow(); while(1) { Bus24V_SetHigh(); //Power Indicator LED for this below part of the code the ISR should be added where to add it and what is the procedure if(UART1_is_rx_ready() == 1 ) { Uart_Data = UART1_Read(); // Read received data // Check if waiting for a new data frame if (Wait_For_Next_Data == DO_NOT_WAIT){ // Check if received byte is the start byte if (Uart_Data == 0x1F) { Wait_For_Next_Data = WAIT_FOR_SI_ADD_BYTE; Quart_Data_Frame[0] = Uart_Data; } } // Check if waiting for sensor address byte else { if (Wait_For_Next_Data == WAIT_FOR_SI_ADD_BYTE) { // Check if received byte is a valid sensor address if ((Uart_Data == SI_Adress_01) || (Uart_Data == SI_Adress_02) || (Uart_Data == SI_Adress_03) || (Uart_Data == SI_Adress_04)) { Wait_For_Next_Data = WAIT_FOR_NC1_BYTE; Quart_Data_Frame[1] = Uart_Data; } } // Check if waiting for NC1 byte else if (Wait_For_Next_Data == WAIT_FOR_NC1_BYTE) { // Check if received byte is NC if (Uart_Data == NC) { Wait_For_Next_Data = WAIT_FOR_NC2_BYTE; Quart_Data_Frame[2] = Uart_Data; } } // Check if waiting for NC2 byte else if (Wait_For_Next_Data == WAIT_FOR_NC2_BYTE) { // Check if received byte is NC if (Uart_Data == NC) { Wait_For_Next_Data = WAIT_FOR_QRY_BYTE; Quart_Data_Frame[3] = Uart_Data; } } // Check if waiting for query byte else if (Wait_For_Next_Data == WAIT_FOR_QRY_BYTE) { // Check if received byte is query byte if (Uart_Data == 0x11) { Wait_For_Next_Data = WAIT_FOR_CRC_BYTE; Quart_Data_Frame[4] = Uart_Data; } } // Check if waiting for CRC byte else if (Wait_For_Next_Data == WAIT_FOR_CRC_BYTE) { // Store received byte as CRC Quart_Data_Frame[5] = Uart_Data; Wait_For_Next_Data = WAIT_FOR_END_BYTE; } // Check if waiting for end byte else if (Wait_For_Next_Data == WAIT_FOR_END_BYTE) { // Check if received byte is end byte if (Uart_Data == 0x07) { Quart_Data_Frame[6] = Uart_Data; Wait_For_Next_Data = DO_NOT_WAIT; Data_ok_Flag = 1; } } } } This below code is uart1.c File for reference to main code #include <xc.h> #include "uart1.h" static volatile uart1_status_t uart1RxLastError; /** Section: UART1 APIs */ void (*UART1_FramingErrorHandler)(void); void (*UART1_OverrunErrorHandler)(void); void (*UART1_ErrorHandler)(void); void UART1_DefaultFramingErrorHandler(void); void UART1_DefaultOverrunErrorHandler(void); void UART1_DefaultErrorHandler(void); void UART1_Initialize(void) { // Disable interrupts before changing states // Set the UART1 module to the options selected in the user interface. // P1L 0; U1P1L = 0x00; // P1H 0; U1P1H = 0x00; // P2L 0; U1P2L = 0x00; // P2H 0; U1P2H = 0x00; // P3L 0; U1P3L = 0x00; // P3H 0; U1P3H = 0x00; // BRGS high speed; MODE Asynchronous 8-bit mode; RXEN enabled; TXEN enabled; ABDEN disabled; U1CON0 = 0x30;//0xB0; // RXBIMD Set RXBKIF on rising RX input; BRKOVR disabled; WUE disabled; SENDB disabled; ON enabled; U1CON1 = 0x80; // TXPOL not inverted; FLO off; C0EN Checksum Mode 0; RXPOL not inverted; RUNOVF RX input shifter stops all activity; STP Transmit 1Stop bit, receiver verifies first Stop bit; U1CON2 = 0x00; // BRGL 103; U1BRGL = 0x9F;//0x67; // BRGH 0; U1BRGH = 0x01;//0x00; // STPMD in middle of first Stop bit; TXWRE No error; U1FIFO = 0x00; // ABDIF Auto-baud not enabled or not complete; WUIF WUE not enabled by software; ABDIE disabled; U1UIR = 0x00; // ABDOVF Not overflowed; TXCIF 0; RXBKIF No Break detected; RXFOIF not overflowed; CERIF No Checksum error; U1ERRIR = 0x00; // TXCIE disabled; FERIE disabled; TXMTIE disabled; ABDOVE disabled; CERIE disabled; RXFOIE disabled; PERIE disabled; RXBKIE disabled; U1ERRIE = 0x00; UART1_SetFramingErrorHandler(UART1_DefaultFramingErrorHandler); UART1_SetOverrunErrorHandler(UART1_DefaultOverrunErrorHandler); UART1_SetErrorHandler(UART1_DefaultErrorHandler); uart1RxLastError.status = 0; } bool UART1_is_rx_ready(void) { return (bool)(PIR4bits.U1RXIF); } bool UART1_is_tx_ready(void) { return (bool)(PIR4bits.U1TXIF && U1CON0bits.TXEN); } bool UART1_is_tx_done(void) { return U1ERRIRbits.TXMTIF; } uart1_status_t UART1_get_last_status(void){ return uart1RxLastError; } uint8_t UART1_Read(void) { while(!PIR4bits.U1RXIF) { } uart1RxLastError.status = 0; if(U1ERRIRbits.FERIF){ uart1RxLastError.ferr = 1; UART1_FramingErrorHandler(); } if(U1ERRIRbits.RXFOIF){ uart1RxLastError.oerr = 1; UART1_OverrunErrorHandler(); } if(uart1RxLastError.status){ UART1_ErrorHandler(); } return U1RXB; } void UART1_Write(uint8_t txData) { while(0 == PIR4bits.U1TXIF) { } U1TXB = txData; // Write the data byte to the USART. } void UART1_DefaultFramingErrorHandler(void){} void UART1_DefaultOverrunErrorHandler(void){} void UART1_DefaultErrorHandler(void){ } void UART1_SetFramingErrorHandler(void (* interruptHandler)(void)){ UART1_FramingErrorHandler = interruptHandler; } void UART1_SetOverrunErrorHandler(void (* interruptHandler)(void)){ UART1_OverrunErrorHandler = interruptHandler; } void UART1_SetErrorHandler(void (* interruptHandler)(void)){ UART1_ErrorHandler = interruptHandler; }
Last edited by a moderator: