SPI interface with LPC2138

Started by namratha harikanth March 27, 2014
I need code for SPI interface with LPC2138 as soon as
possible

An Engineer's Guide to the LPC2100 Series

I need code for SPI interface with LPC2138 as soon as possible

Better get coding! :-)

-- Paul Curtis

==============================================================
Check out our new CrossWorks Technology products that include

Networking, SD/MMC storage, and handy source code packages!

TCP/IP http://goo.gl/hkmpIY Mass Storage http://goo.gl/mZ048i

==============================================================
@namratha... thats sweet !
On Thu, Mar 27, 2014 at 6:50 PM, Paul Curtis wrote:

> I need code for SPI interface with LPC2138 as soon as possible
>
> Better get coding! J
>
> -- Paul Curtis
>
> ==============================================================>
> Check out our new CrossWorks Technology products that include
>
> Networking, SD/MMC storage, and handy source code packages!
>
> TCP/IP http://goo.gl/hkmpIY Mass Storage http://goo.gl/mZ048i
>
> ==============================================================>
>
>

--
Shashank Maheshwari
On 3/27/2014 9:08 AM, namratha harikanth wrote:
> I need code for SPI interface with LPC2138 as soon as possible
>
We all did at some point, better get moving... It gest easier after the
fifth or sixth try!

You might want to invest in a serial debugging probe, just to make sure
you are getting out/in what you are expecting.
There is sample code all over the Internet - try Google!
Some of the very best code is at www.jcwren.com/arm http://www.jcwren.com/arm It is written for an LPC2148 but the SPI interface is identical.
The code I use is simple:
#include "LPC21xx.h"
#include "spi.h"
/*
* P0.4 => SCK connect to AD7327 SCLK - Pin 20
* P0.5 => MISO connect to AD7327 DOUT - Pin 18
* P0.6 => MOSI connect to AD7327 DIN - Pin 2
* P0.7 => SSEL connect to AD7327 CS' - Pin 1
*/
#define SSEL (1 << 7)
#define SPI_SPIE (0x00000080)
#define SPI_LSBF (0x00000040)
#define SPI_MSTR (0x00000020)
#define SPI_CPOL (0x00000010)
#define SPI_CPHA (0x00000008)
#define SPI_BitEnable (0x00000004)
#define SPI_Bits (0x00000000)
#define SPI_SPIF (0x00000080)
#define SPI_DIVIDER (8)
/*
* Set up SPI0 as a master with CPOL = 1, CPHA = 0 and 16 bit transfers.
* Force P0.7 to GPIO, set it for output and set it high
*/
void SPI0_init(void)
{
PINSEL0 |= 0x00001500;
PINSEL0 &= ~0x0000C000;
IO0SET = SSEL;
IO0DIR |= SSEL;
S0SPCR = SPI_MSTR | SPI_CPOL | SPI_BitEnable | SPI_Bits;
S0SPCCR = SPI_DIVIDER;
}
/*
* Send value to ADC and return value shifted in.
*/
unsigned short SPI0_Send(const unsigned short value)
{
IO0CLR = SSEL;
S0SPDR = value;
while (!(S0SPSR & SPI_SPIF))
;
IO0SET = SSEL;
return (S0SPDR);
}

It's up to you to get the clock divider right. I believe the core clock for my project is 60 MHz.
Richard