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.

UART RX interrupt is not working(PIC18f24j11)

Status
Not open for further replies.

kirangkr

Newbie level 6
Joined
Sep 19, 2014
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
183
Hello All,

This is first time i'm posting a thread in edaboard, if i made any mistake please excuse me.
For my new project I'm using PIC18f24j11. I have configured UART and my TX is working fine. But my RX is not working. While debugging I saw no data is entering to RCREG register and hence I'm not getting interrupt. Please see my code and advice if i miss out something!

Best regards
Kirangkr





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
#include <htc.h>                                                               //PIC hardware mapping 
#include <p18f24j11.h> 
#include <xc.h>
 #include <delays.h>
#include <usart.h>
#include<stdio.h>
#include    <stdarg.h>
#define _XTAL_FREQ 2000000                                     //FCPU =Fosc/4
#define FOSC 8000000                                                    // Fosc 8MHz 
 
 
 #pragma config WDTEN = OFF
 #pragma config STVREN = OFF
 #pragma config XINST = OFF
 #pragma config CP0 = OFF
 #pragma config OSC = INTOSC
 #pragma config T1DIG = OFF
 #pragma config LPT1OSC = OFF
 #pragma config FCMEN = OFF
 #pragma config IESO = OFF
 #pragma config DSBOREN = OFF
 #pragma config DSWDTEN = OFF
 #pragma config IOL1WAY = OFF
 
 
/*****************************************************************************/
/*                      Local Macro or Enum Definition                       */
/*****************************************************************************/
#define PIR_DEBOUNCE  4 //400ms
 
#define BLE_SLEEP_CTRL_PIN  PORTCbits.RC4
#define PIR_STATUS          PORTBbits.RB0
 
//typedef enum {FALSE = 0, TRUE = !FALSE} bool_t;
 
typedef enum
{
    PIR_STATE_FIRST_TRIGGER_CHECK = 0,
    PIR_STATE_INIT,
    PIR_STATE_COUNTING_LOW,
    PIR_STATE_COUNTING_HIGH
} pir_state_t;
 
/*****************************************************************************/
/*                        Local Function Declaration                         */
/*****************************************************************************/
 
void init_uart(void);
void UART_TX(unsigned char x);
void uart_send(void);
 
pir_state_t pir_state_frist_trigger_check(void);
pir_state_t pir_state_init(void);
pir_state_t pir_state_counting_low(void);
pir_state_t pir_state_counting_high(void);
 
 
/*****************************************************************************/
/*                   Local Structure or Union Definition                     */
/*****************************************************************************/
//union Timers
//{
//  unsigned int lt;
//  char bt[2];
//};
 
pir_state_t (*p_pir_state[])(void) = 
{
    pir_state_frist_trigger_check,
    pir_state_init,
    pir_state_counting_low,
    pir_state_counting_high
};
 
 /*****************************************************************************/
/*                        Local Variable Definition                          */
/*****************************************************************************/
//bool_t is_report_time_reached = FALSE;
pir_state_t pir_state = PIR_STATE_FIRST_TRIGGER_CHECK;
unsigned int total_time = 0;
unsigned int count_timer_value = 0; //100ms time base (timer3)
 
unsigned int a = 0;
unsigned int b = 0;
unsigned int c = 0;
unsigned int d = 0;
unsigned int e = 0;
 
unsigned char UART_DATA = 0x05;
unsigned char counter = 0; 
unsigned char sec =0;
unsigned short cntr = 0;
unsigned char T1 = 0;
int i =0;
int k = 0;
int frq = 0;
char send[40];
char *ptr;
volatile unsigned char Rx_data[40];
unsigned int time;
 
void delay100ms(unsigned char value)
{
    while(value--)
    {
        __delay_ms(100);
    }
}
 
 
 
void uart_tx_string(char *p_string)
{
    while (*p_string != 0)
    {
        UART_TX((unsigned char)(*p_string));
        p_string++;
    }
}
 
void main ()
{
      OSCCON =0b01110111;   // 8Mhz internal oscillator
      PCFG12 = 1;
    PCFG11 = 1;
    PCFG10 = 1;
    PCFG9   = 1;
    PCFG8   = 1;
       TRISCbits.TRISC4 = 0;   //Set RC4 as output pin; 
     RC4 = 1;
     init_uart();
 //   TRISBbits.TRISB0 = 1;   //Set RB0 as input pin; 
//  RB0 = 0;
//  T1GCON = 0b1110000;
    TMR1GE = 0;
    T1CON = 0b10001111;  //Fosc/4 and Prescale 1:1
    TMR1H = 0;
    TMR1L = 0;
    IPEN =1;
    TMR1IF = 0;      // Timer1 interrupt flag cleared 
    TMR1IE = 1;      //Timer1 interrupt enabled
    TMR1IP = 1;
    PEIE = 1;      // Peripheral interrupt enabled
       GIE =1;       // Enabled interrupt globally
    INT0IF = 0;    //INT0  flag cleared
    INT0IE =1;     
      INTEDG0 = 1;
 
 
    uart_tx_string("AT\r");
    __delay_ms(300);
    uart_tx_string("ATA\r");
  
    TMR0ON = 0;    //Timer0 disabled 
 
    while (1)
    {   
                
        //RC4 = 0;
        if (frq == 1)
        {
        //  RC4 = 1;
              __delay_ms(3);
 
            sprintf(send, "A = %d, B = %d, C = %d, D = %d, E = %d \r\n", a, b, c, d, e);
                   uart_tx_string(send);
            
        
                  __delay_ms(1);    
                   a = 0; b= 0; c=0; d = 0; e =0;frq =0;    
 
        
             }
 
            RC4 = 0;
                    sec =0;
            asm("sleep");
    //  }
       
        // Check PIR
        pir_state = (*p_pir_state[pir_state])();
 
 
    }
}
 
 
void interrupt ISR()
{
       if(RC1IF && RC1IE  )
       {
  //        Rx_data = RCREG1;
 
            if(DataRdy1USART())
           {
             Rx_data[i++] = Read1USART(); //read the byte from rx register
    
            if(i > 5)
             {
               i = 0;
              }
        RC1IF = 0; // clear rx flag      
        }
 
              
    }
 
 
    if(TMR1IF)
    {
            cntr++;
          
        if (cntr >10) // check for 15 minutes
        {
           RC4 = 1; 
           frq = 1;
           T1++;    
           cntr =0;
            } 
        TMR1IF = 0;     
     }
     
    if (PIR2bits.TMR3IF)
    {
        PIR2bits.TMR3IF = 0;
 
        WriteTimer3(63897);
 
        count_timer_value++;    
    }
    
       else if(INT0IF)
    {
        k = 1;
        INT0IF = 0;
    }
    else if(TMR0IE && TMR0IF)
       {
        counter++;           // counter increment 
 
              if (counter == 61)    // check for 1 sec
            {
            sec++;
                      counter = 0;     // Reset counter
            }
        TMR0IF = 0;        // Clear Flag
       }
 
}
void uart_send(void)
{
   TXSTAbits.TXEN=1;                 // enable transmission
    ptr = send;                       // Initialize pointer
    while (*ptr) {                // Loop until end of string
        if (TRMT == 1) {       // Xmtr buffer empty?
            TXREG = *ptr;     // Yes - load next char
            ptr++;                // Point to next char
            }
        }
}
 
void init_uart(void) // init UART module for 9600bps boud, start bit 1, stopbit 1, parity NONE
{
  
     TRISCbits.TRISC7=1; //Make UART RX pin input
     TRISCbits.TRISC6=0; //Make UART TX pin output
     SPBRGH = 0x00;
     SPBRG   = 0xCF;
//    SPBRG   =   0x10;          
     TXSTAbits.SYNC= 0;  //0 = Asynchronous mode
     RCSTA1bits.SPEN=1;   //1 = Serial port enabled (configures RX/DT and TX/CK pins as serial port pins)
     RC1IF = 0; //reset RX pin flag
     TX1IE = 0; //Disable TX interrupt
      RC1IE = 1; //Enable RX interrupt
      RC1IP = 1; //1 =  high priority
      RCSTA1bits.CREN=1;   //1 = Enables receiver
    
    BAUDCONbits.BRG16=1;//1 = 16-bit Baud Rate Generator ?SPBRG AND SPBRGH
 
  
    TXSTAbits.BRGH=1;  //1 = HIGH Speed
    TXSTAbits.TXEN=1;  //1 = Transmit enabled
    
}
void UART_TX(unsigned char c)
{
  TXSTAbits.TXEN=0;                  // disable transmission
  TXREG=c;                                  // load txreg with data
  TXSTAbits.TXEN=1;                 // enable transmission
  while(TXSTAbits.TRMT==0)    // wait here till transmit complete
  {
    Nop();
  }
}

 
Last edited by a moderator:

Might be any problem with baud rate or stop bit configuration, test it in loop back mode.
Connect your TX pin to RX pin transmit some data try out the RX data..


Code C - [expand]
1
if(RC1IF && RC1IE  )



what are the two bits?


Code C - [expand]
1
if(RC1IF)



Try only Interrupt flag.
 

Might be any problem with baud rate or stop bit configuration, test it in loop back mode.
Connect your TX pin to RX pin transmit some data try out the RX data..


Code C - [expand]
1
if(RC1IF && RC1IE  )



what are the two bits?


Code C - [expand]
1
if(RC1IF)



Try only Interrupt flag.

I have tried RC1IF alone, but the result is same!!
 

Hi Venkadesh,

Thank you for your reply.

I have tried RC1IF alone and the result is same!

Best reegards
Kirangkr
 

Yes i have connected my TX pin to RX pin. While debugging I'm getting data in TXREG but in RCREG i'm not getting any data:(
 

you have seen the data in tx reg but did you see that on pin ?

It doesnt mean its transmitted.

Connect the output to hyperterminal and verify it.

what are the other futures sticks with the UART pin?
 

Yes i have seen the data in hyper terminal, also I can see the data on RX line from external module, but my PIC MCU is not receiving the data
 

Hi,
Try enable peripheral interrupt and global interrupt bit also.
 

Hi Sneha,

Those bits are already enabled!!

Regards
Kirangkr
 

Just an idea... what compiler are you using? You seem to include 'htc' and 'xc' headers and if you are using XC you shouldn't need the others as well. Maybe a clash of definitions.

Brian.
 

Hi betwixt,

Im using XC8 V1.32 compiler.
 

verify interrupt operation with a timer interrupt

check and ensure the proper signal in rx pin

check the pin number

check the other operations in the rx pin

if you still find everything right then surely problem is configuration

- - - Updated - - -

Also you can write a small program using polling to test the uart module.
 

Unfortunately I do not have the compilers or documentation with me at the moment but please verify that you do not have to qualify the bit names with their registers. For example, should 'RC1IE' be 'PIR1.RC1IE'. I think adding 'xc.h' is the only header you need to use.

I use both serial ports on 18F46J11 without problems although I use a different compiler.

Brian.
 
Last edited:

Hi betwixt, Venkadesh,

I write a simple program to receive the data from PC and send it back to PC. While debugging I'm getting data in RCREG1 register and it also sets RC1IF interrupt flag, but the control is not going into the "ïf loop-(if(PIR1bits.RC1IF)" .As said I'm using XC8 compiler V1.32. Please see the following and advice if i miss out something

Best regards
Kirangkr




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
#include <xc.h>
 
#define _XTAL_FREQ 2000000                                     //FCPU =Fosc/4
#define FOSC 8000000                                                    // Fosc 8MHz 
 
 
 #pragma config WDTEN = OFF
 #pragma config STVREN = OFF
 #pragma config XINST = OFF
 #pragma config CP0 = OFF
 #pragma config OSC = INTOSC
 #pragma config T1DIG = OFF
 #pragma config LPT1OSC = OFF
 #pragma config FCMEN = OFF
 #pragma config IESO = OFF
 #pragma config DSBOREN = OFF
 #pragma config DSWDTEN = OFF
 #pragma config IOL1WAY = OFF
 
 
void init_uart(void);
void UART_TX(unsigned char x);
void uart_send(void);
 
 
char *ptr;
unsigned char Rx_data;
 
 
void uart_tx_string(char *p_string)
{
    while (*p_string != 0)
    {
        UART_TX((unsigned char)(*p_string));
        p_string++;
    }
}
 
 
void main ()
{
      OSCCON =0b01110111;   // 8Mhz internal oscillator
 //     RCONbits.IPEN =1;
      INTCONbits.GIE =1;       // Enabled interrupt globally
      INTCONbits.PEIE = 1;      // Peripheral interrupt enabled
       
     init_uart();
       uart_tx_string("AT\r");
 //   __delay_ms(1);
    uart_tx_string("ATA\r");
       while (1)
    {
               if(PIR1bits.RC1IF)
                   {
                  PIR1bits.RC1IF = 0;
            Rx_data = RCREG1;
                 __delay_ms(1);
                  uart_tx_string(Rx_data);
                   __delay_ms(1);
             PIE1bits.RC1IE =1;
                    }
 
           }
}
 
 
 
 
void init_uart(void) // init UART module for 9600bps boud, start bit 1, stopbit 1, parity NONE
{
  
     TRISCbits.TRISC7=1; //Make UART RX pin input
     TRISCbits.TRISC6=0; //Make UART TX pin output
     SPBRGH1 = 0x00;
     SPBRG1 = 0xCF;
//    SPBRG   =   0x10;          
     TXSTA1bits.SYNC= 0;  //0 = Asynchronous mode
     RCSTA1bits.SPEN=1;   //1 = Serial port enabled (configures RX/DT and TX/CK pins as serial port pins)
     PIR1bits.RC1IF = 0; //reset RX pin flag
     PIE1bits.TX1IE = 0; //Disable TX interrupt
     PIE1bits.RC1IE= 1; //Enable RX interrupt
     IPR1bits.RC1IP = 1; //1 =  high priority
     RCSTA1bits.CREN=1;   //1 = Enables receiver
    
     BAUDCON1bits.BRG16=1;//1 = 16-bit Baud Rate Generator ?SPBRG AND SPBRGH
 
    TXSTA1bits.BRGH=1;  //1 = HIGH Speed
    TXSTA1bits.TXEN=1;  //1 = Transmit enabled
    
}
 
void UART_TX(unsigned char c)
{
  TXSTA1bits.TXEN=0;                  // disable transmission
  TXREG1=c;                                  // load txreg with data
  TXSTA1bits.TXEN=1;                 // enable transmission
  while(TXSTA1bits.TRMT==0)    // wait here till transmit complete
  {
    Nop();
  }
}

 
Last edited by a moderator:

Now I'm getting confused....... (which is quite easy for me)

You are setting up the serial interrupt but then polling the interrupt flag in the main() routine. When the RC1IF bit is set it should be cleared and the RX data read in the ISR but you don't appear to have one!

Brian.
 

How can you get data? You have recieved a byte and want to send it as a string!!!
How do you saying flag is set but not entering if condition, by not receiving data???
 

Hi Betwixt,

Thank you for the input, now it is working perfectly! Please see the code

Regards
Kirangkr


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
#include <htc.h>                                                               //PIC hardware mapping 
#include <p18f24j11.h> 
#include <p18cxxx.h>
#include <xc.h>
#include <delays.h>
#include <usart.h>
#include<stdio.h>
#include    <stdarg.h>
#define _XTAL_FREQ 2000000                                     //FCPU =Fosc/4
#define FOSC 8000000                                                    // Fosc 8MHz 
 
 
 #pragma config WDTEN = OFF
 #pragma config STVREN = OFF
 #pragma config XINST = OFF
 #pragma config CP0 = OFF
 #pragma config OSC = INTOSC
 #pragma config T1DIG = OFF
 #pragma config LPT1OSC = OFF
 #pragma config FCMEN = OFF
 #pragma config IESO = OFF
 #pragma config DSBOREN = OFF
 #pragma config DSWDTEN = OFF
 #pragma config IOL1WAY = OFF
 
 
void init_uart(void);
void UART_TX(unsigned char x);
void uart_send(void);
 
 
char *ptr;
volatile unsigned char Rx_data[20];
int i =0;
 
 
void uart_tx_string(char *p_string)
{
    while (*p_string != 0)
    {
        UART_TX((unsigned char)(*p_string));
        p_string++;
    }
}
 
 
void main ()
{
      OSCCON =0b01110111;   // 8Mhz internal oscillator
      RCONbits.IPEN =1;
      INTCONbits.GIE =1;       // Enabled interrupt globally
      INTCONbits.PEIE = 1;      // Peripheral interrupt enabled
       
     init_uart();
 
 __delay_ms(300);
 __delay_ms(300);
 __delay_ms(300);
 __delay_ms(300);
 __delay_ms(300);
 __delay_ms(300);
 __delay_ms(300);
 __delay_ms(300);
__delay_ms(300);
 
       uart_tx_string("AT\r");
    __delay_ms(1);
    uart_tx_string("ATA\r");
       while (1)
    {
       }
}
 
 
void interrupt ISR()
{
 
            if(DataRdy1USART())
                   {
                    PIR1bits.RC1IF = 0;
                    Rx_data[i++] = Read1USART();
             if (i== 5)
                      {
                 i =0;
                        uart_tx_string(Rx_data);
                      }
               PIE1bits.RC1IE =1;
                    }
}
 
void init_uart(void) // init UART module for 9600bps boud, start bit 1, stopbit 1, parity NONE
{
  
     TRISCbits.TRISC7=1; //Make UART RX pin input
     TRISCbits.TRISC6=0; //Make UART TX pin output
     SPBRGH1 = 0x00;
     SPBRG1 = 0xCF;
//    SPBRG   =   0x10;          
     TXSTA1bits.SYNC= 0;  //0 = Asynchronous mode
     RCSTA1bits.SPEN=1;   //1 = Serial port enabled (configures RX/DT and TX/CK pins as serial port pins)
     PIR1bits.RC1IF = 0; //reset RX pin flag
     PIE1bits.TX1IE = 0; //Disable TX interrupt
     PIE1bits.RC1IE= 1; //Enable RX interrupt
     IPR1bits.RC1IP = 1; //1 =  high priority
     RCSTA1bits.CREN=1;   //1 = Enables receiver
    
     BAUDCON1bits.BRG16=1;//1 = 16-bit Baud Rate Generator ?SPBRG AND SPBRGH
 
    TXSTA1bits.BRGH=1;  //1 = HIGH Speed
    TXSTA1bits.TXEN=1;  //1 = Transmit enabled
    
}
 
void UART_TX(unsigned char c)
{
  TXSTA1bits.TXEN=0;                  // disable transmission
  TXREG1=c;                                  // load txreg with data
  TXSTA1bits.TXEN=1;                 // enable transmission
  while(TXSTA1bits.TRMT==0)    // wait here till transmit complete
  {
    Nop();
  }
}

 
Last edited by a moderator:

Hi Venkadesh,

I was checking for interrupt flag in main loop, this was the problem. When i write ISR it is working fine!! Thanks !
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top