Vous êtes sur la page 1sur 26

Unit - 4

Interfacing and Programming


Interfacing:
It is the method used to establish the communication between microcontroller and
peripheral. As peripherals may or may not work at speed same as that of microcontroller it
is necessary to have some techniques to interface various peripherals with microcontroller.
So the need of interfacing is to have method to establish communication between slow
devices and fast operating microcontrollers.
Interface is the path for communication between two components. Interfacing is of
two types, memory interfacing and I/O interfacing.

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.

Code for Flashing LEDs


/***********************************************************************
* Flashing_LEDS_main.c
************************************************************************/

#include <lpc21xx.h>

int main(void)
{

volatile unsigned int i;


*PINSEL0 = 00;
*PINSEL1 = 00;

*IODIR0 =*IODIR0 | 0x007F8000; //Set Direction of LED

while(1)
{
*IOSET0 = 0x007F8000; //Set all LED's
for(i=0;i<60000;i++);

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


*IOCLR0 = 0x007F8000; //Clear all LED's
for(i=0;i<60000;i++);
}
return 0;
}

Running LED Program:


/****************************************************************
Running_LED_main.c
*****************************************************************/

#include <lpc21xx.h>

int main(void)
{

volatile unsigned int i,j;


*PINSEL0 = 00;
*PINSEL1 = 00;

*IODIR0 =*IODIR0 | 0x007F8000; //Set Direction of LED

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)
{

volatile unsigned int i,j;


*PINSEL0 = 00;
*PINSEL1 = 00;

*IODIR0 =*IODIR0 | 0x007F8000; //Set Direction of LED

while(1)
{

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


for(j=0;j<8;j++)
{
*IOSET0 = 0x00008000 << j;
for(i=0;i<60000;i++);
}
*IOCLR0 = 0x007F8000; //Clear all LED's

}
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)
{

volatile unsigned int i,j;


*PINSEL2 = 00;

*IODIR1 =*IODIR1 | 0x01000000; //Set Direction


Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar
while(1)
{
*IOCLR1 = 0x01000000; //Turn ON Relay
for(j=0;j<20;j++)
for(i=0;i<60000;i++);
*IOSET1 = 0x01000000; //Turn OFF Relay
for(j=0;j<20;j++)
for(i=0;i<60000;i++);
}
return 0;
}

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;

*IODIR0 =*IODIR0 | 0x00000010; //Set Direction of Buzzer

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


while(1)
{
for(k=0;k<5;k++)
{
*IOCLR0 = 0x00000010; //Turn on Buzzer
for(i=0;i<80;i++)
for(j=0;j<20000;j++);
*IOSET0 = 0x00000010; //Turn off Buzzer
for(i=0;i<80;i++)
for(j=0;j<20000;j++);
}
for(k=0;k<5;k++)
{
*IOCLR0 = 0x00000010; //Turn on Buzzer
for(i=0;i<40;i++)
for(j=0;j<20000;j++);
*IOSET0 = 0x00000010; //Turn off Buzzer
for(i=0;i<40;i++)
for(j=0;j<20000;j++);
}
for(k=0;k<5;k++)
{
*IOCLR0 = 0x00000010; //Turn on Buzzer
for(i=0;i<20;i++)
for(j=0;j<20000;j++);
*IOSET0 = 0x00000010; //Turn off Buzzer
for(i=0;i<20;i++)
for(j=0;j<20000;j++);
}
for(k=0;k<5;k++)
{
*IOCLR0 = 0x00000010; //Turn on Buzzer
for(i=0;i<1;i++)
for(j=0;j<20000;j++);
*IOSET0 = 0x00000010; //Turn off Buzzer
for(i=0;i<1;i++)
for(j=0;j<20000;j++);
}
}
return 0;
}

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


LCD Interfacing:
A 16x2 LCD can be interfaced with LPC2148. The Interfacing diagram is as shown below
in which RS is connected to P1.25, EN is connected P0.29, Backlight is connected to P0.10
and data lines D0 to D7 are connected to P1.16 to P1.23 pins.

Program:
/***********************************************************************
* LCD_main.c
************************************************************************/

#include <LPC21xx.H>
#include<board.h>

void lcd_cmd(unsigned int);


void lcd_data(unsigned int);

int main(void)
{

*PINSEL0 = 0x0;
*PINSEL1 = 0x0;
*PINSEL2 = 0x0;
*IODIR1 = *IODIR1 | (0xFF << 16);
*IODIR1 = *IODIR1 | (1 << 25);
*IODIR0 = *IODIR0 | 0x20000400;

*IOCLR0 = *IOCLR0 | 0x20000000; // EN=0


*IOCLR0 = *IOCLR0 | 0x00000400;//set on lcd back light

lcd_cmd(0x38); //2 lines and 5x7 matrix


lcd_cmd(0x0e); //Display On, Cursor Blinking
Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar
lcd_cmd(0x06); //Clear Display screen
lcd_cmd(0x01); //Increment cursor
lcd_cmd(0x80); //1st Line starting from 1st position.

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;

*IOCLR1 = *IOCLR1 | 0xff << 16;


*IOCLR1 = *IOCLR1 | 0x02000000;
*IOSET1 = *IOSET1 | a << 16;
*IOSET0 = *IOSET0 | 0x20000000; // EN=1
for(i=0;i<5000;i++);
*IOCLR0 = *IOCLR0 | 0x20000000; // EN=0
for(i=0;i<60000;i++);
return;
}

void lcd_data(unsigned int a)


{
int i;
*IOCLR1 = *IOCLR1 | 0xff<<16;
*IOSET1 = *IOSET1 | 0x02000000;
*IOSET1 = *IOSET1 | a << 16;
*IOSET0 = *IOSET0 | 0x20000000; // EN=1
for(i=0;i<5000;i++);
*IOCLR0 = *IOCLR0 | 0x20000000; // EN=0
for(i=0;i<60000;i++);
return;
}

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


Keypad LCD Interfacing:
In this the 4x4 Matrix keypad is interfaced from pin P0.2 to P0.9 as four Rows and four
Columns. The scanning is done and the resultant code is captured by polling through the
rows and columns. The corresponding key pressed is displayed on LCD accordingly. The
Interfacing diagram for the same is as shown below.

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

#define LCD_D0 1<<16


#define LCD_D1 1<<17
#define LCD_D2 1<<18
#define LCD_D3 1<<19
#define LCD_D4 1<<20
#define LCD_D5 1<<21
#define LCD_D6 1<<22
#define LCD_D7 1<<23

#define LCD_BC 1<<10 //BACK LIGHT CONTROL


#define LCD_RS 1<<25 //30
#define LCD_EN 1<<29

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


void dela(void);
void debounce(void);
int keyread (void);
void keyinit(void);
void led_blink(void);

void lcdinit();
void lcdcmd(unsigned int);
void lcddata(unsigned int);
void delay_lcd(void);
void DisplayLCD(char *str, int len);

unsigned int scankey_titan[] = {0xee,0xed,0xeb,0xde,0xdd,0xdb,0xbe,0xbd,


0xbb,0x7d,0xe7,0xd7,0xb7,0x77,0x7b,0x7e };

#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;

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


case 0x32:
lcdcmd(0x01);
DisplayLCD(" Key: 2 Pressed",16);
break;
case 0x33:
lcdcmd(0x01);
DisplayLCD(" Key: 3 Pressed",16);

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);

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


DisplayLCD(" Key:OUT Pressed",16);
break;
case 0x44:
lcdcmd(0x01);
DisplayLCD(" Key: * Pressed",16);
break;
case 0x45:
lcdcmd(0x01);
DisplayLCD(" Key:CLR Pressed",16);
break;
case 0x46:
lcdcmd(0x01);
DisplayLCD(" Key: ENTER ",16);
break;
default:
lcdcmd(0x01);
DisplayLCD(" Key: 0 Pressed",16);

break;
}
}
}

int keyread(void)
{
unsigned int rows,columns,code,key_val;

*IODIR0 = *IODIR0 | 0x000003FC; /* Set P0.2 to P0.9 as Output */


*IOCLR0 |= 0x000003FC; /* Clear all the port pins */
*IODIR0 = *IODIR0 | 0x0000003C; /* P0.2, P0.3, P0.4 and P0.5 are Output ROWS*/
*IODIR0 = *IODIR0 & ~0x000003C0; /* P0.6, P0.7, P0.8 and P0.9 are Input Columns*/
*IOSET0 |= 0x000003C0; /* Set high 4 Cloumns of keybord, P0.14 to P0.17*/
columns = *IOPIN0; /* read columns*/
columns = columns & 0x000003C0; /* Mask the Columns */
columns = columns >> 2; /* right shift*/

*IODIR0 = *IODIR0 | 0x00003FC;


*IOCLR0 |= 0x00003FC;
*IODIR0 = *IODIR0 | 0x000003C0; /* P0.6, P0.7, P0.8 and P0.9 are Output columns */
*IODIR0 = *IODIR0 & ~0x0000003C; /*P0.2, P0.3, P0.4 and P0.5 are Input rows */

rows = *IOPIN0;
rows = rows & 0x0000003C; /* Read rows */
rows = rows >> 2; /* right shift */

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


code = rows | columns; /* merge rows and columns */

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;

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


void dela()
{
unsigned long int i,d;
for(i=0;i<100;i++)
{
for(d=0;d<100;d++);
}
}

void debounce(void)
{
unsigned int j;
for (j=0;j<130000;j++)
{
}
}

void keyinit(void)
{

*IODIR0 = *IODIR0 | 0x000003FC;


}

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);

void lcdcmd(unsigned int cmddata)


{

*IOCLR1 =
LCD_D0|LCD_D1|LCD_D2|LCD_D3|LCD_D4|LCD_D5|LCD_D6|LCD_D7;

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


*IOCLR1 |= LCD_RS;
*IOSET1 = cmddata << 16;
*IOSET0 |= LCD_EN;
delay_lcd();
*IOCLR0 |= LCD_EN;
delay_lcd();
delay_lcd();
return;
}

void DisplayLCD(char *str, int len)


{
while(len-- != 0)
{
lcddata(*str);
str++;
}
}

void lcddata(unsigned int outdata)


{

*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;
}

Interfacing of Seven segment Display:


The common cathode 7 Segment display is interfaced with LPC2148 using Pins P1.16 to
P1.23 where P1.16 is connected to ‘a’ and P1.23 is connected to ‘dp’. The interfacing
diagram is as shown below.

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


Program:
#include<lpc21xx.h>
int main(void)
{
unsigned int j, i=0;
unsigned int count[10] = {0x3f0000, 0x060000, 0x5b0000, 0x4f0000, 0x660000,
0x6d0000, 0x7d0000, 0x070000, 0x7f0000, 0x6f0000};
*IODIR1=0xff0000; //initialise seven segment
while(1)
{
for(i=0;i<10;i++)
{
*IOSET1=count[i]; // display '0' to '9' on receipt of external
interrupt
for (j=0;j<=90000;j++);
for (j=0;j<=90000;j++);
*IOCLR1=0xff0000;
for (j=0;j<=90000;j++);
for (j=0;j<=90000;j++);
}
}
return 0;
}

ADC Interfacing for Temperature Sensor:


In this the LM35 is connected to AD0 channel number 1 and is converted to Digital signal.
Channel number 1 is Pin P0.28 accordingly the PINSEL1 is configured. The data is
displayed on serial port connected to PC via USB to Serial converter and in Hyper-terminal
software the data is received from LM35 by converting analog data to digital data. The
Interfacing diagram is shown below.
Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar
Program:
#include <LPC22xx.h>
#include <board.h>

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>

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


void ExtInt(void) __attribute__ ((interrupt("IRQ")));

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;

*EXTINT = 0x08; // clear EINT3 interrupt

*IOSET1=count[i]; // display '0' to '9' on receipt of external interrupt


for (j=0;j<=90000;j++);
for (j=0;j<=90000;j++);
*IOCLR1=0xff0000;
*VICVectAddr =0x00; // acknowledge interrupt
i++;
if(i>9)
i=0;
}

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


Use of Timer:
The Timer is used for LED Flashing. When Timer is configured we can get precise delay as
per the requirement. Whenever the timer overflow occurs the interrupt is generated and that
interrupt can be used for toggling the status of GPIO Pins where LEDs are connected. The
Interfacing diagram is as shown below.

Program:
#include<lpc21xx.h>

void Timer(void) __attribute__ ((interrupt("IRQ")));

int main(void)
{

*IODIR0 =0x007F8000; //initialise LEDs


*VICVectAddr1 = (unsigned int ) Timer; //install ISR for Timer-interrupt
*VICVectCntl1 = 0x20 | 4; // select slot 1 for Timer-0
*VICIntEnable |= (1<<4); // enable Timer-0 interrupt
*T0MR0 = 150000; // load the count value in MR0 to MR3
*T0MR1 = (*T0MR0) * 2;
*T0MR2 = (*T0MR0) * 3;
*T0MR3 = (*T0MR0) * 4;
*T0MCR = 0x249; // enable interrupts on MR0,MR1,MR2,MR3
*T0TCR = 0x01; // enable Timer mode
while(1);

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


return 0;
}

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);

char String[]="Hello from BE ENTC Class !!! \n\r\n";


unsigned int delay;

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 U0Write(char data)


{
while (!(U0LSR & (1<<5))); // wait till the THR is empty
// now we can write to the Tx FIFO
U0THR = data;
}

void initClocks(void)
{
PLL0CON = 0x01; //Enable PLL
PLL0CFG = 0x24; //Multiplier and divider setup
PLL0FEED = 0xAA; //Feed sequence
PLL0FEED = 0x55;

while(!(PLL0STAT & 0x00000400)); //is locked?

PLL0CON = 0x03; //Connect PLL after PLL is locked


PLL0FEED = 0xAA; //Feed sequence
PLL0FEED = 0x55;
VPBDIV = 0x01; // PCLK is same as CCLK i.e.60 MHz
}

void Send_String(char* StringPtr){

while(*StringPtr != 0x00){
U0Write(*StringPtr);
StringPtr++;}
}

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


PLL Initialization:
The PLL is used to generate the CCLK and PCLK as per the user requirements. In this the
program code is written to show how the different registers are used to set the required
frequency for operation. Here CCLK = PCLK = 60MHz.

Program:

void PLL_Init(void)
{
PLL0CON = 0x01; //Enable PLL
PLL0CFG = 0x24; //Multiplier and divider setup
PLL0FEED = 0xAA; //Feed sequence
PLL0FEED = 0x55;

while(!(PLL0STAT & 0x00000400)); //is locked?

PLL0CON = 0x03; //Connect PLL after PLL is locked


PLL0FEED = 0xAA; //Feed sequence
PLL0FEED = 0x55;
VPBDIV = 0x01; // PCLK is same as CCLK i.e.60 MHz
}

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>

void initPWM(void); // Initialize PWM


Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar
void initClocks(void); // Setup PLL and Clock Frequency

int main(void)
{
initClocks(); //Initialize CPU and Peripheral Clocks @ 60Mhz
initPWM(); //Initialize PWM

//IO1DIR = 0x1; This is not needed!


//Also by default all pins are configured as Inputs after MCU Reset.

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

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar


PWMLER = (1<<0)|(1<<4); // update MR0 and MR4
PWMPCR = (1<<12); // enable PWM output
PWMTCR = (1<<1) ; //Reset PWM TC & PR

//Now , the final moment - enable everything


PWMTCR = (1<<0) | (1<<3); // enable counters and PWM Mode

//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;

while(!(PLL0STAT & 0x00000400)); //is locked?

PLL0CON = 0x03; //Connect PLL after PLL is locked


PLL0FEED = 0xAA; //Feed sequence
PLL0FEED = 0x55;
VPBDIV = 0x01; // PCLK is same as CCLK i.e.60 MHz
}

Embedded System Design (BE – I) (E & TC) Prepared by: R. S. Khamitkar

Vous aimerez peut-être aussi