Serially reading into PIC

Discussion in 'Embedded Systems and Microcontrollers' started by QMD, Mar 24, 2011.

  1. QMD

    Thread Starter Member

    Jun 30, 2010
    30
    0
    One of the aspects of my project is to have an RFID reader which will ouput an RFID tag's ID. I want to serially read this into a PIC 16F877A. Any tips? The manual is rather confusing :(
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Which manual - the PIC or the RFID reader's? What don't you understand?
     
  3. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    RFID reader are really not that hard to use there serial devices. Most use a baud rate of 2400 or 9600.

    Set the pic to receive at the right baud and enable the card reader is about all there is to it.

    You can test the card with a max232 to find the cards number.

    Here a look at it working http://www.youtube.com/watch?v=I4-TXLs4lbk
     
  4. QMD

    Thread Starter Member

    Jun 30, 2010
    30
    0
    Thanks. I was having difficulty understanding the PIC's manual. Do you know if there is any dummy code available for a simple serial read?

    In the meanwhile, I'll check out this video. Thanks
     
  5. QMD

    Thread Starter Member

    Jun 30, 2010
    30
    0
    Another issue we are currently facing is that our RFID reader is not reading as far as we desired...we are trying to read ~1m...but we are only getting 1 foot, at max. Any suggestions on places where one can get a UHF antenna? We need to change the antenna to one with a higher gain, in efforts to get more range
     
  6. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    This should give you a good idea of how to do this The code is by ericgibbs

    You'll have to change to match your carder baud rate

    Code ( (Unknown Language)):
    1.  
    2.     list        p=16f877A   ; list directive to define processor
    3.     #include    <p16f877A.inc>  ; processor specific variable definitions
    4.     errorlevel -302, -207
    5.  
    6.     __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF
    7.  
    8. bank0   macro
    9.     bcf 03h,5
    10.     bcf 03h,6       ; ram page 0
    11.     endm
    12.  
    13.  
    14.  
    15. bank1   macro
    16.     bsf 03h,5
    17.     bcf 03h,6       ; ram page 1
    18.     endm
    19.  
    20. ;---------------------------------------------------------------------------  
    21.  
    22.  
    23.  
    24. ;bit spen (rcsta<7>) and bits trisc<7:6> have tobe set  
    25.  
    26.  
    27.     bank0
    28.     bsf 18h,7
    29.     bsf 07h,6
    30.  
    31.     bank1
    32.  
    33.  
    34.     movlw   00h
    35.     movwf   88h
    36.     bsf 87h,7
    37.  
    38.  
    39.  
    40.  
    41. ;---------------------------------------------------------------------------  
    42.  
    43. ;1. initialize the spbrg register for the appropriate  
    44. ;baud rate. if a high speed baud rate is desired,  
    45. ;set bit brgh (section 10.1).  
    46.     bank1
    47.     bsf 98h,2
    48.  
    49.     movlw   0ch     ;baud rate = 19,2 k
    50.     movwf   99h     ;4 mhz
    51.  
    52.  
    53. ;---------------------------------------------------------------------------  
    54.  
    55. ;2. enable the asynchronous serial port by clearing  
    56. ;bit sync and setting bit spen.  
    57.  
    58.     bank1
    59.     bcf 98h,4
    60.  
    61.     bank0
    62.     bsf 18h,7
    63.  
    64. ;---------------------------------------------------------------------------  
    65.  
    66. ;3. If interrupts are desired, then set enable bit  
    67. ;RCIE..
    68.  
    69.  
    70.     bank1
    71.     bcf 8ch,5
    72.  
    73. ;---------------------------------------------------------------------------  
    74.  
    75. ;4. 4. If 9-bit reception is desired, then set bit RX9.  
    76.  
    77.     bank0
    78.     bcf 18h,6
    79.  
    80.  
    81. ;---------------------------------------------------------------------------  
    82.  
    83.  
    84. ; 5. Enable the reception by setting bit CREN.  
    85.     bank0
    86.     bsf 18h,4
    87.  
    88.  
    89.  
    90. ;---------------------------------------------------------------------------  
    91.  
    92. ;6. Flag bit RCIF will be set when reception is complete  
    93. ;and an interrupt will be generated if enable  
    94. ;bit RCIE is set.  
    95.  
    96.  
    97.  
    98. ;---------------------------------------------------------------------------  
    99.  
    100. ;7. Read the RCSTA register to get the ninth bit (if  
    101. ;enabled) and determine if any error occurred  
    102. ;during reception.  
    103.  
    104.  
    105.  
    106. ;---------------------------------------------------------------------------  
    107.  
    108.  
    109. ;8. Read the 8-bit received data by reading the  
    110. ;RCREG register.  
    111.  
    112. vrti
    113.     bank0
    114.     movf    RCREG,0     ;byte that I send to LED on PORTD
    115.     movwf   PORTD       ;txreg
    116.  
    117.  
    118.  
    119.     ;bank1
    120.  
    121. cekaj   btfss   PIR1,5
    122.     goto    cekaj
    123.  
    124.  
    125.  
    126.     goto    vrti
    127.  
    128. ;---------------------------------------------------------------------------  
    129.  
    130. ;8. if using interrupts, ensure that gie and peie  
    131. ;(bits 7 and 6) of the intcon register are set.  
    132.  
    133.  
    134.  
    135.     end  
    136.  
     
  7. QMD

    Thread Starter Member

    Jun 30, 2010
    30
    0

    Thanks..is there a C version available somewhere?
     
  8. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    It would be much easier to help you if you would tell what C you want to use there C library for UART
     
  9. QMD

    Thread Starter Member

    Jun 30, 2010
    30
    0
    I am using MPLAB, if that is what you meant.
     
  10. QMD

    Thread Starter Member

    Jun 30, 2010
    30
    0
    My friend and I figured out that this will output values from pin C6

    Code ( (Unknown Language)):
    1. #use rs232(baud=9600, xmit=PIN_C6, invert)
    Does anyone know the similar function that is used to read via an RX pin?

    Thanks
     
  11. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    If you would tell what C your using hi-tech, PWC, boostC,

    That sets the pin to sends data

    You want to receive data

    This sets both

    Code ( (Unknown Language)):
    1. #use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, stream=PC)
     
    Last edited: Mar 26, 2011
    QMD likes this.
  12. QMD

    Thread Starter Member

    Jun 30, 2010
    30
    0
    Oh my bad...I am using PIC-C

    I am under the assumption that I will still need a max232 IC for proper rs232 communication

    For output, I've been told that it is via a printf function, outputting via the specified pin
    Would I use a scanf to read data?
     
  13. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    You need the max from the pic to the PC I only have the demo of PIC-C and it ran out years ago I would use MikroC it doesn't have a time limit and is way easier to use.
    Code ( (Unknown Language)):
    1.  
    2.  
    3.   UART1_Init(9600);               // Initialize UART module at 9600 bps
    4.   Delay_ms(100);                  // Wait for UART module to stabilize
    5.  
    6.   UART1_Write_Text("Start");
    7.   while (1) {                     // Endless loop
    8.     if (UART1_Data_Ready()) {     // If data is received,
    9.       uart_rd = UART1_Read();     //   read the received data,
    10.       UART1_Write(uart_rd);       //   and send data via UART
    11.     }
    12.   }
    13. }
    14.  
     
    QMD likes this.
  14. QMD

    Thread Starter Member

    Jun 30, 2010
    30
    0
    Okay...so I have moved over to the dsPIC30f4011, as I need to do some serial I/O from two ports

    Here is some sample code I found online....but I get many errors...

    Code ( (Unknown Language)):
    1. /////////////////////////////////////////////////////////////////////////
    2. ////                             EX_SQW.C                            ////
    3. ////                                                                 ////
    4. ////  This program displays a message over the RS-232 and waits for  ////
    5. ////  any keypress to continue.  The program will then begin a 1khz  ////
    6. ////  square wave over I/O pin B4.                                   ////
    7. ////                                                                 ////
    8. ////  Comment out the printf's and getc to eliminate the RS232 and   ////
    9. ////  just output a square wave.                                     ////
    10. ////                                                                 ////
    11. ////  Change both delay_us to delay_ms to make the frequency 1 hz.   ////
    12. ////  This will be more visable on a LED.                            ////
    13. ////                                                                 ////
    14. ////                                                                 ////
    15. ////  Change the device, clock and RS232 pins for your hardware if   ////
    16. ////  needed.                                                        ////
    17. /////////////////////////////////////////////////////////////////////////
    18.  
    19. #include <p30F4011.h>
    20. #include <stdio.h>
    21. //#device ICD=TRUE  // For using the debugger, un-comment
    22.  
    23. #use delay(crystal=20mhz)
    24.  
    25.  
    26.  
    27. // UART1A specifies the alternate UART pins Pin_C13, Pin_C14
    28. // use UART1 to sprcify UART for pins Pin_F3, Pin_F2
    29.  
    30. #use rs232(baud=9600, UART1A)
    31.  
    32.  
    33. int main() {
    34.  
    35.    printf("Press any key to beginnr");
    36.    getc();
    37.    printf("1 khz signal activatednr");
    38.  
    39.    while (1) {
    40.      output_high(PIN_B4);
    41.      delay_us(500);
    42.      output_low(PIN_B4);
    43.      delay_us(500);
    44.    }
    45. return 0;
    46.  
    47. }
    48.  
    I also tried my own test code, with no avail:

    Code ( (Unknown Language)):
    1. [LEFT][FONT=arial]#include "p30f4011.h"
    2. #include <stdio.h>
    3. #use rs232(baud=9600, xmit=U1TX, [/FONT][FONT=arial][FONT=arial]rcv=U1RZ[/FONT][/FONT][FONT=arial])  //serial port output pin & baud rate[/FONT]
    4.  
    5.  
    6. // Set up the serial connection to use 9600 buad rate
    7. void setupUSART1()
    8. {
    9.        U1BRG = 11;                                        //set baud rate to ~9600bps at 7.37MHz (FRC oscillator)        
    10.        U1MODEbits.STSEL = 0;                //1 stop bit
    11.        U1MODEbits.PDSEL = 0;                //8-bit, no parity
    12.         U1MODEbits.UARTEN = 1;                //enable UART1
    13.        U1STAbits.UTXEN = 1;                //enable UART1 transmitter
    14.        U1STAbits.URXISEL = 3;                //Enable 4 character
    15.  }
    16.  
    17. int main(void)
    18. {
    19. setupUSART1();
    20. printf("Hello");
    21. return 0;
    22. }[/LEFT]
    23.  
    Both gave me an error regarding "heap" and that "#use" was an invalid preprocessing directive
     
  15. QMD

    Thread Starter Member

    Jun 30, 2010
    30
    0
    ok i figured out the issue regarding heap

    this program compiles, but no output..

    Code ( (Unknown Language)):
    1.  
    2. #include "p30f4011.h"
    3. #include <stdio.h>
    4.  
    5. void setupUSART1()
    6. {
    7.        U1BRG = 11;                                        //set baud rate to ~9600bps at 7.37MHz (FRC oscillator)        
    8.        U1MODEbits.STSEL = 0;                //1 stop bit
    9.        U1MODEbits.PDSEL = 0;                //8-bit, no parity
    10.        U1MODEbits.UARTEN = 1;                //enable UART1
    11.        U1STAbits.UTXEN = 1;                //enable UART1 transmitter
    12.        U1STAbits.URXISEL = 3;                //Enable 4 character
    13. }
    14.  
    15. int main()
    16. {
    17.        //Initialization
    18.      //  setupADC();
    19.    //    setupPWMs();
    20.           setupUSART1();
    21.        
    22.        printf("Starting up...\n\r");
    23.    //    delay();
    24.        
    25.        TRISBbits.TRISB2 = 0; //set Port B pin 2 to output
    26.        
    27.  
    28.        
    29.  
    30.        //main loop        
    31.        while (1)
    32.    {        printf("Hello");
    33. }
    34. return 0;
    35. }
    36.  
     
  16. QMD

    Thread Starter Member

    Jun 30, 2010
    30
    0
    Got it to work on the PIC16, will probably just stick to that for now.

    Any advice on reading? (as of now, I am planning to use the getc() function)
     
  17. QMD

    Thread Starter Member

    Jun 30, 2010
    30
    0

    The getc() function compiles, but it kills the program...others have had trouble with this as well it seems...any advice?
     
Loading...