PIC 18f1220 EUSART Baud Rate Cache Problem

Discussion in 'Embedded Systems and Microcontrollers' started by wklose99, Apr 14, 2008.

  1. wklose99

    Thread Starter New Member

    Apr 14, 2008
    4
    0
    I'm in the middle of a project I've been working on for about 3 weeks.
    What I have is an ID-12 RFID reader(http://www.sparkfun.com/commerce/product_info.php?products_id=8419) to read passive RFID tags. The reader outputs at 9600 baud. However, I need to send the data output from the ID-12 reader through a wireless RF transmitter(http://www.sparkfun.com/commerce/product_info.php?products_id=7816) which takes in data at a max of 4800 baud. I am using the built in USART Rx/Tx pins on a PIC 18F1220 to accomplish this cache job. What I am having trouble programming in MPLAB using C language is a routine for setting the baud rate to 9600, receiving all the bits, then changing the baud to 4800, and sending the bits.

    I thought we had the send/receive part down in the code and were just using improper SPBRG values, however it turns out we were using the correct SPBRG values but our code was faulty. I know our SPBRG values work properly and our clock source is alright because we wrote tests for the pic which worked both at 9600 baud and 4800 baud. Our old cache code was faulty mainly due to the interrupts that were being used. So we have given up on the interrupt driven USART and have decided to just do a polled while loop. Since all we need the PIC18f1220 for is caching this ID-12 output we decided interrupts are not essential. We spent hours combing through the datasheet and setting values manually but decided to use the usart.h library functions instead. Below is the
    code we are trying to get working, however for some reason it is not. If someone would take a look at it and suggest what we are doing wrong I would be very appreciative.

    Thanks much, -WK


    Specs on project:
    PIC: 18f1220
    Compiler: C18 C-language compiler used in MPLAB
    Computer OS: Windows XP


    Code:

    Code ( (Unknown Language)):
    1.  
    2. //*
    3.  * The following application is a cache program for converting a 9600 baud RFID card reader output
    4.  * to 4800 baud so it can be fed into a RFID transmitter input. The clock is 8Mhz, BRGH is 1, and BRG16 is 0.
    5.  * The PIC used is an 18f1220.
    6.  * The progam is compiled in C using the MC18 compiler on a Windows XP laptop.
    7.  */
    8. #include <p18f1220.h>
    9. #include <usart.h>
    10.  
    11.  
    12. #pragma config OSC = HS     //external crystal clocked at 8Mhz
    13. #pragma config WDT = OFF    
    14. #pragma config LVP = OFF
    15.  
    16. void main (void)
    17. {
    18.     unsigned char c;            //grab the transmitted char in here
    19.     unsigned char myCache[16];    //we will cache the input in here
    20.     unsigned int counter=0;     //This keeps track of which byte were on
    21.     int j = 0;
    22.  
    23.     //The next 5 initializations are set according to the PIC datasheet
    24.     //requirements for USART recieve/trans
    25.  
    26.     TRISBbits.TRISB1 = 1;    //TX set to output
    27.     TRISBbits.TRISB4 = 1;   //RX set to 1, but will be set to 0 by PIC for input(as specified in datasheet)
    28.     ADCON1bits.PCFG6 = 1;    //not sure what these next two are, but setting as specified in datasheet
    29.     ADCON1bits.PCFG5 = 1;
    30.     RCSTAbits.SPEN = 1;        //setting serial port enabled
    31.    
    32.     TXSTAbits.SYNC = 0;            //asynchronous mode
    33. //    PIE1.TXIE = 0;
    34.  
    35.     BAUDCTLbits.BRG16 = 0;    //8 bit transfer
    36.     TXSTAbits.BRGH = 1;        //BRGH high
    37.  
    38.     TXSTAbits.TXEN = 1;        //enable transmit
    39.  
    40.   /*
    41.    * Open the USART configured as
    42.    * 8N1, 96000 baud, in polled mode
    43.    */
    44.   OpenUSART (USART_TX_INT_OFF &
    45.              USART_RX_INT_OFF &
    46.              USART_ASYNCH_MODE &
    47.              USART_EIGHT_BIT &
    48.              USART_CONT_RX &
    49.              USART_BRGH_HIGH, 51);   //[8000000/(9600*16)] - 1 = 51
    50.  
    51.   /* Loop forever */
    52.   while (1)
    53.   {
    54.         //here, we read the USART and store the char in myCache.
    55.         //Once we have read 16 bytes, we have recieved a full RFID tag ID and need to send it out
    56.         //over the transmit.
    57.  
    58.         c = ReadUSART();
    59.         myCache[counter] = c;
    60.         counter++;
    61.  
    62.         if(counter >= 15)                        //weve read 16 bytes here
    63.         {
    64.             CloseUSART();                        //so we close the USART
    65.               OpenUSART (USART_TX_INT_OFF &        //and reopen it at 4800 baud
    66.                  USART_RX_INT_OFF &
    67.                  USART_ASYNCH_MODE &
    68.                  USART_EIGHT_BIT &
    69.                       USART_CONT_RX &
    70.                  USART_BRGH_HIGH, 103);            //[8000000/(4800*16)] - 1 = 103
    71.        
    72.             for(j = 0; j<16;j++)                //in a for loop, we write each char to the output
    73.             {                                    //pin of the USART
    74.                 WriteUSART(myCache[j]);
    75.             }
    76.            
    77.             counter = 0;        //we set the counter back to 0
    78.            
    79.              CloseUSART();                    //we then close the USART again
    80.               OpenUSART (USART_TX_INT_OFF &    //and reopen it back at 9600 baud to
    81.                  USART_RX_INT_OFF &            //wait for the next transmission
    82.                  USART_ASYNCH_MODE &
    83.                  USART_EIGHT_BIT &
    84.                       USART_CONT_RX &
    85.                  USART_BRGH_HIGH, 103);        //[8000000/(9600*16)] - 1 = 51
    86.         }
    87.        
    88.   }
    89. }
    90.  
     
  2. M5ControlSystems

    New Member

    Apr 12, 2008
    1
    0
    I ran into problems with the hardware UART and it turned out I needed to check for BusyUART() before attempting each Write operation. This solved my problem.

    Also your using the same sprg value of 103??? Is that a typo?
     
  3. wklose99

    Thread Starter New Member

    Apr 14, 2008
    4
    0
    Yea that SPBRG 103 at the end is just a typo. Alright from your suggestions and a few others I changed it to:

    Code ( (Unknown Language)):
    1.  
    2. /* Loop forever */
    3.   while (1)
    4.   {
    5.         //here, we read the USART and store the char in myCache.
    6.         //Once we have read 16 bytes, we have recieved a full RFID tag ID and need to send it out
    7.         //over the transmit.
    8.  
    9.         for(counter = 0; counter <= 15;)
    10.         {
    11.             if (DataRdyUSART())
    12.             {
    13.                 myCache[counter] = ReadUSART();
    14.                 counter++;
    15.             }
    16.         }
    17.  
    18.         if(counter >= 15)                        //weve read 16 bytes here
    19.         {
    20.             CloseUSART();                        //so we close the USART
    21.               OpenUSART (USART_TX_INT_OFF &        //and reopen it at 4800 baud
    22.                  USART_RX_INT_OFF &
    23.                  USART_ASYNCH_MODE &
    24.                  USART_EIGHT_BIT &
    25.                       USART_CONT_RX &
    26.                  USART_BRGH_HIGH, 103);            //[8000000/(4800*16)] - 1 = 103
    27.        
    28.             for(j = 0; j<16;j++)                //in a for loop, we write each char to the output
    29.             {                                    //pin of the USART
    30.                 while(BusyUSART());
    31.                 WriteUSART(myCache[j]);
    32.             }
    33.            
    34.             counter = 0;        //we set the counter back to 0
    35.  
    36.  
    37.              while(BusyUSART());    
    38.                
    39.                 CloseUSART();                    //we then close the USART again
    40.                  OpenUSART (USART_TX_INT_OFF &    //and reopen it back at 9600 baud to
    41.                      USART_RX_INT_OFF &            //wait for the next transmission
    42.                      USART_ASYNCH_MODE &
    43.                      USART_EIGHT_BIT &
    44.                           USART_CONT_RX &
    45.                      USART_BRGH_HIGH, 51);        //[8000000/(9600*16)] - 1 = 51
    46.         }
    47.        
    48.   }
    49.  
    I can't test it until later because the workplace is set up at my friends pad. Thanks so much for the help, I'll let you all know how it goes.

    -WK
     
  4. sosolynutza

    New Member

    Apr 16, 2008
    2
    0

    Hello everybodyI have been working on this project for quite a while now and I am stuck on the programming part of the PIC 16F84 microcontroller. What I want to do is to collect data from a F2F decoder (magnetic data decoder) with the help of a PIC 16F84 and to store it on the external EEPROM 24C256. I also have to programme the PIC to write in the external memory 24C256 the time (like a timer) for each data entry (for each card swipe).I am trying to programme the PIC in MPLAB. Please help me to create the right source code (.ASM).Many thanks can you give some full circuits?
     
  5. sosolynutza

    New Member

    Apr 16, 2008
    2
    0
    Hello I have been working on this project for quite a while now and I am stuck on the programming part of the PIC 16F84 microcontroller. What I want to do is to collect data from a F2F decoder (magnetic data decoder) with the help of a PIC 16F84 and to store it on the external EEPROM 24C256. I also have to programme the PIC to write in the external memory 24C256 the time (like a timer) for each data entry (for each card swipe).I am trying to programme the PIC in MPLAB. Please help me to create the right source code (.ASM).Many thanks Can you give me some full circuits ?
     
  6. wklose99

    Thread Starter New Member

    Apr 14, 2008
    4
    0
    using BusyUSART() worked. Thanks for the help!

    -WK
     
Loading...