#include <p18f2525.h>
#include <stdio.h>
#include <string.h>
#include <delays.h>
#include <usart.h>
#pragma config WDT = OFF, LVP = OFF, OSC = INTIO67
/************************************
Prototypes
*************************************/
void main(void);
void delay_ms(int mS);
void delay_us(int uS);
void delay_s(int S);
char IsNew(char TheID);
char getSlot(void);
/************************************
Variables AND Defines
*************************************/
char MyID;
char tmp;
unsigned char sPosition;
unsigned char cards[100];
unsigned char tmpID[10];
unsigned char inputID[10];
#define RX_TRIS TRISCbits.TRISC7
#define BadLED LATCbits.LATC2
#define GoodLED LATCbits.LATC3
/************************************
Main
*************************************/
void main(void){
char inputstr[12];
char y,x;
OSCCON = 0x72; //8MHz clock
while(!OSCCONbits.IOFS); //wait for osc stable
y = x = 0;
sPosition = 0;
TRISA = TRISB = TRISC = 0;
RX_TRIS = 1;
OpenUSART( USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_LOW,51);
//Blank out variable
for(y=0;y<100;y++){
cards[y] = 0;
}
while(1){
loopit:
getsUSART( inputstr, 12 );
if(inputstr[0] != 0x0A) goto loopit;
for(y=0;y<10;y++)
inputID[y] = inputstr[y+1];
for(y=0;y<12;y++)
inputstr[y] = 0;
if(IsNew(inputID) == 1){ //Card exist
BadLED = 1;
GoodLED = 0;
} else { //Card is new
BadLED = 0;
GoodLED = 1;
sPosition = getSlot();
if(sPosition == 0xFF){
//No Slots Available for new data
} else {
for(x=0;x<10;x++)
cards[sPosition + x] = inputID[x];
}
}
}
}
char IsNew(char TheID){
char cCount,cPos;
char x,y;
for(cCount=0;cCount<10;cCount++){
cPos = cCount * 10;
for(x=0;x<10;x++){
tmpID[x] = cards[cPos + x];
}
if(memcmp(tmpID,TheID,10) == 0)
return 1;
}
return 0;
}
char getSlot(void){
char cCount, cPos;
for(cCount=0;cCount<10;cCount++){
cPos = cCount * 10;
if(cards[cPos] == 0)
return cPos;
}
return 0xFF;
}