0

I interfaced RFID with PIC 18f4550, I tried displaying values of RCREG on LCD, but only got a box displayed. What could be the problem?

I am using MPLAB X with the XC8 compiler

// Program to interface RFID module using EUSART in PIC18F2550 
#include <p18f4550.h>
#include <stdio.h>
/* _CPUDIV_OSC1_PLL2_1L,  // Divide clock by 2
   _FOSC_HS_1H,           // Select High Speed (HS) oscillator  
_WDT_OFF_2H,           // Watchdog Timer off
 MCLRE_ON_3H            // Master Clear on
*/
// CONFIG1L
#pragma config PLLDIV = 1       // PLL Prescaler Selection bits (No     prescale (4 MHz oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection   bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)

// CONFIG1H
#pragma config FOSC = INTOSC_HS // Oscillator Selection bits (Internal oscillator, HS oscillator used by USB (INTHS))
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON         // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting)
#pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)

// CONFIG2H
#pragma config WDT = ON         // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
//#pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is    multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
// #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
//#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
//#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

// CONFIG5H
 #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
//#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
 //#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
//#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
//#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
//#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
//#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF    
// Program to interface RFID module using EUSART in PIC18F4550

// Configuration bits
/* _CPUDIV_OSC1_PLL2_1L,  // Divide clock by 2
   _FOSC_HS_1H,           // Select High Speed (HS) oscillator
   _WDT_OFF_2H,           // Watchdog Timer off
   MCLRE_ON_3H            // Master Clear on
*/

#define FREQ 20000000
#define baud 9600
#define spbrg_value 19
#define rs LATAbits.LATA0
#define rw LATAbits.LATA1
#define en LATAbits.LATA2
#define lcdport LATB

unsigned char rx_data();
void lcd_ini();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
unsigned char data[]="Unique ID No.";
unsigned char card_id[12];
unsigned int i=0,j=0,pos;

void Delay_ms(int t)
{
    int i;
    int j;
    for (i=0;i<t;i++)
        for(j=0;j<100;j++);
}

void main()
{
    TRISB=0;            // Set Port B as output port
    LATB=0;
    TRISA=0;
     LATA=0;
    SPBRG=spbrg_value;      // Fill SPBRG register to set the baud rate
    RCSTAbits.SPEN=1;           // To activate serial port (Tx and Rx pins)                                    
     RCSTAbits.CREN=1;          // To enable continuous reception
    PIE1bits.RCIE=1;            // To enable the Reception (Rx) Interrupt
    INTCONbits.GIE=1;
    INTCONbits.PEIE=1;
    lcd_ini();          // LCD initialization
    while(data[i]!='\0')
    {
        //lcddata(data[i]); // To send characters one by one from 'data' array
        i++;
    }
        while(1)
    {
        i=0;
        while(PIR1bits.RCIF ==0);
    }
}

void interrupt ISR()
{
    i=0;
    unsigned char c = 'P';
    lcddata(c);
    while(i<12)
    {
        card_id[i]=RCREG;
        lcddata(card_id[i]);
        i++;
    }
card_id[11]='\0';
    /*while(i<12)
    {
         char cc=card_id[i];
     lcddata(cc);
    i++;// Print the 12 byte received 
     }}
    while(data[i]!='\0'){
        lcddata(data[i]);
        i++;
    }*/
    }

void lcd_ini()
{
    lcdcmd(0x38);       // Configure the LCD in 8-bit mode, 2 line and 5x7 font
    lcdcmd(0x0C);       // Display On and Cursor Off
    lcdcmd(0x01);       // Clear display screen
    lcdcmd(0x06);       // Increment cursor
    lcdcmd(0x80);       // Set cursor position to 1st line, 1st column
}

void lcdcmd(unsigned char cmdout)
{
    lcdport=cmdout;     //Send command to lcdport=PORTB
    rs=0;                       
    rw=0;
    en=1;
    Delay_ms(10);
    en=0;
}

void lcddata(unsigned char dataout)
{
    lcdport=dataout;    //Send data to lcdport=PORTB
    rs=1;
    rw=0;
    en=1;
    Delay_ms(10);
    en=0;
}

I keep getting an error saying illegal conversion of pointer to string on the line where i assign card_id[i] the value of RCREG and it seems that RCREG is returning the address

  • anything?! How are we going to guess what's wrong if you don't tell us anything but the CPU model?! This is really worse than asking "I used a hammer to build my house. The electricity on the first floor doesn't work. What's the problem?" – Marcus Müller Oct 19 '15 at 10:31
  • I hope the code helps. Sorry about the earlier post – Aditya Ganesh Oct 19 '15 at 10:44
  • no, still not telling what hardware, what you've tried to narrow down the problem, and why on earth are you using stdio on a microcontroller!? – Marcus Müller Oct 19 '15 at 10:46
  • Its a PIC 18f4550 programmed using the PICkit2, interfaced with a RFID reader (local ,so im pretty sure you wont recognise it) http://robokitsworld.com/downloads/Robokits_RFID_UART.pdf is a link to the RFID reader i am using I am using MPlabX and XC8 compiler. Ignore the stdio ,i put it our of frustration It is displaying small boxes on the LCD. Which i know is working – Aditya Ganesh Oct 19 '15 at 10:47

1 Answers1

0

The watchdog timer is enabled #pragma config WDT = ON but never cleared. Periodically call ClrWdt(); or disable it, otherwise the program will reset every time the watchdog timer expires.

Not causing a problem but worth mentioning, with XC8 Microchip recommends using #include <xc.h> which will make including the processor specific header unnecessary.

The character that is in RCREG is being moved into all 12 elements of card_id, was that really intended?

The UART will assert an interrupt and set RCIF each time a character is received. Reading RCREG clears the flag. It's generally not a good idea to do tasks (especially ones with delays) while servicing the interrupt because incoming characters can be missed. Consider establishing a circular queue and doing something like this:

void interrupt ISR()
{
  while(PIR1bits.RC1IF){
    queue1[q1tail] = RCREG1;
    if(++q1tail >= Q_SIZE)
        q1tail = 0;
  }
}

Then in the main forever loop check the queue. When something comes in (q1tail != q1head) process it as necessary and pop it off the queue (increment q1head).

It's also a good idea to check for overrun and framing errors. It can be done when checking the queue pointers:

if (RCSTA1bits.OERR || RCSTA1bits.FERR)
{
  RCSTA1bits.CREN = 0;
  Nop();
  Nop();
  RCSTA1bits.CREN = 1;
}
jolati
  • 700
  • 11
  • 19
  • Hey thanks :D .The thing is I'm not that experienced . The primary error I am facing is that I keep getting returned an error saying illegal conversion of pointer to integer. Why is RCREG returning a pointer ? If I write card_id[i] = *RCREG .I am getting an error saying pointer required. Do excuse me if my doubts are a bit too amateur. – Aditya Ganesh Oct 20 '15 at 08:57
  • Your code compiles here w/o error (MPLAB X 3.1 with XC8 Windows v1.33). Maybe try using xc.h if you haven't. I get `extern volatile unsigned char RCREG @ 0xFAE;` when I right click on it and Navigate to Definition which sounds right. – jolati Oct 20 '15 at 14:15
  • The variable i is declared unsigned int globally. Does declaring it locally within the ISR make a difference? – jolati Oct 20 '15 at 14:33
  • It compiles on my PC as well .But the LED displays the same character irrespective of the RFID card I use I have been unable to get any reading material on the RCREG register. How does the RCREG register work ? – Aditya Ganesh Oct 20 '15 at 18:16
  • Its LCD not LED. I am sorry – Aditya Ganesh Oct 20 '15 at 18:25
  • You stated "I am getting an error saying pointer required." That is a compiler error. What did you mean? As for RCREG download the datasheet for the PIC18F4550 from Microchip. 20.1.3 "The value in the RCREG needs to be read to clear the RCIF interrupt." Look at the snippet above. Read once per interrupt flag set. Show your corrected code. – jolati Oct 20 '15 at 18:45