RfID reader C code

Discussion in 'Embedded Systems and Microcontrollers' started by LauraB, Dec 15, 2011.

  1. LauraB

    Thread Starter Member

    Dec 15, 2011
    43
    0
    Hey Everyone
    I've built the circuit using PIC16F887 (on PicKit 3) and Micro RWD MF-IC.
    http://www.ibtechnology.co.uk/pdf/MF_ICprot.pdf
    I have this
    Code ( (Unknown Language)):
    1. Kod C - [rozwiń]
    2. #include    <pic.h>
    3. #include    "sci.h"
    4. void main(void)
    5. {
    6. // 8-bit mode at 9600 baud using polling
    7. unsigned char data,a=0;
    8. sci_Init(9600,SCI_EIGHT);
    9. sci_PutByte(0x55);  /* send 0xaa when device is ready */
    10. data = sci_GetByte();   /* read data when device is ready */
    11. a=data;
    12. }
    Code ( (Unknown Language)):
    1. #include <pic.h>
    2. #include "sci.h"
    3.  
    4. /* Routines for initialisation and use of the SCI
    5.  * for the PIC processor.
    6.  */
    7.  
    8. /* other options:
    9.  * frame errors
    10.  */
    11.  
    12. unsigned char
    13. sci_Init(unsigned long int baud, unsigned char ninebits)
    14. {
    15.     int X;
    16.     unsigned long tmp;
    17.  
    18.     /* calculate and set baud rate register */
    19.     /* for asynchronous mode */
    20.     tmp = 16UL * baud;
    21.     X = (int)(FOSC/tmp) - 1;
    22.     if((X>255) || (X<0))
    23.     {
    24.         tmp = 64UL * baud;
    25.         X = (int)(FOSC/tmp) - 1;
    26.         if((X>255) || (X<0))
    27.         {
    28.             return 1;   /* panic - baud rate unobtainable */
    29.         }
    30.         else
    31.             BRGH = 0;   /* low baud rate */
    32.     }
    33.     else
    34.         BRGH = 1;   /* high baud rate */
    35.     SPBRG = X;  /* set the baud rate */
    36.  
    37.     SYNC = 0;   /* asynchronous */
    38.     SPEN = 1;   /* enable serial port pins */
    39.     CREN = 1;   /* enable reception */
    40.     SREN = 0;   /* no effect */
    41.     TXIE = 0;   /* disable tx interrupts */
    42.     RCIE = 0;   /* disable rx interrupts */
    43.     TX9  = ninebits?1:0;    /* 8- or 9-bit transmission */
    44.     RX9  = ninebits?1:0;    /* 8- or 9-bit reception */
    45.     TXEN = 1;   /* enable the transmitter */
    46.  
    47.     return 0;
    48. }
    49.  
    50. void
    51. sci_PutByte(unsigned char byte)
    52. {
    53.     while(!TXIF)    /* set when register is empty */
    54.         continue;
    55.     TXREG = byte;
    56.  
    57.     return;
    58. }
    59.  
    60. unsigned char
    61. sci_GetByte(void)
    62. {
    63.     while(!RCIF)    /* set when register is not empty */
    64.         continue;
    65.  
    66.     return RCREG;   /* RXD9 and FERR are gone now */
    67. }
    68.  
    69. unsigned char
    70. sci_CheckOERR(void)
    71. {
    72.     if(OERR)    /* re-enable after overrun error */
    73.     {
    74.         CREN = 0;
    75.         CREN = 1;
    76.         return 1;
    77.     }
    78.  
    79.     return 0;
    80. }
    81.  
    82. #define sci_PutNinth(bitnine)   (TX9D = bitnine?1:0
    83.  
    84. unsigned char
    85. sci_GetNinth(void)
    86. {
    87.     while(!RCIF)
    88.         continue;
    89.  
    90.     return RX9D;    /* RCIF is not cleared until RCREG is read */
    91. }
    92.  
    93. unsigned char
    94. sci_GetFERR(void)
    95. {
    96.     while(!RCIF)
    97.         continue;
    98.  
    99.     return FERR;    /* RCIF is not cleared until RCREG is read */
    100. }
    101.  
    102.  
    103.  
    Code ( (Unknown Language)):
    1. #define FOSC    (4000000L)
    2. #define SCI_EIGHT   (0)
    3. #define SCI_NINE    (1)
    4.  
    5. unsigned char   sci_Init(unsigned long int, unsigned char);
    6. void        sci_PutByte(unsigned char);
    7. unsigned char   sci_GetByte(void);
    8. void        sci_PutNinth(unsigned char);
    9. unsigned char   sci_GetNinth(void);
    10. unsigned char   sci_GetFERR(void);
    11. unsigned char   sci_CheckOERR(void);
    12.  
    13.  
    14.  
    But I can't program it. Maybe somebody had a similar project or wants to help me?

    Regards,
    Laura
     
Loading...