Update

One new tab added. Open in browser view if it is not visible. (25/08/2022 08:48)

Using SPI Protocol to send data to a 7 Segment Display from LPC2148

SPI or Serial Peripheral Interface uses 4 lines of communication, SCLK, MISO, MOSI and SSEL.

SCLK is Serial Clock, as this communication is synchronous. MISO is Master In Slave Out, as the name suggests, Master receives data from the Slave. MOSI is Master Out Slave In, data flow happens from the Master device to the Slave device. SSEL is a Slave Select Pin, One pin for one device.

SPI supports high speed communication as there are more data lines and its a full duplex interface. That means Master can send as well as recieve data from the Slave at one instance of time.

SPI communication makes use of shift registers. During SPI communication, the data is simultaneously transmitted (shifted out serially onto the MOSI/SDO bus) and received (the data on the bus (MISO/SDI) is sampled or read in).

SPI supports upto 60 Mbps and is used for on-chip communication.


In below example, SCLK, MOSI and SSEL lines will be used as data is to be transmitted to a 7-Segment Display from the microcontroller. We can use PLL to increase the frequency or use standard clock frequency.

In SPI, Master controls the communication by controlling the Clock.

The registers used here are S0SPCR (SPI Control Register), S0SPCCR (SPI Clock Counter Register), S0SPDR (SPI Data Register) and S0SPSR (SPI0 Status Register).


 // SEND DATA TO 7 SEGMENT DISPLAY USING SPI PROTOCOL
#include<lpc214x.h>
void delay(unsigned int n)
{
unsigned int i;
for(i=0;i<1240*n;i++);
}
/*
void setClck()
{
PLL0CON=0x01;
PLL0CFG=0x24;
PLL0FEED=0x55;
PLL0FEED=0xAA;
while(PLL0STAT & (1<<10)==0);
PLL0CON|=0x02;
PLL0FEED=0x55;
PLL0FEED=0xAA;
}
*/
void SPI_INIT()
{
// setClck();
// VPBDIV=0x00;// 60/4=15MHZ
PINSEL0=0x00001500;
IO0DIR|=0x00000080;
S0SPCR=0x0020;// ACTIVE LOW AND RISING EDGE
S0SPCCR=16;// 1 mhz
}

void spi_tx(unsigned char a)
{
S0SPDR=a;
while(S0SPSR & (1<<7)==0);

IO0SET = (1<<7);
delay(50);
IO0CLR = (1<<7);
//delay(1);
}



void spi_tx_str(char str[])
{
int i=0;
for(;i<10;i++)
{
spi_tx(str[i]);
delay(400);
}
}
int main(void)
{
char str[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
SPI_INIT();
while(1)
{
delay(1);
spi_tx_str(str);
}
}

No comments: