//Project : I2CSubroutines.c
//Date    : 5/23/2005
//Author  : Curtis Rhodes with Additions from Dr. Song's SCI communication code
//Purpose : I2C Memory Chip Interface Subroutines
// Synchronous Serial Port
//	    RC3 (pin 18) == SCL (Clock for Memory Chip)
//	    RC4 (pin 23) == SDA (Serial Data for Memory Chip)
//          
//Chip type   : PIC16F877A
//Clock frequency     : 20 MHz

//===================================================
// define constants
//===================================================
#define BAUD_RATE	2400
#define FREQUENCY  200				// Crystal frequency in kHz

//#define MEMORY_TRANSMIT_CONTROL 0x90
#define MEMORY_CHIP0_WRITE_CONTROL	0b10100000
#define MEMORY_CHIP1_WRITE_CONTROL	0b10100010
#define MEMORY_CHIP2_WRITE_CONTROL	0b10100100
#define MEMORY_CHIP3_WRITE_CONTROL	0b10100110
#define MEMORY_CHIP4_WRITE_CONTROL	0b10101000
#define MEMORY_CHIP5_WRITE_CONTROL	0b10101010
#define MEMORY_CHIP6_WRITE_CONTROL	0b10101100
#define MEMORY_CHIP7_WRITE_CONTROL	0b10101110
#define MEMORY_CHIP0_READ_CONTROL	0b10100001
#define MEMORY_CHIP1_READ_CONTROL	0b10100011
#define MEMORY_CHIP2_READ_CONTROL	0b10100101
#define MEMORY_CHIP3_READ_CONTROL	0b10100111

// Input pins
#define rs232_transmit_pin	RC6	// pin 25, serial transmit to PC
#define rs232_receive_pin	RC7	// pin 26, serial receive from PC

//===================================================
// function definitions
//===================================================
void rs232_initliazation(unsigned int, unsigned int);	// pass baud rate
void send_byte_to_PC(char);			// pass byte to be sent
void receive_byte_from_PC(char *);	// pass a pointer to get byte
void display_string(const char *, char length);	// pointer & length of a string

//I2C Subroutines
void send_Start_Bit();
void send_Stop_Bit();
void acknowledge();
void write_Data(unsigned int write_data);
void receive_Data();
void wait_For_Memory_Write_Completion(int control_byte);
void initializeI2C();
void memory_chip_write(unsigned char memory_chip, unsigned char address_high, unsigned char address_low, unsigned char data);
void portInitialization();
unsigned char memory_chip_read(unsigned char memory_chip, unsigned char address_high, unsigned char address_low);
