//Master code //SCI used for only checking the data#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */#include <string.h>void sendSPI(unsigned int);void sendSCIvalue(unsigned int);void sendSCIchar(char);#define NAME 0x0Cint dummy = 0xEE;void main(void) {//int i; //Set Oscillator frequency OSCSC = 0x20; // Set bus speed to 12.8 MHZ OSCTRIM = Optional; // this macro loads value at $FFC0 and store it in $0038. // bus clock speed is 3.2MHz +- 5% // Set up configuration registers CONFIG2 = 0x05; // Enable reset and IRQ pins CONFIG1 = 0x01; // disable the watch dog. DDRA = 0x03; DDRB = 0x40; PTAPUE_PTAPUE2=1; //enable pullup resistor on PTA2 // Configure ADC ADCLK = 0x14; // ADC 10 bit ADSCR = 0x23; // continuous conversion, channel 3 (PTA5) // Configure ESCI SCPSC = 0b10000111; //Baudrate = ((12.8Mhz/4) / (64*1*1*5.21875)) = 9580 SCC1 = 0x40; SCC2 = 0x08; // Configure SPI SPSCR = 0x03; SPCR_SPE = 1; SPCR_SPMSTR = 1; SPCR_CPHA = 1;// SPCR_SPRIE = 1; while(1){ sendSPI(NAME); } }void sendSPI(unsigned int data) { unsigned int rec_data; int x; sendSCIvalue(data); SPDR = data; while(!SPSCR_SPRF); x = SPSCR; rec_data = SPDR; sendSCIvalue(rec_data); sendSCIchar(0x20); while(!(rec_data == 0x55)) { SPDR = 0xFF; while(!SPSCR_SPRF); x = SPSCR; rec_data = SPDR; //sendSCIvalue(rec_data); //sendSCIchar(0x20); } sendSCIvalue(rec_data); //sendSCIchar(0x20); SPDR = dummy; while(!SPSCR_SPRF); x = SPSCR; rec_data = SPDR; sendSCIvalue(rec_data); sendSCIchar(0x20); SPDR = dummy; while(!SPSCR_SPRF); x = SPSCR; rec_data = SPDR; sendSCIvalue(rec_data); sendSCIchar(0x20); sendSCIchar(0x0D); sendSCIchar(0x0A);} void sendSCIvalue(unsigned int hex) { int temp4,temp3,temp2,temp1,temp0,temp,tem,te; char dec; temp4 = hex/0x2710; dec = ((temp4 & 0xf) | 0x30); sendSCIchar(dec); te = hex % 0x2710; temp3 = te / 0x3E8; dec = ((temp3 & 0xf) | 0x30); //(char *) ((temp3<12) & 0xF000)|0x30;; sendSCIchar(dec); temp2 = te % 0x3E8; temp1 = temp2 / 0x64; dec = ((temp1 & 0xf) | 0x30); sendSCIchar(dec); temp = temp2 % 0x64; temp0 = temp/0xA; dec = ((temp0 & 0xf) | 0x30); sendSCIchar(dec); tem = temp % 0xA; dec = ((tem & 0xf) | 0x30); sendSCIchar(dec);}void sendSCIchar(char i){ unsigned char u8serialCount; u8serialCount=SCS1; SCDR = i; while(!SCS1_TC);}
//Slave code//SCI used for data verification#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */void init_ports(void);void commandselection(int);void sendSPIdata(unsigned int);void sendSCIvalue(unsigned int);void sendSCIchar(char);void getthreshold (void);unsigned int eread(int);void sendSPIvalue(unsigned int);#define ELECTRODE 1unsigned int thres_value[4];unsigned int thres_setvalue[4];void main(void) { int temp,rec_data; init_ports(); SPDR = 0X01; while(1) { //SPDR = 0X01; while(!SPSCR_SPRF); temp = SPSCR; //To clear the bit rec_data = SPDR; EnableInterrupts; //Master is sending data continuously SPCR_SPRIE = 1; //To clear SPSCR_SPRF bit Interrupt routine is used sendSCIvalue(rec_data); sendSCIchar(0x20); if(rec_data == 0x0C) commandselection(rec_data); }}/**************************************************************************************************///Port initialization/**************************************************************************************************/void init_ports() { //Set Oscillator frequency OSCSC = 0x20; // Set bus speed to 12.8 MHZ OSCTRIM = Optional; // this macro loads value at $FFC0 and store it in $0038. // bus clock speed is 3.2MHz +- 5% // Set up configuration registers CONFIG2 = 0x05; // Enable reset and IRQ pins CONFIG1 = 0x01; // disable the watch dog. DDRA = 0x03; DDRB = 0x40; PTAPUE_PTAPUE2=1; //enable pullup resistor on PTA2 // Configure ADC ADCLK = 0x14; // ADC 10 bit ADSCR = 0x23; // continuous conversion, channel 3 (PTA5) // Configure ESCI SCPSC = 0b10000111; //Baudrate = ((12.8Mhz/4) / (64*1*1*5.21875)) = 9580 SCC1 = 0x40; SCC2 = 0x08; // Configure SPI SPSCR = 0x03; //Baud rate SPCR_SPE = 1; //Enabiling SPI SPCR_SPMSTR = 0; //Enabling slave SPCR_CPHA = 1; SPCR_SPRIE = 0; //Interrupt reciver }void commandselection(int command) { // int l,m; switch(command) { case 0x0C: //sendSCIvalue(command); //sendSCIchar(0x20); //getthreshold(); // To read the proximity elevtrode values sendSPIdata(0x22CC); // For checking purpose using this data SPDR = 0xAA; break; default : break; } //sendSCIvalue(command); //sendSCIchar(0x20); }void getthreshold (){ int ch; for(ch=1;ch<=ELECTRODE;ch++) { thres_value[ch-1] = eread(ch); sendSPIdata(thres_value[ch-1]); //send data to this function to send thru SPI } sendSCIchar(0x0A); sendSCIchar(0x0D); }/**************************************************************************************************/ //Read values of electrode/**************************************************************************************************/unsigned int eread(int ch) { unsigned int evalue; PTA = PTA & 0xFC; //PortA 0 & 1 & port B to select electrode from 1 to 7 PTA = PTA | (ch & 0x03); ch = ch << 4; PTB = PTB & 0xBF; PTB = PTB | (ch & 0x40); while(!(ADSCR & 0x80)); //wait until conversion completes evalue = ADR; return(evalue); }/******************************************************************************************/ // //Interrupt Routine for reciving thru SPI // ///******************************************************************************************/void interrupt 13 ISR_SPI() {int temp=0,rec_data=0;temp = SPSCR; //To clear the bit rec_data = SPDR;} /**************************************************************************************************///Spliting of bytes and sending byte by byte/**************************************************************************************************/ void sendSPIdata(unsigned int hex) { int temp1,temp2,temp,rec_data; //char dec; temp1 = hex/100; //to split data byte by byte temp2 = hex%100; DisableInterrupts; SPCR_SPRIE = 0; while(!SPSCR_SPRF); temp = SPSCR; rec_data = SPDR; sendSPIvalue(0x55); //say to Master the data coming here after is the data it has to get sendSPIvalue(temp1); sendSPIvalue(temp2); }/**************************************************************************************************///Transmit data through SCI only for testing/**************************************************************************************************/void sendSPIvalue(unsigned int i){ int temp,rec_data; SPDR = i ; while(!SPSCR_SPRF); temp = SPSCR; //To clear the bit rec_data = SPDR; sendSCIchar(0x20); sendSCIvalue(rec_data); }void sendSCIvalue(unsigned int hex) { int temp4,temp3,temp2,temp1,temp0,temp,tem,te; char dec; //int divideratio[3] = {0x2710,0x3E8,0x64,0xA} temp4 = hex/0x2710; dec = ((temp4 & 0xf) | 0x30); sendSCIchar(dec); te = hex % 0x2710; temp3 = te / 0x3E8; dec = ((temp3 & 0xf) | 0x30); //(char *) ((temp3<12) & 0xF000)|0x30;; sendSCIchar(dec); temp2 = te % 0x3E8; temp1 = temp2 / 0x64; dec = ((temp1 & 0xf) | 0x30); sendSCIchar(dec); temp = temp2 % 0x64; temp0 = temp/0xA; dec = ((temp0 & 0xf) | 0x30); sendSCIchar(dec); tem = temp % 0xA; dec = ((tem & 0xf) | 0x30); sendSCIchar(dec);}void sendSCIchar(char i){ unsigned char u8serialCount; u8serialCount=SCS1; SCDR = i; while(!SCS1_TC);}
//Master code#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */void sendSCIvalue(unsigned int);void sendSCIchar(char);void main(void) { EnableInterrupts; //Set Oscillator frequency OSCSC = 0x20; // Set bus speed to 12.8 MHZ OSCTRIM = Optional; // this macro loads value at $FFC0 and store it in $0038. // bus clock speed is 3.2MHz +- 5% // Set up configuration registers CONFIG2 = 0x05; // Enable reset and IRQ pins CONFIG1 = 0x01; // disable the watch dog. DDRA = 0x03; DDRB = 0x40; PTAPUE_PTAPUE2=1; //enable pullup resistor on PTA2 // Configure ADC ADCLK = 0x14; // ADC 10 bit ADSCR = 0x23; // continuous conversion, channel 3 (PTA5) // Configure ESCI SCPSC = 0b10000111; //Baudrate = ((12.8Mhz/4) / (64*1*1*5.21875)) = 9580 SCC1 = 0x40; SCC2 = 0x08; // Configure SPI SPSCR_SPR1 = 0; SPSCR_SPR0 = 0; SPCR_SPE = 1; SPCR_SPMSTR = 1; SPCR_CPHA = 1; SPCR_SPRIE = 1; SPDR = 0xFF; while(1); }/******************************************************************************************/ // //Interrupt Routine for reciving thru SPI // ///******************************************************************************************/void interrupt 13 ISR_SPI() {int temp=0,rec_data=0;temp = SPSCR; //To clear the bit rec_data = SPDR;sendSCIvalue(rec_data);sendSCIchar(0x20);SPDR = 0xFF;}
#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */void sendSCIvalue(unsigned int);void sendSCIchar(char);void delay(void);int k=0 ;void main(void) { EnableInterrupts; //Set Oscillator frequency OSCSC = 0x20; // Set bus speed to 12.8 MHZ OSCTRIM = Optional; // this macro loads value at $FFC0 and store it in $0038. // bus clock speed is 3.2MHz +- 5% // Set up configuration registers CONFIG2 = 0x05; // Enable reset and IRQ pins CONFIG1 = 0x01; // disable the watch dog. DDRA = 0x03; DDRB = 0x40; PTAPUE_PTAPUE2=1; //enable pullup resistor on PTA2 // Configure ADC ADCLK = 0x14; // ADC 10 bit ADSCR = 0x23; // continuous conversion, channel 3 (PTA5) // Configure ESCI SCPSC = 0b10000111; //Baudrate = ((12.8Mhz/4) / (64*1*1*5.21875)) = 9580 SCC1 = 0x40; SCC2 = 0x08; // Configure SPI SPSCR_SPR1 = 0; SPSCR_SPR0 = 0; SPCR_SPE = 1; SPCR_SPMSTR = 0; SPCR_CPHA = 1; SPCR_SPRIE = 1; SPDR = 0xAA; while(1);}/******************************************************************************************/ // //Interrupt Routine for reciving thru SPI // ///******************************************************************************************/void interrupt 13 ISR_SPI() {int temp=0,rec_data=0;temp = SPSCR; //To clear the bit rec_data = SPDR;sendSCIvalue(rec_data);SPDR = 0xAA;sendSCIchar(0x20);if(k == 0) { k = 1; delay(); sendSCIchar(0x20); }}void delay() {int i;for(i=0; i<100 ;i++);SPDR = 0xAA; k = 0;}