![]() |
![]() |
![]() |
|
|
|||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
|
|
Thread Tools | Display Modes |
|
|
(permalink) |
|
I'm in the middle of a project I've been working on for about the last 3 weeks.
What I have is an ID-12 RFID reader(http://www.sparkfun.com/commerce/pro...oducts_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/pro...oducts_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:
//*
* The following application is a cache program for converting a 9600 baud RFID card reader output
* to 4800 baud so it can be fed into a RFID transmitter input. The clock is 8Mhz, BRGH is 1, and BRG16 is 0.
* The PIC used is an 18f1220.
* The progam is compiled in C using the MC18 compiler on a Windows XP laptop.
*/
#include <p18f1220.h>
#include <usart.h>
#pragma config OSC = HS //external crystal clocked at 8Mhz
#pragma config WDT = OFF
#pragma config LVP = OFF
void main (void)
{
unsigned char c; //grab the transmitted char in here
unsigned char myCache[16]; //we will cache the input in here
unsigned int counter=0; //This keeps track of which byte were on
int j = 0;
//The next 5 initializations are set according to the PIC datasheet
//requirements for USART recieve/trans
TRISBbits.TRISB1 = 1; //TX set to output
TRISBbits.TRISB4 = 1; //RX set to 1, but will be set to 0 by PIC for input(as specified in datasheet)
ADCON1bits.PCFG6 = 1; //not sure what these next two are, but setting as specified in datasheet
ADCON1bits.PCFG5 = 1;
RCSTAbits.SPEN = 1; //setting serial port enabled
TXSTAbits.SYNC = 0; //asynchronous mode
// PIE1.TXIE = 0;
BAUDCTLbits.BRG16 = 0; //8 bit transfer
TXSTAbits.BRGH = 1; //BRGH high
TXSTAbits.TXEN = 1; //enable transmit
/*
* Open the USART configured as
* 8N1, 96000 baud, in polled mode
*/
OpenUSART (USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH, 51); //[8000000/(9600*16)] - 1 = 51
/* Loop forever */
while (1)
{
//here, we read the USART and store the char in myCache.
//Once we have read 16 bytes, we have recieved a full RFID tag ID and need to send it out
//over the transmit.
c = ReadUSART();
myCache[counter] = c;
counter++;
if(counter >= 15) //weve read 16 bytes here
{
CloseUSART(); //so we close the USART
OpenUSART (USART_TX_INT_OFF & //and reopen it at 4800 baud
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH, 103); //[8000000/(4800*16)] - 1 = 103
for(j = 0; j<16;j++) //in a for loop, we write each char to the output
{ //pin of the USART
WriteUSART(myCache[j]);
}
counter = 0; //we set the counter back to 0
CloseUSART(); //we then close the USART again
OpenUSART (USART_TX_INT_OFF & //and reopen it back at 9600 baud to
USART_RX_INT_OFF & //wait for the next transmission
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH, 103); //[8000000/(9600*16)] - 1 = 51
}
}
}
Last edited by wklose99; 14th April 2008 at 02:53 PM. |
|
|
|
|
|
|
(permalink) |
|
Shouldn't,
Code:
for(j = 0; j<16;j++) //in a for loop, we write each char to the output
{ //pin of the USART
WriteUSART(myCache[counter]);
}
Code:
for(j = 0; j<16;j++) //in a for loop, we write each char to the output
{ //pin of the USART
WriteUSART(myCache[j]);
}
|
|
|
|
|
|
|
(permalink) |
|
Yea sorry about that this is code I sent my partner last night who tested it(since he has the breadboard setup at his place) and caught that. That has been fixed and it still doesn't work. I am thinking its something to do with my approach and not necessarily a syntax in the coding because I have tried numerous different setups, changing the numbers, delays, etc.
Thank you for your help though! -WK note: I edited the original post to fix the error Pommie caught Last edited by wklose99; 14th April 2008 at 02:48 PM. |
|
|
|
|
|
|
(permalink) | |
|
Quote:
Mike. |
||
|
|
|
|
|
(permalink) |
|
Ahh alright I was wondering if there was someway to clean it up. Thanks much again!
-WK |
|
|
|
|
|
|
(permalink) |
|
Why not simplify it,
Code:
while (1)
{
//here, we read the USART and store the char in myCache.
//Once we have read 16 bytes, we have recieved a full RFID tag ID and need to send it out
//over the transmit.
OpenUSART (USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH, 51); //[8000000/(9600*16)] - 1 = 51
for(j = 0; j<16;j++) //in a for loop, we write each char to the output
{ //pin of the USART
myCache[j]=ReadUSART();
}
CloseUSART(); //so we close the USART
OpenUSART (USART_TX_INT_OFF & //and reopen it at 4800 baud
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH, 103); //[8000000/(4800*16)] - 1 = 103
for(j = 0; j<16;j++) //in a for loop, we write each char to the output
{ //pin of the USART
WriteUSART(myCache[j]);
}
CloseUSART(); //so we close the USART
}
Are you sure that the RFID tags are 16 bytes and don't have an additional crlf or something on the end. Do you receive anything on the output? Mike. |
|
|
|
|
|
|
(permalink) |
|
Before reading from the UART, you should make sure there is something in the buffer to read.
try Code:
for(counter = 0; counter <= 15;)
{
if (DataRdyUSART())
{
myCache[counter] = ReadUSART();
counter++;
}
}
Last edited by BronzeG3; 14th April 2008 at 03:37 PM. |
|
|
|
|
|
|
(permalink) |
|
Waiting for datareadyUSART() worked. Thanks for all the help!
-WK |
|
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Latest |
| Problem with PIC programmer. | Dawny | Micro Controllers | 15 | 6th December 2007 08:04 AM |
| Intermittent PIC problem | mikesmixes777 | Micro Controllers | 16 | 3rd December 2007 12:26 PM |
| PIC pin to MOSFET gate problem | Futterama | Electronic Projects Design/Ideas/Reviews | 7 | 8th November 2007 05:30 PM |
| PIC to LCD wiring problem ? | pasanlaksiri | Micro Controllers | 28 | 4th October 2007 10:29 AM |
| Baud rate transmission problem | Spectacular Butter | General Electronics Chat | 18 | 24th February 2005 09:30 AM |