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 CCS Technical Support

#int_rda probelm for RF ID Reader

 
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

#int_rda probelm for RF ID Reader
PostPosted: Fri Oct 24, 2003 11:28 pm     Reply with quote

Mabuhay!

The code below will capture an ASCII character from an RF ID reader. My problem is when I write the decoded data to FM24C256 FRAM memory, it put a double entry.

Sometimes one entry but most of the time double entry will happen and I guess this will consume my memory resources with duplicate entry.

Can anybody from the forum review my code and provide tips and/or suggestions.

Thnx

Code:
[i]
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


#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 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
}

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;
      }
   }
}[/i]
Ttelmah
Guest







Re: #int_rda probelm for RF ID Reader
PostPosted: Sat Oct 25, 2003 2:35 am     Reply with quote

ritchie wrote:
Mabuhay!

The code below will capture an ASCII character from an RF ID reader. My problem is when I write the decoded data to FM24C256 FRAM memory, it put a double entry.

Sometimes one entry but most of the time double entry will happen and I guess this will consume my memory resources with duplicate entry.

Can anybody from the forum review my code and provide tips and/or suggestions.

Thnx

Code:
[i]
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


#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 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
}

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;
      }
   }
}[/i]


Without looking too deeply, I can see one small thing that could cause a problem.
Your 'buffer', is the size of your incoming packet. You then use 'strcpy', which will only stop, when it gets to a 'null' character (rather than strncpy, which can be limited to a set number of characters). The two buffers are immediately adjacent, so if the source buffer (rfbuffer), does not end in a null, the routine, will copy the contents of rfbuffer, 'down' into the buffer 'gaRawRFCode', and keep going till it hits a null. This could potentially result in data corruption. You have obviously tried using strncpy at one point, and this is the safer route to take.
Now you say you are seeing a 'double entry', but do not say whether this is on a 'per character' basis, of the whole string being duplicated?. Also, you print the data to the LCD, is this duplicated as well, or is it only the data in the FRAM?. If the latter, then the problem is probably in the FRAM code, which you don't post.
Prove _where_ the fault is. Generate a fixed 'synthetic' buffer, with a known sequence of characters, and a counter, and write this to the display and FRAM. If it works, then you have narrowed the fault down to the serial transmit/receive. If the same duplication occurs, then you have excluded the serial code.
You don't show the routines that actually transfer data to the buffer used for the FRAM write (fill_memory_buffer), or the definitions used inside the 'write_data_to_memory' routine. Also, you stop handling characters when the buffer is full, for a potentially significant period of time (not knowing how slow your LCD code, and memory buffer transfer code is, it is difficult to estimate how long). You apparently leave the serial IRQ enabled, and have 'getch' to prevent a COMM overflow, and the potential may still exist for the overrun error to happen, resulting in some garbage being seen.
You don't say which processor is involved?. You are making significant use of array accesses in both the main code, and the interrupt code. On some chips, this can result in interrupts being disabled during array I/O, and might again cause oddities.

Best Wishes
ritchie



Joined: 13 Sep 2003
Posts: 87

View user's profile Send private message

Re: #int_rda probelm for RF ID Reader
PostPosted: Sat Oct 25, 2003 6:52 am     Reply with quote

Quote:
Without looking too deeply, I can see one small thing that could cause a problem.
Your 'buffer', is the size of your incoming packet. You then use 'strcpy', which will only stop, when it gets to a 'null' character (rather than strncpy, which can be limited to a set number of characters). The two buffers are immediately adjacent, so if the source buffer (rfbuffer), does not end in a null, the routine, will copy the contents of rfbuffer, 'down' into the buffer 'gaRawRFCode', and keep going till it hits a null. This could potentially result in data corruption. You have obviously tried using strncpy at one point, and this is the safer route to take.
Now you say you are seeing a 'double entry', but do not say whether this is on a 'per character' basis, of the whole string being duplicated?. Also, you print the data to the LCD, is this duplicated as well, or is it only the data in the FRAM?. If the latter, then the problem is probably in the FRAM code, which you don't post.
Prove _where_ the fault is. Generate a fixed 'synthetic' buffer, with a known sequence of characters, and a counter, and write this to the display and FRAM. If it works, then you have narrowed the fault down to the serial transmit/receive. If the same duplication occurs, then you have excluded the serial code.
You don't show the routines that actually transfer data to the buffer used for the FRAM write (fill_memory_buffer), or the definitions used inside the 'write_data_to_memory' routine. Also, you stop handling characters when the buffer is full, for a potentially significant period of time (not knowing how slow your LCD code, and memory buffer transfer code is, it is difficult to estimate how long). You apparently leave the serial IRQ enabled, and have 'getch' to prevent a COMM overflow, and the potential may still exist for the overrun error to happen, resulting in some garbage being seen.
You don't say which processor is involved?. You are making significant use of array accesses in both the main code, and the interrupt code. On some chips, this can result in interrupts being disabled during array I/O, and might again cause oddities.

Best Wishes


This is the entire code:

Code:

#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
}

void read_fram_hdr()   // read FRAM header (0~10)
{
   unsigned int32 uStartHi,uStartLo;
   unsigned int32 uEndHi,uEndLo;
   unsigned int8 uRecFlag;

   // get the flag bit if FRAM has data
   uRecFlag = read_fram_byte(10);
   if (uRecFlag == 0xFF)
      guFramCont = 0;      // FRAM has no data
   else if (uRecFlag == 0x55)
      guFramCont = 1;      // FRAM has data

   if (guFramCont)
   {
      // get the 32-Bit start address from FRAM (0~3)
      // read_fram_byte(0) -> start address word (MSW)
      // read_fram_byte(1)
      // read_fram_byte(2) -> start address word (LSW)
      // read_fram_byte(3)
      // get the MSW of start address
      uStartHi = make16(read_fram_byte(0),read_fram_byte(1));
      uStartHi <<= 16;
      // get the LSW of start address
      uStartLo = make16(read_fram_byte(2),read_fram_byte(3));
      // 32bit start address
      guStrtAddr = uStartHi | uStartLo;

      // get the 32-Bit end address from FRAM (4~7)
      // read_fram_byte(4) -> end address word (MSW)
      // read_fram_byte(5)
      // read_fram_byte(6) -> end address word (LSW)
      // read_fram_byte(7)
      // get the MSW of end address
      uEndHi = make16(read_fram_byte(4),read_fram_byte(5));
      uEndHi <<= 16;
      // get the LSW of end address
      uEndLo = make16(read_fram_byte(6),read_fram_byte(7));
      // 32bit end address
      guEndAddr = uEndHi | uEndLo;

      // get the log count
      guLogCount = make16(read_fram_byte(8),read_fram_byte(9));
   }

   if (!guFramCont)
   {
      guStrtAddr = 32;  // set FM24C256 start address
      guEndAddr  = 32;  // set FM24C256 end address
      guLogCount =  0;  // set log count to zero
   }

}

void update_fram_hdr()     // update fram header for next
                           // write/read cycle
{
   // store start address in 8-bit
   write_fram_byte(0,make8(guStrtAddr,3));   // 25~32 bits (MSB)
   write_fram_byte(1,make8(guStrtAddr,2));   // 16~24 bits
   write_fram_byte(2,make8(guStrtAddr,1));   // 8~15  bits
   write_fram_byte(3,make8(guStrtAddr,0));   // 0~7   bits (LSB)

   // store end address in 8-bit
   write_fram_byte(4,make8(guEndAddr,3));    // 25~32 bits (MSB)
   write_fram_byte(5,make8(guEndAddr,2));    // 16~24 bits
   write_fram_byte(6,make8(guEndAddr,1));    // 8~15  bits
   write_fram_byte(7,make8(guEndAddr,0));    // 0~7   bits (LSB)

   // store log count
   write_fram_byte(8,make8(guLogCount,1));   // 8~15  bits (MSB)
   write_fram_byte(9,make8(guLogCount,0));   // 0~7   bits (LSB)

   // store Content Flag bit
   write_fram_byte(10,0x55);
}

// 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 int8  uCount=0;
   unsigned int32 uLoc;
   unsigned int32 uTempEnd;

   if (guStrtAddr == guEndAddr)
   {
      for (uLoc=guStrtAddr; uLoc<(guEndAddr+DATA_SIZE); uLoc++)
      {
         write_fram_byte(uLoc,gcMemBuffer[uCount]);
         uCount++;         // increment gcMemBuffer for next char
         guEndAddr++;      // increment guEndAddr for next write cycle
      }
   }
   else if (guStrtAddr != guEndAddr)
   {
      uTempEnd = guEndAddr;
      for (uLoc=uTempEnd; uLoc<(uTempEnd+DATA_SIZE); uLoc++)
      {
         write_fram_byte(uLoc,gcMemBuffer[uCount]);
         uCount++;         // increment gcMemBuffer for next char
         guEndAddr++;
      }
   }
}*/

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;
      }
   }
}


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...

Thnx
ritchie



Joined: 13 Sep 2003
Posts: 87

View user's profile Send private message

PostPosted: Sat Oct 25, 2003 7:01 am     Reply with quote



Last edited by ritchie on Sat Oct 25, 2003 7:26 am; edited 1 time in total
ritchie



Joined: 13 Sep 2003
Posts: 87

View user's profile Send private message

PostPosted: Sat Oct 25, 2003 7:03 am     Reply with quote



Last edited by ritchie on Sat Oct 25, 2003 7:24 am; edited 1 time in total
ritchie



Joined: 13 Sep 2003
Posts: 87

View user's profile Send private message

PostPosted: Sat Oct 25, 2003 7:03 am     Reply with quote



Last edited by ritchie on Sat Oct 25, 2003 7:28 am; edited 1 time in total
ritchie



Joined: 13 Sep 2003
Posts: 87

View user's profile Send private message

PostPosted: Sat Oct 25, 2003 7:06 am     Reply with quote



Last edited by ritchie on Sat Oct 25, 2003 7:30 am; edited 1 time in total
ritchie



Joined: 13 Sep 2003
Posts: 87

View user's profile Send private message

PostPosted: Sat Oct 25, 2003 7:08 am     Reply with quote



Last edited by ritchie on Sat Oct 25, 2003 7:32 am; edited 1 time in total
ritchie



Joined: 13 Sep 2003
Posts: 87

View user's profile Send private message

PostPosted: Sat Oct 25, 2003 7:14 am     Reply with quote

BTW, the RF ID Reader sends 12 ASCII character or 12bytes of data..

Thnx
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