|
|
//RC7 = SDO - transmit data to memory chip
//RB6 = SCK - clock for transmitting data to memory chip
//RC4 = Chip select for memory chip
//RC3= data in for memory chip
//RB7 = LRCIN
//RB5 = used for serial port reception
#include <pic.h>
__CONFIG(INTCLK & WDTDIS & PWRTDIS & MCLRDIS & BORDIS); // Setup the configuration word
void init_Device(void) //Initializes the device, setting the oscillator speed, pin direction, and pull-ups
{
OSCCON=0b01110111; //Oscillator 8 Mhz
TRISA=0; // all Port A output
TRISB=0; // all Port B output
TRISB5=1; // make serial port reception pin input
TRISC=0; // all Port C output
TRISC3=1; // Make RC3 input
PEIE=1; //enable interrupts for RCIF
ANSEL=0; //All inputs digital
ANSELH=0; //All inputs digital
SBOREN=0; //disable brown out reset
}
void init_SPI(void) //Inializes the SSP module to simplify clocking information into the memory chip
{
CKP=0; //0=idle clock state is low
CKE=1; //0=Data transmitted on falling edge of SCK
SMP=1; //1=SPI mode-Input data sampled at end of data output time
SSPM3=0; //FOSC/4
SSPM2=0; //FOSC/4
SSPM1=0; //FOSC/4
SSPM0=0; //FOSC/4
}
void Delay(long x) //Delay routine
{
while (x>0){x--;}
}
void WriteSPI(int x) //writes a bit to the memory chip through the SPI module
{
SSPEN=1; //enable SPI
SSPBUF=x;
Delay(1);
SSPEN=0; //disable SPI
}
void PlaySound(void) //This method clocks initiates the read of the memory chip, then manages the flow of data to the DAC and handles the operations of the DAC.
{
RC4=0; //bring CS to low
WriteSPI(0b00000011); //Read command ("3"
WriteSPI(0b00000000); //Address1
WriteSPI(0b00000000); //Address2
WriteSPI(0b00000000); //Address3
//standard timing, 256fs. This can't be the most efficient way to handle this, but this portion is a repeating loop that toggles the RB6 pin (bit clock) and the RB7 (LRCIN). The RB6 pin is toggled 64 times per sample to clock in 32 bits (though only 16 are used) while the RB7 pin is toggled once per sample (once up and once down) to clock in the left and right samples.
for(;;){
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b10000000;
PORTB=0b11000000;
PORTB=0b00000001;
PORTB=0b01000000; //bring LRCIN low PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
PORTB=0b00000001;
PORTB=0b01000000;
RB6=0;
RB6=1;
}
}
void main(void) //Starting point of the code. Initializes the device then calls the PlaySound method
{
init_Device(); //initialize device
init_SPI(); //initialize SPO module
PORTA=0;
PORTB=0;
PORTC=0;
RC4=1; //set chip select to high
RB6=0; //Set clock to low
RB7=0; //set LRCIN to high
RC7=0;
Delay(100000); //Delays about a second
RC6=0;
for(;;)
{
PlaySound();
}
}
|
|
|