Hallo Rajendra
When the SPI (master) sends 8 bits of data over the MOSI line it
also clocks in 8 bits of data via the MISO line. Therefore when a
byte of data has successfully been sent (SPI_SPSR & 0x80 becomes
true) it also has a byte of received data which can be read from
SPI_SPDR. (when no slave it is probably 0xff or 0x00 depending on
the line state - if the slave has nothing to send it depends on the
device - eg. the LPC210x in slave mode sends byte the last received
byte)
This means that the slave must prepare and send data - the exact
sequence will depend on your peripheral device but it generally
means that you must first send a command byte to inform which data
it should sent to you and then you can read it (one or a sequence of
bytes). If the master has no data to send while reading the byte(s)
it can send a dummy byte (0xff or 0x00 etc.).
The code to send and also read in full duplex (assuming the slave
supports it) could look like this:
void fnSendReceive(unsigned char *out, unsigned char *in, unsigned
short usLength)
{
while (usLength--) {
IOCLR = SPI_SSEL_OUT; // activate SSEL
SPDR = *out; // send data byte
while (!(SPSR & SPIF)) {}; // wait for transmission to
complete
IOSET = SPI_SSEL_OUT; // remove SSEL
*in++ = (unsigned char)SPDR; // save data
}
}
Note that the LPC210x has a very simple SPI which has only one
interrupt flag to indicate when the transmission has completed and
the received data is available. There is no buffering in the
transmitter and so it is imperative that the flag is set before
sending the next byte otherwise it may be lost - as consequence the
throughput tends to suffer since the software processing has to take
place almost completely between transmission periods and it is
hardly possible to do any work during the byte transmission interval.
Regards
Mark Butcher
www.mjbc.ch
--- In lpc2000@yahoogroups.com, "rockraj_2003" <rockraj_2003@y...>
wrote:
>
> Hello Mark,
>
> Thanks for Ur Response.I tried Ur Code its working fine.
> I aslo changed my prog.This Code is also giving the Same response.
> I didn't CLR the SSEL pin at all.But I regularly changed SPIF to
0x00.
>
> Now, my doubt is if i want to use MISO as well what should i do.
> Since in the Data Sheet THE mentioned SPI as duplex.
>
> How I can use MISO.
>
> Silly Question, but I don't know about this.
> If the SPI is in MOSI, it can send DATA (MASTER OUTPUT) at the
Same
> time it receive data in SLAVE.(SLAVE INPUT).How much time it will
> wait to receive DATA. If again I put some Data to the SPI_SPDR it
> will transmit or it will wait to receive.
>
> > > #define SPI_SPCR (*((volatile unsigned char *)
0xE0020000))
> > > #define SPI_SPSR (*((volatile unsigned char *)
0xE0020004))
> > > #define SPI_SPDR (*((volatile unsigned char *)
0xE0020008))
> > > #define SPI_SPCCR (*((volatile unsigned char *)
0xE002000C))
> > > #define SPI_SPTCR (*((volatile unsigned char *)
0xE0020010))
> > > #define SPI_SPTSR (*((volatile unsigned char *)
0xE0020014))
> > > #define SPI_SPTOR (*((volatile unsigned char *)
0xE0020018))
> > > #define SPI_SPINT (*((volatile unsigned char *)
0xE002001C))
> > >
> > > int main (void)
> > > {
> > > int i;
> > > VPBDIV = 1;
> > > IODIR = 0x80;
> > > IOSET = 0x80;
> > > PINSEL0 = 0x5500;
> > > SPI_SPCCR = 0x93;
> > > SPI_SPCR = 0x20;
> > > while (1) {
> > > do SPI_SPDR = 0x55;
> > > while((SPI_SPSR & 0x80)==0);
> > > SPI_SPSR = 0x00;
> > > do SPI_SPDR = 0x0F;
> > > while((SPI_SPSR & 0x80)==0);
> > > SPI_SPSR = 0x00;
> > > do SPI_SPDR = 0xF0;
> > > while((SPI_SPSR & 0x80)==0);
> > > SPI_SPSR = 0x00;
> > > }
> > > }
>
>
> with Regards
> Rajendra R
>Message
Re: SPI RESPONDING
2005-03-15 by Mark Butcher
Attachments
- No local attachments were found for this message.