//Project : memory_to_DAC.c
//Date    : 5/23/2005
//Author  : Curtis Rhodes
//Purpose : Reads audio data from memory chips and
		//	sends the information to serial DAC
// Synchronous Serial Port (for memory chip interface)
//	    RC3 (pin 18) == SCL (Clock for Memory Chip)
//	    RC4 (pin 23) == SDA (Serial Data for Memory Chip)
//   
//Algorithm:
// 		Reads sequentially through all 8 memory chips.
//		After each read, the information is sent to a
//		serial DAC.
//Chip type   : PIC16F877A
//Clock frequency     : 20 MHz

#include <pic.h>
#include "memory_to_DAC.h"
#include "stdlib.h"
#include "stdio.h"
#if defined(_16F84) 
__CONFIG(HS & WDTDIS & PWRTEN & MPEDIS);
#endif
#if defined(_16F877) || defined(_16F877A) 
__CONFIG(HS & WDTDIS & PWRTEN & LVPDIS);
#endif
//===================================================
// main program

unsigned char SCIword;
unsigned char hexValue1;
unsigned char hexValue2;
unsigned int i;
unsigned int read_control;
unsigned char write_data;
unsigned char address_high;
unsigned char address_low;
unsigned char data;

//Serial Clock Definitions
#define DAC_clock		 RC2
#define DAC_latch_enable RC1
#define DAC_data 	 	 RD7

//Play Status is an LED that toggles when
// the program switches between memory chips
#define Play_Status_LED	 RC0

void main(void)
{
	portInitialization();
	initializeI2C();
	address_high = 0x00;
	address_low = 0x00;							// Starts read at first address
	read_control = MEMORY_CHIP0_READ_CONTROL;	// of first memory chip

	Play_Status_LED = 1;  // RC0 is used to 
	while(1) {
		start_sequential_read(read_control,0x00,0x00);
	
		for(i=0; i<32768; i++) {
			hexValue1 = sequential_memory_chip_read();
			acknowledge();
			hexValue2 = sequential_memory_chip_read();
			if(i!=32767) {
				acknowledge();
			}
	///////////////////////////////Clock in Serial DAC data
			DAC_clock = 0;
			PORTD = hexValue1;
			DAC_latch_enable = 0;
			DAC_clock = 1;
	
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_clock = 1;
	
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_clock = 1;
	
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_latch_enable = 1;
			DAC_clock = 1;
	
	///////////////////////////////Clock in Serial DAC data
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_clock = 1;
	
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_clock = 1;
	
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_clock = 1;
	
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_clock = 1;
	///////////////////////////////Clock in Serial DAC data
			DAC_clock = 0;
			PORTD = hexValue2;
			DAC_clock = 1;
	
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_clock = 1;
	
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_clock = 1;
	
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_clock = 1;
	///////////////////////////////Clock in Serial DAC data
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_clock = 1;
	
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_clock = 1;
	
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_clock = 1;
	
			DAC_clock = 0;
			PORTD = (PORTD<<1);
			DAC_clock = 1;
		}
		Play_Status_LED = !Play_Status_LED;
		send_Stop_Bit();
	
		if(read_control == MEMORY_CHIP7_READ_CONTROL) {
			// Loops back to beginning of audio
			read_control = MEMORY_CHIP0_READ_CONTROL;
		}
		else {
			read_control = read_control + 2;
		}
	}
}

void portInitialization() {
	TRISD = 0x00; // set PORTD as output
	TRISE = 0x00; // set PORTE as output

	TRISC2 = 0;	// RC5 is DAC clock
	TRISC1 = 0;	// RC4 is DAC latch enable

	TRISC0 = 0; //Toggled when moving to the next chip
}
