2015-10-19 76 views
0

RFID接口与PIC 18F4550,我试图在LCD上显示RCREG的价值,而只是显示了一个盒子。可能是什么问题呢?RCREG数据,PIC 18F4550不显示值

我使用的MPLAB X与XC8编译

// 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分配card_id的[I] RCREG的价值,似乎RCREG的返回地址

+0

什么?如果你不告诉我们除了CPU模型之外的任何东西,我们如何猜测出现了什么问题?这实在比问“我用锤子打造我的房子更糟糕,一楼的电不起作用,这有什么问题?” –

+0

我希望代码有帮助。对不起,前面的文章 –

+0

没有,还没有告诉硬件,你试图缩小问题的范围,以及为什么你在微控制器上使用stdio?! –

回答

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

然后在永久循环中检查队列。当有事情发生时(q1tail!= q1head)根据需要处理它,并将其从队列中弹出(递增q1head)。

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

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

嘿谢谢:D。事情是我没那么有经验。我面临的主要错误是我不断返回一个错误,指出非法将指针转换为整数。为什么RCREG返回一个指针?如果我写card_id [i] = * RCREG。我收到一个错误,说需要指针。 请原谅我,如果我的疑虑有点太业余。 –

+0

您的代码在这里编译时没有错误(MPLAB X 3.1与XC8 Windows v1.33)。也许尝试使用xc.h,如果你没有。我得到'extern volatile unsigned char RCREG @ 0xFAE;'当我右键点击它并导航到定义时,听起来是正确的。 – jolati

+0

变量i被全局声明为unsigned int。在ISR中本地声明是否有所作为? – jolati