embedded software development lab manual 3
embedded software development lab manual 3
PART B
6 Interface a simple Switch and display its status through Relay, Buzzer 35
and LED.
7 Interface a Stepper motor and rotate it in clockwise and anti-clockwise 37
direction.
8 Display the Hex digits 0 to F on a 7-segment LED interface, with an 40
appropriate delay in between.
9 Interface a DAC and generate Triangular and Square waveforms. 42
10 Display Hello World message using Internal UART. 46
11 Demonstrate the use of an external interrupt to toggle an LED On/Off. 51
12 Using the Internal PWM module of ARM controller generate PWM and 55
vary its duty cycle.
13 Interface and Control a DC Motor. 59
14 Interface a 4×4 keyboard and display the key code on an LCD. 62
15 Measure Ambient temperature using a sensor and SPI ADC IC. 68
16 Interface 12 bit internal ADC to convert the analog to digital and display 76
the same on LCD.
DEPARTMENT VISION & MISSION
VISION
To become a pioneer in developing competent professionals with societal and ethical values through
transformational learning and interdisciplinary research in the field of Electronics and
Communication Engineering.
MISSION
The department of Electronics and Communication is committed to:
M1: Offer quality technical education through experiential learning to produce competent
engineering professionals.
M4: Create a congenial environment for the faculty and students to achieve their desired goals
and to serve society by upholding ethical values.
EMBEDDED SYSTEMS LAB MANUAL
EMBEDDED SYSTEM LAB SYLLABUS
PART-A:
(Conduct the following experiments on an ARM CORTEX M3 evaluation board to learn ALP and using
evaluation version of Embedded 'C' &Keil Uvision-4tool/compiler.)
1. ALP to multiply two 16 bit binary numbers.
2. ALP to find the sum of first 10 integers.
3. ALP to find the number of 0’s and 1’s in a 32 bit data.
4. ALP to determine the given 16 bit number is ODD or EVEN.
5. ALP to write data in RAM.
PART-B:
(Conduct the following experiments on an ARM CORTEX M3evaluation board using evaluation version of
Embedded 'C' &Keil Uvision-4tool/compiler.)
6. Display “Hello World” message using Internal UART.
7. Interface and Control a DC Motor.
8. Interface a Stepper motor and rotate it in clockwise and anti-clockwisedirection.
9. Interface a DAC and generate Triangular and Square waveforms.
10. Interface a 4x4 keyboard and display the key code on an LCD.
11. Deonstrate the use of an external interrupt to toggle an LED On/Off.
12.Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate delay in
between.
13. Measure Ambient temperature using a sensor and SPI ADC IC.
Beyond Syllabus:
i).Using the Internal PWM module of ARM controller generate PWM and vary itsduty cycle.
ii).Interface a simple Switch and display its status through Relay, Buzzer andLED.
The ARM Cortex-M3 is a general purpose 32-bit microprocessor, which offers high performance and very
low power consumption. The Cortex-M3 offers many new features, including a Thumb-2 instruction set, low
interrupt latency, hardware divide, interruptible/continuable multiple load and store instructions, automatic
state save and restore for interrupts, tightly integrated interrupt controller with Wake-up Interrupt Controller
and multiple core buses capable of simultaneous accesses.
Pipeline techniques are employed so that all parts of the processing and memory systems can operate
continuously. Typically, while one instruction is being executed, its successor is being decoded, and a third
instruction is being fetched from memory.
The processor has a Harvard architecture, which means that it has a separate instruction bus and data bus.
This allows instructions and data accesses to take place at the same time, and as a result of this, the
performance of the processor increases because data accesses do not affect the instruction pipeline. This
feature results in multiple bus interfaces on Cortex-M3, each with optimized usage and the ability to be used
simultaneously. However, the instruction and data buses share the same memory space (a unified memory
system). In other words, you cannot get 8 GB of memory space just because you have separate bus interfaces.
A simplified block diagram of the Cortex-m3 architecture is shown below
It is worthwhile highlighting that the Cortex-M3 processor is not the first ARM processor to be used to create
generic micro controllers. The venerable ARM7 processor has been very successful in this market, The
Cortex-M3 processor builds on the success of the ARM7 processor to deliver devices that are significantly
easier to program and debug and yet deliver a higher processing capability.
The Thumb-2
Technology
and
Instruction
Set
Architecture
The Thumb-2 technology extended the Thumb Instruction Set Architecture (ISA) into a highly efficient and
powerful instruction set that delivers significant benefits in terms of ease of use, code size, and performance.
The extended instruction set in Thumb-2 is a super set of the previous 16-bit Thumb instruction set, with
additional 16-bit instructions alongside 32-bit instructions. It allows more complex operations to be carried
out in the Thumb state, thus allowing higher efficiency by reducing the number of states switching between
ARM state and Thumb state.
Focused on small memory system devices such as micro controllers and reducing the size of the processor,
the Cortex-M3 supports only the Thumb-2 (and traditional Thumb) instruction set. Instead of using ARM
instructions for some operations, as in traditional ARM processors, it uses the Thumb-2 instruction set for all
operations. As a result, the Cortex-M3 processor is not backward compatible with traditional ARM
processors.
Nevertheless, the Cortex-M3 processor can execute almost all the 16-bit Thumb instructions, including all 16-
bit Thumb instructions supported on ARM7 family processors, making application porting easy. With support
for both 16-bit and 32-bit instructions in the Thumb-2 instruction set, there is no need to switch the processor
between Thumb state (16-bit instructions) and ARM state (32-bit instructions). For example, in ARM7 or
ARM9 family processors, you might need to switch to ARM state if you want to carry out complex
calculations or a large number of conditional operations and good performance is needed, whereas in the
Cortex-M3 processor, you can mix 32-bit instructions with 16-bit instructions without switching state, getting
high code density and high performance with no extra complexity.
The Thumb-2 instruction set is a very important feature of the ARMv7 architecture. Compared with
the instructions supported on ARM7 family processors (ARMv4T architecture), the Cortex-M3 processor
instruction set has a large number of new features. For the first time, hardware divide instruction is available
on an ARM processor, and a number of multiply instructions are also available on the Cortex-M3 processor to
improve data-crunching performance. The Cortex-M3 processor also supports unaligned data accesses, a
feature previously available only in high-end processors.
Applications of Cortex M3 processors
Low-cost micro controllers:
AutomotiveIndustry
Data communications
Industrial control applications
Consumer products:
The Cortex-M3 processor is the central processing unit (CPU) of a micro controller chip. In addition, a
number of other components are required for the whole Cortex-M3 processor-based micro controller. After
chip manufacturers license the Cortex-M3 processor, they can put the Cortex-M3 processor in their silicon
designs, adding memory, peripherals, input/output (I/O), and other features. Cortex-M3 processor-based chips
from different manufacturers will have different memory sizes, types, peripherals, and features.
Interrupt sources
Each peripheral device has one interrupt line connected to the NVIC but may have several interrupt flags.
Individual interrupt flags may also represent more than one interrupt source.
Any pin on Port 0 and Port 2 (total of 42 pins) regardless of the selected function, can be programmed to
generate an interrupt on a rising edge, a falling edge, or both.
General purpose DMA controller
The GPDMA (General Purpose Direct Memory Access) is an AMBA AHB (Advanced Micro controller Bus
Architecture Advance high performance bus) compliant peripheral allowing selected peripherals to have
DMA support.
The GPDMA enables peripheral-to-memory, memory-to-peripheral, peripheral-to-peripheral, and memory-to-
memory transactions. The source and destination areas can each be either a memory region or a peripheral,
and can be accessed through the AHB master. The GPDMA controller allows data transfers between the USB
and Ethernet controllers and the various on-chip SRAM areas. The supported APB peripherals are SSP0/1, all
UARTs, the I2S-bus interface, the ADC, and the DAC. Two match signals for each timer can be used to
trigger DMA transfers.
Function Configuration block
The selected pins of the micro controller to have more than one function. Configuration registers control the
multiplexers to allow connection between the pin and the on-chip peripherals. Peripherals should be
connected to the appropriate pins prior to being activated and prior to any related interrupt(s) being enabled.
Activity of any enabled peripheral function that is not mapped to a related pin should be considered
undefined. Most pins can also be configured as open-drain outputs or to have a pull-up, pull-down, or no
resistor enabled.
Fast general purpose parallel I/O
Device pins that are not connected to a specific peripheral function are controlled by the GPIO registers. Pins
may be dynamically configured as inputs or outputs. Separate registers allow setting or clearing any number
of outputs simultaneously. The value of the output register may be read back as well as the current state of the
port pins.
USB interface
The Universal Serial Bus (USB) is a 4-wire bus that supports communication between a host and one or more
(up to 127) peripherals. The host controller allocates the USB bandwidth to attached devices through a token-
based protocol. The bus supports hot plugging and dynamic configuration of the devices. All transactions are
initiated by the host controller.
The USB interface includes a device, Host, and OTG controller with on-chip PHY for device and Host
functions. The OTG switching protocol is supported through the use of an external controller.
Crystal oscillators
The LPC1768 include three independent oscillators. These are the main oscillator, the IRC oscillator, and the
RTC oscillator. Each oscillator can be used for more than one purpose as required in a particular application.
Any of the three clock sources can be chosen by software to drive the main PLL and ultimately the CPU.
Following reset, the LPC1768 will operate from the Internal RC oscillator until switched by software. This
allows systems to operate without any external crystal and the boot loader code to operate at a known
frequency.
Power control
The LPC1768 support a variety of power control features. There are four special modes of processor power
reduction: Sleep mode, Deep-sleep mode, Power-down mode, and Deep power-down mode. The CPU clock
rate may also be controlled as needed by changing clock sources, reconfiguring PLL values, and/or altering
the CPU clock divider value. This allows a trade-off of power versus processing speed based on application
requirements. In addition, Peripheral Power Control allows shutting down the clocks to individual on-chip
peripherals, allowing fine tuning of power consumption by eliminating all dynamic power use in any
peripherals that are not required for the application. Each of the peripherals has its own clock divider which
provides even better power control.
Integrated PMU (Power Management Unit) automatically adjust internal regulators to minimize power
consumption during Sleep, Deep sleep, Power-down, and Deep power- down modes.
The LPC1768 also implement a separate power domain to allow turning off power to the bulk of the device
while maintaining operation of the RTC and a small set of registers for storing data during any of the power-
down modes.
Clock generation block diagram for LPC1768 is shown below
System Control
Reset
Reset has four sources on the
LPC1768: the RESET pin, the
Watchdog reset, power-on
reset (POR), and the Brown-
Out Detection (BOD) circuit.
PART A
(Assembly Level Programming-ARM Cortex M3)
Software : Keil µvision 4
4. Select the target device(here,LPC1768 from NXP) from the list or type the exact name of the device. Press OK.
5.”Copy start up to Project folder and add to project file”?- Press NO.
6.In the project window, right click on source and select Add new item to group “source group 1”.
7.Select Asm file and give name of the file with .s extension and press ADD.
9.Translate the program by select the icon from tool bar or from menu bar.
10.If no error,Select “Build” icon from tool bar or from menu bar.
12.PressOK
Department of ECE, Atria IT Page 24
EMBEDDED SYSTEMS LAB MANUAL
13.Press function key F11 or select “step” option under Debug menu for single step execution and verify the output in
register window/Memory window/xPSR.
Result:
(0xFFFF) x(0xFFFF) =0xFFFE0001 in the product memory location.
XSS B XSS
ENTRY
EXPORT Reset_Handler
Reset_Handler
MOV r0,#num
MOV r1,#0
MOV r2,#0
MOV r3,#32
loop LSRS r0,r0,#1
BCS l1
ADD r2,#1
B l2
l1 ADD r1,#1
l2 SUBS r3,#1
BNE loop
Result:
If num=15dno of 1’s=4 and No.of 0’s=28d=1Ch.
ENTRY
EXPORT Reset_Handler
Reset_Handler
LDR r1,=num
LDR r0,[r1]
RORS r0,#1
BCS l1
MOV r2,#resu
B l2
l1 MOV r2,#res
l2 LDR r3,=result
STR r2,[r3]
stop B stop
Method1:
stop B stop
Method 2:
;ALP TO MOVE A BLOCK OF DATA FROM CODE TO RAM MEMORY-USING LDM and STM
INSTRUCTIONS(MULTIPLE DATA TRANSFER)
AREA Reset, DATA, READONLY
EXPORT __Vectors
__Vectors
DCD 0X20001000
DCD Reset_Handler;
stop B stop
PART B
(INTERACING HARDWARE WITH LPC 1768 MICROCONTROLLER)
SOFTWARE: Keil µVision 4, Flash Magic
Language: Embedded C
LPC1768
RELAY
P1.28
SWITCH P2.11 P1.19
LED
P1.27
BUZZER
Algorithm:
1. Configure PORT 1 and PORT 2 as GPIO.
2. Configure the direction of Port 2 as input and Port 1 as output .
3. Read the status of the switch.
4. If the switch is pressed, turn on LED,Relay and Buzzer else turn them off.
5. Repeat from step 3 unconditionally.
Program:
#include<LPC17xx.h>
#define switch 11
#define LED 19
#define relay 28
#define buzzer 27
int main(void)
{
LPC_PINCON->PINSEL3=0x00000000;
LPC_PINCON->PINSEL4=0x00000000;
LPC_GPIO2->FIODIR=0x00000000;
LPC_GPIO1->FIODIR=0xFFFFFFFF;
LPC_GPIO1->FIOCLR=0xFFFFFFFF;
while(1)
{
if (!((LPC_GPIO2->FIOPIN>>switch)& 0x1))
{
LPC_GPIO1->FIOPIN=(1<<LED)|(1<<relay)|(1<<buzzer);
Department of ECE, Atria IT Page 36
EMBEDDED SYSTEMS LAB MANUAL
}
else
{
LPC_GPIO1->FIOPIN=(0<<LED)|(0<<relay)|(0<<buzzer);
}
}
}
LPC1768 PO.15
SW1 P2.11 STEPPER
MOTOR STEPPER
P2.12 INTERFACING MOTOR
SW2 CIRCUIT
PO.18
ALGORITHM:
Program:
#include<lpc17xx.h>
#define SW1 11
#define SW2 12
void delay(unsigned int x)
{
unsignedinti,j;
for(i=0;i<x;i++)
{
for(j=0;j<90000;j++);
}
}
int main(void)
Exp.No:3
Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate delay in between.
Department of ECE, Atria IT Page 40
EMBEDDED SYSTEMS LAB MANUAL
Connection Details:
ALGORITHM:
1. Configure port 0 and Port 4 as GPIO.
2. Configure the direction of Port 0 and Port 4 as output.
3. Create a look up table containing 7 segment equivalent code for the digits 0 to 9 and hexa
digits A to F.
4. Select the display unit. Send logic 1 to Port line P0.2 for display unit 1 or to Port 4.28 for
display unit 2.
5. Send each 7 segment equivalent code taken from look up table toPort 0.0 to Port line P0.7
with appropriate delay in between.
6. Clear the selected display before sending the next data to display and insert a delay.
7. Repeat from step 4.
PROGRAM:
#include<lpc17xx.h>
Exp.No: 4
Interface a DAC and generate Triangular and Square waveforms.
Connection Details:
LPC1768
10 BIT
INTERNAL
P0.26 CRO
DAC
It is a string DAC consisting of 2N resistors in series where N = no. of bits LPC176x DAC has
only 1 output pin, referred to as AOUT. The Analog voltage at the output of this pin is given
as:
Where VALUE is the 10-bit digital value which is to be converted into its Analog counterpart
and VREF is the input reference voltage.
Pins relating to LPC1768 DAC block:
Pin Description
AOUT (P0.26) Analog Output pin. Provides the converted Analog signal which
is referenced to VSSA i.e. the Analog GND. Set Bits[21:20] in PINSEL1
register to “10” to enable this function.
DACR register Format: (32 bit register):
D/A Converter Register (DACR - 0x4008 C000)
This read/write register includes the digital value to be converted to analog, and a bit
thattrades off performance vs. power. Bits 5:0 are reserved for future, higher-resolution
D/Aconverters.
ALGORITHM:
TRIANGULAR WAVEFORM:
1. Configure the Port 0.26 as second alternate function to carry the analog output by using
the PINSEL1 register.
2. Initialize 10 bit digital dataas 0.
3. Send the digital data to the DACR[6:15].
4. Increment the digital data and check whether it is equal to 102410.
5. If it is less than 102410, repeat from step 3.
6. Else decrement the digital value and send to the DACR[6:15]
7. Check whether the digital data is greater than 0.
8. if yes, repeat from step 6 else repeat from step 3.
PROGRAM FOR TRIANGULAR WAVEFORM:
#include<lpc17xx.h>
#define p0_26 21
#define ddata 6
uint32_tdacv = 0x0;
int main()
{
SystemInit();
LPC_PINCON -> PINSEL1 = (1<<p0_26);
while(1)
{
while(1)
{
dacv++;
Program:
#include<lpc17xx.h>
#define p0_26 21
#define ddata 6
uint32_tdacv = 0x0;
void delay(unsigned int x)
{
unsignedinti,j;
for (i=0;i<x;i++)
{
Department of ECE, Atria IT Page 45
EMBEDDED SYSTEMS LAB MANUAL
for(j=0;j<9000;j++);
}
}
int main()
{
SystemInit();
LPC_PINCON -> PINSEL1 = (1<<p0_26);
while(1)
{
while(1)
{
dacv = 0x0;
LPC_DAC->DACR=(dacv<<ddata);
delay (15);
dacv = 0x3ff;
LPC_DAC ->DACR =(dacv<<ddata);
delay(15);
}
}
}
Exp.No:5:
Display “Hello World” message using Internal UART.
Connection Details:
LPC1768 UART0
TxD0 (P0.2)
USB CABLE PC
RxD0 (P0.3) MONITOR
UART Registers:
The below table shows the registers associated with LPC1768 UART.
Register Description
RBR Contains the recently received Data
THR Contains the data to be transmitted
FCR FIFO Control Register
LCR Controls the UART frame formatting(Number of Data Bits, Stop
bits)
DLL Least Significant Byte of the UART baud rate generator value.
DLM Most Significant Byte of the UART baud rate generator value.
FCR
31:8 7:6 5:4 3 2 1 0
RESERVED RX RESERVED DMA TX FIFO RX FIFO FIFO
TRIGGER MODE RESET RESET ENABLE
Bit 0 – FIFO:This bit is used to enable/disable the FIFO for thedata received/transmitted. 0--
FIFO is Disabled, 1--FIFO is Enabled for both Rx and Tx.
Bit 1 – RX_FIFO:This is used to clear the 16-byte Rx FIFO.0-No impact.
Department of ECE, Atria IT Page 47
EMBEDDED SYSTEMS LAB MANUAL
1-Clears the 16-byte Rx FIFO and the resets the FIFO pointer.
Bit 2 – Tx_FIFO:This is used to clear the 16-byte Tx FIFO.
0-No impact.
1-Clears the 16-byte Tx FIFO and the resets the FIFO pointer.
Bit 3 – DMA_MODE:
This is used for Enabling/Disabling DMA mode.
0--Disables the DMA.
1--Enables DMA only when the FIFO(bit-0) bit is SET.
Bit 7:6 – Rx_TRIGGER:This bit is used to select the number of bytes of the receiver data to be written
so as to enable the interrupt/DMA.
00-- Trigger level 0 (1 character or 0x01)
01-- Trigger level 1 (4 characters or 0x04)
10-- Trigger level 2 (8 characters or 0x08)
11-- Trigger level 3 (14 characters or 0x0E)
Format:
31:8 7 6 5:4 3 2 1:0
Reserved DLAB Break Parity Parity Stop Bit Word Length
COntrol Select Enable Select Select
Bit 1:0 : Word Length Select: These two bits are used to select the character length
00-- 5-bit character length
01-- 6-bit character length
10-- 7-bit character length
11-- 8-bit character length
Bit 2 – Stop Bit Selection:This bit is used to select the number(1/2) of stop bits
0-- 1 Stop bit
1-- 2 Stop Bits
Bit 3 – Parity Enable:This bit is used to Enable or Disable the Parity generation and checking.
0-- Disable parity generation and checking.
1-- Enable parity generation and checking.
Bit 5:4 – Parity Selection:These two bits will be used to select the type of parity.
00-- Odd parity. Number of 1s in the transmitted character and the
attached parity bit will be odd.
01-- Even Parity. Number of 1s in the transmitted character and the
attached parity bit will be even.
10-- Forced "1" stick parity.
11-- Forced "0" stick parity
Bit 6 – Break Control:0-- Disable break transmission.
1-- Enable break transmission. Output pin UARTn TXD is forced to logic 0
Department of ECE, Atria IT Page 48
EMBEDDED SYSTEMS LAB MANUAL
TER (Transmitter Enable register): This register is used to Enable/Disable the transmission
TER Format:
31:8 7 6-0
Reserved TXEN Reserved
void initUART0(void)
{
LPC_PINCON->PINSEL0 =(1<<4)|(1<<6);
LPC_UART0->LCR=0x83;
LPC_UART0->DLL=163;
LPC_UART0->DLM=0;
LPC_UART0->FCR =0x7;
LPC_UART0->FDR=0x0;
LPC_UART0->LCR = 0x03;
}
int main(void)
{
charmsg[]= "Hello World";
int i=0;
initUART0();
for(i=0;msg[i];i++)
{
U0Write(msg[i]);
}
}
Description:
LPC1768 has four external interrupts EINT0-EINT3.
Port Pin PINSEL_FUNC_0 PINSEL_FUNC_1 PINSEL_FUNC_2
P2.10 GPIO EINT0 NMI
P2.11 GPIO EINT1 I2STX_CLK
P2_12 GPIO EINT2 I2STX_WS
P2.13 GPIO EINT3 I2STX_SDA
Note:
since the two general purpose switches have been connected with port lines P2.11 and
P2.12, only two interrupts EINT1 and EINT2 have been used in this experiment.
EXTINT Format:
31:4 3 2 1 0
RESERVED EINT3 EINT2 EINT1 EINT0
EINTx: Bits will be set whenever the interrupt is detected on the particular interrupt
pin.If the interrupts are enabled then the control goes to ISR.
Writing one to specific bit will clear the corresponding interrupt.
EXTMODE Format:
31:4 3 2 1 0
RESERVED EXTMODE3 EXTMODE2 EXTMODE1 EXTMODE0
EXTMODEx: This bits is used to select whether the EINTx pin is level or edge
Triggered.
0: EINTx is Level Triggered.
1: EINTx is Edge Triggered.
EXTPOLAR Format:
31:4 3 2 1 0
RESERVED EXTPOLAR3 EXTPOLAR2 EXTPOLAR1 EXTPOLAR0
ALGORITHM:
Department of ECE, Atria IT Page 53
EMBEDDED SYSTEMS LAB MANUAL
void EINT1_IRQHandler(void)
{
LPC_SC->EXTINT = (1<<SBIT_EINT1); /* Clear Interrupt Flag */
LPC_GPIO1->FIOPIN ^= (1<< LED1); /* Toggle the LED1 everytime INTR1 is generated */
}
void EINT2_IRQHandler(void)
{
LPC_SC->EXTINT = (1<<SBIT_EINT2); /* Clear Interrupt Flag */
LPC_GPIO1->FIOPIN ^= (1<< LED2); /* Toggle the LED2 everytime INTR2 is generated */
}
LPC1768 has 6 PWM output pins which can be used as 6-Single edged or 3-Double edged. There as seven
match registers to support these 6 PWM output signals. Below block diagram shows the PWM pins and the
associated Match(Duty Cycle) registers.
PWM channel 1(Port Line P2.0) has been connected to the PWM output point in a trainer Kit.So configure
the P2.0 as a first alternate function to carry the PWM channel 1output using PINSEL4 register.
LPC1768 PWM Registers
The below table shows the registers associated with LPC1768 PWM Module.
Register Description
Interrupt Register: The IR can be read to identify which of eight possible interrupt sources are
IR
pending. Writing Logic-1 will clear the corresponding interrupt.
Timer Control Register: The TCR is used to control the Timer Counter
TCR
functions(enable/disable/reset).
Timer Counter: The 32-bit TC is incremented every PR+1 cycles of PCLK. The TC is controlled
TC
through the TCR.
PR Prescalar Register: This is used to specify the Prescalar value for incrementing the TC.
Prescale Counter: The 32-bit PC is a counter which is incremented to the value stored in PR.
PC
When the value in PR is reached, the TC is incremented.
Match Control Register: The MCR is used to control the reseting of TC and generating of
MCR
interrupt whenever a Match occurs.
MR0 Match Register: This register hold the max cycle Time(Ton+Toff).
MR1- Match Registers: These registers holds the Match value(PWM Duty) for corresponding PWM
MR6 channels(PWM1-PWM6).
PWM Control Register: PWM Control Register. Enables PWM outputs and selects PWM channel
PCR
types as either single edge or double edge controlled.
LER Load Enable Register: Enables use of new PWM values once the match occurs.
MCR
31:21 20 19 18 -5 4 3 2 1 0
Reserved PWMMR6S PWMMR6R PWMMR6I - PWMMR1S PWMMR1R PWMMR1I PWMMR0S PWMMR0R P
PWMMRxI
This bit is used to Enable or Disable the PWM interrupts when the PWMTC matches PWMMRx (x:0-6)
0- Disable the PWM Match interrupt
1- Enable the PWM Match interrupt.
PWMMRxR
This bit is used to Reset PWMTC whenever it Matches PWMRx(x:0-6)
0- Do not Clear.
1- Reset the PWMTC counter value whenever it matches PWMRx.
PWMMRxS
This bit is used to Stop the PWMTC,PWMPC whenever the PWMTC matches PWMMRx(x:0-6).
0- Disable the PWM stop o match feature
1- Enable the PWM Stop feature. This will stop the PWM whenever the PWMTC reaches the Match register
value.
PCR
31:15 14-9 8-7 6-2 1-0
Unused PWMENA6-PWMENA1 Unused PWMSEL6-PWMSEL2 Unused
PWMSELx
This bit is used to select the single edged and double edge mode form PWMx (x:2-6)
0- Single Edge mode for PWMx
1- Double Edge Mode for PWMx.
PWMENAx
LER
31-7 6 5 4 3 2 1 0
Unused LEN6 LEN5 LEN4 LEN3 LEN2 LEN1 LEN0
LENx
This bit is used Enable/Disable the loading of new Match value whenever the PWMTC is reset(x:0-6)
PWMTC will be continously incrementing whenever it reaches the PWMMRO, timer will be reset depeding
on PWMTCR configuraion. Once the Timer is reset the New Match values will be loaded from MR0-MR6
depending on bits set in this register.
0- Disable the loading of new Match Values
1- Load the new Match values from MRx when the timer is reset.
PWM Working
The TC is continuously incremented and once it matches the MR1(Duty Cycle) the PWM pin is pulled Low.
TC still continues to increment and once it reaches the Cycle time(Ton+Toff) the PWM module does the
following things:
Reset the TC value.
Pull the PWM pin High.
Loads the new Match register values.
PROGRAM:
#include<lpc17xx.h>
void delay(unsigned int k)
{
unsignedinti,j;
for(i=0;i<k;i++)
for(j=0;j<60000;j++);
}
#define SBIT_CNTEN 0
#define SBIT_PWMEN 2
#define SBIT_PWMMR0R 1
EXP.NO:8:
Interface and Control a DC Motor.
Connection details:
Algorithm:
Steps to Configure PWM
1. Configure the GPIO pins for PWM operation in respective PINSEL register.
2. Configure TCR to enable the Counter for incrementing the TC, and Enable the PWM block.
3. Set the required pre-scalar value in PR. In our case it will be zero.
4. Configure MCR to reset the TC whenever it matches MR0.
5. Update the Cycle time in MR0. Here, it will be 100.
6. Load the Duty cycles for required PWM3 channel in respective match register MR3.
7. Enable the bits in LER register to load and latch the new match values.
8. Enable the pwm channel 1 in PCR register.
steps to control the speed of DC motor:
After configuring the PWM module,
1. Read the status of the switch 1. For each press, decrease the MR3 by 10.
check whether it is greater than 0.Else assume MR3 is always zero for further
continuous press of Switch 1.
2. Read the status of the switch 2. For each press, increase the MR3 by 10. Check whether it is
less than 100.Else assume MR3 is always 99 for further continuous press of Switch 2.
PROGRAM:
Program:
#include "lpc17xx.h"
#include "lcd.h"
/////////////////////////////////////////////
// Matrix Keypad Scanning Routine
//
// COL1 COL2 COL3 COL4
// 0 1 2 3 ROW 1
// 4 5 6 7 ROW 2
// 8 9 A B ROW 3
// C D E F ROW 4
//////////////////////////////////////////////
#define COL1 0
#define COL2 1
#define COL3 4
#define COL4 8
#define ROW1 9
#define ROW2 10
#define ROW3 14
#define ROW4 15
KEY_CTRL_CLR |= COLMASK;
KEY_CTRL_SET |= temp;
}
while (1)
{
key = 0;
for(i = 0; i< 4; i++ )
{
// turn on COL output one by one
col_write(rval[i]);
key++;
if (!(KEY_CTRL_PIN & (1<<ROW2)))
break;
key++;
if (!(KEY_CTRL_PIN & (1<<ROW3)))
break;
key++;
if (!(KEY_CTRL_PIN & (1<<ROW4)))
break;
key++;
}
if (key == 0x10)
lcd_putstring16(1,"Key Pressed = ");
else
{
lcd_gotoxy(1,14);
lcd_putchar(keyPadMatrix[key]);
}
}
LCD code:-
#include "lpc17xx.h"
#include "lcd.h"
voidLcd_CmdWrite(unsigned char cmd);
voidLcd_DataWrite(unsigned char dat);
#define LCDRS 9
#define LCDRW 10
#define LCDEN 11
#define LCD_D4 19
#define LCD_D5 20
#define LCD_D6 21
#define LCD_D7 22
#define LcdData LPC_GPIO0->FIOPIN
#define LcdControl LPC_GPIO0->FIOPIN
#define LcdDataDirn LPC_GPIO0->FIODIR
#define LcdCtrlDirn LPC_GPIO0->FIODIR
#define LCD_ctrlMask ((1<<LCDRS)|(1<<LCDRW)|(1<<LCDEN))
voidsendNibble(char nibble)
{
LcdData&=~(LCD_dataMask); // Clear previous data
LcdData|= (((nibble >>0x00) & 0x01) << LCD_D4);
LcdData|= (((nibble >>0x01) & 0x01) << LCD_D5);
LcdData|= (((nibble >>0x02) & 0x01) << LCD_D6);
LcdData|= (((nibble >>0x03) & 0x01) << LCD_D7);
}
delay(10000);
delay(1000);
}
delay(1000);
delay(1000);
}
voidlcd_clear( void)
{
Lcd_CmdWrite( 0x01 );
}
intlcd_gotoxy( unsigned char x, unsigned char y)
{
unsigned char retval = TRUE;
Exp.No:10:
Measure Ambient temperature using a sensor and SPI ADC IC.
Serial Peripheral Interface (SPI)
Serial Peripheral Interface (SPI) is an interface bus commonly used to send data between microcontrollers and
small peripherals such as shift registers, sensors, and SD cards. It uses separate clock and data lines, along
with a select line to choose the device.
ADC (Analog to Digital Converter):
The Microchip Technology Inc. MCP3202 is a successive approximation 12-bit Analog-to-Digital (A/D)
Converter with on-board sample and hold circuitry. The MCP3202 is programmable to provide a single
pseudo-differential input pair or dual single-ended inputs. Differential Nonlinearity (DNL) is specified at ±1
LSB, and Integral Nonlinearity (INL) is offered in ±1 LSB (MCP3202-B) and ±2 LSB (MCP3202-C)
versions. Communication with the device is done using a simple serial interface compatible with the SPI
protocol. The device is capable of conversion rates of up to 100ksps at 5V and 50ksps at 2.7V.The MCP3202
is a Dual Channel 12-Bit A/D Converter with SPI Serial Interface By Microchip . In this tutorial i will
interface this ADC using lpc1768 microcontroller using SPI Protocol in mode(0,0) . Maximum clock rate
supported by MCP3202 is 1.8 MHz.
Fsample= 100 KSPS
Fclk = 18*Fsample
Configuring SPI Control Register :
first we need to send start bit , it is last bit of first byte we are going to send to ADC and then we have to
select Configuration modes.there are two modes single ended and pseudo differential mode here choose
single ended mode . MSBF bit choses order of format of byte either MSB first or LSB first herechoose MSB
bit first format so MSBF=1.ransferingSecondbyte=0xA0forchannel0and Secondbyte=0xE0 for channel 1.
ADC will return B11 to B8 of data in the lower nibble of byte so we need to perform some mathematical
manipulation. after that we need to send any byte to receive third byte of data.
CS P0.28
MCP
3202 CLK P0.27
Dout P3.26
Din P3.25
ALGORITHM:
PROGRAM:
#include <LPC17xx.H>
#include <stdint.h>
#include <stdio.h>
#include "delay.h"
#include "spi_manul.h"
#include "lcd.h"
#define pulse_val 2
main()
{
unsignedintspi_rsv=0;
float vin;
charbuf[20];
SystemInit ();
lcd_init();
lcd_str("SPI 3202-b");
delay(60000);
delay(60000);
while(1)
{
lcd_clr();
lcd_cmd(0x80);
spi_rsv = spi_data1(15);
vin = ( ( spi_rsv& 0xfff ) * (3.3) ) / 4096 ;
sprintf(buf,"Temp: %0.2f degC",(vin*100) );
lcd_str(buf);
delay(50000);
delay(50000);
}
}
LPC_GPIO0->FIOSET = CS|CLK;
LPC_GPIO3->FIOCLR = DOUT;
nop_delay(100);
#if spi_stst
if ( LPC_GPIO3->FIOPIN & DIN )
{
return 'P';
}
#endif
LPC_GPIO0->FIOCLR = CS;
nop_delay(pulse_val);
while(clks)
{
LPC_GPIO0->FIOCLR = CLK;
nop_delay(pulse_val);
LPC_GPIO0->FIOSET = CLK;
nop_delay(pulse_val);
clks--;
}
LPC_GPIO0->FIOCLR = CLK;
nop_delay(pulse_val);
if (!( LPC_GPIO3->FIOPIN & DIN ) )
{
return 'U';
}
clks = 12;
LPC_GPIO0->FIOSET = CS|CLK;
LPC_GPIO3->FIOSET = DOUT;
LPC_GPIO3->FIOPIN = DIN;
nop_delay(100);
LPC_GPIO0->FIOCLR = CS;
//start condi
LPC_GPIO0->FIOCLR = CLK;
LPC_GPIO3->FIOSET = DOUT;
nop_delay(pulse_val);
LPC_GPIO0->FIOSET = CLK;
nop_delay(5);
//single mode
LPC_GPIO0->FIOCLR = CLK;
LPC_GPIO3->FIOSET = DOUT;
nop_delay(pulse_val);
LPC_GPIO0->FIOSET = CLK;
nop_delay(5);
//chanl 1
LPC_GPIO0->FIOCLR = CLK;
LPC_GPIO3->FIOSET = DOUT;
nop_delay(pulse_val);
LPC_GPIO0->FIOSET = CLK;
nop_delay(5);
//msb first
LPC_GPIO0->FIOCLR = CLK;
LPC_GPIO3->FIOSET = DOUT;
nop_delay(pulse_val);
LPC_GPIO0->FIOSET = CLK;
nop_delay(5);
//smpling
// LPC_GPIO0->FIOCLR = CLK;
// nop_delay(pulse_val);
// LPC_GPIO0->FIOSET = CLK;
// nop_delay(2);
//null bit
LPC_GPIO0->FIOCLR = CLK;
nop_delay(pulse_val);
LPC_GPIO0->FIOSET = CLK;
// while ( ( LPC_GPIO3->FIOPIN & DIN ) == DIN );
// if( !( LPC_GPIO3->FIOPIN & DIN ) );
// {
// return 'U';
// }
nop_delay(5);
clks = 12;
while(clks)
{
LPC_GPIO0->FIOCLR = CLK;
nop_delay(pulse_val);
LPC_GPIO0->FIOSET = CLK;
if( ( LPC_GPIO3->FIOPIN & DIN ) )
{
spi_reg |= 1<<(clks-1);
}
else
{
spi_reg = spi_reg;
}
clks--;
nop_delay(5);
}
nop_delay(1);
returnspi_reg;
}
#include <LPC17xx.H>
#include "delay.h"
#define RRW (7<<9)
#define DATA_L (15<<19)
voidlcd_pin(void)
{
LPC_GPIO0->FIODIR |= RRW|DATA_L;
}
LPC_GPIO0->FIOCLR |= RRW|DATA_L;
delay(10);
LPC_GPIO0->FIOCLR |= RRW|DATA_L;
delay(10);
voidlcd_str(char *lstr)
{
while(*lstr)
{
lcd_data(*lstr);
lstr++;
}
}
voidlcd_clr(void)
{
lcd_cmd(0x03);
delay(10);
lcd_cmd(0x2);
lcd_cmd(0x28);
lcd_cmd(0x0e);
lcd_cmd(0x06);
lcd_cmd(0x01);
delay(10);
}
ALGORITHM:
Steps for Configuring ADC
1. Configure the GPIO pin for ADC function using PINSEL register.
3. Deselect all the channels and Power on the internal ADC module by setting
ADCR.PDN bit.
4. Select the Particular channel for A/D conversion by setting the corresponding bits in
ADCR.SEL
5. Set the ADCR.START bit for starting the A/D conversion for selected channel.
6. Wait for the conversion to complete, ADGR.DONE bit will be set once conversion is
over.
}
////////// DISPLAY ADC VALUE /////////////////
Display_ADC()
{
unsignedintadc_value = 0;
charbuf[4] = {5};
float voltage = 0.0;
adc_value = Read_ADC();
sprintf((char *)buf, "%3d", adc_value); // display 3 decima place
To become a pioneer in developing competent professionals with societal and ethical values through
transformational learning and interdisciplinary research in the field of Electronics and
Communication Engineering.
MISSION
The department of Electronics and Communication is committed to:
M1: Offer quality technical education through experiential learning to produce competent
engineering professionals.
M4: Create a congenial environment for the faculty and students to achieve their desired goals
and to serve society by upholding ethical values.