Thursday 9 June 2016

SPI using LPC2148 (ARM)

SPI using LPC2148 (ARM):


Data Transfer Controller to Controller:


Master Mode:


#include<lpc214x.h>

void  clk_init()
{
PLL0CON = 0x01;
PLL0CFG = 0x24;
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
while(!(PLL0STAT & (1<<10)));
PLL0CON = 0x03;
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
}
int main()
{
clk_init();
PINSEL0 = 1<<8 | 1<<10 | 1<<12 | 0x5;
IO0DIR = 1<<2;//cs
 U0LCR = 0x83;
U0DLL = 97;
  U0DLM = 0;
U0LCR = 0x03;
IO0SET = 1<<2;
S0SPCCR = 8;
S0SPCR = 0x864;
IO0CLR = 1<<2;
S0SPDR = 'A';
while(!(S0SPSR & (1<<7)));//monitor SPIF

}



Slave Mode:


#include<lpc214x.h>
#define SPIF 7
#define TEMT 6
#define PLLE 0
#define PLLC 1
#define PLOCK 10
#define DLAB 7
void  pll0_init()
{
PLL0CON = 0x01;
PLL0CFG = 0x24;
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
while(!(PLL0STAT & (1<<10)));
PLL0CON = 0x03;
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
VPBDIV=0x00;
}
void uart0_init()
{
U0LCR = 0x83;
U0DLL = 97;
U0DLM = 0;
U0LCR = 0x03;
}
int main()
{
pll0_init();
uart0_init();
IO0DIR|=(0xff<<10);
PINSEL0 = 1<<8 | 1<<10 | 1<<12 | 0x5 | 1<<14;
S0SPCR = 0x804;
while(!(S0SPSR & (1<<SPIF)));//monitor SPIF 
IO0SET= IO0SET|(S0SPDR<<10);
}
 

Simulation:







Second  Program:


MASTER Mode:

#include<lpc214x.h>
#define SPIF 7
#define TEMT 6
#define PLOCK 10
#define PLLE 0
#define PLLC 1
#define DLAB 7
void uart0_init()
{
    PINSEL0|=5;
    U0LCR=0x83;
    U0DLL=97;
    U0DLM=0x00;   
    U0LCR=U0LCR&~(1<<DLAB);
}
void pll0_init()
{
 PLL0CFG=0x24;
 PLL0CON=PLL0CON|(1<<PLLE);
 PLL0FEED=0xAA;
 PLL0FEED=0x55;
 //PLL0CON=PLL0CON|(1<<PLLC);
 while( (PLL0STAT & (1<<PLOCK))==0);
 PLL0CON=PLL0CON|3;
 PLL0FEED=0xAA;
 PLL0FEED=0x55;   
 VPBDIV=0x00;
}

void spi0_init()
{
    PINSEL0 |=0x5505;// 1<<8 | 1<<10 | 1<<12 | 0x5;
    IO0DIR = 1<<2;//cs
    IO0CLR = 1<<2;
    IO0DIR|=(0xff<<10);
    S0SPCCR = 16;
    S0SPCR = 0x864;
    IO0CLR = 1<<2;
}
int main()
{
    pll0_init();
    //uart0_init();
    spi0_init();
    S0SPDR = 'A';
    while(!(S0SPSR & (1<<SPIF)));//monitor SPIF
    //IO0SET= IO0SET|(S0SPDR<<10);
    U0THR = S0SPDR;
    while(!(U0LSR & (1<<TEMT)));   
   
   
}



SLAVE Mode:


#include<lpc214x.h>
#define SPIF 7
#define TEMT 6
#define PLLE 0
#define PLLC 1
#define PLOCK 10
#define DLAB 7
void  pll0_init()
{
    PLL0CON = 0x01;
    PLL0CFG = 0x24;
    PLL0FEED = 0xAA;
    PLL0FEED = 0x55;
    while(!(PLL0STAT & (1<<10)));
    PLL0CON = 0x03;
    PLL0FEED = 0xAA;
    PLL0FEED = 0x55;
    VPBDIV=0x00;
}
void uart0_init()
{
    U0LCR = 0x83;
    U0DLL = 97;
    U0DLM = 0;
    U0LCR = 0x03;
}
int main()
{
    pll0_init();
    //uart0_init();
    IO0DIR|=(0xff<<10);
    PINSEL0 = 1<<8 | 1<<10 | 1<<12 | 0x5 | 1<<14;
    S0SPCR = 0x844;
    //S0SPDR = 'B';
    while(!(S0SPSR & (1<<SPIF)));//monitor SPIF
    IO0SET= IO0SET|(S0SPDR<<10);
    //U0THR = S0SPDR;
    //while(!(U0LSR & (1<<TEMT)));   
}



0 comments:

Post a Comment

if you have any doubt please let me know