Wednesday, 22 August 2012

Simple SPI0 - LPC21


led module can access using SPI0

Ref: BlueBoard_schematics_V1R1-3(LPC2148)


------------------------------------------------------------
#include <LPC214X.H>

void spiInit(void);
void spiSend(unsigned short);
void delay(void);

#define SPIF (1<<7)
#define DATA 0xFF


int main()
{
int i=1;
spiInit();

while(1) /*rotating led*/
{
spiSend(i);
delay();

i <<= 1;
if(i==0x0100)
i=1;
}
}

void spiInit()
{
PINSEL0=0x01500; /*MOSI0-bit 13:12 = 01, MISO0-bit 11:10 = 01, SCK0-bit 9:8 = 01 ,(SSEL0-not enabled, it is generated from code)*/ // ..,0001,0101,0000,0000 => 0x1500
IO0SET = 1<<7;   /* chip sel kept high*/
IO0DIR |= 1<<7; /* chip sel dir to output*/
S0SPCCR=0x8; /* SPI Clock Counter Register. This register controls the frequency of a master’s SCK0 */
S0SPCR=0x20; /*SPI Control Register - Device selected as master */
}

void spiSend(unsigned short data)
{
IOCLR0 = 1<<7; // enable slave select pin SSEL0
S0SPDR = data;
while (!(S0SPSR & SPIF)); /*SPI Status Register-SPI transfer complete flag. When 1, this bit indicates when a SPI
data transfer is complete */
IOSET0 = 1<<7; // disable slave select pin SSEL0
}

void delay(void)
{
int i, j;
  for(i=0; i<3000; i++)
  for(j=0; j<300; j++);
}
-----------------------------------------------------------------------------

No comments:

Post a Comment