RCREG数据,PIC 18f4550不显示值

时间:2015-10-19 10:25:20

标签: pic rfid

我将RFID与PIC 18f4550接口,我尝试在LCD上显示RCREG的值,但只显示了一个框。可能是什么问题?

我正在使用带有XC8编译器的MPLAB X

// 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;
}

我一直收到一条错误,指出非法将指针转换为字符串的行,我在其中为card_id [i]指定了RCREG的值,而RCREG似乎正在返回地址

1 个答案:

答案 0 :(得分:0)

看门狗定时器已启用#pragma config WDT = ON但从未清除。定期调用ClrWdt();或禁用它,否则每次看门狗定时器到期时程序都会重置。

不会引起问题,但值得一提的是,使用XC8 Microchip建议使用#include <xc.h>,这将使得不需要包含处理器特定的标头。

RCREG中的字符正被移动到card_id的所有12个元素中,这是真的有意吗?

每次接收到一个字符时,UART都会置位一个中断并设置RCIF。读RCREG清除标志。在服务中断时执行任务(特别是有延迟的任务)通常不是一个好主意,因为可能会错过传入的字符。考虑建立一个循环队列并执行以下操作:

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

然后在main forever循环中检查队列。当有东西进来时(q1tail!= q1head)根据需要处理它并将其从队列中弹出(增加q1head)。

检查超限和框架错误也是一个好主意。检查队列指针时可以完成:

if (RCSTA1bits.OERR || RCSTA1bits.FERR)
{
  RCSTA1bits.CREN = 0;
  Nop();
  Nop();
  RCSTA1bits.CREN = 1;
}