CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

RF ID Reader Help

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ritchie



Joined: 13 Sep 2003
Posts: 87

View user's profile Send private message

RF ID Reader Help
PostPosted: Sat Oct 25, 2003 9:14 pm     Reply with quote

The code for writing to FRAM does work coz I already test it for a fixed systhetic data to write and it did...

as for the LCD display... I just diplay what the serial gets? dis is not a duplicate... my concern is the duplicate entry in the memory?

Need Help on this...

This my alpha version of my code.. I need help on how I can eliminate the duplicate entry in my memory storage...

Thnx

+++++++++++++++++++++++++++
#include <18F452.h> // Target PIC Microcontroller IC
#fuses HS,NOPROTECT,NOWDT // PIC MCU Configuration
#use delay(clock=20000000) // 20MHz Crystal clock speed
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include <LCD.C> // LCD routines are here
#include <stdlib.h> // Standard library routines

int1 glRfMemWriteFlag = 0; // Memory write flag bit
int1 glRfIdBufferAvailable = 0; // RF Rx buffer is available
int1 glRfIdBufferDataReady = 0; // RF data receive and ready

int8 seconds ; // 00 - 59 seconds
int8 minutes ; // 00 - 59 minutes
int8 hours ; // 00 - 23 hours, military format
int8 mthdays ; // 01 - 31 days in a month
int8 months ; // 01 - 12 months, 1-JAN, ..., 12-DEC
int8 years ; // 00 - 99 2-digit year within a century
int8 y2kyrs ; // 20 - 99 Y2K year or century
int8 dummy ; // scratch pad or dummy variable 1
int8 i ; // scratch pad or dummy variable 2

char gaRawRFCode[12]; // RF raw code data

#define RF_SIZE 12 // RF Rx buffer size for RS-232 receive
byte rfbuffer[RF_SIZE]; // RF receive serial data buffer
byte rf_in = 0; // Rx data in index
byte rf_out = 0; // Rx data out index

// I2C Configuration
#define FRAM_SDA PIN_C4 // i2c data
#define FRAM_SCL PIN_C3 // i2c clock
#define FRAM_WP PIN_C5 // write protect pin

// define I2C read/write address
#define FRAM_WR_ADDR 0xA0 // initial F-RAM write address at 1st chip
#define FRAM_RD_ADDR 0xA1 // initial F-RAM read address at 1st chip

//#define FRAM_SIZE 32768 // for FM24C256
#define FRAM_SIZE 262144 // max bytes for 8chips of FM24C256
#define DATA_SIZE 32 // data size for one(1) swipe transaction

char gcMemBuffer[DATA_SIZE]; // dummy memory buffer before storage;
unsigned int32 guStrtAddr = 32; // FM24C256 start address pointer
unsigned int32 guEndAddr = 32; // FM24C256 end address pointer
unsigned int16 guLogCount = 0; // Log Counter (how many swipe?)
unsigned int1 guFramCont = 0; // FRAM content flag bit

// Configuration Registers for I2C
unsigned char SSPADD;
#locate SSPADD=0x0FC8

unsigned char SSPBUF;
#locate SSPBUF=0x0FC9

unsigned char SSPCON1;
#locate SSPCON1=0x0FC6

struct {
unsigned char SSPM0:1;
unsigned char SSPM1:1;
unsigned char SSPM2:1;
unsigned char SSPM3:1;
unsigned char CKP:1;
unsigned char SSPEN:1;
unsigned char SSPOV:1;
unsigned char WCOL:1;
} SSPCON1bits ;
#locate SSPCON1bits=0x0FC6


unsigned char SSPCON2;
#locate SSPCON2=0x0FC5

struct {
unsigned char SEN:1;
unsigned char RSEN:1;
unsigned char PEN:1;
unsigned char RCEN:1;
unsigned char ACKEN:1;
unsigned char ACKDT:1;
unsigned char ACKSTAT:1;
unsigned char GCEN:1;
} SSPCON2bits ;
#locate SSPCON2bits=0x0FC5


unsigned char SSPSTAT;
#locate SSPSTAT=0x0FC7

struct {
unsigned char BF:1;
unsigned char UA:1;
unsigned char R_W:1;
unsigned char S:1;
unsigned char P:1;
unsigned char D_A:1;
unsigned char CKE:1;
unsigned char SMP:1;
} SSPSTATbits ;
#locate SSPSTATbits=0x0FC7


unsigned char PIR2;
#locate PIR2=0x0FA1

struct {
unsigned char CCP2IF:1;
unsigned char TMR3IF:1;
unsigned char LVDIF:1;
unsigned char BCLIF:1;
unsigned char EEIF:1;
} PIR2bits ;
#locate PIR2bits=0x0FA1

// Useful defines for writing to the I2C bus
#define I2C_IDLE() while ((SSPCON2 & 0x1F) || (SSPSTATbits.R_W))

#define I2C_START() \
SSPCON2bits.SEN = 1; \
while (SSPCON2bits.SEN) \
{ \
#asm nop #endasm \
}

#define I2C_RESTART() \
SSPCON2bits.RSEN = 1; \
while (SSPCON2bits.RSEN) \
{ \
#asm nop #endasm \
}

#define I2C_STOP() \
SSPCON2bits.PEN = 1; \
while (SSPCON2bits.PEN) \
{ \
#asm nop #endasm \
}

#define I2C_WRITE(x) \
SSPBUF = (x); \
while (SSPSTATbits.BF) \
{ \
#asm nop #endasm \
} \
I2C_IDLE()

#define I2C_READ(x) \
SSPCON2bits.RCEN = 1; \
while (SSPCON2bits.RCEN) \
{ \
#asm nop #endasm \
} \
SSPCON2bits.ACKDT = (x); \
SSPCON2bits.ACKEN = 1; \
while (SSPCON2bits.ACKEN) \
{ \
#asm nop #endasm \
}

static BOOLEAN I2C_Device_Ready(unsigned int8 uID);

#int_rda
void RFid_isr() // RF ID Reader interrupt routines
{
if(glRfIdBufferAvailable)
{
rfbuffer[rf_in]=getc();
rf_in++;

if(rf_in==RF_SIZE)
{
glRfIdBufferAvailable = 0; // buffer is full
glRfIdBufferDataReady = 1; // RF data is available
glRfMemWriteFlag = 1; // set flag for memory write
rf_in = 0; // restart pointer to zero
}
}
else getch();
}

void fram_init() // initialize FRAM
{
output_float(FRAM_SCL); // set clock to input
output_float(FRAM_SDA); // set data to input
}

void fill_memory_buffer() // fill memory buffer before storage
{
char aTempBuffer[21];
char cMode='M'; // log mode is set to manual
int16 iAddress = 0xABBA; // initial device address

for (i=1; i<10; i++)
{ // fill gcMemBuffer with decoded barcode data
gcMemBuffer[i-1] = gaRawRFCode[i];
}
// fill aTemp_Buffer with data
sprintf(aTempBuffer,"%02u:%02u%02u-%02u-%02u%02u%C%04LX",
hours,minutes,mthdays,months,y2kyrs,years,cMode,iAddress);

for (i=9; i<30; i++)
{ // fill gcMemBuffer with data
gcMemBuffer[i] = aTempBuffer[i-9];
}
// set the last two array content to NULL
gcMemBuffer[30] = 0;
gcMemBuffer[31] = 0;
}

static BOOLEAN I2C_Device_Ready(unsigned int8 uID)
{
do
{
// Important to clear these bits before we attempt to write
PIR2bits.BCLIF = 0;
SSPCON1bits.WCOL = 0;
I2C_IDLE(); // ensure module is idle
I2C_START();
} while (PIR2bits.BCLIF);

I2C_WRITE(uID); // send memID

if (!SSPCON2bits.ACKSTAT) // test for ACK condition, if received
return (TRUE);
return (FALSE); // no ACK
}

// Write one block of data to the FM24C256 chip
void write_fram_block(
unsigned int32 uAddress, // FM24C256 address to write data
unsigned int8 Block, // block length or one page write
unsigned int8 *gcMemBuffer) // pointer to gcMemBuffer to get data
{
unsigned int8 uControl; // FM24C256 chipselect
unsigned int8 uI;

// chipselect for specific FM24C256 to write
uControl = (FRAM_WR_ADDR | make8(uAddress<<2,2)) & 0xFE;

output_low(FRAM_WP); // set write protect low

while (!I2C_Device_Ready(uControl));
I2C_WRITE((make8(uAddress,1)) & 0x7F); // send MSB of mem address
I2C_WRITE(make8(uAddress,0)); // send LSB of mem address
for (uI=0; uI<Block; uI++)
{
I2C_WRITE(*gcMemBuffer++); // send data to FM24C256
}
I2C_STOP();

output_high(FRAM_WP); // set write protect high
}

void read_fram_block(
unsigned int32 uAddress,
unsigned int8 Block,
unsigned int8 *gcReadMemBf)
{
unsigned int8 uControl;

// chipselect for specific FM24C256
uControl = (FRAM_WR_ADDR | make8(uAddress<<2,2)) & 0xFE;

while (!I2C_Device_Ready(uControl));
I2C_WRITE((make8(uAddress,1)) & 0x7F); // send MSB of mem address
I2C_WRITE(make8(uAddress,0)); // send LSB of mem address
I2C_RESTART();
I2C_WRITE(uControl | 0x01); // set address for read

while (Block > 1)
{
I2C_READ(0);
*gcReadMemBf = SSPBUF;
gcReadMemBf++;
Block--;
}

I2C_READ(1);
*gcReadMemBf = SSPBUF;
I2C_STOP();
}

// Write one byte to the Ramtron FM24C256 chip
void write_fram_Byte(
unsigned int32 uAddress, // FRAM address to write data
unsigned int8 uData) // byte written to FRAM
{
unsigned int8 uControl;

// chipselect for specific FM24C256 to write
uControl = (FRAM_WR_ADDR | make8(uAddress<<2,2)) & 0xFE;

output_low(FRAM_WP); // set write protect low

while (!I2C_Device_Ready(uControl));
I2C_WRITE((make8(uAddress,1)) & 0x7F); // send MSB of mem address
I2C_WRITE(make8(uAddress,0)); // send LSB of mem address
I2C_WRITE(uData); // send data
I2C_STOP();

output_high(FRAM_WP); // set write protect high
}

// Read one byte from the Ramtron FM24C256 chip
unsigned int8 read_fram_byte(
unsigned int32 uAddress) // FM24C256 address to write data
{
unsigned int8 uControl;
// chipselect for specific FM24C256
uControl = (FRAM_WR_ADDR | make8(uAddress<<2,2)) & 0xFE;

while (!I2C_Device_Ready(uControl));
I2C_WRITE((make8(uAddress,1)) & 0x7F); // send MSB of mem address
I2C_WRITE(make8(uAddress,0)); // send LSB of mem address
I2C_RESTART();
I2C_WRITE(uControl | 0x01); // set address for read
I2C_READ(1);
I2C_STOP();
return (SSPBUF); // return with data
}

// Configure the MSSP as an I2C Port for PIC18F452
// Relevant port pins configured as inputs
void ConfigureI2C(void)
{
SSPCON1bits.SSPEN = 1; // bit_set(SSPCON,SSPEN); // Enable I2C mode

SSPCON1bits.SSPM3 = 1; // bit_set(SSPCON,SSPM3); // Setup I2C
SSPCON1bits.SSPM2 = 0; // bit_clear(SSPCON,SSPM2);
SSPCON1bits.SSPM1 = 0; // bit_clear(SSPCON,SSPM1);
SSPCON1bits.SSPM0 = 0; // bit_clear(SSPCON,SSPM0);

SSPCON2 = 0;

SSPSTATbits.SMP = 0; // bit_clear(SSPSTAT,SMP);
SSPSTATbits.CKE = 0; // bit_clear(SSPSTAT,CKE); // Set I2C Levels

// Set I2C Speed. The formula is:
//
// SSPADD value = (Fosc/(i2c clock speed * 4)) -1
//
// Examples:
//
// SSPADD values for an Fosc of 8 MHz:
// *** NOTE: If you run your PIC at a different Fosc, then you
// *** you need to calculate new values.
//
// For an i2c clock of 100 KHz, SSPADD = 19
// For an i2c clock of 400 KHz, SSPADD = 4
// For an i2c clock of 1 MHz, SSPADD = 1

SSPADD = 4; // 1 MHz i2c clock at 20MHz oscillator
SSPSTATbits.SMP = 1; // bit_set(SSPSTAT,SMP);
// Disable slew rate control
// for 1 MHz operation ONLY

// *** NOTE: Based on my oscilloscope readings, the i2c clock
// *** speed seems to run a little slower than what the formula says.
// *** For example, "400 KHz" is really 370 KHz or so.
// *** "1 MHz" is really running slightly less than that speed.
}

void write_data_to_memory()
{
unsigned int32 uLoc;

// get the start address
//guStrtAddr = make32(read_program_eeprom(0x7FE0),
// read_program_eeprom(0x7FE1));
// get the end address
//guEndAddr = make32(read_program_eeprom(0x7FE2),
// read_program_eeprom(0x7FE3));

if (guStrtAddr == guEndAddr)
uLoc = guStrtAddr; // set uLoc to guStrtAddr
if (guStrtAddr != guEndAddr)
uloc=guEndAddr; // set uLoc to guEndAddr
if (guEndAddr == FRAM_SIZE)
uLoc = 32; // memory overflow... max size reach
// reset end pointer to location 32 of
// chip address A0

// write data to FM24C256
write_fram_block(uLoc,DATA_SIZE,gcMemBuffer);
guEndAddr = uLoc + DATA_SIZE; // increment guEndAddr by 32bytes
}

void nulrfbuffer() { // put a NULL
for (i=0;i<RF_SIZE;i++) // to entire buffer size
rfbuffer[i]=0 ; // of rfbuffer
rf_in=0; // restart pointer to zero
}

void nulrfrawcode() {
for (i=0;i<RF_SIZE;i++)
gaRawRFCode[i]=0 ; // put a NULL to gaRawRFCode array
}

void clear_lcd_display()
{
lcd_gotoxy(1,2);
printf(lcd_putc," ");
}

void init_chip() { // Initialize the MCU Chip

lcd_init(); // LCD should be initialized first

fram_init(); // Initialize ports for I2C
ConfigureI2C(); // Configure I2C bus
read_fram_hdr(); // read FRAM header (e.g. start, end, log count)

seconds = 59 ; // 00 - 59 seconds
minutes = 1 ; // 00 - 59 minutes
hours = 18 ; // 00 - 23 hours, military format
mthdays = 3 ; // 01 - 31 days in a month
months = 10 ; // 01 - 12 months, 1-JAN, ..., 12-DEC
years = 03 ; // 00 - 99 2-digit year within a century
y2kyrs = 20 ; // 20 - 99 Y2K year or century

enable_interrupts(int_rda); // Rx serial receive interrupt
enable_interrupts(GLOBAL); // Global interrupt
}

main()
{

nulrfbuffer(); // put NULL at RF buffer array
nulrfrawcode(); // put NULL at RF raw code array

init_chip(); // Initialize the MCU Chip

lcd_gotoxy(1,1);
printf(lcd_putc,"HTI-200R");

glRfIdBufferAvailable = 1; // Rx buffer is empty
glRfIdBufferDataReady = 0; // RF data buffer is empty
glRfMemWriteFlag = 0; // memory write disabled

while (TRUE)
{
if (glRfIdBufferDataReady)
{
//strncpy(gaRawRFCode,buffer,12);
strcpy(gaRawRFCode,rfbuffer);

for(dummy=0; dummy<RF_SIZE; dummy++)
{
//lcd_putc(Buffer[uCount]);
//gaRawRFCode[uCount]=Buffer[uCount];
lcd_putc(gaRawRFCode[dummy]);
}
glRfIdBufferAvailable = 1;
glRfIdBufferDataReady = 0;
}

if(glRfMemWriteFlag)
{
//for (uIndex=0; uIndex<DATA_SIZE; uIndex++)
fill_memory_buffer();
write_data_to_memory();
//write_fram_byte(uIndex,rfBuffer[uIndex]);
glRfMemWriteFlag = 0;
}
}
}
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sun Oct 26, 2003 9:04 am     Reply with quote

This is the same thread that Ttelmah answer you with several tips and a very good analisys. It would be nice to know if you implemented those suggestions.

Reading your code I see in your #INT_RDA that the buffer pointer [rf_in] in not initialized to start storing the first char you expect (of your packet), instead is inialized to start storing the first char that arrive.


Code:
 

#int_rda
void RFid_isr() // RF ID Reader interrupt routines
{
if(glRfIdBufferAvailable)
{
rfbuffer[rf_in]=getc();
rf_in++;

if(rf_in==RF_SIZE)
{
glRfIdBufferAvailable = 0; // buffer is full
glRfIdBufferDataReady = 1; // RF data is available
glRfMemWriteFlag = 1; // set flag for memory write
rf_in = 0; // restart pointer to zero
}
}
else getch();
}

 



I will try this:

Code:



#int_rda
void RFid_isr() // RF ID Reader interrupt routines
{
int char_rcved;

  if(glRfIdBufferAvailable)
   {
     char_rcved=getc();
     
     if(char_rcved == PacketLeader)  // the first char you expect
       {
         rf_in=0;
       }

     rfbuffer[rf_in]= char_rcved;
     rf_in++;
   
     if(rf_in==RF_SIZE)
      {
       glRfIdBufferAvailable = 0; // buffer is full
       glRfIdBufferDataReady = 1; // RF data is available
       glRfMemWriteFlag = 1; // set flag for memory write
      }
   }
   else getch();
}
 


Hope this help you,

Humberto
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group