#include #include #include #include #include "samples_table.h" // from the CCS C compiler suite #define bitset(var,bitno) ((var) |= 1 << (bitno)) #define bitclr(var,bitno) ((var) &= ~(1 << (bitno))) /* Set up the configuration bits */ #pragma config OSC = HS #pragma config PWRT = OFF #pragma config BOR = OFF #pragma config WDT = OFF #pragma config LVP = OFF #define BAUD 9600 #define FOSC 20000000L #define HIGH_LIMIT 200 #define LOW_LIMIT 80 #define MIPS 5 #define LOW_PULSE_PERIOD (500L) // in ms #define HIGH_PULSE_PERIOD (100L) // in ms #define LOW_PULSE_COUNT (0xFFFF - (LOW_PULSE_PERIOD*MIPS*1000L)/256L) #define HIGH_PULSE_COUNT (0xFFFF - (HIGH_PULSE_PERIOD*MIPS*1000L)/256L) void SPI_setup(void) { // Set RA2 for output (Chip Select) TRISDbits.TRISD0 = 0; // Device select (1 -> not selected); PORTDbits.RD0 = 1; // Set RC5 for output (SDO) TRISCbits.TRISC5 = 0; // Set RC3 for output (SCK) TRISCbits.TRISC3 = 0; // Disable the SPI subsystem SSPCON1bits.SSPEN = 0; // Sample setting SSPSTATbits.SMP = 1; // Set the bus mode 1,1 SSPSTATbits.CKE = 0; SSPCON1bits.CKP = 1; // Clear the collision detection SSPCON1bits.WCOL = 0; // SPI Master at Fosc/16 SSPCON1 = ((SSPCON1 & 0b11110000) | 0b00000001); // SPI module is on SSPCON1bits.SSPEN = 1; } void send_SPI(unsigned short data) { unsigned char first_byte; unsigned char second_byte; // data (4 MSb) first_byte = ((data >> 8) & 0b00001111); // select A unit; bitclr(first_byte,7); // Set gain to 2 bitclr(first_byte,5); // !SHDN bitset(first_byte,4); // Data (8 LSb) second_byte = (data) & 0b11111111; // Device select (0 -> selected) PORTDbits.RD0 = 0; // Send first byte SSPBUF = first_byte; while (SSPSTATbits.BF == 0); // Send second byte SSPBUF = second_byte; while (SSPSTATbits.BF == 0); // Device select (1 -> not selected) PORTDbits.RD0 = 1; } void main( void) { unsigned char first_byte; unsigned char second_byte; unsigned char current_sample; unsigned int sample_value; // setup SPI SPI_setup(); // select A unit; bitclr(first_byte,7); // Set gain to 2 bitclr(first_byte,5); // !SHDN bitset(first_byte,4); // Loop forever current_sample = 0; while (1) { sample_value = samples_table_16[current_sample++]; // data (4 MSb) first_byte = (first_byte & 0b11110000) | ((sample_value >> 8) & 0b00001111); // Data (8 LSb) second_byte = (sample_value) & 0b11111111; // Device select (0 -> selected) PORTDbits.RD0 = 0; // Send first byte SSPBUF = first_byte; while (SSPSTATbits.BF == 0); // Send second byte SSPBUF = second_byte; while (SSPSTATbits.BF == 0); // Device select (1 -> not selected) PORTDbits.RD0 = 1; } }