Stuck in ISR, RCIF is not Firing when uart with pic18f5420 and Sim800

Thread Starter

Raushan Kumar 1

Joined Jun 26, 2017
5
Hello, I am new to PIC, I am able to transmit through pic18f4520 and sim800 but could not able to receive it. I run debugger, program is stuck in ISR but RCIF is not fired at all. here are my codes
/*
* File: interruptSetup.c
* Author: Raushan
*
* Created on 23 June, 2017, 7:30 PM
*/

#include <stdio.h>
#include <stdlib.h>
#include "ConfigurationBits.h"

void InterruptSetup() {
T1CON = 0x01; //Configure Timer1 interrupt
PIE1bits.TMR1IE = 1;
RCONbits.IPEN = 0x01; // TMR1 high priority ,TMR1 Overflow Interrupt Priority bit
PIR1bits.TMR1IF = 0;
T0CON = 0X00;
INTCONbits.T0IE = 1; // Enable interrupt on TMR0 overflow
INTCON2bits.TMR0IP = 0x00;
T0CONbits.TMR0ON = 1;
INTCONbits.PEIE = 1; //periferal interrupt enable
INTCONbits.GIE = 1;
INTCON2 =0x00; // Set Falling Edge Trigger

}

/*
* File: uartSetup.c
* Author: Raushan @intellicar
* Created on 23 June, 2017, 6:14 PM
*/
#include "ConfigurationBits.h"
#include <pic18f4520.h>

char UART_Init(const long int baudrate) {
//SPBRG = (_XTAL_FREQ - baudrate * 16) / (baudrate * 16); //Writing SPBRG Register
SPBRG = 12; // set baud rate to 9600 baud (2MHz/(16*baudrate))-1
SYNC = 0; //Setting Asynchronous Mode, ie UART
RCSTAbits.SPEN = 1; // enable USART// got effected
RCSTAbits.CREN = 1; // enable continous receiving
PIE1bits.RCIE = 1;
TXSTAbits.TXEN = 1; //Enables Transmission
TXSTAbits.BRGH = 1; // high baud rate mode
TRISCbits.RC7 = 1; //Rx
TRISCbits.RC6 = 0; //Tx
PIE1bits.TXIE = 1;
if (RCSTAbits.FERR = 1) {
RCSTAbits.CREN = 0; // enable continous receiving
RCSTAbits.CREN = 1; // enable continous receiving
}
if (RCSTAbits.OERR = 1) {
RCSTAbits.CREN = 0; // enable continous receiving
RCSTAbits.CREN = 1; // enable continous receiving
}
//BRG16 =1; //
return 1; //Returns 1 to indicate Successful Completion
}

/*
* File: work_23-06-17.c
* Author: Raushan
*
* Created on 23 June, 2017, 6:12 PM
*/

#include <stdio.h>
#include <stdlib.h>
#include <pic18f4520.h>
#include "ConfigurationBits.h"
#include "uartSetup.h"
#include "iterruptSetup.h"
#define STRLEN 512

char UART_Init(const long int baudrate);
void I2C_Master_Init(const unsigned long c);
void printToArduino(unsigned char *printOutput);
volatile unsigned char nextChar = '\n';
unsigned char buffer[STRLEN];

void main() {
OSCCONbits.IRCF = 0x55; // INTOSC frequency 2MHz
UART_Init(9600);
2C_Master_Init(9600);
UART_Write_Text("Setup complete");
printToArduino("Setup complete");
InterruptSetup();
while (1) {
UART_Write_Text("AT+BTPOWER=?\r\n");
__delay_ms(1000);
}
}

void interrupt interruptRoutine()
{
printToArduino("I am stuck here\n");
if (RCIF) {
nextChar = UART_Read();
printToArduino("This is Receive interrupt\n");
}
PIR1bits.RCIF = 0;
}


Kindly help. ..
 

geekoftheweek

Joined Oct 6, 2013
1,201
Shouldn't

if (RCIF) {

actually be

if (PIR1bits.RCIF) {

?

It also looks like

OSCCONbits.IRCF = 0x55; // INTOSC frequency 2MHz

is not right? I don't normally use C but it looks like your trying to assign the whole OSCCON register instead of just the bits with the 0x55... in which case neither will give you a 2 MHz clock which will cause the UART to run at the wrong baud rate.
 
Last edited:

Thread Starter

Raushan Kumar 1

Joined Jun 26, 2017
5
First of all, Thanks a lot for your reply to thread...
I have included pic18f4520.h so , PIR1bits.RCIF or RCIF works same, I tried.
Yeah its shouldn't be OSCCONbits.IRCF = 0x55; instead of that OSCCON = 0x55; I replaced , but still getting same issue. RCIF couldn't set.
 

AlbertHall

Joined Jun 4, 2014
12,345
If it is in the interrupt routine then at least one interrupt flag is set.
You have also enabled the transmit interrupt (PIE1bits.TXIE = 1; ) but you have no handler for it. If that interrupt is triggered then your program will go round and round the interrupt handler and will do nothing else. At the very least you must clear that interrupt flag if it is set.
 

geekoftheweek

Joined Oct 6, 2013
1,201
First of all, Thanks a lot for your reply to thread...
I have included pic18f4520.h so , PIR1bits.RCIF or RCIF works same, I tried.
Yeah its shouldn't be OSCCONbits.IRCF = 0x55; instead of that OSCCON = 0x55; I replaced , but still getting same issue. RCIF couldn't set.
I'm still questioning your OSCCON myself? What are you using exactly for your instruction clock? I checked the datasheet and found your OSCCON is using the timer 1 external clock source as the system clock. Other than the system clock source being wrong I can't think of anything else. I've only used the UART in synchronous mode myself so I'm kind of unsure what else it could be.
 

Thread Starter

Raushan Kumar 1

Joined Jun 26, 2017
5
PrintArduino is function where I initiate i2c communication between pic18f4520 and arduino mega2560. Actually I wanted to see whatever communication is happening between pic18f4520 and sim800 module.

My AT commands are being transmitted to sim800 module and I check SimTX/PIC's receiver , response from sim is coming fine to that pin. My issue is I am not able to read those response and printArduino() to using I2C to see what got receive. sometimes when I run, one or two lines are coming then stopped.

Is PrintArduino() is lagging or I need to implement a circular buffer to get data.
 

geekoftheweek

Joined Oct 6, 2013
1,201
PrintArduino is function where I initiate i2c communication between pic18f4520 and arduino mega2560. Actually I wanted to see whatever communication is happening between pic18f4520 and sim800 module.

My AT commands are being transmitted to sim800 module and I check SimTX/PIC's receiver , response from sim is coming fine to that pin. My issue is I am not able to read those response and printArduino() to using I2C to see what got receive. sometimes when I run, one or two lines are coming then stopped.

Is PrintArduino() is lagging or I need to implement a circular buffer to get data.
Now I kind of understand. I guess I didn't realize it kind of worked. I'm kind of clueless actually as to what to do next. My only guess is you have data coming in faster than it is going out.

Have you tried different speeds for I2C and UART? I2C is usually going to take a anywhere from a little longer to much longer (depending on the program) than the UART due to acknowledge bits, the address byte, and both start and stop conditions. Maybe try speeding up the I2C and see what happens. If it holds together a little longer you'll know you're on the right track.

Good luck!!
 

Thread Starter

Raushan Kumar 1

Joined Jun 26, 2017
5
Thanks , My problem is solved and actually timing was the issue as you suggested. So I use a flag to printArduino and I make it 1 everytime buffer is terminated by termination \r.

Appreciate your help, Thanks
 
Top