Embedded Controller Lab Manual 15ECL67
ACHARYA INSTITUTE OF TECHNOLOGY
.
Dept of Electronics & Communication Engineering
Embedded Controller Lab Manual
[15ECL67] VI- SEMESTER
ECE
CBCS Scheme
Raghunath.B.H
Page 1 Dept of ECE, Acharya Institute of Technology
[email protected]
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 bit binary numbers. numbers. 2. ALP to find the sum of first 10 integer integer numbers.
PART-B: Conduct the following experiments experiments on an ARM CORTEX M3 evaluation board using evaluation version of Embedded 'C' & Keil Uvision-4 tool/compiler.
1. Display “Hello World” message using Internal UAR T. T. 2. Interface and Control a DC Motor. 3. Interface a Stepper Stepper motor and rotate it it in clockwise and anti-clockwise anti-clockwise direction. 4. Determine Digital output for a given given Analog input using using Internal ADC of ARM controller. 5. Interface a DAC and generate Triangular and Square waveforms. 6. Interface a 4x4 keyboard and display the key code on an LCD. 7. Using the Internal PWM PWM module of ARM controller controller generate PWM and vary its duty cycle. 8. Demonstrate the use of an external interrupt t o toggle an LED On/Off. 9. Display the Hex digits 0 to F on a 7-segment 7-segment LED interface, with with an appropriate delay delay in between. 10. Interface a simple Switch and display its status through Relay, Buzzer
and LED
11. Measure Ambient Ambient temperature using using a sensor and SPI ADC ADC IC. LAB Cycles 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.
Page 2 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
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 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. 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 transmit a single character data from from Host to UART.
ii. To receive a single character character data data from UART UART to Host. iii. To transmit a string of character data data from Host to UART
5. Usage of internal DAC of Cortex Cortex M3 controller i.To generate Square waveform. ii. To generate Triangular waveform. waveform. 6. Interface a Stepper Stepper motor motor to a GPIO of Cortex M3 Controller i.
Rotate stepper motor in clockwise direction
ii. Rotate stepper stepper motor in an anti-clockwise 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: 10. Interface of LCD
module to Cortex M3 controller – controller –
i. To display single line message. message. ii. To display two line messages. messages. 11. Usage of
Internal ADC – ADC –
i. To determine Digital output for a given Analog input and display display digital data on to LCD. 12. Interface
a 4x4 keyboard to Cortex M3 controller and display the key code on a LCD.
13. Measurement Measurement of
ambient temperature using a sensor and SPI ADC IC.
Page 3 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
Keil Micro Vision® 5 IDE tool : Step by step Tutorial Open Keil 5 IDE tool
Step 1. Open the Keil IDE, under main menu goto “Project -> New uVision Project…” 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
[email protected]
Embedded Controller Lab Manual 15ECL67
Step 3. Now, inside the “Manage RunRun-Time Environment Window” select Window” select the check boxes for “CORE” “CORE” under “CMSIS” and “CMSIS” and “Startup” under “Startup” under “Device”. “Device” . If you want to select any other libraries you can do so by selecting the respective checkboxes. Selecting “Startup” will “Startup” will automatically added all the necessary startup/boot files required for LPC17xx device, so we don‟t have to import them from external sources. The selection of libraries can be changed any time later.
Step 4. Now click on “Options for Target” button Target” button as shown below:
Page 5 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
Step 5. Now, click on the “Output” tab. “Output” tab. If you want to generate hex file then you can check “Create HEX File”. File”.
Step 6. Then click on the “Linker” tab “Linker” tab and Under that tab check the checkbox option which says “Use Memory Layout from Target Dialog”. Dialog”.
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
[email protected]
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
[email protected]
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:
Now open the flash flash magic software and follow follow the below steps. 1. 2. 3. 4. 5. 6. 7. 8.
Select the Device as LPC1768 from Select Menu. Select the COM Port. Check the device manager for detected COM port . Select the Baud Baud rate from 9600-115200. Ex: Set as 115200 115200 Select None ISP Option. Oscillator Freq in Mhz . Type 12 . i.e 12Mhz Check the Erase all Flash + Code Rd Prot Browse and Select Select the desired hex file. [project.hex file] Press the Start Button to flash the hex file.
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
[email protected]
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 PINSEL
FIODIR
FIOSET, FIOCLR, FIOPIN
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 first f 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 usi ng two bits of the PINSEL register.
Value
Function
Enumeration
00
Primary (default) function, typically GPIO port
PINSEL_FUNC_0 PINSEL_FUNC_0
01
First alternate function
PINSEL_FUNC_1 PINSEL_FUNC_1
10
Second alternate function
PINSEL_FUNC_2 PINSEL_FUNC_2
11
Third alternate function
PINSEL_FUNC_3 PINSEL_FUNC_3
FIODIR:Fast GPIO Direction Control Register.
This register individually controls the direction of each port pin. Values
Direction
0
Input
1
Output
Page 9 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
FIOSET:Fast Port Output Set Register.
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
FIOCLR:Fast Port Output Clear Register.
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
FIOPIN:Fast Port Pin Value Register
.
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. GPIO Registers in LPC1768
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 datasheet uses a prefix of “FIO” instead of something like “GIO” for all the registers related to GPIO. Lets go through these 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 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
[email protected]
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
P0.15
29
28
27
P0.14
26
25
P0.13
24
23
P0.12
22 22
21
P0.11
20
19
P0.10
18
17
P0.9
16
15
P0.8
14
13
P0.7
12
11
P0.6
10
9
P0.5
8
7
P0.4
6
5
P0.3
4
3
P0.2
2
1
P0.1
0
P0.0
PINSEL1 31
P0.31
30
29
P0.30
28
27
P0.29
26
25
P0.28
24
23
22
P0.27
21
P0.26
20
19
P0.25
18
17
P0.24
16
15
14
P0.23
13
P0.22
12
11
10
P0.21
9
P0.20
8
7
P0.19
6
5
P0.18
4
3
P0.17
2
1
0
P0.16
Example : Port 0.0 pin pin‟s ‟s func function tion is decided decided by contents of PINSEL0 PINSEL0 register bit 0 and bit 1 P0.0 function decided by PINSEL0.1 PINSEL0.0 0 0 0 1 1 0 1 1
P0.0 Function as when when when when
PINSEL0.1= 0 , PINSEL0.0= 0 Then P0.0 Function as General Purpose I/O PINSEL0.1= 0, PINSEL0.0= 1 Then P0.0 Function as RD1 PINSEL0.1= 1, PINSEL0.0= 0 Then P0.0 function as TXD3 PINSEL0.1= 1, PINSEL0.0= 1 Then P0.0 function as SDA1
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
[email protected]
Embedded Controller Lab Manual 15ECL67 Example #1)
To configure Pin 4 of Port 0 i.e P0.4 as Output and want want to make it High(Logic 1)
LPC_GPIO0->FIODIR LPC_GPIO0->FIODIR = (1<< 4); LPC_GPIO0->FIOSET LPC_GPIO0->FIOSET = (1<< 4); Alternate way is LPC_GPIO->FIODIR LPC_GPIO->FIODIR = 0x0000 0010;
// Configure P0.4 as Ouput // Make ouput High for P0.4 // in 32 bit form 0000 0000 0000 0000 0000 0000 0001 0000; 4th is 1
// omitting left side zeros ; we can write as 0x10 ; x stands for Hexadecimal number LPC_GPIO->FIODIR LPC_GPIO->FIODIR = 0x10; LPC_GPIO->FIOSET= LPC_GPIO->FIOSET=0x10; 0x10;
// Configure P0.4 as Ouput //Configure P0.4 as Ouput
Example #2
Making output configured Pin 17 High of Port 1 i.e P1.17 and then Low can be does as follows:
LPC_GPIO1->FIODIR LPC_GPIO1->FIODIR = (1<<17); LPC_GPIO1->FIOSET LPC_GPIO1->FIOSET = (1<<17); LPC_GPIO1->FIOCLR LPC_GPIO1->FIOCLR = (1<<17);
// P1.17 is Output pin // Output for P1.17 becomes High // Output for P1.17 becomes Low
Example #3)
Configuring P0.5 and P0.11 as Ouput and Setting them High:
LPC_GPIO0->FIODIR LPC_GPIO0->FIODIR = (1<<5) | (1<<11); LPC_GPIO0->FIOSET LPC_GPIO0->FIOSET = (1<<5) | (1<<11);
// Config P0.5 and P0.11 as Ouput // Make ouput High for P0.5 and P0.11
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; LPC_GPIO0->FIOSET LPC_GPIO0->FIOSET = 0x000000FF;
// Config P0.0 to P0.7 as Ouput // Make output High for P0.0 to P0.7
Example #5)
Configuring Pins of Port 0 (P0.4 to P0.11) as Ouput and Setting them them High: and Low
LPC_GPIO0->FIODIR LPC_GPIO0->FIODIR = 0xFF0; LPC_GPIO0->FIOSET LPC_GPIO0->FIOSET = 0xFF0; LPC_GPIO0->FIOCLR LPC_GPIO0->FIOCLR = 0xFF0;
// Config P0.0 to P0.7 as Ouput // 1111 1111 0000 Make output High for P0.4 to P0.11 // Make output LOW for P0.4 to P0.11
Example #6)
Configuring Pins of Port 1 (P1.14 to P0.21) as Input : LPC_GPIO1->FIODIR LPC_GPIO1->FIODIR &= ~(0xFF0); LPC_GPIO1->FIODIR LPC_GPIO1->FIODIR &=~(1<<14); &=~(1<<14);
// Config P1.14 to P1.21 as Input // only Pin p1.14 is configured as input
Page 12 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 Note:
On reset by default all registers are zero. So all port‟s all the pins acts as input. setting i/p lines
We can skip
L E D Pin Pi n Configur Configura ati on 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 LPC-1768 device drives Logic „1‟ the corresponding corresponding LED turns on.
LED Pin connection table. LED
D1
D2
D3
D4
D5
D6
D7
D8
LPC-1768 Pin No
81
80
79
78
77
76
48
49
LPC-1768 Port No
P0.4
P0.5
P0.6
P0.7
P0.8
P0.9
P0.10
P0.11
Blink a LED
#include
int main(void) { LPC_GPIO0->FIODIR LPC_GPIO0->FIODIR |= (1<< 4); LPC_GPIO1->FIODIR LPC_GPIO1->FIODIR &= ~ ((1<< 21)) ;
// Configuring P0.4 as Output // explicitly making P0.21 as Input . we can skip this line //by default all port pins are i/p
while(1) { if ( ! (LPC_GPIO0->FIOPIN (LPC_GPIO0->FIOPIN & (1<< 21)) ) { LPC_GPIO0->FIOSET LPC_GPIO0->FIOS ET |= (1<< 4); } else LPC_GPIO0->FIOCLR LPC_GPIO0->FIOC LR = (1<< 4);
//if key P1.21 pressed // SET P0.4 High LED ->ON
// P0.4 low LED-> OFF
} }
Page 13 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
Lab Program # 1. Interfacing of “LED” LED” – 8 8 no‟s no‟s to to a GPIO G PIO of Cortex M3 Controller a.
To blink LEDs
#include int main() { LPC_GPIO0LPC_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=100000; i >0; i- -);
// delay
LPC_GPIO0 - >FIOCLR = 0x0FF0;
//clear P0.4 to P.11 LEDS Turns OFF
for (i=100000;i>0; i--);
//delay
} } 1.b to Scroll LEDs (Ring Counter)
#include unsigned int x, y, z; int main(void) { LPC_GPIO0->FIODIR LPC_GPIO0->FIODIR = 0x0FF0;
// Configure P0.4 to p0.11 as Output
while(1)
//infinite loop
{ z = 0x0800;
// initialize with 1000 0000 0000;
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
} } }
Page 14 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 Lab Program # 2.a Interface a simple Switch and display its status through LED.
#include int main() { LPC_GPIO0->FIODIR LPC_GPIO0->FIODIR = 1<< 4; LPC_GPIO1->FIODIR LPC_GPIO1->FIODIR & = ~ (1<< 21); while (1) { if ( ! ( LPC_GPIO1-> LPC_GPIO1-> FIOPIN & (1<< 21) ) ) { LPC_GPIO0->FIOSET LPC_GPIO0->FIOSET = (1<< 4);
// port P0.4 pin is configured as o/p // port p1.21 pin is configured as i/p // infinite loop // if switch P2.1 is pressed? // if Yes, SET the Port P0.4 pin
} else { LPC_GPIO0->FIOCLR LPC_GPIO0->FIOCLR = (1<< 4);
// Else Clear Port P0.4
} } }
2.b. Interface a simple Switch and display its status through Buzzer .
#include int main() { LPC_GPIO3->FIODIR LPC_GPIO3->FIODIR = 1<< 25; LPC_GPIO1->FIODIR LPC_GPIO1->FIODIR & = ~ (1<< 21); while(1) { if (! (LPC_GPIO1->FIOPIN (LPC_GPIO1->FIOPIN & (1<< 21))) { LPC_GPIO3->FIOSET LPC_GPIO3->FIOSET = (1<< 25);
// port 3.25 pin is configured as o/p // port p1.21 pin is configured as i/p
// if switch P.21 pressed // Port 3.25 pin is set [ High]
} else { LPC_GPIO3->FIOCLR LPC_GPIO3->FIOCLR = (1<< 25);
//
Port 3.25 pin is Clear [Low]
} } }
Page 15 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 2.c. Interface a simple Switch and display its status through Buzzer,Relay and LED.
#include int main() { LPC_GPIO0->FIODIR LPC_GPIO0->FIODIR = (1<<4) | (1<<25); LPC_GPIO3->FIODIR LPC_GPIO3->FIODIR = (1<<25); LPC_GPIO1->FIODIR LPC_GPIO1->FIODIR &= ~(1<<21);
// port P0.4-P0.11[LEDS] P0.4-P0.11[LEDS] & P0.25[Relay] config as o/p // port 3.25[Buzzer] 3.25[Buzzer] configured as o/p // 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
LPC_GPIO0->FIOCLR LPC_GPIO0->FIOCLR = (1 << 4) | (1 << 25); LPC_GPIO3->FIOCLR = (1 << 25);
// turn OFF LED, Relay //turn //tur n OFF Buzzer
} else {
} } }
Applications of Relay
Commonly used in used in switching circuits. For Home Automation projects to switch AC l oads To Control (On/Off) Heavy loads at a pre-determined pre-determined time/condition Used in safety circuits to disconnect the load from supply in event of failure Used in Automobiles electronics for controlling indicators glass motors etc.
Buzzer Symbol
Light Emitting Diode
Page 16 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 LPC1768: External Interrupts EINTx Pins
LPC1768 has four external interrupts EINT0, EINT1, EINT2, EINT3. As LPC1768 pins are multi functional, these four interrupts are available on multiple pins. Below table shows mapping of EINTx pins.
EINT RegistersThe registers associated with LPC1768 external interrupts. Register
Description
PINSELx
To configure the pins as External Interrupts
EXTINT
External Interrupt Flag Register contains interrupt flags for EINT0,EINT1, EINT0,EINT1, EINT2 & EINT3.
EXTMODE
External Interrupt Mode register(Level/Edge register( Level/Edge Triggered)
EXTPOLAR EXTPOLAR External Interrupt Interrupt Polarity(Falling/Rising Polarity(Falling/Rising Edge, Edge, Active Active Low/High) Low/High)
Page 17 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 EXTINT
31:4
3
2
1
0
RESERVED
EINT3
EINT2
EINT1
EINT0
EINTx: Bits will be set whenever the interrupt is detected 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
31:4
3
2
1
0
RESERVED
EXTMODE3
EXTMODE2
EXTMODE1
EXTMODE0
EXTMODEx: This bits is used to select whether the EINTx EINTx pin is level or edge Triggered 0: EINTx is Level Triggered. 1: EINTx is Edge Triggered. EXTPOLAR
31:4 RESERVED
3 EXTPOLAR3
2
1
EXTPOLAR2
EXTPOLAR1
0 EXTPOLAR0
EXTPOLARx: This bits is used to select polarity(LOW/HIGH, polarity(LOW/HIGH, FALLING/RISING) FALLING/RISING) of the EINTx interrupt depending on the EXTMODE register.
0: EINTx is Active Low or Falling Edge (depending on EXTMODEx). 1: EINTx is Active High or Rising Edge (depending on EXTMODEx). Steps to Configure Interrupts 1. 2. 3. 4. 5.
Configure the pins as external interrupts in PINSELx PINSELx register. Clear any pending interrupts in EXTINT. Configure the EINTx as Edge/Level Edge/Level triggered in EXTMODE EXTMODE register. Select the polarity(Falling/Rising Edge, Active Low/High) of the interrupt in EXTPOLAR EXTPOLAR register. Finally enable the i nterrputs by calling NVIC_EnableIRQ() with IRQ number.
3. a.Demonstrate the use of an external interrupt EINT0 to toggle an LED -ON/OFF.
#include void EINT0_IRQHandler(void) EINT0_IRQHandler(void) { LPC_SC->EXTINT LPC_SC->EXTINT = 1; LPC_GPIO0->FIOPIN LPC_GPIO0->FIOP IN ^ = (1<<4);
/* Clear Interrupt Flag ext int 0 */ /* Toggle the LED1 everytime INTR0 is generated */
} Page 18 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
int main( ) { LPC_SC->EXTINT LPC_SC->EXTINT = 1;
/* Clear Pending interrupts */
LPC_PINCON->PINSEL4 = (1<< 20);
/* Configure Configur e P2_10 as EINT0 */
LPC_SC->EXTMODE
=1;
/* Configure Configur e EINTx as Edge Triggered*/
LPC_SC->EXTPOLAR
= 1;
/* Configure Configur e EINTx as Falling Edge */
LPC_GPIO0->FIODIR LPC_GPIO0->FIODIR = (1<<4);
/*configure LED pins as OUTPUT */
LPC_GPIO0->FIOPIN LPC_GPIO0->FIOP IN = 0x00; NVIC_EnableIRQ(EIN NVIC_EnableIRQ(EINT0_IRQn); T0_IRQn);
/* Enable the EINT0 EINT0 interrupts */
while(1) {
// any main program running
} }
3. b.Demonstrate the use of an external interrupt EINT1 to toggle an LED On/Off
#include 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->FIOP IN ^ = (1<<11) ;
/* Toggle the LED1 everytime INTR1 is generated */
} int main( ) { LPC_SC->EXTINT LPC_SC->EXTINT = 2;
/* Clear Pending interrupts */
LPC_PINCON->PINSEL4 = (1<< 22);
/* Configure Configur e P2_10 as EINT01*/
LPC_SC->EXTMODE
=1;
/* Configure Configur e EINTx as Edge Triggered*/
LPC_SC->EXTPOLAR
= 1;
/* Configure Configur e EINTx as Falling Edge */
LPC_GPIO0->FIODIR LPC_GPIO0->FIOD IR = (1<<11);
/*configure /*confi gure LED pins as OUTPUT */
LPC_GPIO0->FIOPIN LPC_GPIO0->FIOP IN = 0x00; NVIC_EnableIRQ(EINT NVIC_EnableIRQ(EINT1_IRQn); 1_IRQn);
/* Enable the EINT0 interrupts */ */
while(1) {
// any main program running
} }
Page 19 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
3. c .Demonstrate the use of an external interrupts EINT0 & EINT1to toggle an LED On/Off
#include 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->FIOP IN ^ = (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_SC->EXTMODE
=1;
/* Configure Configur e EINTx as Edge Triggered*/
LPC_SC->EXTPOLAR
= 1;
/* Configure Configur e EINTx as Falling Edge */
LPC_GPIO0->FIODIR LPC_GPIO0->FIODIR = (1<< 4) | (1<< 11);
/*configure LED pins as OUTPUT */
LPC_GPIO0->FIOPIN LPC_GPIO0->FIOP IN = 0x00; 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 20 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 UART module
UART module and registers. LPC1768 has 4-UARTs numbering 0-3, similarly the pins are also named as RXD0RXD3 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
RBR
Contains the recently received Data
THR
Contains the data to be transmitted
FCR
FIFO Control Register
LCR
Controls the UART frame formatting(Number 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.
UART Register Configuration
Now lets see how to configure the individual registers registers for UART communication. communication. FCR ( FIFO Control Register )
LPC1768 has inbuilt 16byte FIFO for Receiver/Transmitter. Receiver/Transmitter. Thus it can store 16-bytes of data received on UART without overwriting. If the data is not read before the Queue(FIFO) Queue(FIFO) is filled then the new data will be lost and the OVERRUN error bit will be set. 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. 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. 0--No impact. 1--CLears the 16-byte Rx FIFO and the resets t he FIFO pointer.
Page 21 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 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. 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)
LCR ( Line Control Register )
This register is used for defining the UART frame format ie. Number of Data bits, STOP bits etc. LCR 31:8 Reserved Set as->
7
6
DLAB
Break Control
1
5:4 Parity Select
0
00
3 Parity Enable
2 Stop Bit Select
0
0
1:0 Word Length Select 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 Bit 6 – 6 – Break Break Control 0-- Disable break transmission . 1-- Enable break transmission. Output pin UARTn TXD is f orced to logic 0
Page 22 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 Bit 8 – 8 – DLAB: DLAB: Divisor Latch Access Bit This bit is used to enable the access access to divisor latch. 0-- Disable access to divisor latch 1-- Enable access to divisor latch
LSR (Line Status Register) The is a read-only register that provides status information of the UART TX and RX blocks. LSR 31:8 Reserved
7
6
5
4
3
2
RXFE
TEMT
THRE
BI
FE
PE
1
0
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 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 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. 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.
Page 23 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 TER (Transmitter Enable register)
This register is used to Enable/Disable the transmission TER 31:8
7
6-0
Reserved
TXEN
Reserved
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 t hat 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.
Note: By default this bit will be set after after Reset.
Baudrate Calculation
LPC1768 generates the baud rate depending on the values of DLM,DLL. Baudrate = PCLK/ (16 * ( 256 * DLM + DLL) * (1+ DivAddVal/MulVal)) Getting the PCLK value. PCLKSELx registers registers contains the PCLK info for all the clock dependent peripherals in which Bit6,Bit7 contains the Uart Clock(ie.UART_PCLK) Clock(ie.UART_PCLK) information. The UART_PCLK and the actual Peripheral Clock(PCLK) is calculated as below. (Refer data sheet for more info) UART_PCLK
PCLK
0
SystemFreq/4
1
SystemFreq
2
SystemFreq/2
3
SystemFreq/8
DivAddVal/MulVal DivAddVal/MulVal == 0 Using the above parameters , DLL/DLM is calculated as below. (256 * DLL + DLM) = PCLK / (16* Baudrate).
S
teps for Configuring UART0
Page 24 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
Below are the steps for configuring the UART0.
1. 2. 3. 4. 5. 6. 7.
Step1: Configure the GPIO pin for UART0 function using PINSEL register. Step2: Configure the FCR for enabling the FIXO and Reste both the Rx/Tx FIFO. Step3: Configure LCR for 8-data bits, 1 Stop bit, Disable Parity and Enable DLAB. Step4: Get the PCLK from PCLKSELx register 7-6 bits. Step5: Calculate the DLM,DLL vaues for required baudrate from PCLK. Step6: Updtae the DLM,DLL DLM,DLL with the calculated values. 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 25 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 4 .Usage of Internal I nternal 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 a string of character data from Host to UART
#include "LPC17xx.h" void UART0_Init (void) { LPC_PINCON->PINSE L0 |= (1 << 4)| (1 << 6); LPC_PINCON->PINSEL0 LPC_UART0->LCR LPC_UART0->LCR = 0x83; LPC_UART0->DLM = 0; LPC_UART0->DLL = 9; LPC_UART0->LCR LPC_UART0->LCR = 0x03; LPC_UART0->FDR = 0x21;
/* Pin P0.2 used as TXD0 & Pin P0.3 used as RXD0 / /* 8 bits, no polarity, 1stop bit */ // 115200 baus rate @25MHz clk /* Lock the baud rate */ //MULVAL=2,DIVADDval=1 //MULVAL=2,DIVADDva l=1
}
int UART0_SendChar (int Data) { while (!(LPC_UART0->LSR (!(LPC_UART0->LSR & 0x20)); return (LPC_UART0->THR (LPC_UART0->THR = Data);
//if LSR.5th bit =0 wait for valid data. data. th // LSR.5 bit =1 ,THR has valid data to be transmitted
}
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 }
void UART0_SendString (unsigned char *s) { while (*s != 0) UART0_SendChar(*s++); UART0_SendChar(*s++); } int main(void) { char ch; SystemInit( ); UART0_Init( ); UART0_SendString("hello UART0_SendString("hello world! "); while(1){ ch=UART0_GetChar( ch=UART0_GetChar( ); UART0_SendChar(ch); UART0_SendChar(ch); } }
// if content of string is not 0 //send char and auto increment increment i.e
s=s+1
//initialize UART0 //string=hello world! // read character character into ch. //send to uart terminal
Page 26 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
LPC1768 PWM Module
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 PINSEL Register
Corresponding Match Register
0-GPIO, 1-PWM1[1], 2-TXD1, 3-
0,1 bits of PINSEL4
MR1
P2.1
0-GPIO, 1-PWM1[2], 2-RXD1, 3-
2,3 bits of PINSEL4
MR2
PWM_3
P2.2
0-GPIO, 1-PWM1[3], 2-CTS1, 3TRACEDATA[3]
4,5 bits of PINSEL4
MR3
PWM_4
P2.3
0-GPIO, 1-PWM1[4], 2-DCD1, 3TRACEDATA[2]
6,7 bits of PINSEL4
MR4
PWM_5
P2.4
0-GPIO, 1-PWM1[5], 2-DSR1, 3TRACEDATA[1]
8,9 bits of PINSEL4
MR5
PWM_6
P2.5
0-GPIO, 1-PWM1[6], 2-DTR1, 3TRACEDATA[0]
10,11 bits of PINSEL4
MR6
PWM Channel
Port Pin
PWM_1
P2.0
PWM_2
Pin Functions
LPC7168 PWM Registers
The below table shows the registers associated with LPC1768 PWM. Register
Description
IR
Interrupt Register: The IR can be read to identify which of eight possible interrupt sources are pending. 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).
TC
Timer Counter: The 32-bit TC is incremented incremented every PR+1 cycles of PCLK. The TC is controlled through the TCR.
PR
Prescalar Prescalar Register: This is used to specify the Prescalar Prescalar value for incrementing the TC.
PC
Prescale Counter: The 32-bit PC is a counter which is incremented to the value stored in PR. When the value in PR is reached, the TC is incremented.
MCR
Match Control Register: The MCR is used to control the reseting of TC and generating of interrupt whenever a Match occurs.
Page 27 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
MR0
Match Register: This register hold the max cycle Time(Ton+Toff).
MR1MR6
Match Registers: These registers holds the Match value(PWM value(PWM Duty) for corresponding PWM channels(PWM1-PWM6).
PCR
PWM Control Register: PWM Control Register. Enables PWM outputs and selects PWM channel t ypes as either single edge or double edge controlled.
LER
Load Enable Register: Enables use of new PWM values once the match occurs.
Register Configuration
The below table shows the registers associated with LPC1768 PWM. TCR
31:4
3
2
1
0
Reserved
PWM Enable
Reserved
Counter Reset
Counter Enable
Bit 0 – 0 – Counter Counter Enable This bit is used to Enable or Disable the PWM Timer and PWM Prescalar 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 Counter values. 0- Do not Clear. 1- The PWM Timer Counter and the PWM Prescale Counter are 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
MCR
31:21 Reserved
20 PWMMR6S
19 PWMMR6R
18 PWMMR6I
-
5
- PWMMR1S
4 PWMMR1R
3 PWMMR1I
2 PWMMR0 S PWMMR0S
1 PWMMR0R PWMM R0R
0 PWMMR0I P WMMR0I
PWMMRxI This bit is used to Enable or Disable the PWM interrupts i nterrupts when the PWMTC matches 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) Page 28 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
0- Do not Clear. 1- Reset the PWMTC counter value whenever it matches PWMRx. PWMMRxS This bit is used to t o Stop the t he 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
31:15
14-9
8-7
6-2
1-0
Unused
PWMENA6PWMENA1
Unused
PWMSEL6PWMSEL2
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 This bit is used to enable/disable the PWM output for PWMx(x:1-6) 0- PWMx Disable. 1- PWMx Enabled.
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 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.
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:
Reset the TC value. Pull the PWM pin High. Loads the new Match register values.
Page 29 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 Steps to Configure PWM
1. 2. 3. 4. 5. 6. 7. 8.
Configure the GPIO pins for PWM operation in respective PINSEL PINSEL register. Configure TCR to enable the Counter for incrementing the TC, and Enable the PWM block. Set the required pre-scalar value value in PR. In our case it will be zero. Configure MCR to reset the TC whenever it matches MR0. Update the Cycle time in MR0. In our case it will be 100. Load the Duty cycles for required PWMx channels in respective match registers MRx(x: 1-6). Enable the bits in LER register to load and latch the t he new match values. Enable the required pwm channels in PCR register.
//Program //Progr am PWM Demo on Scope 5.
// Put Scope in DC Mode
Usage of Internal PWM to generate PWM and and vary its duty cycle
#include int main(void) { int j,DutyCycle; LPC_PINCON->PINSEL7 LPC_PINCON->PINSE L7 = 3 << 20; LPC_PWM1->TCR = (1<< 0) | (1<< 2); LPC_PWM1->MCR LPC_PWM1->MCR = (1<< 1); LPC_PWM1->MR0 = 100; LPC_PWM1->PCR LPC_PWM1->PCR = 1 << 11;
/* Cofigure pin P3.26 for PWM mode. */ //enable counter and PWM /*Reset on PWMMR0, reset TC if it matches MR0 */ /* set PWM cycle(Ton+Toff)=100) cycle(Ton+Toff) =100) */ /* 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
[email protected]
Embedded Controller Lab Manual 15ECL67
LPC1768 DAC Programming Tutorial
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: VAOUT = VALUE * (VREFP-VREFN)/ 1024 When we have V REFN = 0, the t he equation boils down to: VAOUT = ( VALUE * VREFP)/1024 Where VALUE is the 10-bit digital value which is to be converted into its Analog counterpart and VREF is the input reference voltage.
Page 31 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
Pi ns rela relating ting to L PC PC17 1768 68 D A C blo lock: ck: 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.
VREFP, VREFN
These are reference voltage input pins used for both ADC and DAC. VREFP is positive reference voltage and VREFN is negative reference voltage pin. In example shown below we will use VREFN=0V(GND).
VDDA, VSSA
VDDA is Analog Power pin and VSSA is Ground pin used to power the ADC module. These These are generally same as V CC and GND but with additional filtering to reduce noise.
DAC Registers in ARM Cortex-M3 LPC176x
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. Bit[15:6] – VALUE VALUE starts from bit 6 [ In programming 2. Bit[15:6] – programming we use Value <<6; ] Bit[16] – BIAS: BIAS: Setting this bit to 0 selects settling time of 1us max with max current consumption 3. Bit[16] – 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 32 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 6a.Interface a DAC and generate Triangular and Square waveforms.
#include PINSE LPC_PINCON->PINSEL1 L1 = (1<< 21);
//Select AOUT 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--); } } }
6.b. Interface a DAC and generate Square waveforms.
#include int main (void) { int i; LPC_PINCON->PINSE LPC_PINCON->PINSEL1 L1 = (1<<21) ;
//Select AOUT function for P0.26
while (1) { LPC_DAC -> DACR = (1024 << 6); for (i = 120000; i > 1; i--);
// 10bit = 1024 Vmax = 3.3 V // maintain this value for some delay
LPC_DAC -> DACR = (512 << 6); for (i = 120000; i > 1; i--);
// for Vmax/2 = 1.74V // delay
} }
Page 33 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
STEPPPER MOTOR INTERFACE
Step Stepper Moto Motor Confi Configur gura ation: ion: stepper motor is a special type of electric motor that moves in increments, or steps, rather than turning A ste 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 sequence required to run the stepper Motor. Motor. On Motor. On the LPC-1768, P1.22 to P1.25 is used to generate the pulse sequence 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. Stepper Motor Pin connection table. Stepper Motor Coil
LPC-1768 Pin No
LPC-1768 Port No
A
39
P1.22
B C
37 38
P1.23 P1.24
D
39
P1.25
P1.25 1 0 0
P1.24 0 1 0
P1.23 0 0 1
P1.22 0 0 0
0
0
0
1
P1.25
P1.24
P1.23
P1.22
0
0
0
1
0
0
1
0
0
1
0
0
1
0
0
0
Stepper motor rotates Clockwise
Stepper motor rotates Anti Clockwise
Page 34 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 7.a. Interface a Stepper Stepper motor and rotate it in clockwise direction.
#include int main (void) { unsigned int k, z; LPC_GPIO1->FIODIR LPC_GPIO1->FIODIR = (1<< 22)|(1<< 23)|(1<< 24)|(1<< 25); while (1) { k = 0x02000000; for(z=0; z<4; z++) { LPC_GPIO1->FIOPIN LPC_GPIO1->FIOPIN =k; for ( i=120000; i >0 ; i-- ) ; k = k>>1; } }
// P1.22 to P1.25 CONFIG AS O/P
//initially P.25 =1 [ 1000 ] // four coils to be made high one by one //Port1 pins assigned with Ka value // Delay , value of i decides speed of rotation. // k content rotate right 1 time. [0100] [0010] [0001]
}
7.b. Interface a Stepper motor and rotate it in anti-clockwise anti-clockwise direction.
#include int main (void) { unsigned int i k, z; LPC_GPIO1->FIODIR LPC_GPIO1->FIODIR = 0x03C00000; while (1) { k = 0x00400000; for(z=0; z<4; z++) { LPC_GPIO1->FIOPIN LPC_GPIO1->FIOPIN =k; for (i=120000 ; i > 0 ; i--) ; k = k<<1; } }
// alternate way. To make P1.22 to P1.25 config. as o/p,
// initialize p1.22 as high [0001] // executes 4 times //port 1 pins assigned with k value //delay // rotate left shift by 1 [0010] , [0100], [1000]
}
Page 35 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 8.Interface and Control a DC Motor. a. Clock wise Direction
#include int main() { LPC_GPIO2->FIODIR LPC_GPIO2->FIOD IR = (1<<8); LPC_GPIO4->FIODIR LPC_GPIO4->FIODIR = (1<<28)|(1<<29); (1<<28)|(1<<29);
//Configure //Confi gure the PORT2 pins as OUTPUT //configure port4 pins p4.28,p4.29 as output
while(1) { LPC_GPIO2->FIOSET LPC_GPIO2->FIOSET=(1<<8); =(1<<8); LPC_GPIO4->FIOSET LPC_GPIO4->FIOSET = (1<<28); LPC_GPIO4->FIOCLR LPC_GPIO4->FIOCLR = (1<<29);
//enable DC motor by making P2.8=1 // P4.28 =1 // P4.29 = 0 DC motor Anti Clockwise rotation
} } b. Anticlockwise Direction #include int main() { LPC_GPIO2->FIODIR LPC_GPIO2->FIOD IR = (1<<8); LPC_GPIO4->FIODIR LPC_GPIO4->FIODIR = (1<<28)|(1<<29); (1<<28)|(1<<29);
//Configure //Confi gure the PORT2 pins as OUTPUT //configure port4 pins p4.28,p4.29 as Input
while(1) { LPC_GPIO2->FIOSET=( 1<<8); LPC_GPIO2->FIOSET=(1<<8); LPC_GPIO4->FIOCLR LPC_GPIO4->FIOCLR = (1<<28); LPC_GPIO4->FIOSET LPC_GPIO4->FIOSET = (1<<29);
//enable DC motor by making P2.8=1 // P4.28 = 0 // P4.29 =1 clockwise rotation
} } c. Control DC motor using switches. switches. Sw 1.21- clockwise, 1.20 – 1.20 – Anticlockwise Anticlockwise , 1.19- Stop rotation #include 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); (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); 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 Anti 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 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. } } }
Page 36 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
D C Mo M otor Pi P i n Confi Configur gura ation 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.
DC Motor Pin connection table. Motor Selection
Motor Direction
LPC-1768 Pin No
DCM0
Clockwise
82
DCM1
Anti clock
85 65
DCM_EN
LPC-1768 Port No
P4.28=1 & P4.29=0 P4.28=0 & P4.29=1 P2.8=1
We can‟t drive a dc motor directly through a microcontroller pin. It usually operates in 3.3v -5v and lower currents but DC motor normally works works in 6-12v, 300mA and it has other drawbacks drawbacks like the back EMF produced produced by the DC motor may harm the controller ports and damage them. The solution to the above problems is using motor driving circuit usually known as H-Bridges. They are basically built using FETs and many dedicated ICs are also available like L293D etc. 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. P4.28 0/1 1 0 1 0
P4.29 0/1 0 1 1 0
EN 0 1 1 1 1
DC motor Action Stop. Not enabled Rotates Anticlockwise Rotates Clockwise Stops rotating Stops rotating
Page 37 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
9. Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate delay in between.
#include unsigned char dat7seg[] = {0x88,0xeb,0x4c,0x49,0x2b,0x19,0x1 {0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0xcb,0x8,0x9, 8,0xcb,0x8,0x9,0xa,0x38,0x9C,0x68, 0xa,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_GPIO2->FIOPIN LPC_GPIO2->FIOPIN = 0x000000FF; // Display blank LPC_GPIO1->FIODIR LPC_GPIO1->FIODIR = 0x3C000000; LPC_GPIO1->FIOPIN LPC_GPIO1->FIOPIN &= ~(0x3C000000);
//Dig control on PORT1 p1.26 to p1.29 are output // P1.26 to 1.27 are made 0
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 } }
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 details.
D11
D10
D9
D12
Seven Segment Pin connection table. Seven Segment Data Lines
g
f
a
b
p
c
d
e
LPC-1768 Pin No
75
74
73
70
69
68
67
66
LPC-1768 Port No
P2.0
P2.1
P2.2
P2.4
P2.5
P2.6
P2.3
P2.7 Page 38
Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
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.
Disp1 (D9)
Disp2 (D10)
Disp3 (D11)
Disp4 (D12)
LPC-1768 Pin No
40
43
44 44
45
LPC-1768 Port No
P1.26
P1.27
P1.28
P1.29
Seven Segment Selection Lines
Display Encoding:
Digits to display
Hex code
Display Segments
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
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
[email protected]
Embedded Controller Lab Manual 15ECL67 9.b.Interface of Seven segment module to Cortex M3 Controller – Controller – ii.
To display hexa digits 00 to FF FF with suitable suitable delay
#include
/* NXP LPC17xx definitions
*/
unsigned char dat7seg[] = {0x88,0xeb,0x4c,0x49,0x2b,0x19,0x {0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0xcb,0x8,0x9,0 18,0xcb,0x8,0x9,0xa,0x38,0x9C,0x68,0x xa,0x38,0x9C,0x68,0x1c,0x1e}; 1c,0x1e}; int main (void) { unsigned int i,j; unsigned int count=0; LPC_GPIO2->FIODIR LPC_GPIO2->FIODIR = 0x000000FF; LPC_GPIO2->FIOPIN LPC_GPIO2->FIOPIN = 0x000000FF; LPC_GPIO1->FIODIR LPC_GPIO1->FIODIR |= 0x3C000000; LPC_GPIO1->FIOPIN LPC_GPIO1->FIOPIN &= ~(0x3C000000); while(1) { if (count > 0xF) count = 0; for (i=0; i < 20000; i++) { LPC_GPIO2->FIOPIN = dat7seg[count]; dat7seg[cou nt]; LPC_GPIO1->FIOSET = (1<<26)|(1<<27); (1<<26)|( 1<<27); for (j=0;j<1000;j++); LPC_GPIO1->FIOCLR = (1<<26) | (1<<27); } count++; }
// Seven seg on PORT2 p2.0 to p2.7 are output // Display blank //Dig control on PORT1 p1.26 to p1.29 are output //Display blank
//change to inc/dec speed of count // get data from array offset by count //first and second 7 segment devices //change to inc/dec brightness of display
ON
}
Page 40 Dept of ECE, Acharya Institute of Technology
[email protected]
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 LCD Pin Functions
LPC1768 Pin No
LPC1768 Port No
1
GND
-
-
2
VCC
-
-
3 4
RS
24
P2.28
5
R/W
GND
GND
6 7
EN
27 75
P2.27 P2.0
74 73
P2.1 P2.2
70 69
P2.3 P2.4
LCD Pin No
8 9
DAT1 DAT2
10 11
DAT3 DAT4 DAT5
12 13
DAT6 DAT7
68 67
P2.5 P2.6
14 15
DAT8 VCC
66 -
P2.7 -
16
GND
-
-
Page 41 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
10.)
Interface LCD with 1768 and write a Program to display Message on LCD screen.
#include #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; LPC_GPIO0->FIOSET LPC_GPIO0->FIOS ET |= LCD_EN; for(int k=0;k<100000;k++); k=0;k<100000;k++); LPC_GPIO0->FIOCLR LPC_GPIO0->FIOCL R |= LCD_EN; } void LCD_Data(unsigned char data) { LPC_GPIO2->FIOPIN = data; LPC_GPIO0->FIOSET LPC_GPIO0->FIOS ET |= LCD_RS; LPC_GPIO0->FIOSET LPC_GPIO0->FIOS ET |= LCD_EN; for(int j=0;j<100000;j++); LPC_GPIO0->FIOCLR LPC_GPIO0->FIOCL R |= LCD_EN; }
// RS=0 // EN=1 // delay //EN=0
// RS=1 //EN=1 // delay // 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; LCD_Init(); LCD_Command(0x80); LCD_Command(0x80); while(str[x] != '\0') { LCD_Data(str[x]); LCD_Data(str[x]); x++;
// initialize x=0 // Force cursor to beginging of I row, [ 0xc0 for II row] // if content of str in not a null char // send to LCD as Data //increment x
} } Page 42 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
10 .a) Interface a 4x4 keyboard keyboard and display the key code on an LCD. LCD.
#include #include "lcd.h" void col_write( unsigned char data ) { LPC_GPIO1->FIOSE LPC_GPIO1->FIOSET T |= (data << 14) ; }
// shift shift by 14. Since Since Column Port pins pins are are P1.14,P1.15, P1.16, P1.17
int main (void) { unsigned char key, i; unsigned char rval[] = {0x7,0xB,0xD,0xE}; {0x7,0xB,0xD,0xE}; // to make one coloumn low 0111, 1011, 1101, 1110 unsigned char keyPadMatrix[ keyPadMatrix[ ] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D',' '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; E','F'}; 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 LPC_GPIO1->FIOCLR |= (1<<14 | 1<<15 | 1<<16 | 1<<17); //clear all column selection col_write(rval[i]); col_write(rval[i]); // select one Column .. 0111, 1011, 1101,1110 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++ increments increments 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
[email protected]
Embedded Controller Lab Manual 15ECL67
HEX KEY PAD Pin configurations:
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. HEX KEY Pad matrix table. 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
[email protected]
Embedded Controller Lab Manual 15ECL67
11. Determine Digital Digital output for a given Analog input using using Internal ADC of ARM controller //Program display Internal ADC value on 16x2 lcd
#include #include #include "lcd.h"
/* NXP LPC17xx definitions
#define #define
ADC_DONE ADC_DONE 0x80000000 ADC_OVERRUN ADC_OVERRUN 0x40000000
#define
ADC_CLK ADC_CLK
1000000
*/
/* set to 1Mhz */
volatile int ADCValue; ADCValue; void ADCInit( int Clk ) { LPC_SC->PCONP LPC_SC->PCONP |= (1 << 12);
/* Enable CLOCK into ADC controller */
LPC_PINCON->PINSEL1 LPC_PINCON->PINSEL 1 |= 0x00004000; 0x00004 000; LPC_ADC->ADCR LPC_ADC->ADCR = ( 1 << 0 ) | (14 << 8 ) | ( 1 << 21 ) ; return ;
// config P0.23 work as ADO[0] or make 1<<14; 1<< 14;
/* select channel ; Here 0th bit is 1 ie channel 0 is selected*/ /* CLKDIV = Fpclk / ADC_Clk - 1 * CLKDIV starts from 8 bit /* PDN = 1, normal operation */
} int ADCRead( ) { int regVal, ADC_Data; LPC_ADC->ADCR &= 0xFFFFFF00; 0xFFFFFF00 ; LPC_ADC->ADCR LPC_ADC->ADCR |= (1 << 24) | 1; while ( 1 ) { regVal = LPC_ADC->ADDR0; LPC_ADC->ADDR0; if ( regVal & ADC_DONE ADC_DONE ) break; } LPC_ADC->ADCR &= 0xF8FFFFFF; if ( regVal & ADC_OVERRUN ) return ( 0 ); ADC_Data = ( regVal >> 4 ) & 0xFFF; return ( ADC_Data );
// clear data /* switch channel,start channel,start A/D convert */ /* wait until end of A/D convert */ //Contains the recent ADC value for respective channel /* read result of A/D conversion */
/* stop ADC now */
/* save data when it's not overrun, otherwise, return zero */ //read data in 4:15 so shift by 4 times to bring to 0:7 /* return A/D conversion value */
} int main (void) { char lstr[10]; double volt; ADCInit( ADC_CLK ); init_lcd(); lcd_putstring16(0,"ADC lcd_putstring16(0,"ADC Val Val = 000 "); Page 45 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
lcd_putstring16(1,"VOLTAGE lcd_putstring16(1,"VOLTAGE = 0.00V "); while(1) { ADCValue = ADCRead(); sprintf (lstr, "%4u", ADCValue); ADCValue); lcd_gotoxy(0,10); lcd_putstring(lstr); volt = ADCValue * 3.3/4096; sprintf (lstr, "%0.2f", volt); lcd_gotoxy(1,10); lcd_putstring(lstr); delay(500);
// read ADC value //goto 1st line, 10 char place //print //(ADCValue * 3.3) / 4096) ; //goto 2nd line 10th char place //print
} }
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 = 3.3/4096 =0.000805 = 0.8mV Now the resolution of ADC = 3.3/ 2
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 Channel
Port Pin
AD0
P0.23
0-GPIO, 1-AD0[0] , 2-I2SRX_CLK, 14,15 bits of PINSEL1 PINSEL1 3-CAP3[0]
AD1
P0.24
0-GPIO, 1-AD0[1], 2-I2SRX_WS, 2-I2SRX_WS, 3-CAP3[1]
AD2
P0.25
0-GPIO, 1-AD0[2], 2-I2SRX_SDA, 18,19 bits of PINSEL1 PINSEL1 3-TXD3
Pin Functions
Associated PINSEL Register
16,17 bits of PINSEL1 PINSEL1
Page 46 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
AD3
P0.26
0-GPIO, 1-AD0[3] , 2-AOUT, 3RXD3
20,21 bits of PINSEL1 PINSEL1
AD4
P1.30
0-GPIO, 1-VBUS, 2- , 3-AD0[4]
28,29 bits of PINSEL3 PINSEL3
AD5
P1.31
0-GPIO, 1-SCK1, 2- , 3-AD0[5]
30,31 bits of PINSEL3 PINSEL3
AD6
P0.3
0-GPIO, 1-RXD0, 2-AD0[6], 3-
6,7 bits of PINSEL0 PINSEL0
AD7
P0.2
0-GPIO, 1-TXD0, 2-AD0[7], 3-
4,5 bits of PINSEL0 PINSEL0
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
ADCR
A/D COntrol COntrol Register: Register: Used for Configuring Configuring the ADC
ADGDR
A/D Global Data Register: This register contains the ADC‟s DONE bit and the result of the most recent A/D conversion
ADINTEN ADINTEN
A/D Interrupt Enable Register
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
ADC Register Configuration
Now lets see how to configure the individual registers registers for ADC conversion. conversion. ADCR 31:28
27
26:24
23:22
21
20:17
Reserved
EDGE
START
Reserved
PDN PDN Reserved
16
15:8
7:0
BURST
CLCKDIV
SEL
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].
Page 47 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 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 1 - On Rising Edge
ADGDR ( ADC Global Data Register ) ADGDR 31
27
26:24
23:16
15:4
3:0
DONE
OVERRUN
CHN
Reserved
RESULT
Reserved
Bit 15:4 - RESULT This field contains the 12bit A/D conversion value for the selected channel in ADCR.SEL The vale for this register should be read oncve the conversion is completed ie DONE bit is set.
Bit 26:24 - CHN : Channel These bits contain the channel number for which the A/D conversion is done and the converted value is available in RESULT bits(e.g. 000 identifies channel 0, 011 channel 3...).
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.
Page 48 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
Temperature Sensor
The LM35 series are precision integrated-circuit temperature temperature sensors, whose output voltage is linearly li nearly proportional to the Celsius (Centigrade) temperature. The output of sensor converted to digital that easy connecting with microcontroller.
12. Measurement of ambient temperature using a
sensor and SPI ADC IC.
#include #include #include "lcd.h" #include "spi.h" int main (void) { int msb,lsb; int ADCValue; ADCValue; float temp; char lstr[10]; SPI_Init(); init_lcd(); lcd_putstring16(0,"TEMP lcd_putstring16(0,"TEMP = 000 Deg_C"); while(1) { SPI_EnableChipSelect(); delay(1); SPI_Write(0x01); msb = SPI_Write(0xA0); SPI_Write(0xA0); lsb = SPI_Write(0x00); SPI_Write(0x00);
// SGL=1, ODD=0, MSBF=1
SPI_DisableChipSelect(); msb &= 0x0F; ADCValue = (msb << 8) | lsb; temp = ((ADCValue * 3.3) / 4096) * 100; sprintf (lstr, "%0.1f", temp); lcd_gotoxy(0,7); lcd_putstring(lstr); delay(500); } }
Page 49 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
Spi.c #include #include "spi.h" uint8_t dummy_u8; static uint32_t spi_GetPclk(void); spi_GetPclk(void); void SPI_Init(void) { LPC_PINCON->PINSEL0 |= 0xC0000000; 0xC0000 000; LPC_PINCON->PINSEL1 |= 0x0000003C; 0x00000 03C;
//SCK //SSEL, MISO, MOSI
LPC_GPIO0->FIODIR LPC_GPIO0->FIODIR |= 0x00058000; LPC_GPIO0->FIODIR LPC_GPIO0->FIODIR &= ~0x00020000;
//SSEL, MOSI, SCK are Outputs 15,16,18 //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
LPC_SPI->SPCR = ((0<SPSR; LPC_SPI->SPSR ; dummy_u8 = LPC_SPI->SPDR;
/* Dummy read to clear the flags */ /* Dummy read to clear the flags */
} uint8_t SPI_Write (uint8_t spiData_u8) { LPC_SPI->SPDR = spiData_u8; dummy_u8 = 0; while(dummy_u8 while(dummy_u8 == 0) { dummy_u8 dummy_u8 = LPC_SPI->SPSR LPC_SPI->SPSR & 0x80; } dummy_u8 = LPC_SPI->SPSR; spiData_u8 = (uint8_t)LPC_SPI->SPDR; (uint8_t)LPC_SPI->SPDR; return spiData_u8; } static uint32_t spi_GetPclk(void) spi_GetPclk(void) { uint32_t v_spiPclk_u32,v_Pclk_u32; v_spiPclk_u32,v_Pclk_u32; v_spiPclk_u32 = (LPC_SC->PCLKSEL0 >> 16) & 0x03; switch( v_spiPclk_u32 ) { case 0x00: v_Pclk_u32 = SystemCoreClock/4; SystemCoreClock/4; break; case 0x01: v_Pclk_u32 = SystemCoreClock; SystemCoreClock; break; case 0x02: v_Pclk_u32 = SystemCoreClock/2; SystemCoreClock/2; break; case 0x03: v_Pclk_u32 = SystemCoreClock/8; SystemCoreClock/8; break; } return (v_Pclk_u32); }
// wait until data is sent
//SystemFrequency //SystemFrequency or SystemCoreClock SystemCoreClock
Page 50 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
Lcd.c #include #include "lcd.h"
/* NXP LPC17xx definitions
*/
void delay(unsigned int count) { unsigned int j=0,i=0; for(j=0;j
void lcd_command_write( unsigned char command ) { //LCD_DATA_SET |= (command & LCD_DATA_MASK); LCD_DATA_SET = command; LCD_CTRL_CLR |= LCDRS; LCD_CTRL_SET |= LCDEN; delay(1); LCD_CTRL_CLR |= LCDEN; delay(1); } void lcd_data_write( unsigned char data ) { //LCD_DATA_SET = (data & LCD_DATA_MASK); LCD_DATA_SET = data; LCD_CTRL_SET |= LCDRS; LCD_CTRL_SET |= LCDEN; delay(1); LCD_CTRL_CLR |= LCDEN; delay(1); } void lcd_clear( void) { lcd_command_write( lcd_command_write( 0x01 ); } int lcd_gotoxy( unsigned char x, unsigned char y) { unsigned char retval = TRUE; Page 51 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
if( (x > 1) && (y > 15) ) { retval = FALSE; } else { if( x == 0 ) lcd_command_write( lcd_command_write( 0x80 + y ); else if( x==1 ) lcd_command_write( lcd_command_write( 0xC0 + y ); } return retval; } void lcd_putchar( unsigned char c ) { lcd_data_write( lcd_data_write( c ); } void lcd_putstring( char *string ) { while(*string != '\0') { lcd_putchar( *string ); string++; } } void lcd_putstring16( unsigned char line, char *string ) { unsigned char len = 16; lcd_gotoxy( line, 0 ); while(*string != '\0' && len--) { lcd_putchar( *string ); string++; } } void init_lcd( void ) { SEG_CTRL_DIR |= SEG_DIG_MASK; SEG_CTRL_SET &= ~SEG_DIG_MASK; LPC_GPIO0->FIODIR LPC_GPIO0->FIODIR |= 0x00000FF0; LPC_GPIO0->FIOPIN LPC_GPIO0->FIOPIN &= ~0x00000FF0;
//switch off 7-seg //switch off 7-seg /* LEDs on PORT0 are output /* LEDs off */
*/
LCD_CTRL_DIR |= ( LCDEN | LCDRS ); LCD_DATA_DIR = LCD_DATA_MASK; delay(1000); lcd_command_write(0x38); lcd_command_write(0x38); lcd_command_write(0x38); lcd_command_write(0x38); lcd_command_write(0x10); lcd_command_write(0x10); lcd_command_write(0x0C); lcd_command_write(0x0C); lcd_command_write(0x06) lcd_command_write(0x06) ; lcd_command_write(0x01) lcd_command_write(0x01) ; delay(1000);
/* 8-bit interface, two line, 5X7 dots.
*/
/* display shift */ /* display on */ /* cursor move direction */ /* cursor home */
} Page 52 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
Lcd.h
#ifndef _LCD_H #define _LCD_H #define TRUE 1 #define FALSE 0 #define LINE1 0x80 #define LINE2 0xC0 #define CONTROL_REG 0x00 #define DATA_REG 0x01 #define #define #define #define #define
LCD_DATA_DIR LCD_DATA_SET LCD_CTRL_DIR LCD_CTRL_SET LCD_CTRL_CLR
LPC_GPIO2->FIODIR0 LPC_GPIO2->FIOD IR0 LPC_GPIO2->FIOPIN0 LPC_GPIO2->FIOPI N0 LPC_GPIO0->FIODIR LPC_GPIO0->FIOD IR LPC_GPIO0->FIOSET LPC_GPIO0->FIOCLR LPC_GPIO0->FIOC LR
#define #define #define #define
SEG_DATA_DIR SEG_DATA_SET SEG_CTRL_DIR SEG_CTRL_SET
LPC_GPIO2->FIODIR LPC_GPIO2->FIOD IR LPC_GPIO1->FIOPIN LPC_GPIO1->FIOPI N LPC_GPIO1->FIODIR LPC_GPIO1->FIOD IR LPC_GPIO1->FIOPIN LPC_GPIO1->FIOPI N
#define LCDEN #define LCDRS
(1 << 27) (1 << 28)
//scale 31,30,29,28,27,26,25,24,23,22,21,20,19,18,17 31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,09,0 ,16,15,14,13,12,11,10,09,08,07,06,05,04,03,02,01 8,07,06,05,04,03,02,01,00 ,00 #define LCD_DATA_MASK 0x000000FF 0x00000 0FF #define SEG_DIG_MASK SEG_DIG_MASK 0x0F000000 void delay(unsigned int count); void init_lcd(void); void lcd_putstring(char *string); void lcd_putstring16(unsigned char line, char *string); void lcd_clear(void); lcd_clear(void); int lcd_gotoxy(unsigned char x, unsigned char y); void lcd_putchar(unsigned char c); #endif
Page 53 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
Spi.h
#ifndef _SPI_H_ #define _SPI_H_ #define MOSI_PIN P0_18 #define MISO_PIN P0_17 #define SSEL_PIN P0_16 #define SCK_PIN P0_15 #define SPI_FUNCTION PINSEL_FUNC_3 #define SCK_Freq 4000000 // SPI clock frequency #define SBIT_CPHA 3 #define SBIT_CPOL 4 #define SBIT_MSTR 5 #define SBIT_LSBF 6 #define SBIT_SPIE 7 #define SBIT_SPIF 7 void SPI_Init(void); uint8_t SPI_Write (uint8_t var_data_u8); #define SPI_EnableChipSelect() SPI_EnableChipSelect() LPC_GPIO0->FIOCLR LPC_GPIO0->FIOCLR |= (1 << 16); #define SPI_DisableChipSelect() SPI_DisableC hipSelect() LPC_GPIO0->FIOSET LPC_GPIO0->FIOS ET |= (1 << 16); #endif
Page 54 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67
ARM Cortex M3 Series: An Introduction Brief History ARM was founded in 1990 as Advanced RISC Machines Ltd., a joint venture of Apple Computer, Acorn Computer Group, and VLSI Technology. In 1991, ARM introduced the ARM6 processor family, and VLSI became the initial licensee. Subsequently, additional companies, including Texas Instruments, NEC, Sharp and ST Microelectronics, licensed the ARM processor designs. Nowadays ARM partners ship in excess of 2 billion ARM processors each year. Unlike many semiconductor companies, ARM does not manufacture processors or sell the chips directly. Instead it licenses the processor designs to business partners. This business model is commonly called intellectual property (IP) licensing. Architecture Versions Over the years, ARM has continued to develop new processors and system blocks. These include the popular ARM7TDMI processors, more recently the ARM11 processor family and latest being the ARM Cortex Processor family. Table 2.1 summarizes the various architecture versions and processor families. Architecture Processor Family ARMv1 ARM1 ARMv2 ARM2, ARM3 ARMv3 ARM6, ARM7 ARMv4 StrongARM, ARM7TDMI, ARM7TDMI, ARM9TDMI ARMv5 ARM7EJ, ARM9E, ARM10E, XScale ARMv6 ARM11, ARM Cortex – Cortex – M ARMv7 ARM Cortex-A, ARM Cortex-R, ARM ARM Cortex-M
Page 55 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 ARM Cortex Processor Profiles In the ARMv7 architecture the design is divided into three profiles – 1. A Profile (ARMv7-A) - is designed for high performance open application platforms. They can handle complex applications such as high end embedded operating systems. Example products include high-end smartphones, tablet PCs and PDAs. 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. Evolution of the ARM Instruction Set Enhancement and extension of instruction sets used by the ARM processors has been one of the key driving forces of the architecture’s archit ecture’s evolution. Historically, two different instruction sets were supported on the ARM processor: the ARM instructions, which are 32 bits bits and Thumb instructions, which are 16 bits. During program execution, the processor can be dynamically switched between the ARM state and the Thumb state. The Thumb instruction set provides only a subset of the ARM instructions, but it can provide higher code density. It is useful for products with tight memory requirements. 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 superset 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. The Cortex-M3 processor supports only the Thumb-2 (and traditional Thumb) instruction set. Instead of using ARM instructions, as in traditional ARM processors, it uses Thumb-2 instruction set for all operations. As a result, the Cortex-M3 processor is not backward compatible with traditional ARM processors, which use the ARM as well as Thumb instruction set. The Thumb-2 instruction set is a very important feature of the ARMv7 architecture. For the first time, hardware divide instruction is available on an ARM processor, and a number of multiply instructions are also available. Salient Features of the Cortex-M3 • 32-bit 32-bit microprocessor. • 32-bit 32-bit data path, 32-bit register bank and 32 -bit memory interfaces. • Harvard Architecture – separate separate instruction bus and data bus. • 3-stage 3-stage pipeline with branch speculation. • Thumb-2 Thumb-2 instruction set. • No switching between ARM state and thumb state. • Instruction fetches are 32 bits. Up to two instructions can be fetched in one cycle. As a result, there’s more available bandwidth for data transfer. • ALU with hardware divide and single cycle multiply. • Configurable Nested Vector Interrupt Controller (NVIC). • Maximum of 240 external interrupts can be configured. • Low gate count, suitable for low power designs. • Memory Protection Unit (MPU). • Operation Mode Selection – user user and privilege modes. • Advanced debug components. Registers • The Cortex-M3 Cortex -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 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 • Program Status registers (PSR) • Interrupt Mask registers regis ters (PRIMASK, FAULTMASK and BASEPRI). • Control register (CONTROL)
Page 56 Dept of ECE, Acharya Institute of Technology
[email protected]
Embedded Controller Lab Manual 15ECL67 • The Cortex-M3 Cortex -M3 has 18 registers in total compared to 37 registers for traditional ARM. Memory Map The Cortex-M3 has predefined memory maps, which allows built in peripherals, such as the interrupt controller and debug components, to be accessed by simple memory access instructions. The predefined memory map also allows the Cortex-M3 processor to be highly optimized for speed and ease of integration in system-on-a-chip (SoC) designs. The Cortex-M3 design has an internal bus infrastructure optimized for this memory usage. In addition, the design allows these regions to be used differently. For example, data memory can still be put into the CODE region, and program code can be executed from an external Random Access Memory (RAM) region. The Cortex-M3 memory map is outlined in figure as shown. Operation Modes The Cortex-M3 processor has two modes of operation and two privilege levels. The operation modes (thread mode and handler mode) determine whether the processor is running a normal program or running an exception handler like an interrupt handler or system exception handler. The privilege levels provide a mechanism for safeguarding memory accesses to critical regions as well as providing a basic security model. When the processor is running a main program (thread mode), it can be either in a privileged state or a user state, but exception handlers can only be in a privileged state. When the processor exits reset, it is in thread mode with privileged access right. In this state, a program has access to all memory ranges and can use all supported instructions. Software in the privileged access level can switch the program into the user access level using the control register. When an exception takes place, the processor will always switch back to the privileged state and return to the previous state when exiting the exception handler. A user program cannot change back to the privileged state by writing to the control register . It has to go through an exception handler that programs the control register to switch the processor back into the privileged access level when returning to thread mode. Nested Vector Interrupt Controller (NVIC) The Cortex-M3 processor includes an interrupt controller called the Nested Vectored Interrupt Controller (NVIC). It is closely coupled to the processor core and provides a number of features as follows: Nested interrupt support Vectored interrupt support support Dynamic priority changes support Reduction of interrupt latency Interrupt masking Nested Interrupt support - All the external interrupts and most of the system exceptions can be programmed to different priority levels. When an interrupt occurs, the NVIC compares the priority of this interrupt to the current running priority level. If the priority of the new interrupt handler is higher than the current level, the interrupt handler of the new interrupt will override the current running task. Vectored Interrupt Support Support - When an interrupt is accepted, the starting address of the interrupt service routine (ISR) is located from a vector table in memory. Dynamic Priority Changes Support Support - Priority levels of interrupts can be changed by software during run time. Interrupts that are being serviced are blocked from further activation until the ISR is completed, so their priority can be changed without risk of accidental reentry. Reduction of Interrupt Latency Latency - The Cortex-M3 processor also includes a number of advanced features to lower the interrupt latency. These include automatic saving and restoring some register contents and reducing delay in switching from one ISR to another. Interrupt Masking Masking - Interrupts and system exceptions can be masked based on their priority level or masked completely using the interrupt masking registers BASEPRI, PRIMASK, and FAULTMASK.
Page 57 Dept of ECE, Acharya Institute of Technology
[email protected]