I have tried to loop back MOSI and MISO and test.
The code appears to be working sometimes based on the led signals... This is what I could infer:
1)code is sending a character 2)Code is receiving a character 3)The received character is not 0xff or the looped back character but something else.
Is there a specific clock i need to use for spi?I use 12Mhz and use 1/8 th for spi. Should I initialize anything for loopback or receive?
The datasheet says read the S0SPSR to clear register before receiving data...how do I do that?
Here is the code for your reference
#include <lpc214x.h> //Includes LPC2148 register definitions void spi_init(); //To define SPI void spi_send(char p); char spi_receive(); int main(void) { char p='l'; int i=0,j=0,k=0; for(j=0;j<12000;j++) { for(k=0;k<1000;k++); } PINSEL0 = (1<<8)|(1<<10)|(1<<12)|(1<<14); // Enable MISO MOSI SCK and Slave Select PINSEL1 = 0x00000000; PINSEL2 = 0x00000000; IO0DIR = (1<<14); IO0SET |=(1<<14); IO1DIR = (1<<19) | (1<<18) | (1<<17) | (1<<16); spi_init(); //Initialize spi while (1) { spi_send('0'); //Send a character and receive the same character while(p!='0') { p=spi_receive(); if(p==(char)0xff) { IO1CLR=(1<<19)|(1<<18)|(1<<17)|(1<<16); //to check if no data is received without interrupt flag } } while(p=='0') { IO1CLR=(1<<19)|(1<<18)|(1<<17)|(1<<16); //if data is received properly IO1SET=(1<<19)|(1<<17)|(1<<16); while(1); } } } void spi_init() { S0SPCR|=0x20; //Master Mode S0SPCCR=0x8; } void spi_send(char p) { IO0CLR |=(1<<14); S0SPDR = p; while ((!S0SPSR & 0x80)); S0SPSR &=~ 0x80; IO0SET |=(1<<14); } char spi_receive() { char p; IO0CLR |=(1<<14); S0SPDR=0xff; IO1SET=(1<<18)|(1<<17)|(1<<16); //to check if function is entered while ((!S0SPSR & 0x80)); p=S0SPDR; IO1SET=(1<<19)|(1<<17)|(1<<16); //to check if data is received return p; IO0SET |=(1<<14); }
Any help is gratefully appreciated.
Thank You