#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <delays.h>
#include <p18f4620.h>
int main(void) {
//WaveTables (0-FF)
//Sine table values
char SineWave[32] = {0x80,0x98,0xb0,0xc6,0xda,0xea,0xf5,0xfd,
0xff,0xfd,0xf5,0xea,0xda,0xc6,0xb0,0x98,
0x80,0x67,0x4f,0x39,0x25,0x15,0xa,0x2,
0x0,0x2,0xa,0x15,0x25,0x39,0x4f,0x67};
//Triangle wave table values
char TriWave[32] ={0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,
0x8f,0x9f,0xaf,0xbf,0xcf,0xdf,0xef,0xff,
0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f,0x80,
0x70,0x60,0x50,0x40,0x30,0x20,0x10,0x0};
//Prepare SPI Pins
TRISCbits.TRISC3 = 0; // SCK (output)
TRISCbits.TRISC5 = 0; // SD0 (output)
TRISCbits.TRISC6 = 0; // CS (output)
//Initialize SPI
OSCTUNE = 0x40;
OSCCON = 0x70; //8MHz (INTOSC drives clock directly)
SSPCON1 = 0x20; //Serial ports enabled idle state is clock low
SSPSTAT = 0x40; //Transmit occurs from active to idle clock transition
while(1){
//SPI Transfer
PORTCbits.RC6 = 0; //Make CS (chip select) low
for(int i = 0;i<31;i++){ // Loop through table
//Upper byte
PIR1bits.SSPIF = 0; // Make Transmission flag low
SSPBUF = SineWave[i]; //Send table data
while(PIR1bits.SSPIF == 0); // Wait until transmission is complete
i++; // Move to next element in table for lower byte
//Lower byte
PIR1bits.SSPIF = 0; // Make Transmission flag low
SSPBUF = SineWave[i]; // Send table data data.
while(PIR1bits.SSPIF == 0); // Wait until transmission is complete
PORTCbits.RC6 = 1; //Make CS high, transfer done
}
}
}