Thursday 9 June 2016

Serial Communication using LPC2148 (ARM)


Serial Communication:





BASICS OF SERIAL COMMUNICATION:
·        Serial communication uses single data line making it much cheaper
·        enables two computers in different cities to communicate over the telephone
·        Byte of data must be converted to serial bits using a parallel-in-serial-out shift register and transmitted over a single data line
·        receiving end there must be a serial-in-parallel-out shift register
·        If transferred on the telephone line, it must be converted to audio tones by modem
·        For short distance the signal can be transferred using wire
·        how PC keyboards transfer data to the motherboard
·        2 methods, asynchronous and synchronous
·        synchronous method transfers a block of data (characters) at a time
·        asynchronous method transfers a single byte at a time
·        Uses special IC chips called UART (universal asynchronous receiver-transmitter) and USART (universal synchronous asynchronous receiver-transmitter)







·       


·        in modern PCs one stop bit is standard
·        when transferring a text file of ASCII characters using 1 stop bit there is total of 10 bits for each character
·        8 bits for the ASCII code (1 parity bit), 1 bit each for the start and stop bits
·        for each 8-bit character there are an extra 2 bits, which gives 20% overhead
·        Data transfer rate
·        rate of data transfer bps (bits per second)
·        widely used terminology for bps is baud rate
·        baud and bps rates are not necessarily equal
·        baud rate is defined as the number of signal changes per second
·        RS232 standards
·        most widely used serial I/O interfacing standard
·        input and output voltage levels are not TTL compatible
·        1 bit is represented by -3 to -25 V
·        0 bit is +3 to +25 V
·        -3 to +3 is undefined
·        to connect RS232 to a microcontroller system must use voltage converters such as MAX232 to convert the TTL logic levels to the RS232 voltage levels, and vice versa
·        MAX232 IC chips are commonly referred to as line drivers


·       



Data communication classification
·        DTE (data terminal equipment)
·        DCE (data communication equipment)
·        DTE - terminals and computers that send and receive data
·        DCE - communication equipment responsible for transferring the data
·        simplest connection between a PC and microcontroller requires a minimum of three pins, TxD, RxD, and ground



·       

·        MAX233
·        MAX233 performs the same job as the MAX232
·        eliminates the need for capacitors
·        much more expensive than the MAX232


LPC2148 SERIAL PORT PROGRAMMING IN C:

·        Baud rate in theLPC2148:
·        serial communications of the LPC2148 with the COM port of the PC
·        must make sure that the baud rate of the LPC2148 system matches the baud rate of the PC's COM port
·        can use Windows HyperTerminal program

Ex1(a):

Write a program to transfer letter 'A' serially at 9600 baud, continuously.

#include<lpc214x.h>
#define PLLE 0
#define PLLC 1
#define PLOCK 10
#define DLAB 7
#define TEMT 6
//void pllinit(void);
void delay(int);

int main()

PINSEL0|=0x01;
//pllinit();
U0LCR=0x83;
U0DLL=97;
U0DLM=0x00;
U0LCR=U0LCR&~(1<<DLAB);
while(1)
{
U0THR='A';
while( (U0LSR&(1<<TEMT))==0);
}

return 0;
}

void delay(int ms)
{
T1CTCR=0x00;
T1TC=0;
T1PC=0;
T1PR=59999;
T1TCR=0x02;
T1TCR=0x01;
while( T1TC != ms);
T1TCR=0;

}

Simulation:






Ex1(b):
Write a program to transfer letter "WELCOME TO ALL" and "GOD BLESS YOU " serially at 9600 baud, continuously.

#include<lpc214x.h>
#define PLLE 0
#define PLLC 1
#define PLOCK 10
#define DLAB 7
#define TEMT 6
void pll_init(void);
void delay(int);
void uart0_init();
void tx();
int main()
{

pll_init();
uart0_init();
while(1)
{
tx();
}
return 0;
}
void tx()
{
int i; char a[]="WELCOME TO ALL\r";
char b[]="GOD BLESS YOU\r";
for(i=0;i<=14;i++)
  {
U0THR=a[i];
while( (U0LSR&(1<<TEMT))==0);
delay(10);
  }
for(i=0;i<=16;i++)
  {
U0THR=b[i];
while( (U0LSR&(1<<TEMT))==0);
delay(10);
 }
}
void uart0_init()
{
PINSEL0|=0x01;
U0LCR=0x83;
U0DLL=97;
U0DLM=0x00;
U0LCR=U0LCR&~(1<<DLAB);
}

void delay(int ms)
{
T1CTCR=0x00;
T1TC=0;
T1PC=0;
T1PR=59999;
T1TCR=0x02;
T1TCR=0x01;
while( T1TC != ms);
T1TCR=0;
}
void pll_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;
}

Simulation:



EX-2:
Program the LPC2148 to receive bytes of data serially and put them on LCD. Set the baud rate at 9600, 8-bit data, and 1 stop bit. 

#include<lpc214x.h>
#define PLLE 0
#define PLLC 1
#define PLOCK 10
#define DLAB 7
#define DR 0
#define rs 16
#define rw 17
#define en 18
#define lcdport IO0SET
#define lcdportclr IO0CLR
void uart0_init();
void daten(void);
void cmnd(void);
void pll_init(void);
void delay(int);
void lcdcmd(char);
void lcddata(char);
void lcd_init(void );
int main()
{

IO0DIR=IO0DIR|(0xff<<16);
pll_init();
uart0_init();
lcd_init();
while(1)
{
  while( (U0LSR & (1<<DR)) ==0);
     lcddata(U0RBR);
}
return 0;
}
void uart0_init()
{
PINSEL0|=5;
U0LCR=0x83;
U0DLL=97;
U0DLM=0x00;
U0LCR=U0LCR&~(1<<DLAB);
}

void delay(int ms)
{
T1CTCR=0x00;
T1TC=0;
T1PC=0;
T1PR=59999;
T1TCR=0x02;
T1TCR=0x01;
while( T1TC != ms);
T1TCR=0;
}
void pll_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 lcd_init()
{
lcdcmd(0x02);
lcdcmd(0x28);
lcdcmd(0x01);
lcdcmd(0x0e);
lcdcmd(0x80);
}
void lcdcmd(char ch)
{
lcdport = ((ch&0xf0)<<15);
cmnd();
lcdportclr = ((ch&0xf0)<<15);

lcdport = (((ch<<4)&0xf0)<<15);
cmnd();
lcdportclr = (((ch<<4)&0xf0)<<15);
}

void lcddata(char ch)
{
lcdport = ((ch&0xf0)<<15);
daten();
lcdportclr = ((ch&0xf0)<<15);

lcdport = (((ch<<4)&0xf0)<<15);
daten();
lcdportclr = (((ch<<4)&0xf0)<<15);
}
void cmnd()
{
lcdportclr = (1<<rs);
lcdportclr = (1<<rw);
lcdport = (1<<en);
delay(1);
lcdportclr = (1<<en);
delay(2);
}
void daten()
{
lcdport = (1<<rs);
lcdportclr = (1<<rw);
lcdport = (1<<en);
delay(1);
lcdportclr = (1<<en);
delay(50);
}


Simulation:


1 comment:

if you have any doubt please let me know