Professional Documents
Culture Documents
Embedded Controller Arm Lab Manual
Embedded Controller Arm Lab Manual
[15ECL67]
VI- SEMESTER ECE/TCE CBCS Scheme
By
RAGHUNATH .B.H
USN: _____________________________________
Page 1
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Syllabus :
PART-A : Conduct the following Study experiments to learn ALP using ARM Cortex M3 Registers using an
Evaluation board and the required software tool.
1. ALP to multiply
multiply two 16 bit binary
binary numbers.
numbers.
2. ALP to find the sum of first 10 integer
integer numbers.
Page 2
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Cycle 1:
I. Differentiate between Microprocessor and Microcontroller.
II. Introduction to Cortex M3 Controller –
Controller –
i. Features of Cortex M3 Controller.
ii. GPIO Operations –
Operations – Selection
Selection and Configuration
III. Usage of Keil Microvision Tool.
i. Creating Project – selection of target controller and setting environment
ii. Creating Embedded
Embedded C code.
code.
iii. Compilation and Execution of Embedded
Embedded C Code
IV. Interface of Cortex
Cortex M3 Evaluation board with PC.
i. Connection of Cortex M3 board
board to host PC
ii. Usage of Flash magic
magic to download Embedded
Embedded C code to Cortex M3 board.
Embedded C Codes
1. Interfacing of “LED”
LED” – 8 8 no‟s
no‟s to
to a GPIO of Cortex
Cort ex M3 Controller
i. To blink a single LED bit by bit.
ii. To scroll
scroll a LED from bottom to top and top to bottom.
2. Interface of toggle switch to a GPIO
GPIO of Cortex
Cortex M3 Controller.
i. Switch Status to be displayed on LED.
LED.
ii. Switch Status to be displayed
displayed on Buzzer. iii. Switch status to be displayed
displayed on Relay.
Cycle 2:
3. Usage of an external
external interrupt to toggle an LED on/off.
4. Usage of Internal UART.
i. To transmit a single character data from Host to UART.
ii. To receive a single character data from UART to Host.
iii. To transmit
transmit a string of character
character data from Host to UART
UART
6. Interface a Stepper
Stepper motor
motor to a GPIO of Cortex M3 Controller
i. Rotate stepper motor in clockwise direction
ii. Rotate stepper motor in an anti-clockwise direction
Cycle 3:
7. Usage of
of Internal PWM to generate PWM
PWM and vary its duty cycle
cycle
8. Interface a DC Motor to GPIO of cortex M3 controller and and perform it‟s
it‟s control
control
operation using switches.
9. Interface of Seven segment module to Cortex M3 Controller –
Controller –
i. To display hexa digits 0 to F with suitable delay.
ii. To display hexa digits 00 to FF with suitable delay
Cycle 4:
Page 3
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Step 1. Open the Keil IDE, under main menu goto “Project -> New uVision Project…” and
Project…” and a window prompt
will open asking a name to save the new project. Type your desired project name and save.
Step 2. After that, a new window will appear as shown below. Make sure “Software Packs” is
Packs” is selected for the 1st
drop down. In the search box below it, type “lpc1768” and
“lpc1768” and then select the device from list below. Finally click
“OK”.
“OK”.
Page 4
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Page 5
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Step 7. Now,
Now, under the “Debug” tab,
“Debug” tab, change the values for “Dialog DLL” and
DLL” and “Parameter” under
“Parameter” under both simulation
and hardware debugger settings group. You can select “Use Simulator” to
Simulator” to debug your code using simulator itself. If
you are using JTAG programmer or any other hardware debugger supported by KEIL then you can select it from
Page 6
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
top-right drop down box. We will see flashing and debugger options in detail in an upcoming post. Finally to click
“OK” to
“OK” to apply settings and close window.
Step 8. Now, in the source navigation pane on the left area, right click on “Source Group 1” and
1” and select “Add New
Item to Group „Source Group 1′”.
1′”.
Page 7
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Step 10. Now you can write your code in the editor. To compile your program Press “F7” key
“F7” key or in the main menu
goto “Project-
“Project->Build Target”. To
Target”. To check for any compilation errors you can have a look at the build output at the
bottom of main window. A screenshot of the Keil MDK uVision 5 is given below. The top red box shows the
Source Tree Navigation and the bottom red box shows the build output.
Step 11. To initiate a debug session using simulator or debug hardware/probe click “Debug->Start/Stop
“Debug->Start/Stop Debug
Session” or
Session” or you can hit “Ctrl+F5” as
“Ctrl+F5” as shown below:
Check COM port number from Device manager. Right click my computer, then click manage. Double click
device manger
Page 8
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Register Configuration
LPC1768 has its GPIOs divided into five ports PORT0 - PORT4, although many of them are not physically 32bit
wide. Refer the data sheet for more info. The Below registers will be used for Configuring and using the GPIOs
registers for sending and receiving the Digital signals. A structure LPC_GPIOn(n= 0,1,2,3) contains all the
registers for required for GPIO operation. Refer lpc17xx.h file for more info on the registers.
r egisters.
LPC1768 GPIO
Selects Pin Function Configure Pin Direction Access the Port Pin
PINSEL: GPIO Pins Select Register Almost all the LPC1768 pins are multiplexed to support
more than 1 function. Every GPIO pin has a minimum of one function and max of four functions. The
required function can be selected by configuring the PINSEL register . As there can be up to 4 functions
associated with a GPIO pin, two bits for each pin are available to select the function. This implies that we need
two PINSEL registers to configure a PORT pins . By this the firstf irst 16(P0.0-P0.16) pin functions of PORT0 can be
selected by 32 bits of PINSELO register. The remaining 16 bits(P0.16-P0.32) are configured using 32bits of
PINSEL1 register. As mentioned earlier every pin has max of four functions. Below table shows how to select the
function for a particular pin using two bits of the PINSEL register.
Values Direction
0 Input
1 Output
Page 9
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
This register controls the state of output pins. Writing 1s produces highs at the corresponding port pins. Writing 0s
has no effect. Reading this register returns the current contents of the port output register not the physical port
value.
Values FIOSET
0 No Effect
1 Sets High on Pin
This register controls the state of output pins. Writing 1s produces lows at the corresponding port pins. Writing 0s
has no effect.
Values FIOCLR
0 No Effect
1 Sets Low on Pin
This register is used for both reading and writing data from/to the PORT.
Output: Writing to this register places corresponding values in all bits of the particular PORT pins.
Input: The current state of digital port pins can be read from this register, regardless of pin direction or alternate
function selection (as long as pins are not configured as an input to ADC).
Note:It is recommended to configure the PORT direction and pin function before using it.
Registers on LPC1768 are present on Peripheral AHB bus( bus(Advanced High performance Bus)
Bus ) for fast read/write
timing. So, these are basically Fast I/O or Enhanced I/O and hence the naming convention in datasheet uses a prefix
of “FIO” instead of something like “GIO” for all the registers related to GPIO. Lets go through these as given
below.
1) FIODIR : This is the GPIO direction control register. Setting a bit to 0 in this register will configure the
corresponding pin to be used as an Input while setting it to 1 will configure it as Output.
2) FIOMASK : This gives masking mechanism for any pin i.e. it is used for Pin access control. Setting a bit to 0
means that the corresponding pin will be affected by changes to other registers like FIOPIN, FIOSET, FIOCLR.
Writing a 1 means that the corresponding pin won‟t be affected by other registers.
3) FIOPIN : This register can be used to Read or Write values directly to the pins. Regardless of the direction set
for the particular pins it gives the current start of the GPIO pin when read.
4) FIOSET : It is used to drive an „output‟ configured pin to Logic 1 i.e HIGH. Writing Zero does NOT have any
effect and hence it cannot be used to drive a pin to Logic 0 i.e LOW. For driving pins LOW FIOCLR is used which
is explained below.
5) FIOCLR : It is used to drive an „output‟ configured pin to Logic 0 i.e LOW. Writing Zero does NOT have any
effect and hence it cannot be used to drive a pin to Logic 1.
Page 10
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Most of the PINS of LPC176x MCU are Multiplexed i.e. these pins can be configured to provide up to 4 different
functions. By default, after Power-On or Reset : all pins of all ports are set as GPIO so we can directly use
them when learning GPIO usage . The different functions that any particular pin provides can be selected by
setting appropriate value in the PINSEL register for the corresponding pin. Each pin on any port has 2
corresponding bits in PINSEL register. The first 16 pins (0-15) on a given port will have a corresponding 32 bit
PINSEL register and the rest 16 bits will have another register. For example bits 0 & 1 in PINSEL0 are used to
select function for Pin 1 of Port 0, bits 2 & 3 in PINSEL0 are used to select function for PIN 2 of port 0 and so on.
The same is applicable for PINMODE register which will go through in last section of this article. Have a look at
the diagram given below.
PINSEL0 Register
31 30 29 28 27 26 25 24 23 22
22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
P0.15 P0.14 P0.13 P0.12 P0.11 P0.10 P0.9 P0.8 P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0
PINSEL1
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
P0.31 P0.30 P0.29 P0.28 P0.27 P0.26 P0.25 P0.24 P0.23 P0.22 P0.21 P0.20 P0.19 P0.18 P0.17 P0.16
Since by default all pins are configured as GPIOs we don‟t need to explicitly assign a „0‟ value to PINSEL
register in Programming
Programming examples.
Page 11
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Example #1)
To configure Pin 4 of Port 0 i.e P0.4 as Output and want to make it High (Logic 1)
LPC_GPIO0->FIODIR
LPC_GPIO0 ->FIODIR = 0x0000 0010; 0001 0000; 4 th bit is 1
// in 32 bit form 0000 0000 0000 0000 0000 0000 0001
Alternate way is
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<< 4); // 1 in 32 bit form left shift by 4 times.
0000 0000 0000 0000 0000 0000 0000 0001 left shit by 4 times results as
0000 0000 0000 0000 0000 0000 0001 0000. which Configures P0.4 as Ouput
LPC_GPIO0->FIOSET
LPC_GPIO0->FIOSET = (1<< 4); // SET High for P0.4 pin
LPC_GPIO0->FIODIR
LPC_GPIO0 ->FIODIR = 0x0000 0010; // omitting left side zeros ; we can write as 0 x10 ; x stands for Hexadecimal number
LPC_GPIO->FIODIR
LPC_GPIO ->FIODIR = 0x10; // Configure pin 4 of port 0 as Output
LPC_GPIO->FIOSET=0x10
LPC_GPIO ->FIOSET=0x10;; //pin 4 of port 0 is made HIGH
Example #2
Configure Pin 17 of Port 1 [ P1.17] as output and then SET that Pin and
and CLEAR it:
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR = (1<<17); // configuring 17th pin of port 1 [P1.17] as Output pin
LPC_GPIO1->FIOSET
LPC_GPIO1 ->FIOSET = (1<<17); // Output for P1.17 becomes High
LPC_GPIO1->FIOCLR
LPC_GPIO1 ->FIOCLR = (1<<17); // Output for P1.17 becomes Low
Example #3) Configure Pin 3 and 5 of port 0 as output and SET them Logic Hig h.
i.e Configuring P0.3 and P0.5 as Output and Setting them High:
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<<3) | (1<<5)
(1<<5) // 1 left shift 3 logically ORed
ORed with 1 left shift by 5
0000 0000 0000 0000 0000 0000 0000 1000 [ 1 << 3 ]
LOGIC OR with 0000 0000 0000 0000 0000 0000 0010 0000 [ 1 << 5 ]
0000 0000 0000 0000 0000 0000 0010 1000 3 rd and 5th bits are 1. So P0.3 and P0.5 configures as
output
Alternate way is
LPC_GPIO0->FIODIR
LPC_GPIO0 ->FIODIR =0x0000 0028; // grouping above terms or we can also write as 0x28
LPC_GPIO0->FIOSET
LPC_GPIO0->FIOSET = (1<<3) | (1<<5) // Make ouput High for P0.3 and P0.5
Example #4)
Configuring 1st 8 Pins of Port 0 (P0.0 to P0.7) as Ouput and Setting them High:
LPC_GPIO0->FIODIR
LPC_GPIO0 ->FIODIR = 0x000000FF; // Config P0.0 to P0.7 as Ouput. 0000 0000 0000 0000 0000 1111 1111
LPC_GPIO0->FIOSET
LPC_GPIO0 ->FIOSET = 0x000000FF; // Make output High for P0.0 to P0.7
Example #5)
LPC_GPIO0->FIODIR
LPC_GPIO0 ->FIODIR = 0xFF0; // Config P0.4 to P0.11 as Ouput. 0000 0000 0000 0000 0000 1111 1111 0000
LPC_GPIO0->FIOSET
LPC_GPIO0 ->FIOSET = 0xFF0; // 1111 1111 0000 Make output High for P0.4 to P0.11
LPC_GPIO0->FIOCLR
LPC_GPIO0 ->FIOCLR = 0xFF0; // Make output LOW for P0.4 to P0.11
LPC_GPIO1->FIODIR
LPC_GPIO1 ->FIODIR &= ~(0xFF0); // Config P1.14 to P1.21 as Input. ~ means complement
LPC_GPIO1->FIODIR
LPC_GPIO1 ->FIODIR &=~(1<<14); // only Pin p1.14 is configured as input
Note: On reset by default all registers are zero. So all port‟s all the pins acts as input. We can skip setting i/p lines
Page 12
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Blink a LED
#include <lpc17xx.h>
int main(void)
{
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<< 4); // Configuring P0.4 as Output
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR &= ~ ((1<< 21)) ; // explicitly making P0.21 as Input . we can skip this line
a. To blink LEDs
#include <LPC17xx.h>
int main()
{
LPC_GPIO0-
LPC_GPIO0- >FIODIR = 0x0FF0; // P0.4-P0.11 pins as o/p.
while(1) //infinite loop
{
int i;
LPC_GPIO0 - >FIOSET = 0x0FF0; // SET po.4 to p0.11 LEDs Turns ON
for (i=10000000; i >0; i- -); // delay
LPC_GPIO0
LPC_GPIO0 - >FIOCLR = 0x0FF0; //clear P0.4 to P.11 LEDs Turns OFF
for (i=10000000;i>0; i--); //delay
}
}
Page 13
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
1.b to Scroll LEDs (Ring Counter) P0.11 P0.10 P0.9 P0.8 P0.7 P0.6 P0.5 P0.4
for (x=8;
(x=8; x > 0; x - - )
{
LPC_GPIO0
LPC_GPIO0 - > FIOPIN = z; // Initially P0.11 to turn LED ON
for (y=1000000; y > 0; y- -); // delay
z = z >> 1; // right shift Z content by 1 time
}
}
}
Lab Program # 2.a Interface a simple Switch and display its status through LED.
#include <LPC17xx.h>
int main()
{
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = 1<< 4; // port P0.4 pin is configured as o/p
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR & = ~ (1<< 21); // port p1.21 pin is configured as i/p
}
else
{
LPC_GPIO0->FIOCLR
LPC_GPIO0->FIOCLR = (1<< 4); // Else Clear Port P0.4
}
}
}
Page 14
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
2.b. Interface a simple Switch and display its status through Buzzer .
#include <LPC17xx.h>
int main()
{
LPC_GPIO3->FIODIR
LPC_GPIO3->FIODIR = 1<< 25; // port 3.25 pin is configured as o/p
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR & = ~ (1<< 21); // port p1.21 pin is configured as i/p
while(1)
{
if (! (LPC_GPIO1->FIOPIN
(LPC_GPIO1->FIOPIN & (1<< 21))) // if switch P.21 pressed
{
LPC_GPIO3->FIOSET
LPC_GPIO3->FIOSET = (1<< 25); // Port 3.25 pin is set [ High]
}
else
{
LPC_GPIO3->FIOCLR
LPC_GPIO3->FIOCLR = (1<< 25); // Port 3.25 pin is Clear [Low]
}
}
}
2.c. Interface a simple Switch and display its status through Buzzer,Relay and LED.
#include <LPC17xx.h>
int main()
{
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<<4) | (1<<25); // port P0.4-P0.11[LEDS]
P0.4-P0.11[LEDS] & P0.25[Relay] config as o/p
LPC_GPIO3->FIODIR
LPC_GPIO3->FIODIR = (1<<25); // port 3.25[Buzzer]
3.25[Buzzer] configured as o/p
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR &= ~(1<<21); // port P1.21[Switch KEY] configured as i/p
while(1)
{
if (! (LPC_GPIO1->FIOPIN
(LPC_GPIO1->FIOPIN & (1<<21))) // if switch P1.21 pressed
{
LPC_GPIO0->FIOSET = (1<<4) | (1<<25); // turn on LED, RELAY
LPC_GPIO3->FIOSET
LPC_GPIO3->FIOSET = (1<<25); //turn on Buzzer
}
else
{
LPC_GPIO0->FIOCLR
LPC_GPIO0->FIOCLR = (1 << 4) | (1 << 25); // turn OFF LED, Relay
LPC_GPIO3->FIOCLR = (1 << 25); //turn
//tur n OFF Buzzer
}
}
}
Page 15
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Applications of Relay
Page 16
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
EINT Registers
Register Description
External Interrupt Flag Register contains interrupt flags for EINT0,EINT1, EINT2 &
EXTINT
EINT3.
EXTPOLAR
EXTPOLAR External Interrupt Polarity(Falling/Rising Edge,
Edge, Active Low/High)
EXTINT
31:4 3 2 1 0
EXTMODE
31:4 3 2 1 0
Page 17
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
EXTPOLAR
31:4 3 2 1 0
#include <lpc17xx.h>
void EINT0_IRQHandler(void)
EINT0_IRQHandler(void)
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 1; //0000 0000 0000 0001, EXTINT
EXTINT pin0 is One. Clearing EXTINT0
EXTINT0
LPC_GPIO0->FIOPIN ^ = (1<<4); /* Toggle the LED1 everytime INTR0 is generated */
}
int main( )
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 1; / 0000 0000 0000 0000 0000 0000 0000 0001 Clear EXTINT0
EXTINT0
LPC_PINCON->PINSE
LPC_PINCON->PINSEL4
L4 = (1<< 20); // 1 left shift by 20 times makes P2.10 function as EINT0
EINT0
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<<4); /*configure LED pins as OUTPUT */
NVIC_EnableIRQ(EINT0_
NVIC_EnableIRQ(EINT0_IRQn);
IRQn); /* Enable the EINT0
EINT0 interrupts */
while(1)
{ // any main program running
}
}
Page 18
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
#include <lpc17xx.h>
void EINT1_IRQHandler(void)
EINT1_IRQHandler(void)
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 2; // 0000 0000 0000 0010 Clear Interrupt Flag EXTINT1.
EXTINT1. Reset interrupt
LPC_GPIO0->FIOPIN
LPC_GPIO0->FIOP IN ^ = (1<<11) ; /* Toggle the LED1 everytime INTR1 is generated */
}
int main( )
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 2; // 0000 0000 0000 0010 Clear interrupt flag EXTINT1
LPC_PINCON->PINSEL4 = (1<< 22); /make PINSEL4.22 bit high to make P2.10 pin function as EINT01
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<<11); /*configure LED P0.11 as OUTPUT */
NVIC_EnableIRQ(EINT1_
NVIC_EnableIRQ(EINT1_IRQn);
IRQn); /* Enable the EINT0 interrupts */
*/
while(1)
{ // any main program running
}
}
3. c .Demonstrate the use of an external interrupts EINT0 & EINT1to toggle an LED On/Off
#include <lpc17xx.h>
void EINT0_IRQHandler(void)
EINT0_IRQHandler(void)
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 1; /* Clear Interrupt Flag ext int 1 */
LPC_GPIO0->FIOPIN
LPC_GPIO0->FIOPIN ^ = (1<< 4) ; /* Toggle the LED1 everytime INTR1 is generated */
}
void EINT1_IRQHandler(void)
EINT1_IRQHandler(void)
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 2; /* Clear Interrupt Flag ext int 1 */
LPC_GPIO0->FIOPIN
LPC_GPIO0->FIOPIN ^ = (1 << 11) ; /* Toggle the LED1 everytime INTR1 is generated */
}
int main( )
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 3; /* Clear Pending interrupts */
LPC_PINCON->PINSEL4 = (1<< 20) | (1<< 22); /* Configure P2_10 as EINT01*/
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<< 4) | (1<< 11); /*configure LED pins as OUTPUT */
NVIC_EnableIRQ
NVIC_EnableIRQ (EINT0_IRQn);
(EINT0_IRQn); /* Enable the EINT0
EINT0 interrupt */
NVIC_EnableIRQ
NVIC_EnableIRQ (EINT1_IRQn);
(EINT1_IRQn); /* Enable the EINT1
EINT1 interrupt */
*/
while(1)
{ // any main program running
}
}
Page 19
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
UART module and registers. LPC1768 has 4-UARTs numbering 0-3, similarly the pins are also named as RXD0-
RXD3 and TXD0-TXD3.As the LPC1768 pins are multiplexed for multiple functionalities, first they have to be
configured as UART pins.
Below table shows the multiplexed UARTs pins.
UART Registers
The below table shows the registers associated with LPC1768 UART.
Register Description
DLM Most Significant Byte of the UART baud rate generator value.
FCR
31:8 7:6 5:4 3 2 1 0
RESERVED RX TRIGGER RESERVED DMA MODE TX FIFO RESET RX FIFO RESET FIFO ENABLE
Bit 0 –
0 – FIFO:
FIFO:
This bit is used to enable/disable the FIFO for the data received/transmitted.
received/transmitted.
0--FIFO is Disabled.
1--FIFO is Enabled for both Rx and Tx.
Bit 1 –
1 – RX_FIFO:
RX_FIFO:
This is used to clear the 16-byte Rx FIFO.
Page 20
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
0--No impact.
1--CLears the 16-byte Rx FIFO and the resets t he FIFO pointer.
Bit 2 –
2 – Tx_FIFO:
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 –
3 – DMA_MODE:
DMA_MODE:
This is used for Enabling/Disabling
Enabling/Disabling DMA mode.
0--Disables the DMA.
1--Enables DMA only when the FIFO(bit-0) bit is SET.
Bit 7:6 –
7:6 – Rx_TRIGGER:
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.
interrupt/DMA.
00-- Trigger level 0 (1 character
character or 0x01)
01-- Trigger level 1 (4 characters
characters or 0x04)
10-- Trigger level 2 (8 characters or 0x08)
11-- Trigger level 3 (14 characters
characters or 0x0E)
This register is used for defining the UART frame format ie. Number of Data bits, STOP bits etc.
LCR
31:8 7 6 5:4 3 2 1:0
Break Parity Stop Bit Word Length
Reserved DLAB Parity Select
Control Enable Select Select
Set as-> 1 0 00 0 0 11
Bit 1:0 –
1:0 – WLS
WLS : WordLenghtSelect
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 –
2 – Stop
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 –
3 – Parity
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.
checking.
Bit 5:4 –
5:4 – Parity
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
Page 21
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Bit 6 –
6 – Break
Break Control
0-- Disable break transmission .
1-- Enable break transmission. Output pin UARTn TXD is forced to logic 0
Bit 8 –
8 – DLAB:
DLAB: Divisor Latch Access Bit
This bit is used to enable the access to divisor latch.
0-- Disable access to divisor latch
1-- Enable access to divisor latch
The is a read-only register that provides status information of the UART TX and RX blocks.
LSR
31:8 7 6 5 4 3 2 1 0
Reserved RXFE TEMT THRE BI FE PE OE RDR
Bit 0 –
0 – RDR:
RDR: Receive Data Ready
This bit will be set when there is a received data in RBR register. This bit will be automatically cleared when RBR
is empty.
0-- The UARTn receiver FIFO is empty.
1-- The UARTn receiver FIFO is not empty.
Bit 1 –
1 – OE:
OE: Overrun Error
The overrun error condition is set when the UART Rx FIFO is full
f ull and a new character
character is received. In this case, the
UARTn RBR FIFO will not be overwritten and the character in the UARTn RSR will be lost.
0-- No overrun
1-- Buffer over run
Bit 2 –
2 – PE:
PE: Parity Error
This bit is set when the receiver detects a error in the Parity.
0-- No Parity Error
1-- Parity Error
Bit 3 –
3 – FE:
FE: Framing Error
This bit is set when there is error in the STOP bit(LOGIC
bit(LOGIC 0)
0-- No Framing Error
1-- Framing Error
Bit 4 –
4 – BI:
BI: Break Interrupt
This bit is set when the RXDn is held in the spacing state (all zeroes) for one full character transmission
0-- No Break interrupt
1-- Break Interrupt detected.
Bit 5 –
5 – THRE:
THRE: Transmitter Holding Register Empty
THRE is set immediately upon detection of an empty THR. It is automatically cleared when the THR is written.
0-- THR register is Empty
1-- THR has valid data to
t o be transmitted
Bit 6 –
6 – TEMT:
TEMT: Transmitter Empty
TEMT is set when both UnTHR and UnTSR are empty; TEMT is cleared when any of them contain valid data.
0-- THR and/or the TSR contains valid data.
1-- THR and the TSR are empty.
Page 22
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Bit 7 –
7 – RXFE:
RXFE: Error in Rx FIFO
This bit is set when the received data is affected by Framing Error/Parity Error/Break Error.
0-- RBR contains no UARTn RX errors.
1-- RBR contains at least one RX error.
TER
31:8 7 6-0
Bit 7 –
7 – TXEN:
TXEN: Trsnamitter Enable
When this bit is 1, the data written to the THR is output on the TXD pin.
If this bit is cleared to 0 while a character is being sent, the transmission of that character
character is completed, but no
further characters are sent until this bit is set again.
In other words, a 0 in this
thi s bit blocks the transfer of characters.
characters.
Baudrate Calculation
UART_PCLK PCLK
0 SystemFreq/4
1 SystemFreq
2 SystemFreq/2
3 SystemFreq/8
DivAddVal/MulVal
DivAddVal/MulVal == 0
Page 23
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
1. Step1: Configure the GPIO pin for UART0 function using PINSEL register.
2. Step2: Configure the FCR for enabling the FIXO and Reste both the Rx/Tx FIFO.
3. Step3: Configure LCR for 8-data bits, 1 Stop bit, Disable Parity and Enable DLAB.
4. Step4: Get the PCLK from PCLKSELx register 7-6 bits.
5. Step5: Calculate the DLM,DLL vaues for r equired baudrate from PCLK.
6. Step6: Updtae the DLM,DLL
DLM,DLL with the calculated values.
7. Step6: Finally clear DLAB to disable the access to DLM,DLL.
DLM,DLL.
After this the UART will be ready to Transmit/Receive Data at the specified baudrate.
Page 24
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
4 .Usage of Internal
I nternal UART.
#include "LPC17xx.h"
int UART0_GetChar
UART0_GetChar (void) //LSR line status register
{
while (!(LPC_UART0->LSR
(!(LPC_UART0->LSR & 0x01)); // If LSR.0th bit = 0 The UARTn receiver FIFO is empty. Wait
for data
return (LPC_UART0->RBR);
(LPC_UART0->RBR); //If LSR.0 bit=1 UART receiver not empty. Send it to RBR
}
int main(void)
{
char ch;
SystemInit( );
UART0_Init( ); //initialize UART0
UART0_SendString("hello
UART0_SendString("hello world! "); //string=hello world!
while(1){
ch=UART0_GetChar(
ch=UART0_GetChar( ); // read character
character into ch.
UART0_SendChar(ch);
UART0_SendChar(ch); //send to uart terminal
}
}
Page 25
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
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.
Associated Corresponding
PWM Channel Port Pin Pin Functions
PINSEL Register Match Register
0,1 bits of
PWM_1 P2.0 0-GPIO, 1-PWM1[1], 2-TXD1, 3- MR1
PINSEL4
2,3 bits of
PWM_2 P2.1 0-GPIO, 1-PWM1[2], 2-RXD1, 3- MR2
PINSEL4
The below table shows the registers associated with LPC1768 PWM.
Register Description
Interrupt Register: The IR can be read to identify which of eight possible interrupt sources are pending.
IR
Writing Logic-1 will clear the corresponding interrupt.
TCR Timer Control Register: The TCR is used to control the Timer Counter functions(enable/disable/res
functions(enable/disable/reset).
et).
Page 26
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
PR Prescalar
Prescalar Register: This is used to specify the Prescalar
Prescalar value for incrementing
incrementing the TC.
Prescale Counter: The 32-bit PC is a counter which is incremented to the value stored in PR. When the
PC
value in PR is reached, the TC is incremented.
Match Control Register: The MCR is used t o control the reseting of TC and generating of interrupt
MCR
whenever a Match occurs.
MR0 Match Register: This register hold the max cycle Time(Ton+Toff).
Time(Ton+Toff) .
MR1- Match Registers: These registers holds the Match value(PWM Duty) for corresponding PWM
MR6 channels(PWM1-PWM6).
PWM Control Register: Enables PWM outputs and selects PWM channel types as either single edge or
PCR
double edge controlled.
LER Load Enable Register: Enables use of new PWM values once the match occurs.
Register Configuration
31:4 3 2 1 0
Bit 0 –
0 – Counter
Counter Enable
This bit is used to Enable or Disable the
t he PWM Timer and PWM Prescalar Counters
0- Disable the Counters
1- Enable the Counter incrementing.
incrementing.
Bit 1 –
1 – Counter
Counter reset
This bit is used to clear the PWM Timer and PWM Prescalar
Prescalar Counter values.
0- Do not Clear.
1- The PWM Timer Counter and the PWM Prescale Counter are synchronously
synchronously reset on the next positive edge of
PCLK.
Bit 3 –
3 – PWM
PWM Enable
Used to Enable or Disable the PWM Block.
0- PWM Disabled
1- PWM Enabled
Page 27
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
31:21 20 19 18 - 5 4 3 2 1 0
PWMMRxI
This bit is used to Enable or Disable the PWM interrupts when the PWMTC matches PWMMRx
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
t o Stop the PWMTC,PWMPC
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 PWM Control Register Enables PWM outputs and selects PWM channel types as either single
edge or double edge controlled.
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
This bit is used to enable/disable the PWM output for PWMx(x:1-6)
0- PWMx Disable.
1- PWMx Enabled.
LER Load Enable Register: Enables use of new PWM values once the match occurs
31-7 6 5 4 3 2 1 0
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 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.
Page 28
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
PWM Working
After looking into the PWM registers, its time to see how the LPC1768 PWM module works.
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)
time(Ton+Toff) the PWM module does the following
things:
1. Configure the GPIO pins for PWM operation in respective PINSELPINSEL register.
2. Configure TCR to enable the Counter for incrementing the TC, and Enable the PWM block.
3. Set the required pre-scalar value
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. In our case it will be 100.
6. Load the Duty cycles for required PWMx channels in respective match registers MRx(x: 1-6).
7. Enable the bits in LER register to load and latch the
t he new match values.
8. Enable the required pwm channels in PCR register.
//Program
//Progr am PWM Demo on Scope // Put Scope in DC Mode
Page 29
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
5. Usage of Internal PWM to generate PWM and vary its duty cycle
#include <lpc17xx.h>
int main(void)
{
int j,DutyCycle;
LPC_PINCON->PINSEL
LPC_PINCON->PINSEL77 = 3 << 20; //3 in binary left shift by 20
20 times. Configure
Configure pin P3.26 for PWM mode.
LPC_PWM1->TCR = (1<< 0) | (1<< 2); //enable counter and PWM
LPC_PWM1->MCR
LPC_PWM1->MCR = (1<< 1); /*Reset on PWMMR0, reset TC if it matches MR0 */
LPC_PWM1->MR0
LPC_PWM1->MR0 = 100; /* set PWM cycle(Ton+Toff)=100)
cycle(Ton+Toff)=100) */
LPC_PWM1->PCR
LPC_PWM1->PCR = 1 << 11; /* Enable the PWM output pin for PWM_3 */
while(1)
{
for(DutyCycle=0;
for(DutyCycle=0; DutyCycle < 100; DutyCycle++)
{
LPC_PWM1->MR3
LPC_PWM1->MR3 = DutyCycle; /* Increase the DutyCycle from 0-100 */
for(j=0;j<1000000;j++);
}
}
}
Page 30
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
LPC1768/LPC1769 DAC Block
ARM Cortex-M3 LPC176x MCUs incorporate a 10 bit DAC and provide buffered analog output. As per the datasheet, it is implemented as a
string DAC which is the most simplest form of DAC consisting of 2 N resistors in series where N = no. of bits which simply forms a Kelvin-
Varley Divider. 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 V REF is the input reference voltage.
Pi ns rela
relating
ting to L PC
PC17
1768
68 D A C blo
lock:
ck:
Pin Description
AOUT Analog Output pin. Provides the converted Analog signal which is referenced to VSSA i.e. the
(P0.26) Analog GND. Set Bits[21:20] in PINSEL1 register to [10] to enable this function.
These are reference voltage input pins used for both ADC and DAC. VREFP is positive
VREFP, VREFN reference voltage and V REFN is negative reference voltage pin. In example shown below we
will use VREFN=0V(GND).
VDDA is Analog Power pin and VSSA is Ground pin used to power the ADC module. These
These are
VDDA, VSSA
generally same as V CC and GND but with additional filtering to reduce noise.
The DAC module in ARM LPC1768/LPC1769 has 3 registers viz. DACR, DACCTRL, DACCNTVAL. In this
tutorial we will only go through to DACR register since the other two are related with DMA operation, explaining
which is not in the scope of this tutorial. Just note that DMA is used to update new values to DACR from memory
without the intervention of CPU. This is particularly useful when generation different types of waveforms using
DAC. We will cover this in another tutorial.
Also note that the DAC doesn‟t have a power control it in PCONP register. Simply select the AOUT alternate
function for pin P0.26 using PINSEL1 register to enable DAC ouput.
The D AC R regist
registe
er in LPC
L PC17
1768
68
The field containing bits [15:6] is used to feed a digital value which needs to be converted and bit 16 is used to
select settling time. The bit significance is as shown below:
1. Bit[5:0]: Reserved.
2. Bit[15:6] – VALUE starts from bit 6 [ In programming we use Value <<6; ]
Bit[15:6] – VALUE
3. Bit[16] – BIAS: Setting this bit to 0 selects settling time of 1us max with max current consumption
Bit[16] – BIAS: consumption of
700uA at max 1Mhz update rate. Setting it to 1 will select settling time of 2.5us but with reduce max
max
current consumption of 300uA at max 400Khz update rate.
4. Bits[31:17]: Reserved
Page 31
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
LPC1768
The total voltage provided by the DAC OUT pin is function of binary number written on to bits 6 (D0) to bit 15(D9) of DAC
Register(DACR) and the reference voltage Vref(3.3V),it is as follows
Vout =Vref [
9
2
+
8
4
+
7
8
+
6
16
+
5
32
+
4
64
+
3
128
+
2
256
+
1
512
+
0
1024
]
Where D0 is the LSB and D9 is MSB
#include <LPC17xx.H
int main (void)
{
int i, m;
LPC_PINCON->PINSE
LPC_PINCON->PINSEL1
L1 = (1<< 21); // make PINSEL1.21
PINSEL1.21 pin high to Select AOUT function
function for P0.26
while(1)
{
for (i = 0; i <= 1023; i++) //send value start from 0 to 1023 insteps of 1
{
LPC_DAC->DACR
LPC_DAC->DACR = (i << 6); // DACR register value should placed in 15:6
for(m = 10; m > 1; m--); //delay to vary freq
}
For (i = 1023 ; i > 0; i--) //once reaches max of 3.3v starts decreasing
{
LPC_DAC->DACR = (i << 6);
for(m = 10; m > 1; m--);
}
}
Page 32
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
#include <LPC17xx.H>
LPC_PINCON->PINSE
LPC_PINCON->PINSEL1
L1 = (1<<21) ; //Select AOUT function for P0.26
while (1)
{
LPC_DAC -> DACR = (1024 << 6); // 10bit = 1024 Vmax = 3.3 V
for (i = 120000; i > 1; i--); // maintain this value for some delay
Step
Stepper Moto
Motor Confi
Configur
gura
ation:
ion:
A ste
stepper motor is a special type of electric motor that moves in increments, or steps, rather than turning
smoothly as a conventional motor does. Typical increments are 0.9 or 1.8 degrees, with 400 or 200 increments
thus representing a full circle. The speed of the motor is determined by the time delay between each incremental
movement.
U8 is a Driver Buffer (ULN2003) device connected to LPC-1768 Device and can be used for driving Stepper
Motor. On
Motor. On the LPC-1768, P1.22 to P1.25 is used to generate the pulse sequence required to run the stepper Motor.
Also, the Stepper Motor is powered by its own power supply pin (COM), which is connected to a 12V supply.
Table shows connections for stepper Motor.
Page 33
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
A 39 P1.22
B 37 P1.23
C 38 P1.24
D 39 P1.25
0 0 1 0
0 1 0 0
1 0 0 0
#include <LPC17xx.H>
while (1)
{
k = 0x02000000; //initially P.25 =1 [ 1000 ]
for(z=0; z<4; z++) // four coils to be made high one by one
{
LPC_GPIO1->FIOPIN
LPC_GPIO1->FIOPIN =k; //Port1 pins assigned with Ka value
for ( i=120000; i >0 ; i-- ) ; // Delay , value of i decides speed of rotation.
k = k>>1; // k content rotate right 1 time. [0100] [0010] [0001]
}
}
}
Page 34
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
#include <LPC17xx.H>
while (1)
{
k = 0x00400000; // initialize p1.22 as high [0001]
for(z=0; z<4; z++) // executes 4 times
{
LPC_GPIO1->FIOPIN
LPC_GPIO1->FIOPIN =k; //port 1 pins assigned with k value
for (i=120000 ; i >0 ; i--) ; //delay
k = k<<1; // rotate left shift by 1 [0010] , [0100], [1000]
}
}
}
#include <lpc17xx.h>
int main()
{
LPC_GPIO2->FIODIR
LPC_GPIO2->FIOD IR = (1<<8); //Configure
//Confi gure the PORT2 pins as OUTPUT which enable DC motor
LPC_GPIO4->FIODIR
LPC_GPIO4->FIODIR = (1<<28) | (1<<29); //configure port4 pins p4.28,p4.29 as output by making 1
while(1)
{
LPC_GPIO2->FIOSET =(1<<8);
LPC_GPIO2->FIOSET=(1<<8); //enable DC motor by making P2.8=1
LPC_GPIO4->FIOSET
LPC_GPIO4->FIOSET = (1<<28); // P4.28 =1
LPC_GPIO4->FIOCLR
LPC_GPIO4->FIOCLR = (1<<29); // P4.29 = 0 DC motor Anti Clockwise rotation
}
}
b. Clockwise Direction
#include <lpc17xx.h>
int main()
{
LPC_GPIO2->FIODIR
LPC_GPIO2->FIOD IR = (1<<8); //Configure
//Confi gure the PORT2 pins as OUTPUT
LPC_GPIO4->FIODIR
LPC_GPIO4->FIODIR = (1<<28) | (1<<29); //configure port4 pins p4.28,p4.29 as Input
while(1)
{
LPC_GPIO2->FIOSET = (1<<8); //enable DC motor by making P2.8=1
LPC_GPIO4->FIOCLR
LPC_GPIO4->FIOCLR = (1<<28); // P4.28 = 0
LPC_GPIO4->FIOSET
LPC_GPIO4->FIOSET = (1<<29); // P4.29 =1 clockwise rotation
}
}
Page 35
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
c. Control DC
DC motor using switches.
switches. Sw 1.21- clockwise, 1.20 –
1.20 – Anticlockwise
Anticlockwise , 1.19- Stop rotation
#include <lpc17xx.h>
int main()
{
LPC_GPIO2->FIODIR
LPC_GPIO2->FIOD IR = (1<<8); //Configure
//Confi gure the PORT2 pins as OUTPUT
LPC_GPIO4->FIODIR
LPC_GPIO4->FIODIR = (1<<28) | (1<<29); //configure port4 pins p4.28,p4.29 as Input
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR= = 00; //config all the pins of Port1 as i/p
while(1)
{
LPC_GPIO2->FIOSET = (1<<8); //enable DC motor by making P2.8=1
if (! ( LPC_GPIO1->
LPC_GPIO1-> FIOPIN & (1<<21) ) )
{
LPC_GPIO4->FIOSET = (1<<28); // P4.28 =1
LPC_GPIO4->FIOCLR
LPC_GPIO4->FIOCLR = (1<<29); // P4.29 = 0 DC motor Clockwise rotation
}
if (! ( LPC_GPIO1->
LPC_GPIO1-> FIOPIN & (1<<20) ) )
{
LPC_GPIO4->FIOCLR
LPC_GPIO4->FIOCLR = (1<<28); // P4.28 = 0
LPC_GPIO4->FIOSET
LPC_GPIO4->FIOSET = (1<<29); // P4.29 =1 Anti-clockwise
Anti-clockwise rotation
}
if (! ( LPC_GPIO1->
LPC_GPIO1-> FIOPIN & (1<<19) ) )
{
LPC_GPIO4->FIOSET = (1<<28); // P4.28 =1
LPC_GPIO4->FIOSET
LPC_GPIO4->FIOSET = (1<<29); // P4.29 = 1 DC motor Stops rotating.
}
}
}
D C Mo
M otor Pi
P i n Confi
Configur
gura
ati on
DC (direct current) motor rotates continuously. It has two terminals positive and negative. Connecting DC
power supply to these terminals rotates motor in one direction and reversing the polarity of the power
supply reverses the direction of rotation.
U9 is L293 driver IC used to drive the dc motor. It has enable lines which is used to switch on the DC
motor. It is connected to P4.28. Logic „1‟ enables the driver and logic „0‟ disables the driver.P4.28 and
P4.29 are used for Motor 1 direction and speed control.
Page 36
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
These are dual H-bridge motor drivers, i.e., by using one IC we can control two DC motors in both clock wise and
counter clockwise directions.
directions. The L293D can provide bidirectional drive currents of up to 60 0mA at voltages from
4.5 V to 36 V.
#include <LPC17xx.H>
unsigned char dat7seg[] = {0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0x
{0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0xcb,0x8,0x9,0xa
cb,0x8,0x9,0xa,0x38,0x9C,0x68,
,0x38,0x9C,0x68,
0x1c,0x1e};
int main (void)
{
unsigned int i, j;
unsigned int count=0; // initially count=0;
LPC_GPIO2->FIODIR
LPC_GPIO2->FIODIR = 0x000000FF; // Seven seg on PORT2 p2.0 to p2.7 are output
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR = 0x3C000000; //Dig control on PORT1 p1.26 to p1.29 are output
while(1)
{
if (count > 0xF) count
count = 0; // if count Greater than F, re initialize to 0
for (i=0; i < 20000; i++) //change to inc/dec speed of count
{
LPC_GPIO2->FIOPIN
LPC_GPIO2->FIOPIN = dat7seg[count]; // & 0x000F];
LPC_GPIO1->FIOSET
LPC_GPIO1->FIOSET = (1<<26); // 26 for first Sev segment , 27 for second
for (j=0; j<1000; j++); //change to inc/dec brightness of display
LPC_GPIO1->FIOCLR = (1<<26);
}
Count++; //count = count+1
}
}
LE D Pin
Pi n Configura
Configurattion
Page 37
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
VTCM3_3board provides eight individual SMD led‟s connected to LPC-1768 LPC-1768 device through
74HC151driver IC. D1 to D8 are connected
connected to general purpose I/O pins on LPC-1768 device as shown
in table(14) When LPC-1768 device drives Logic „1‟ the corresponding LED turns on.
LED D1 D2 D3 D4 D5 D6 D7 D8
LPC-1768
Pin No
LPC-1768
Port No
SE V E N SE G M E NT DI
D I PL AY Pin
P in Config
Configura
urattion:
ion:
D11, D12, D13 and D14 are Common Cathode segments connected to LPC-1768 device so that each
segment is individually controlled by a general purpose I/O pin. When the LPC-1768 device drives logic
„0‟ the corresponding segment turns on. See fig and table for more
more details.
Seven Segment
g f a b p c d e
Data Lines
LPC-1768 Pin
75 74 73 70 69 68 67 66
No
LPC-1768 Port
P2.0 P2.1 P2.2 P2.3 P2.4 P2.5 P2.6 P2.7
No
SE V E N SE G M E NT Sele
Select
ction
ion Pi n Confi
Configurat
guratii ons:
ns:
As VTCM3_3 board comes with 4 digit seven segment unit. Displays connected to the microcontroller
usually occupy a large number of valuable I/O pins, which can be a big problem especially if it i s needed to
display multi digit numbers. The problem is more than obvious if, for example, it is needed to display four
digit numbers (a simple calculation shows that 32 output pins are needed in this case). The solution to this
problem is called MULTIPLEXING.
MULTIPLEXING. This is how an optical illusion based on the same operating principle
as a film camera is made. Only one digit is active at a time, but they change their state so quickly making
impression that all digits of a number are simultaneously active. Each digit can made active using
switching transistors Q1, Q2, Q3 and Q4 and these on switched on and off by selection lines which are in
turn connected to LPC-1768
to LPC-1768 ports.
ports. Table shows
shows the details
details of seven segment
segment selection
selection lines.
Page 38
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Digits to Hex
Display Segments
display code
g f a B p c d e CODE
0 1 0 0 0 1 0 0 0 88
1 1 1 1 0 1 0 1 1 EB
2 0 1 0 0 1 1 0 0 4C
3 0 1 0 0 1 0 0 1 49
4 0 0 1 0 1 0 1 1 2B
5 0 0 0 1 1 0 0 1 19
6 0 0 0 1 1 0 0 0 18
7 1 1 0 0 1 0 1 1 CB
8 0 0 0 0 1 0 0 0 08
9 0 0 0 0 1 0 0 1 09
A 0 0 0 0 1 0 1 0 0A
B 0 0 1 1 1 0 0 0 38
C 1 0 0 1 1 0 0 0 98
Display Encoding:
D 0 1 1 0 1 0 0 0 68
E 0 0 0 1 1 1 0 0 1C
F 0 0 0 1 1 1 1 0 1E
Page 39
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
while(1)
{
if (count > 0xF) count = 0;
for (i=0; i < 20000; i++) //change to inc/dec speed of count
{
LPC_GPIO2->FIOPIN = dat7seg[count];
dat7seg[cou nt]; // get data from array offset by count
LPC_GPIO1->FIOSET = (1<<26)|(1<<27);
(1<<26)|( 1<<27); //first and second 7 segment devices ON
for (j=0;j<1000;j++); //change to inc/dec brightness of display
LPC_GPIO1->FIOCLR = (1<<26) | (1<<27);
}
count++;
}
}
Page 40
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
16 X 2 LC
L C D D i spla
splayy
An LCD display is specifically manufactured to be used with microcontrollers, which means that it cannot
be activated by standard IC circuits. It is used for displaying different messages
messages on a miniature liquid
crystal display. It displays all the letters of alphabet, Greek letters, punctuation marks, mathematical
symbols etc. In addition, it is possible to display symbols made up by the user. Other useful features
include automatic message shift (left and right), cursor appearance, LED backlight etc.
There are pins along one side of a small printed board. These are used for connecting to the
microcontroller. There are in total of 14 pins marked with numbers (16 if it has backlight). Their function is
described in the table below:
L CD Pi n Confi
Configur
gura
ati on
LPC- LPC-
LCD
LCD Pin 1768 1768
Pin
Functions Pin Port
No
No No
1 GND - -
2 VCC - -
3 - -
4 RS 24 P2.28
5 R/W GND GND
6 EN 27 P2.27
7 DAT1 75 P2.0
8 DAT2 74 P2.1
9 DAT3 73 P2.2
10 DAT4 70 P2.3
11 DAT5 69 P2.4
12 DAT6 68 P2.5
13 DAT7 67 P2.6
14 DAT8 66 P2.7
15 VCC - -
16 GND - -
Page 41
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
10.) Interface LCD with 1768 and write a Program to display Message on LCD screen.
#include <LPC17xx.h>
#define LCD_RS (1<<28)
#define LCD_EN (1<<27)
void LCD_Command(unsigned
LCD_Command(unsigned char cmd)
{
LPC_GPIO2->FIOPIN = cmd ;
LPC_GPIO0->FIOCLR
LPC_GPIO0->FIOCL R |= LCD_RS; // RS=0
LPC_GPIO0->FIOSET
LPC_GPIO0->FIOS ET |= LCD_EN; // EN=1
for(int k=0;k<100000;k++);
k=0;k<100000;k++); // delay
LPC_GPIO0->FIOCLR
LPC_GPIO0->FIOCL R |= LCD_EN; //EN=0
}
void LCD_Init(void)
{
LPC_GPIO0->FIODIR
LPC_GPIO0->FIOD IR |= (LCD_RS | LCD_EN); // EN and Read Select Pins are configured as o/p
LPC_GPIO2->FIODIR0
LPC_GPIO2->FIODIR0 = 0x00FF; // Config P2.0 to P2.7 as o/p pins
LCD_Command(0x38);
LCD_Command(0x38); // 8 bits, 2 lines, 5x7 Dots
LCD_Command(0x01);
LCD_Command(0x01); // Clear Display screen
LCD_Command(0x0e);
LCD_Command(0x0e); // Display on & cursor On
}
int main()
{
unsigned char str[]={"Acharya
str[]={"Acharya "};
unsigned int x=0; // initialize x=0
LCD_Init();
LCD_Command(0x80);
LCD_Command(0x80); // Force cursor to beginging of I row, [ 0xc0 for II row]
while(str[x] != '\0') // if content of str in not a null char
{
LCD_Data(str[x]);
LCD_Data(str[x]); // send to LCD as Data
x++; //increment x
}
}
Page 42
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
#include <LPC17xx.H>
#include "lcd.h"
init_lcd();
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR = (1<<14 | 1<<15 | 1<<16 | 1<<17); //Set COLs as Outputs
lcd_putstring16(0,"Press
lcd_putstring16(0,"Press HEX_KeyBoard..");
HEX_KeyBoard..");
lcd_putstring16(1,"Key
lcd_putstring16(1,"Key Pressed = ");
while (1)
{
key = 0;
for( i = 0; i < 4; i++ )
{
LPC_GPIO1->FIOCLR |= (1<<14 | 1<<15 | 1<<16 | 1<<17);
col_write(rval[i]);
col_write(rval[i]); // turn on COL output one by one
if (!(LPC_GPIO1->FIOPIN
(!(LPC_GPIO1->FIOPIN & 1<<21)) // read rows - break when key press detected
break;
key++;
if (!(LPC_GPIO1->FIOPIN
(!(LPC_GPIO1->FIOPIN & 1<<20))
break;
key++;
if (!(LPC_GPIO1->FIOPIN
(!(LPC_GPIO1->FIOPIN & 1<<19))
break;
key++;
if (!(LPC_GPI
( !(LPC_GPIO1->FIOPIN
O1->FIOPIN & 1<<18))
break;
key++;
}
if (key == 0x10) // if no key pressed key++ ncrements
ncrements upto 16 i.e 0x10
lcd_putstring16(1,"Key
lcd_putstring16(1,"Key Pressed = ");
else
{
lcd_gotoxy(1,14);
lcd_putchar(keyPadMatrix[key]);
}
}
}
Page 43
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
The hex keypad is a peripheral that is organized in rows and Columns. Hex key Pad 16 Keys arranged in a 4 by 4
grid, labeled with the hexadecimal digits 0 to F. An example of this can been seen in Figure 1, below. Internally,
the structure of the hex keypad is very simple. Wires run in vertical columns (we call them C0 to C3) and in
horizontal rows (called R0 to R3). These 8 wires are available externally, and will be connected to the lower 8 bits
of the port. Each key on the keypad is essentially a switch that connects a row wire to a column wire. When a key is
pressed, it makes an electrical connection between the row and column. Table shows connections for HEX KEY
Pad matrix.
ROW/COLOUMNS R1 R2 R3 R4 C1 C2 C3 C4
LPC-1768 Pin No 32 33 34 35 89 88 87 86
LPC-1768 Port No P1.18 P1.19 P1.20 P1.21 P1.14 P1.15 P1.16 P1.17
Page 44
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
#define ADC_DONE
ADC_DONE 0x80000000
#define ADC_OVERRUN
ADC_OVERRUN 0x40000000
#define ADC_CLK
ADC_CLK 1000000 /* set to 1Mhz */
LPC_PINCON->PINSEL1
LPC_PINCON->PINSEL 1 |= 0x00004000;
0x00004 000; // config P0.23 work as ADO[0] or make 1<<14;
1<< 14;
LPC_ADC->ADCR
LPC_ADC->ADCR = ( 1 << 0 ) | /* select channel ; Here 0th bit is 1 ie channel 0 is selected*/
(14 << 8 ) | /* CLKDIV = Fpclk / ADC_Clk - 1 * CLKDIV starts from 8 bit
( 1 << 21 ) ; /* PDN = 1, normal operation */
return ;
}
int ADCRead( )
{
int regVal, ADC_Data;
LPC_ADC->ADCR &= 0xFFFFFF00;
0xFFFFFF00 ; // clear data
LPC_ADC->ADCR
LPC_ADC->ADCR |= (1 << 24) | 1; /* switch channel,start
channel,start A/D convert */
ADCInit( ADC_CLK );
init_lcd();
lcd_putstring16(0,"ADC
lcd_putstring16(0,"ADC Val
Val = 000 ");
lcd_putstring16(1,"VOLTAGE
lcd_putstring16(1,"VOLTAGE = 0.00V ");
while(1)
Page 45
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
{
ADCValue = ADCRead(); // read ADC value
sprintf (lstr, "%4u", ADCValue);
ADCValue);
lcd_gotoxy(0,10); //goto 1st line, 10 char place
lcd_putstring(lstr); //print
Page 46
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
LPC1768 has an inbuilt 12 bit Successive Approximation ADC which is multiplexed among 8 input pins.
The ADC reference voltage is measured across VREFN to VREFP, meaning it can do the conversion within this
range. Usually the VREFP is connected to VDD and VREFN is connected to GND.
As LPC1768 works on 3.3 volts, this will be the ADC reference voltage.
12
Now the resolution of ADC = 3.3/ 2 = 3.3/4096 =0.000805 = 0.8mV
The ADC pin can be enabled by configuring the corresponding PINSEL register to select ADC function.
When the ADC function is selected for that pin in the Pin Select register, other Digital signals are disconnected
from the ADC input pins.
ADC Registers
The below table shows the registers associated with LPC1768 ADC.
We are going to focus
f ocus only on ADCR and ADGDR as these are sufficient for simple A/D conversion.
Register Description
ADGDR A/D Global Data Register: This register contains the ADC‟s DONE bit and the result of the
Page 47
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
ADDR0 - ADDR7 A/D Channel Data Register: Contains the recent ADC value for
f or respective channel
ADSTAT A/D Status Register: Contains DONE & OVERRUN flag for all the ADC channels
Bit 7:0 –
7:0 – SEL
SEL : Channel Select
These bits are used to select a particular channel for ADC conversion. One bit is allotted for each channel. Setting
the Bit-0 will make the ADC to sample AD0[0] for conversion. Similary setting bit-7 will do the conversion for
AD0[7].
Bit 15:8 –
15:8 – CLCKDIV
CLCKDIV : Clock Divisor
The APB clock (PCLK_ADC0) is divided by (this value plus one) to produce the clock for the A/D converter,
which should be less than or equal to 13 MHz.
Bit 16 –
16 – BURST
BURST
This bit is used for BURST conversion. If this bit is set the ADC module will do the conversion for all the channels
that are selected(SET) in SEL bits.
CLearing this bit will disable the BURST conversion.
Bit 21 –
21 – PDN
PDN : Power Down Mode
Setting this bit brings ADC out of power down mode and makes
makes it operational.
Clearing this bit will power down the ADC.
Bit 24:26 –
24:26 – START
START
When the BURST bit is 0, these bits control whether and when an A/D conversion is started:
000 - Conversion Stopped
001- Start Conversion Now
The remaining cases (010 to 111) are about starting conversion on occurrence of edge on a particular CAP or MAT
pin.
Bit 27 - EDGE
This bit is significant only when the START field contains 010-111. It starts conversion on selected CAP or MAT
input.
0 - On Falling Edge
Page 48
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
1 - On Rising Edge
Bit 27 - OVERRUN
This bit is set during the BURST mode where the previous conversion data is overwritten by the new A/D
conversion value.
Bit 31 - DONE
This bit is set to 1 when an A/D conversion completes . It is cleared when this register is read and when the
ADCR is written. If the ADCR is written while a conversion is still in progress, this bit is set and a new conversion
is started.
Temperature Sensor
12. Measurement of ambient temperature using a sensor and SPI ADC IC.
#include <LPC17xx.H>
#include <stdio.h>
#include "lcd.h"
#include "spi.h"
SPI_Init();
init_lcd();
lcd_putstring16(0,"TEMP
lcd_putstring16(0,"TEMP = 000 Deg_C");
Page 49
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
while(1)
{
SPI_EnableChipSelect();
delay(1);
SPI_Write(0x01);
msb = SPI_Write(0xA0);
SPI_Write(0xA0); // SGL=1, ODD=0, MSBF=1
lsb = SPI_Write(0x00);
SPI_Write(0x00);
SPI_DisableChipSelect();
msb &= 0x0F;
ADCValue = (msb << 8) | lsb;
delay(500);
}
}
Spi.c
#include <lpc17xx.h>
#include "spi.h"
uint8_t dummy_u8;
static uint32_t spi_GetPclk(void);
void SPI_Init(void)
{
LPC_PINCON->PINSEL0 |= 0xC0000000;
0xC0000 000; //SCK
LPC_PINCON->PINSEL1 |= 0x0000003C;
0x00000 03C; //SSEL, MISO, MOSI
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR |= 0x00058000; //SSEL, MOSI, SCK are Outputs 15,16,18
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR &= ~0x00020000; //MISO is Input 17
SPI_DisableChipSelect();
SPI_DisableChipSelect(); // Disable the Slave Select
LPC_SC->PCONP
LPC_SC->PCONP |= (1 << 8); // enable power to spi clock
LPC_SPI->SPCCR
LPC_SPI->SPCCR = spi_GetPclk()/SCK_Freq;
spi_GetPclk()/SCK_Freq; // Set Spi Clock
dummy_u8 = LPC_SPI->SPSR;
LPC_SPI->SPSR ; /* Dummy read to clear the flags */
dummy_u8 = LPC_SPI->SPDR; /* Dummy read to clear the flags */
}
Page 50
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Vref =3.3V
LM35
1 2 3
5V
ADC1
75E
LPC1768
1µf
Page 51
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Page 52
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
2. R Profile (ARMv7-R) - is designed for high end end embedded systems in which
which real time performance is
needed. Example applications include high-end car braking systems.
3. M-Profile
M-Profile (ARMv7-M) – is designed for deeply embedded microcontroller type systems. Processors
belonging to this profile are the subject for this manual and are studied in greater detail.
Registers
• The Cortex-M3
Cor tex-M3 has registers R0 through R15. R13 (the stack pointer) is banked, with only one copy of the
R13 visible at a time.
• R0 – R12:
R12: General Purpose Registers.
Registers. These are 32 bit registers for data operations. Some 16-bit Thumb
instructions can only access a subset of these registers (low registers R0-R7).
• R13:
R13: Stack Pointers.
Pointers. Contains two stack pointers. They are banked so that only one is visible
visible at a time.
Main Stack Pointer (MSP) – The main stack pointer used by the Operating system and
exception handlers.
Process Stack Pointer (PSP) –
(PSP) – used
used by the application code.
• R14:
R14: Link Register.
Register. When a subroutine is called, the return address is stored in the link register.
• R15:
R15: Program Counter.
Counter. The program counter is the current program address.
• The Cortex-M3
Cortex -M3 also has a number of special registers. They are
Page 53
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
Page 54
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in