0% found this document useful (0 votes)
113 views39 pages

ES Record

Pulse width modulation (PWM) is a technique used to control analog circuits with digital outputs from a processor. It works by varying the on-off duty cycle of digital pulses. PWM has many applications from power control to communications. Interfacing PWM involves connecting PWM output pins to a circuit and varying the duty cycle to encode different analog levels.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views39 pages

ES Record

Pulse width modulation (PWM) is a technique used to control analog circuits with digital outputs from a processor. It works by varying the on-off duty cycle of digital pulses. PWM has many applications from power control to communications. Interfacing PWM involves connecting PWM output pins to a circuit and varying the duty cycle to encode different analog levels.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 39

PWM (Pulse Width Modulation)

Pulse width modulation (PWM) is a powerful technique for controlling analog circuits with a
processor's digital outputs. PWM is employed in a wide variety of applications, ranging from
measurement and communications to power control and conversion.

Interfacing PWM
Figure 1 shows four different PWM signals. One is PWM output at a 25% duty cycle. That is, the
signal is on for 25% of the period and off the other 75%. Next shows PWM output at 50%, 75%
and 100% duty cycles, respectively. These three PWM outputs encode three different analog
signal values, at 10%, 50%, and 90% of the full strength.
Pin Assignment with LPC2148

Circuit Diagram to Interface PWM with LPC2148


Source Code
The Interfacing PWM with LPC2148 program is very simple and straight forward, which
generates a pulse pattern in a particular frequency. An ADC signal is used to varying the duty
cycle of PWM signal. The C program is written in Keil software.
Stepper Motor
A stepper motor is a brushless, synchronous electric motor that converts digital pulses into
mechanical shaft rotation. Every revolution of the stepper motor is divided into a discrete
number of steps, and the motor must be sent a separate pulse for each step.

Interfacing Stepper Motor


Fig. 1 shows how to interface the Stepper Motor to microcontroller. As you can see the stepper
motor is connected with Microcontroller output port pins through a ULN2803A array. So when
the microcontroller is giving pulses with particular frequency to ULN2803A, the motor is rotated
in clockwise or anticlockwise.

Interfacing Stepper Motor to Microcontroller

Interfacing Stepper Motor with LPC2148


We now want to control a stepper motor in LPC2148 Slicker Board. It works by turning ON &
OFF a four I/O port lines generating at a particular frequency. The ARM7 LPC2148 Slicker
board has four numbers of I/O port lines, connected with I/O Port lines (P1.16 – P1.19) to rotate
the stepper motor. ULN2803 is used as a driver for port I/O lines, drivers output connected to
stepper motor, connector provided for external power supply if needed. Stepper Motor can
connect JP17 or J6 connector.
Pin Assignment with LPC2148

Circuit Diagram to Interface Stepper Motor with LPC2148


The Interfacing stepper motor control with LPC2148 program is very simple and straight
forward, which control the stepper motor in clockwise, counter clockwise and also a particular
angular based clockwise by using switches. The I/O port lines are used to generate pulses for
stepper motor rotations. C programs are written in Keil software.
Temperature Sensor
The LM35 series are precision integrated-circuit temperature sensors, whose output voltage is
linearly proportional to the Celsius (Centigrade) temperature. The output of sensor converted to
digital that easy connecting with microcontroller.
Interfacing LM35
Fig. 1 shows how to interface the LM35 to microcontroller. As you can see the third pin is
connected to GND, the first pin is connected to VCC & the second pin is connected to the
Microcontroller input. Just use single PIN female to female wire to connect with the leads of
LM35 temperature sensor. So when the temperature is sensing, it give the sensor reading to
controller.

Interfacing LM35 to Microcontroller


Interfacing LM35 with LPC2148
We now want to read the temperature in LPC2148 Tyro Board from temperature sensor LM35.
The ARM7 LPC2148 Tyro board uses the ADC pin for reading temperature from temperature
sensor LM35. The reading output is displayed into PC through UART1. The 10 bit ADC used for
reading the temperature from LM35. Basic clocking for the A/D converters is provided by the
VPB clock. A programmable divider is included in each converter, to scale this clock to the 4.5
MHz (max) clock needed by the successive approximation process. A fully accurate conversion
requires 11 of these clocks.
Pin Assignment with LPC2148
Circuit Diagram to Interface LM35 with LPC2148

The Interfacing LM35 with LPC2148 program is very simple and straight forward, that reading
temperature from temperature sensor LM35 and it display into PC through serial port. The C
programs are written in Keil software. Some delay is occurring when a single data is sent to PC.
LED (Light Emitting Diodes)

Light Emitting Diodes (LED) is the most commonly used components, usually for displaying
pins digital states. Typical uses of LEDs include alarm devices, timers and confirmation of user
input such as a mouse click or keystroke.
Interfacing LED
Fig. 1 shows how to interface the LED to microcontroller. As you can see the Anode is connected
through a resistor to GND & the Cathode is connected to the Microcontroller pin. So when the
Port Pin is HIGH the LED is OFF & when the Port Pin is LOW the LED is turned ON.

Fig. 1 Interfacing LED to Microcontroller

Interfacing LED with LPC2148


Flash a LED using LPC2148 Primer Board. It works by turning ON a LED & then turning it
OFF & then looping back to START. However the operating speed of microcontroller is very
high so the flashing frequency will also be very fast to be detected by human eye.

The ARM7 LPC2148 Primer board has eight numbers of point LEDs, connected with I/O Port
lines (P1.16 – P1.23) to make port pins high.

Pin Assignment with LPC2148

LPC2148
Point LEDs Lines LED Selection
LD1 P1.16
LD2 P1.17
LD3 P1.18
LD4 P1.19
LD5 P1.20
LD6 P1.21
DIGITAL
LD7 P1.22
OUTPUTSLD8 P1.23

Circuit Diagram to Interface LED with LPC2148


The Interfacing LED with LPC2148 program is very simple and straight forward, that uses a
delay procedure loop based software delay. In C programs you cannot be sure of delay, because it
depends on compiler how it optimizes the loops as soon as you make changes in the options the
delay changes.

#include<LPC214x.h> // Define LPC2148 Header File


#define led IOPIN1 // Define LED to Port1
#define tled IO1DIR // Define Port1 as output
void delay(int x);
void main()
{
PINSEL2 = 0x00000000; // Define port lines as GPIO
tled = 0x00FF0000; // Define P1.16 – P1.23 as O/P
led = 0x00000000; // Define P1.16 – P1.23 as zero
while(1) // Loop forever
{
led = 0x00FF0000; // Turn ON P1.16 – P1.23
delay(2000);
led = 0x00000000; // Turn OFF P1.16 – P1.23
delay(2000);
}
}

void delay(int x)
{
unsigned int k,l;
for(k = x;k > 0;k--)
for(l = 0;l < x;l++);
}

Keypad

A keypad is a set of buttons arranged in a block or "pad" which usually bear digits, symbols and
usually a complete set of alphabetical letters. If it mostly contains numbers then it can also be
called a numeric keypad. Here we are using 4 X 4 matrix keypad.

Interfacing keypad
Fig. 1 shows how to interface the 4 X 4 matrix keypad to two ports in microcontroller. The rows
are connected to an output port and the columns are connected to an input port.
To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output latch,
and then it reads the columns. If the data read from the columns is D3-D0=1111, no key has been
pressed and the process continues until a key press is detected. However, if one of the column
bits has a zero, this means that a key press has occurred. For example, if D3-D0=1101, this
means that a key in the D1 column has been pressed.
After a key press is detected, the microcontroller will go through the process of identifying the
key. Starting with the top row, the microcontroller grounds it by providing a low to row D0 only;
then it reads the columns.
If the data read is all 1s, no key in that row is activated and the process is moved to the next row.
It grounds the next row, reads the columns, and checks for any zero. This process continues until
the row is identified. After identification of the row in which the key has been pressed, the next
task is to find out which column the pressed key belongs to.
Fig. 1 Interfacing keypad to Microcontroller
Interfacing keypad with LPC2148
Scan a keypad in LPC2148 Primer Board. In case of 4X4 matrix Keypad both the ends of
switches are connected to the port pin i.e. four rows and four columns. So in all sixteen switches
have been interfaced using just eight lines.
1Keypads arranged by matrix format, each row and column section pulled by high or low by
selection J5, all row lines(P1.24 – P1.27) and column lines(P1.28 to P1.31) connected directly by
the port pins.

Pin Assignment with LPC2148

4x4 MatrixLPC2148
Lines Lines 4x4 Matrix Keypad
ROW-0 P1.24
ROW-1 P1.25
ROW-2 P1.26
ROW ROW-4 P1.27
COLUMN-0 P1.28
COLUMN-1 P1.29
COLUMN-2 P1.30
COLUMN COLUMN-3 P1.31
Circuit Diagram to Interface keypad with LPC2148

The Interfacing keypad with LPC2148 program is very simple and straight forward, that scan a
keypad rows and columns. When the rows and columns are detected then it will display in PC
through UART0. The C programs are developed in Keil software.

#include <LPC214x.h>
#include <stdio.h>
#include "Keypad.h"
#include "UART_Utility.c"

extern void Delay(void);


unsigned char Row_Data, Col_Data;
unsigned char Msg[4][4] = { '0','1','2','3',
'4','5','6','7',
'8','9','A','B',
'C','D','E','F'
};

void main(void)
{
VPBDIV = 0x02;
UART0_Init (9600);
PINSEL2 |= 0x0;
UART0_PutS ("\nPS-Primer ARM LPC2148 Keypad Demo\n\r");
UART0_PutC (0xB8);
UART0_PutS (" Pantech Solutions Pvt Ltd., \n\r");
UART0_PutS (" www.pantechsolutions.net\n\r");
UART0_PutS ("----------------------------------------- \n\n\r");
UART0_PutS ("Keypad Ports : P1.24 - P1.31 \n\n\n\r");
UART0_PutS
("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n\r");

while (1)
{
Delay();
Delay();
KeyScan(&IOPIN1,24,&Row_Data,&Col_Data);
UART0_PutS ("The Key You Pressed is : ");
if (Row_Data < 4 && Col_Data < 4)
{
U0THR = Msg[Row_Data][Col_Data];
Delay();
Delay();
U0THR = '\r';
}
}
}
void Delay(void)
{
unsigned int i,j;
for(i=0;i<35;i++)
for(j=0;j<1234;j++);
}

LCD (Liquid Crystal Display)

Liquid Crystal Display also called as LCD is very helpful in providing user interface as well as
for debugging purpose. A liquid crystal display (LCD) is a flat panel display that uses the light
modulating properties of liquid crystals (LCs). LCD Modules can present textual information to
user.

Interfacing LCD
Fig. 1 shows how to interface the LCD to microcontroller. The 2x16 character LCD interface
card with supports both modes 4-bit and 8-bit interface, and also facility to adjust contrast
through trim pot. In 4-bit interface 7 lines needed to create 4-bit interface; 4 data bits (D0 – D3),
three control lines, address bit (RS), read/write bit (R/W) and control signal (E).
Fig. 1 Interfacing 4 bit LCD to Microcontroller
Interfacing 4 bit LCD with LPC2148
We now want to display a text in LPC2148 Primer Board by using 4 bit LCD module.
The ARM7 LPC2148 Primer board has seven numbers of LCD connections are needed to create
4-bit interface; connected with 4 data bits (P0.19 – P0.22, D4-D7), address bit (RS-P0.16),
read/write bit (R/W-P0.17) and control signal (E-P0.18) to make LCD display.

Pin Assignment with LPC2148

LCD LPC2148
MODULE LINES 2x16 LCD Selection
RS P0.16
RW P0.17
CONTROLE P0.18
D0-D3 NC
D4 P0.19
D5 P0.20
DATA
D6 P0.21
LINES D7 P0.22

Make switch SW28 to ‘LCD’ label


marking position
Circuit Diagram to Interface 4 bit LCD with LPC2148
The Interfacing 4 bit LCD with LPC2148 program is very simple and straight forward, which
display a text in 2 X 16 LCD module using 4 data lines only. Some delay is occurring when a
single command / data is executed.
ADC
Analog to Digital Converter (ADC) is used to convert analog signal/voltage into its equivalent
digital number so that microcontroller can process that numbers and make it human readable.
The ADC characterized by resolution. The resolution of ADC indicates the number of digital
values. Let’s take example: In LPC2148 microcontroller we have in-built 10-bit ADC. So for 10-
bit ADC resolution is 10-bit and maximum value will be 210=1024. This means our digital value
or discrete level lies between 0 to 1023. There is one more term important to understand while
dealing with ADC and it is step size. Step size is the minimum change in input voltage which
can be resolved by ADC. The concept of step size is closely associated with the resolution of
ADC.

Calculate: ADC Resolution in LPC2148 ARM7

So in this case we can measure minimum 2.23 mV (Approx.) with our microcontroller. This is
how step size defines an accuracy of ADC circuit.
Introduction: ADC in LPC2148 ARM7 Microcontroller
The ADC in LPC2148 ARM7 Microcontroller is 10-bit successive approximation analog to
digital converter. The features are listed as:
 LPC2148 has two inbuilt ADC Modules, named as ADC0 & ADC1.
 ADC0 has 6-Channels (AD0.1-AD0.6).
 ADC1 has 8-Channels (AD1.0-AD1.7).
 ADC operating frequency is 4.5 MHz (max.), operating frequency decides the
conversion time.
 Supports power down mode.

 Burst conversion mode for single or multiple inputs.


There are several registers associated with ADC feature but we will mainly discussing about
ADC Control Register (ADCR) & ADC Global Data Register (ADGDR). For more details on
register description keep datasheet in hand UM10130, Chapter: 19.
Let’s have a look at table which illustrate ADC related channels and pins:

ADC Related Pins in LPC2148

Registers of ADC in LPC2148 Microcontroller


There are several registers which will be used to setup and configure ADC feature in LPC2148.
The two registers we will be concerning about: ADCR (A/D Control Register) and ADGDR
(A/D Global Data register).

Register
Function
Name

A/D Control Register: The ADCR register must be written to select


ADCR
the operating mode before A/D conversion can occurs.

A/D Global Data Register: This register contains


ADGDR
ADC’s DONE bit and the result of the most recent A/D conversion.

ADSTAT A/D Status Register: This register contains DONE and


OVERRUN flag for all the A/D Channels, as well as the A/D
interrupt flag.

A/D Global Start Register: This address can be written (in the
ADGSR AD0 address range) to start conversions in both A/D converters
simultaneously.

A/D Interrupt Enable Register: This register contains enable bits


ADINTEN that allow the DONE flag of each A/D channel to be included or
excluded from contributing to the generation of an A/D interrupt.

ADDRx A/D Channel x Data: ‘x’ varies from 0 to 7

Programming Steps: ADC in LPC2148 ARM7 Microcontroller


 Configure input pin as analog input pin for ADC block

 Select channel we wish to use in our conversion


 Select clock for conversion & start conversion

 Wait for conversion to complete


 Read 10-bit conversion result

 Convert this result i.e. ADC output into decimal number


 Display reading on serial emulator or display device

Example Project: In this example project we will be using 10K POT to provide analog input
at ADC Channel 0 i.e. Pin P0.28 of LPC2148 Microcontroller. To read ADC count from POT
we will configure UART0 of LPC2148 and read data on terminal emulator (PuTTY). When we
vary resistance of POT the change in ADC count will reflect on PuTTY terminal in real time.
The circuit connection shown as:
CIRCUIT DIAGRAM: ADC in LPC2148 ARM7 Microcontroller
Circuit: ADC in LPC2148 ARM7 Microcontroller

#include <lpc214x.h>

#include "serial.h"

#include <stdio.h>

char String[]="Wel-Come to BINARYUPDATES.COM, ADC and UART Configured


@LPC2148 \n\r Serial Communication @ 9600 baudrate, 8 bits, no Parity, 1 Stop bit\n\r\n";

char Newline[]="\n\r\n";

char adcreading[16] ;

void ADC_Init (void)

PINSEL1 = 0x01000000 ; // P0.28, AD0.1

}
unsigned int ADC_GetAdcReading()

unsigned int adcdata;

AD0CR = 0x01200302 ; // Select AD0.1, Select clock for ADC, Start of conversion

while(!((adcdata = AD0GDR) & 0x80000000)) // Check end of conversion (Done bit) and
read result

return((adcdata >> 6) & 0x3ff) ; // Return 10 bit result

int main(void)

unsigned int delay, adc;

initClocks(); // Set CCLK=60Mhz and PCLK=60Mhz

initUART0();

ADC_Init() ;

Send_String(String);

while(1)

adc = ADC_GetAdcReading();

sprintf(adcreading,"ADC0 CH1= %u",adc); // read data in decimal format


//sprintf(adcreading,"ADC0 CH1= 0x%03X",adc); // read data in hexx format

Send_String(adcreading);

Send_String(Newline);

for(delay=0; delay<10000000; delay++); // delay

ARM LPC2148 : DIGITAL TO ANALOG CONVERTER (DAC)


Digital to Analog Converter : The function of DAC is to convert digital values or binary patterns
to analog signals usually. The DAC performs exactly reverse of ADC. LPC2148 has one Digital
to Analog Converter which receives digital value from register and give output to A OUT analog
output pin.
DAC Applications:
1. The DAC is generally used in the area like signals processing to sent data to Loudspeaker
or earphone and
2. Output of DAC will be also fed to AC drive to control the speed of AC Motor.
Features of LPC2148 DAC
 10 bit digital to analog converter
 Resistor string architecture
 Buffered output
 Power-down mode
 Selectable speed vs. power
Pin Decription:
AOUT Output This pin gives Analog Output from digital values
VREF Reference This pin provides a voltage reference level for the D/A converter.
VDDA, VSSA Power Analog Power and Ground. These should be nominally the same voltages
as V3and VSSD, but should be isolated to minimize noise and error.
PINSEL1 register is used to configure GPIO P0.25 to AOUT pin.
Table 1: PINSEL1 configuration bits for configuring GPIO P0.25 to AOUT
DAC PIN Configuration
DAC Value on Pin Function Register
OUTPUT Pin Name PINSEL1 for Configures DAC Pin

Bit 19 18
Analog Output AOUT Value 1 0
PINSEL1= 0x00008000
DAC Register: The DAC Register is only register used to control and configure of LPC2148
DAC. The purpose is to receive digital value from LPC2148 microcontroller and give output on
output AOUT (Pin No. 9 in case of LPC2148).

Reversed BIAS Value Reversed


31:17 16 15:8 5:0
BIAS:
BIAS = 0 : The settling time of the DAC is 1μs max, and the maximum current is 700μA.
BIAS = 1: The settling time of the DAC is 2.5μs and the maximum current is 350μA
VALUE:
VALUE field is filled up with digital value which we have to convert from Digital to Analog
voltage on the AOUT pin (with respect to VSSA) is given as below:
To write the 10-bit value to the DACR regjster, a half-word store moves the value v that has been
shifted 6 bits so that it sits in bits [15:6]. So the code would be
DACR = (i << 6)

Generate Sinusoidal waveform from LPC2148 microcontroller.

#include “LPC214x.h” /* LPC21xx definitions */


#define DAC_BIAS 0x00010000
unsigned int SinTable[45] ={
410, 467, 523, 576, 627, 673, 714,
749, 778, 799,813, 819, 817, 807,
789, 764, 732, 694, 650, 602,
550, 495,438, 381, 324, 270, 217,
169, 125, 87, 55, 30, 12, 2, 0,
6, 20, 41, 70, 105, 146, 193, 243,
297,353};

void DACInit( void )


{
/* setup the related pin to DAC output */
PINSEL1 = 0x00080000; /* set p0.25 to DAC output */
return;
}
void delay(unsigned int i)
{
unsigned int m,n;
for(m=0;m<i;m++)
{
for(n=0;n<120;n++);
}
}
int main (void)
{
int i = 0;
DACInit();
while ( 1 )
{
delay(500);
DACR = (SinTable[i] << 6);
}
return 0;
}

ARM LPC2148– Internal Real Time Clock (RTC)

Sample code to use the internal RTC of LPC 2138/48 is given below. Here seconds, minutes &
hour are initialized to 0. Time gets updated every second and is displayed on LCD in the
format hh:mm:ss.
Changing the initial time : To change the initial time, just load the desired values into the
corresponding registers HOUR, MIN & SEC.
Running the RTC : If you want to run the RTC forever after loading the initial values, follow
these steps
i. Ensure that the RTC battery is placed in battery holder.

ii. Flash the code.

iii. Comment the lines which load the initial values of time.

iv. Build the code & flash it again

#include<lpc21xx.h>

unsigned char flag=0;


INTERRUPT SERVICE ROUTINE

void RTC_int(void)__irq
{
ILR = 0X01; /* Clears counter increment interrupt */
flag = 1; /* Set flag on interrupt */
VICVectAddr = 0X00000000;
}

void RTC_Init()
{
ILR = 0X03; /* Interrupt from counter increment block */
CCR = 0X11; /* RTC Time counters enable */
CIIR = 0X01; /* Interrupt generated in each second */
SEC = 0x00; /* Load initial value into seconds register*/
MIN = 0x00; /* Load initial value into minutes register*/
HOUR = 0x00; /* Load initial value into hour register */
VICIntEnable = 0x00002000; /* RTC interrupt enabled */
VICVectCntl0 = 0x0000002D; /* RTC interrupt assigned to slot0 */
VICVectAddr0 = (unsigned)RTC_int;
}

void Delay(unsigned long b)


{
while (--b!=0); /* Decrement b for delay */
}

void Write_Command(int cmd)


{
IO1CLR |= 0x00f00000; /* Clear D4-D7 */
IO1CLR |= 0x00040000; /* Read/Write = 0 */
IO1CLR |= 0X00020000; /* RS = 0,Command register */
IO1SET |= 0x00f00000 & cmd; /* Set D4-D7 */
IO1SET |= 0X00080000; /* Enable - high */
Delay(30000);
IO1CLR |= 0x00080000; /* Enable - low */
}

void Write_Data(int dat)


{
IO1CLR |= 0x00f00000; /* Clear D4-D7 */
IO1CLR |= 0x00040000; /* Read/Write = 0 */
IO1SET |= 0X00020000; /* RS = 1,Data register */
IO1SET |= 0x00f00000 & dat; /* Set D4-D7 */
IO1SET |= 0X00080000; /* Enable - high */
Delay(30000);
IO1CLR |= 0x00080000; /* Enable - low */
}

void LCD_Data(char dat)


{
Write_Data(dat << 16);
Write_Data(dat << 20);
}

void LCD_Command(char cmd)


{
Write_Command(cmd << 16);
Write_Command(cmd << 20);
}

void LCD_String(char *CPtr)


{
while(*CPtr != '\0') /* Check for string termination */
{
LCD_Data(*CPtr); /* Display the character */
CPtr++; /* Increment the pointer */
Delay(20000);
}
}

void LCD_Init(void)
{
IO1DIR |= 0x00FE0000; /* Set P1.17-P1.23 as output */
Delay(200000) ;
Write_Command(0x30 << 16);
Delay(100000);
Write_Command(0x30 << 16);
Delay(100000);
Write_Command(0x30 << 16);
Delay(100000);
Write_Command(0x20 << 16); /* Enable 4 bit mode */

LCD_Command(0x01); /* Clear display */


LCD_Command(0x06); /* Auto address inc */
LCD_Command(0x0c); /* Cursor off */
LCD_Command(0x80); /* First location */
}

int main(void)
{
LCD_Init(); /* Initialize LCD module */
RTC_Init(); /* Initialize RTC module */
while(1)
{
if(flag)
{
LCD_Command(0x80); /* 1st row, 1st location */
flag=0; /* Clear flag */
LCD_Data(HOUR/10 + '0'); /* Display hour */
LCD_Data(HOUR%10 + '0');
LCD_Data(':') ;
LCD_Data(MIN/10 + '0'); /* Display Minute */
LCD_Data(MIN%10 + '0');
LCD_Data(':') ;
LCD_Data(SEC/10 + '0'); /* Display Second */
LCD_Data(SEC%10 + '0');
}
}
}

You might also like