Académique Documents
Professionnel Documents
Culture Documents
Memory Interfacing
When we are executing any instruction, we need the microprocessor to access the
memory for reading instruction codes and the data stored in the memory. For this, both the
memory and the microprocessor requires some signals to read from and write to registers.
The interfacing process includes some key factors to match with the memory requirements
and microprocessor signals. The interfacing circuit therefore should be designed in such a
way that it matches the memory signal requirements with the signals of the microprocessor.
IO Interfacing
There are various communication devices like the keyboard, mouse, printer, etc. So,
we need to interface the keyboard and other devices with the microprocessor by using latches
and buffers. This type of interfacing is known as I/O interfacing.
Interfacing Techniques:
From above diagram it can be seen that interfacing techniques are broadly classified
as Digital and Analog. In Analog interfacing Voltage based Interfacing and Current based
Interfacing Techniques are used.
In Digital Interfacing it is classified as ON/OFF, Parallel, Serial Interfaces. In Serial
Interfaces it is further classified as Synchronous and Asynchronous. 1-wire, RS232, Ethernet
are Examples of Asynchronous. And 2-wire (I2C) , 4-wire (SPI) are examples of
synchronous way of Interfacing.
Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar
Interfacing of LEDs.
The eight LEDs can be interfaced with microcontroller as shown in figure below. In
this the LEDs are connected in sinking mode and external source of 3.3V is used to drive
the LEDs through the current Limiting resistor. Also flashing , running and chasing LEDs
program is written.
#include <lpc21xx.h>
int main(void)
{
while(1)
{
*IOSET0 = 0x007F8000; //Set all LED's
for(i=0;i<60000;i++);
#include <lpc21xx.h>
int main(void)
{
while(1)
{
for(j=0;j<8;j++)
{
*IOSET0 = 0x00008000 << j;
for(i=0;i<60000;i++);
*IOCLR0 = 0x00008000 << j;
for(i=0;i<60000;i++);
}
}
return 0;
}
Chasing LEDs Program:
/***********************************************************************
* Chasing_LEDS_main.c
************************************************************************/
#include <lpc21xx.h>
int main(void)
{
while(1)
{
}
return 0;
}
Relay Interfacing:
In this program the Relay is interfaced with LPC2148 at pin P1.24. as per requirement the
Relay will be turned ON and OFF. The Interfacing Diagram is as shown in figure below.
Program:
/***********************************************************************
* Relay_interfacing_main.c
************************************************************************/
#include <lpc21xx.h>
int main(void)
{
Buzzer Interfacing:
The Buzzer is interfaced with LPC2148 by connecting it to pin P0.4. The Interfacing diagram
is as shown below.
Program:
/***********************************************************************
* Buzzer_interfacing_main.c
************************************************************************/
#include <lpc21xx.h>
int main(void)
{
volatile unsigned int i,j,k;
*PINSEL0 = 00;
Program:
/***********************************************************************
* LCD_main.c
************************************************************************/
#include <LPC21xx.H>
#include<board.h>
int main(void)
{
*PINSEL0 = 0x0;
*PINSEL1 = 0x0;
*PINSEL2 = 0x0;
*IODIR1 = *IODIR1 | (0xFF << 16);
*IODIR1 = *IODIR1 | (1 << 25);
*IODIR0 = *IODIR0 | 0x20000400;
lcd_data('S');
lcd_data('I');
lcd_data('N');
lcd_data('H');
lcd_data('G');
lcd_data('A');
lcd_data('D');
while(1);
}
void lcd_cmd(unsigned int a)
{
int i;
Program:
#define ROW1 1<<2
#define ROW2 1<<3
#define ROW3 1<<4
#define ROW4 1<<5
#define COL1 1<<6
#define COL2 1<<7
#define COL3 1<<8
#define COL4 1<<9
void lcdinit();
void lcdcmd(unsigned int);
void lcddata(unsigned int);
void delay_lcd(void);
void DisplayLCD(char *str, int len);
#include <lpc21xx.h>
int main(void)
{
unsigned char key;
*PINSEL0=0X00;
*PINSEL1=0X00;
*PINSEL2=0X00;
lcdinit(); /* initialise the LCD*/
keyinit();
lcdcmd(0x80);
DisplayLCD("NBNSCOE Solapur.",16);
lcdcmd(0xc0);
DisplayLCD("ENTC Dept.",10);
while(1)
{
while((key = keyread()) == 0); //polling for key press and for message on
UART recv.
// key=key+0x30;
switch(key)
{
case 0x31:
lcdcmd(0x01);
DisplayLCD(" Key: 1 Pressed",16);
break;
break;
case 0x34:
lcdcmd(0x01);
DisplayLCD(" Key: 4 Pressed",16);
break;
case 0x35:
lcdcmd(0x01);
DisplayLCD(" Key: 5 Pressed",16);
break;
case 0x36:
lcdcmd(0x01);
DisplayLCD(" Key: 6 Pressed",16);
break;
case 0x37:
lcdcmd(0x01);
DisplayLCD(" Key: 7 Pressed",16);
break;
case 0x38:
lcdcmd(0x01);
DisplayLCD(" Key: 8 Pressed",16);
break;
case 0x39:
lcdcmd(0x01);
DisplayLCD(" Key: 9 Pressed",16);
break;
case 0x41:
lcdcmd(0x01);
DisplayLCD(" Key: Backspace",16);
break;
case 0x42:
lcdcmd(0x01);
DisplayLCD(" Key: IN Pressed",16);
break;
case 0x43:
lcdcmd(0x01);
break;
}
}
}
int keyread(void)
{
unsigned int rows,columns,code,key_val;
rows = *IOPIN0;
rows = rows & 0x0000003C; /* Read rows */
rows = rows >> 2; /* right shift */
debounce();
switch(code)
{
case 0x7E:key_val = 'D';
break;
case 0x7D:key_val = '0';
break;
case 0x7B:key_val = 'E';
break;
case 0x77:key_val = 'F';
break;
case 0xBE:key_val = '7';
break;
case 0xBD:key_val = '8';
break;
case 0xBB:key_val = '9';
break;
case 0xB7:key_val = 'C';
break;
case 0xDE:key_val = '4';
break;
case 0xDD:key_val = '5';
break;
case 0xDB:key_val = '6';
break;
case 0xD7:key_val = 'B';
break;
case 0xEE:key_val = '1';
break;
case 0xED:key_val = '2';
break;
case 0xEB:key_val = '3';
break;
case 0xE7:key_val = 'A';
break;
default:key_val=0;
break;
}
return key_val;
void debounce(void)
{
unsigned int j;
for (j=0;j<130000;j++)
{
}
}
void keyinit(void)
{
void lcdinit()
{
*IODIR1 =
LCD_D0|LCD_D1|LCD_D2|LCD_D3|LCD_D4|LCD_D5|LCD_D6|LCD_D7|LCD_RS;
*IODIR0 |= LCD_EN;
*IODIR0 |= LCD_BC;
//*IOCLR0=LCD_BC; //set on lcd back light
lcdcmd(0x38); //2 lines and 5x7 matrix
lcdcmd(0x0e); //Display On, Cursor Blinking
lcdcmd(0x06); //Clear Display screen
lcdcmd(0x01); //Increment cursor
lcdcmd(0x80);
*IOCLR1 =
LCD_D0|LCD_D1|LCD_D2|LCD_D3|LCD_D4|LCD_D5|LCD_D6|LCD_D7;
*IOCLR1 =
LCD_D0|LCD_D1|LCD_D2|LCD_D3|LCD_D4|LCD_D5|LCD_D6|LCD_D7;
*IOSET1 |= LCD_RS;
*IOSET1 |= outdata << 16;
*IOSET0 |= LCD_EN;
delay_lcd();
*IOCLR0 |= LCD_EN;
delay_lcd();
delay_lcd();
return;
}
void delay_lcd(void)
{
int j;
for(j=0;j<500;j++);
return;
}
void delay(void);
int main(void)
{
unsigned int adcdata;
*PINSEL1 = *PINSEL1 & 0XFCFFFFFF;
*PINSEL1 = *PINSEL1 | 0X01000000;
*ADCR = 0X01210302;
// Channel 1 , Clock 4Mhz, Burst Mode, 11 clocks per 10 bit ,
//AD conversion is operational, start conversion
unsigned int a = 15;
q_printf("\n %x",a);
while(1)
{
if(*ADDR & 0X80000000)
{
adcdata = (*ADDR & 0X0000FFC0);
adcdata = adcdata >> 6;
q_printf (" \n Temperature = %d",adcdata); //current_temp);
delay();
}
}
return 0;
}
Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar
void delay(void)
{
int i;
for(i=0;i<200000;i++);
}
RTC Interfacing
The Real Time clock is interfaced from External RTC using I2C protocol. Here the Pins
are configured and used for I2C communication. The Interfacing diagram is as shown
below.
Program:
#include "address_c.h"
#include "functions.h"
#include "board.h"
u8 SEC,MIN,HRS,DATE,MNTH,YEAR;
static u8 txbuf[10],rxbuf[10];
int main(void)
{
txbuf[0] = 0x00;
txbuf[1] = 0x59; // minute
txbuf[2] = 0x10; // hour
txbuf[3] = 0x01; // seconds
i2c_init();
rtc_init();
i2c_rtc_set(txbuf);
q_printf("\nInterfacing of External RTC[DS1307] to LPC2148 via I2C\n\n");
while(1)
Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar
{
rtc_set_addr(0x00);
i2c_rtc_get(rxbuf);
q_printf("Todays Calendar: %s\n",__DATE__);
q_printf("The time is : ");
q_printf("%x", rxbuf[3]);
q_printf(":");
q_printf("%x", rxbuf[2]);
q_printf(":");
q_printf("%x", rxbuf[1]);
q_printf("\n\n");
delay(600);
}
return 0;
}
External Interrupt:
In this the External Interrupt 3 is configured for operation. In normal condition the LPC2148
is flashing the LEDS and whenever the interrupt is occurring the count on 7 Segment display
is incremented by one and is shown on the display too. The interfacing diagram is shown
below.
Program
#include<lpc21xx.h>
int main(void)
{
unsigned int j;
*IODIR0 =0x007F8000; //initialise LEDs
*IODIR1=0xff0000; //initialise seven segment
*VICVectAddr0 = (unsigned int ) ExtInt; //install ISR for External-interrupt
*VICVectCntl0 = 0x20 | 17; // select slot-0 for EINT3
*VICIntEnable |= (1<<17); // enable EINT3 interrupt
*PINSEL1 = 0x20000000; //configure P0.30 for External interrupt-3
*EXTMODE = 0x08; //assign EINT3 as edge sensitive
*EXTPOLAR = 0x08; //assign EINT3 as rising edge triggered
while(1)
{
*IOSET0 = 0x7F8000; // turn on all LEDs
for (j=0;j<=200000;j++);
*IOCLR0 = 0x7F8000; // turn off all LEDs
for (j=0;j<=200000;j++);
}
return 0;
}
void ExtInt(void)
{
static int i=1;
unsigned int
count[10]={0x3f0000,0x060000,0x5b0000,0x4f0000,0x660000,0x6d0000,0x7d0000,0x07
0000,0x7f0000,0x6f0000};
unsigned int j;
Program:
#include<lpc21xx.h>
int main(void)
{
void Timer(void)
{
unsigned int j;
*T0IR = 0x01; // clear Timer-0 interrupt
*IOSET0 = 0x7F8000; // turn on all LEDs
for (j=0;j<=200000;j++);
*IOCLR0 = 0x7F8000; // turn off all LEDs
for (j=0;j<=200000;j++);
*VICVectAddr =0x00; // acknowledge interrupt
}
UART Programming:
In UART the serial communication is established between Microcontroller and PC using
RS232 connnections. RxD and TxD lines are used. The Interfacing diagram is as shown
below.
Program:
#include <lpc214x.h>
void initClocks(void);
void initUART0(void);
void U0Write(char data);
void Send_String(char* StringPtr);
int main(void)
{
initClocks(); // Set CCLK=60Mhz and PCLK=60Mhz
initUART0();
while(1)
{
Send_String(String); //Pass the string to the USART_putstring function and
sends it over the serial
Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar
for(delay=0; delay<500000; delay++); // delay
}
}
void initUART0(void)
{
PINSEL0 = 0x5; /* Select TxD for P0.0 and RxD for P0.1 */
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit | DLAB set to 1 */
U0DLL = 110;
U0DLM = 1;
U0FDR = 0xF1; /* MULVAL=15(bits - 7:4) , DIVADDVAL=0(bits - 3:0)*/
U0LCR &= 0x0F; // Set DLAB=0 to lock MULVAL and DIVADDVAL
//BaudRate is now ~9600 and we are ready for UART communication!
}
void initClocks(void)
{
PLL0CON = 0x01; //Enable PLL
PLL0CFG = 0x24; //Multiplier and divider setup
PLL0FEED = 0xAA; //Feed sequence
PLL0FEED = 0x55;
while(*StringPtr != 0x00){
U0Write(*StringPtr);
StringPtr++;}
}
Program:
void PLL_Init(void)
{
PLL0CON = 0x01; //Enable PLL
PLL0CFG = 0x24; //Multiplier and divider setup
PLL0FEED = 0xAA; //Feed sequence
PLL0FEED = 0x55;
PWM Programming:
PWM signals are used for the speed control of DC Motors. And there are different
applications of it too. Here the code is written to show how to generate PWM signal using
LPC2148. There are 4 switches and as per the switch pressed the % duty cycle output
changes for the pin P0.8 where LED is connected. So we can see the amount of glow in LED
is proportional to the duty cycle of signal.
Program:
#include <lpc214x.h>
int main(void)
{
initClocks(); //Initialize CPU and Peripheral Clocks @ 60Mhz
initPWM(); //Initialize PWM
while(1)
{
if( !((IO1PIN) & (1<<16)) ) // Check P1.16
{
PWMMR4 = 2500; //T-ON=25% , Hence 25% Bright
PWMLER = (1<<4); //Update Latch Enable bit for PWMMR4
}
else if( !((IO1PIN) & (1<<17)) ) // Check P1.17
{
PWMMR4 = 5000; //50% Bright
PWMLER = (1<<4);
}
else if( !((IO1PIN) & (1<<18)) ) // Check P1.18
{
PWMMR4 = 7500; //75% Bright
PWMLER = (1<<4);
}
else if( !((IO1PIN) & (1<<19)) ) // Check P1.19
{
PWMMR4 = 10000; //100% Bright
PWMLER = (1<<4);
}
}
//return 0; //normally this wont execute ever
}
void initPWM(void)
{
PINSEL0 = (PINSEL0 & ~(1 << 16)) | (1 << 17); // Select PWM4 output for
Pin0.8
PWMPCR = 0x0; //Select Single Edge PWM - by default its single Edged so this
line can be removed
PWMPR = 60-1; // 1 micro-second resolution
PWMMR0 = 10000; // 10ms period duration
PWMMR4 = 500; // 0.5ms - pulse duration i.e width (Brigtness level)
PWMMCR = (1<<1); // Reset PWMTC on PWMMR0 match
//PWM Generation goes active now - LED must be 25% Bright after boot!!
//Now you can get the PWM output at Pin P0.8!
}
void initClocks(void)
{
PLL0CON = 0x01; //Enable PLL
PLL0CFG = 0x24; //Multiplier and divider setup
PLL0FEED = 0xAA; //Feed sequence
PLL0FEED = 0x55;