Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Stuck in ISR, RCIF is not firing when UART with pic18f5420 and Sim800

Status
Not open for further replies.

raushankumar586

Junior Member level 1
Joined
Jun 26, 2017
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
191
Stuck in ISR, RCIF is not Firing when uart with pic18f5420 and Sim800

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

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
/* 
* 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. ..
 

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

You probably want to move line 100 inside the "if(RCIF)" code so it only resets if RCIF was already set.

The other problem is if you are trying to receive more than one character, the "printToArduino" routine probably takes too long before returning so the next characters in the USART may be lost.

Brian.
 

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

I removed TXIE.

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.
 

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

I would recommend a buffer, it doesn't have to be 'circular' as long as it has enough space to hold an entire reply from the sim800.

I think your problem at the moment is that after receiving the first character, you enter into a fairly long process of displaying the message on the Arduino and by the time it has finished the following characters from the sim800 have already been lost.

I suggest this:
1. allocate a buffer of 'char' elements, big enough to hold the longest anticipated reply from the sim800. ("char SimBuffer[10];") for example
2. declare a variable as pointer into the buffer ("volatile unsigned charBufferPtr")
3. declare a variable to signal the full message has been received. ("volatile unsigned char SimMsgReceived")

then do something like this, it isn't 'C' but you will get the idea:
Code:
RCIF is set;
read the UART to get the character;

if (CR or whatever terminates the sim message) 
{
SimMsgReceived = 1;  // tell the main program the Sim reply is complete
}

else
if ((BufferPtr < sizeof(SimBuffer)) // prevent overflow if the message is too long
{
SimBuffer[BufferPtr++] = received character;
SimBuffer[BufferPtr] = 0;  // append a null to the end of the buffer
}
RCIF = 0;
}

Then in your main program, check the variable "SimMsgReceived" and if = 1 you send the Arduino message then reset it to zero again.
You must also then use "BufferPtr = 0" to reset the character buffer back to it's first location.

Brian.
 

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

Thank you very much sir , I forgot to thanx you, now its working fine. yes it was taking way more time due to arduino printing and i need to wait for it.

Now another problem I came across -

I want to implement a delay function using timer0. -----here is code --->>

Code:
if (timerInterruptFired()) {
        INTCONbits.T0IF = 0;
        //printToArduino(""); // don't know why to add this without this blink led is not working
        ledCount++;
        delayCount++;
        if (ledCount == blinkTime) { // blinkTime=8
            ledCount = 0;
            blinkLed(); // here I just toggle the corresponding port
        }
        if (delayCount == delayTime) { //delayTime = 4
            delayCount = 0;
            delayFlag = 1;
        }
    }

and in main loop---------->>
       while(true){
                  while(!delayFlag); // stucking here 
                  printToArduino("I am a delay!!");
}
but code is stucking in at while(!delayFlag); , what to do now?

I run debugger and observed that ledCount and delayCount is just changing its value to 0-1 ----- 0-1.... (just toggling) it should have increased with timer till 8 and 4 respectively.
help me ..
 

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

Code:
and in main loop---------->>
       while(true){
                  while(!delayFlag); // stucking here 
                  printToArduino("I am a delay!!");
}
The code will try to repeat until 'delayFlag' is zero and your print code is probably delaying the loop so it can't get back to check in time for the next time it is zero again. It is possible there is also something in 'timerInterruptFired()' that causes problems, especially if there is more than one timer running. Try replacing it with "if(INTCONbits.T0IF)" so it only checks Timer 0.

You can fix it by adding a line to load a value into delayFlag again after the 'while' line or a better method that probably uses less code is to reverse the way it counts. For example, instead of
Code:
if (delayCount == delayTime) { //delayTime = 4
            delayCount = 0;
            delayFlag = 1;
        }

use

if (delayCount == 0) { 
            delayCount = 4;  //delayTime = 4
            delayFlag = 1;
        }
and make the '++' into '--'

Note that you should declare 'delayFlag' as volatile and make sure some sensible values are in ledCount and delayCount at the start of your program or it could take longer than expected for the values to fall inside the test range.

Brian.
 

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

Code:
and in main loop---------->>
       while(true){
                  while(!delayFlag); // stucking here 
                  printToArduino("I am a delay!!");
}
The code will try to repeat until 'delayFlag' is zero and your print code is probably delaying the loop so it can't get back to check in time for the next time it is zero again. It is possible there is also something in 'timerInterruptFired()' that causes problems, especially if there is more than one timer running. Try replacing it with "if(INTCONbits.T0IF)" so it only checks Timer 0.

You can fix it by adding a line to load a value into delayFlag again after the 'while' line or a better method that probably uses less code is to reverse the way it counts. For example, instead of
Code:
if (delayCount == delayTime) { //delayTime = 4
            delayCount = 0;
            delayFlag = 1;
        }

use

if (delayCount == 0) { 
            delayCount = 4;  //delayTime = 4
            delayFlag = 1;
        }
and make the '++' into '--'

Note that you should declare 'delayFlag' as volatile and make sure some sensible values are in ledCount and delayCount at the start of your program or it could take longer than expected for the values to fall inside the test range.

Brian.


Thanks Brain For your valuable reply, I tried what you said--
For reference -->>
Code:
char timerInterruptFired() {
    return TMR0IF & TMR0IE;
}
is already there as you suggested , so I dont think its having a multiple timer problem.
Yaah I am getting the point that my printToArduino Function is getting delayed and for that I changed my code as you said but Its still stucking in same line as previous.

Let me tell you what I am trying to implement----->>
I actually trying to implement a delay function using timers instead of using __delay_ms() or __delay_us() functions which waste my cpu. That's why doing all this.

let me tell you how I am receiving data in arduino using I2c slave --->

Code:
#include <Wire.h>

void setup() {
  Wire.begin(9);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
  while (!Serial);
  Serial.println("Setup Complete!");
  
}
void loop() {       
  delay(100);
}
void receiveEvent(int howMany) {
  char c;
 
  while (Wire.available() > 0) { // loop through all but the last
     c = Wire.read(); // receive byte as a character
     if(c=='\n')
     Serial.println();
      else
    Serial.print(c);
  }
  
}
Where I am doing wrong, I am not getting ..... My timer is also not running as I calculated ...


let me tell you how my code looks like now ....

Code:
void Timer0Setup() {

    /*
     * FCPU=8MHz/4= 2MHz
     * TimePeriod = 1/2MHZ = 0.5 us.
     * PrescalerPeriod = TimePeriod x 256 = 128us;
     * OverflowPeriod = PrescalerPeriod x 2^8 = 32768us ;
     * So we need 1/OverflowPeriod = 30.51 ~ 31 - Overflows to count for 1 sec 
     * 
     * TMRO = 256 - (Delay time *fosc/ 4*prescaler value)  
     * 
     * taking Delay time = 10ms then 
     * 
     * TMR0 = 248
     */
    T0CON = 0xD7;
    TMR0IE = 1;
    RCONbits.IPEN = 1; // ENABLE interrupt priority
    TMR0 = 248;

}

void main() {
    OSCCON = 0x75; // INTOSC frequency 2MHz
    TRISBbits.RB7 = 0;
    UART_Init(9600);
    I2C_Master_Init(9600);
    InterruptSetup();
    Timer0Setup();
    while (1) {
        while (!delayFlag); // still stucking here 
        printToArduino("I am a delay!!");
    }
}


void interrupt interruptRoutine() // High priority interrupt
{
    if (timerInterruptFired()) {
        INTCONbits.T0IF = 0;
        TMR0 = 249;
        ledCount++;
        delayCount--;
        if (ledCount == blinkTime) { // blinkTime = 400; 
            ledCount = 0;
            blinkLed();
        }
        if (delayCount == 0) {
            delayCount = 300; //delayTime = 400
            delayFlag = 1;
        }
    }
}
actully I am calculating for 10ms , so I should wait for blinkTime =100 to get 1 sec delay in blinking but I am getting that in someting around blinkTime = 400;

kindly help-Thanks a lot brian...
 

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

I'm assuming the PIC is an 18F4520 and not 18F5420 in the subject of your thread.

Check your OSCCON value. 0x75 = 01110101
b7 = 0 = IDLEN (enter sleep mode with SLEEP instruction)
b6,b5,b4 = 111 = 8MHz (INTOSC drives clock directly)
b3 = 0 = OSTS (its read only so the bit value doesn't matter)
b2 = 1 = IOFS (also read only so it doesn't matter)
b1,b0 = 01 = Timer1 oscillator used as system clock.

So that doesn't look right at all.
I suggest the value should be 01010010 = 0x52 instead.

Brian.
 

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

I'm assuming the PIC is an 18F4520 and not 18F5420 in the subject of your thread.

Check your OSCCON value. 0x75 = 01110101
b7 = 0 = IDLEN (enter sleep mode with SLEEP instruction)
b6,b5,b4 = 111 = 8MHz (INTOSC drives clock directly)
b3 = 0 = OSTS (its read only so the bit value doesn't matter)
b2 = 1 = IOFS (also read only so it doesn't matter)
b1,b0 = 01 = Timer1 oscillator used as system clock.

So that doesn't look right at all.
I suggest the value should be 01010010 = 0x52 instead.

Brian.

I want to run my oscillator at 8MHz by setting OSCCON = 0x52, I am bound to use 2MHz instead. Please explain how do you think this will work, I am not getting. ....

-- Raushan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top