/************************************
Includes
*************************************/
#include <p18f448.h>
#include <delays.h>
#include <stdio.h>
#include <i2c.h>
#include "lcd.h"
/************************************
Config
*************************************/
#pragma config WDT = OFF, LVP = OFF, OSC = HSPLL
/************************************
Constants / Definitions
*************************************/
#define RA 0xA1 //Read Address
#define WA 0xA0 //Write address
#define CDD PORTDbits.RD0
/************************************
Global Variables
*************************************/
char myName[16];
char myCode[5];
char LcdBuff[17];
//char tempName[6] = {'I','r','e','n','e',0};
//char tempCode[5] = {0x30,0x34,0x35,0x36,0x00};
/************************************
Prototypes
*************************************/
void main(void);
void init_i2c(char speed);
void ReadCard(void);
void WriteCard(char address, char* string);
/************************************
Main
*************************************/
void main(void)
{
ADCON1 = 0x0F; //Digital Pins (we dont need ADC)
TRISA = 0;
TRISB = 0;
TRISC = 0;
TRISD = 0;
//All Outputs
LATA = 0;
LATB = 0;
LATC = 0;
LATD = 0;
//All Low
CMCON = 0x07;
TRISDbits.TRISD0 = 1; //This is the Card Detect
init_i2c(0);
LCD_Init();
while(1)
{
LCD_LINE(1);
LCD_STR((unsigned rom char*)" AtomSoft ");
DelayMS(1);
ReadCard();
LCD_LINE(2);
LCD_STR2(myName);
while(CDD == 1);
LCD_CLEAR(2);
DelayMS(100);
}
}
void WriteCard(char address, char* string)
{
while(CDD != 1);
DelayMS(1);
while(*string)
{
EEByteWrite(WA,address, *string++);
DelayMS(5);
address++;
}
EEByteWrite(WA, address, 0x00);
DelayMS(5);
}
void ReadCard(void)
{
char x;
for(x=0;x<16;x++)
myName[x] = 0x00;
for(x=0;x<5;x++)
myCode[x] = 0x00;
while(CDD != 1);
DelayMS(100);
EESequentialRead(WA,0,myName,16);
EESequentialRead(WA,16,myCode,5);
}
void init_i2c(char speed)
{
//Clock is 10MHz w/PLL = 40MHz
OpenI2C(MASTER, SLEW_OFF);// Initialize I2C module
//KHz = FOSC/(4 * (SSPADD + 1))... or ((FOSC / Speed) / 4) - 1 = SSPADD
switch(speed)
{
case 0:
SSPADD = 99; //100KHz
break;
case 1:
SSPADD = 49; //200KHz
break;
case 2:
SSPADD = 24; //400KHz
break;
}
}