Lab Set IV
Lab Set IV
Lab Set IV
Lab Set IV
ADC, UART, PWM
Objective:
In this lab, you will understand how the following three modules of a microcontroller are used:
ADC (Analog to Digital Converter) to interface sensors,
UART (Universal Asynchronous Receiver/Transmitter) to communicate with computer or
other microcontroller and
PWM (Pulse Width Modulation) to control power delivery.
Grading:
i. Attend the lab session and demonstrate your work (60%), you must sign attendance sheet.
ii. Write a brief report and email (40%) addressing those questions labeled with Q .
Introduction:
In this lab you will use analog to digital converter (ADC) to read in analog signal from photo sensor and convert
to digital value. Unlike in lab III, where you used hardware to set lux value, here, you will use ADC to read
analog signal from the photo sensor and perform switching at any luminous intensity you like as the same
time send the value read to UART interface where you can connect PC or another microcontroller. The ADC
technique is much better because since you can monitor your room’s real-time luminous intensity, you can
do some automation like controlling a window shutter or power to a lighting source to maintain a constant
illumination. In general, ADC gives the digital version of analog values read from sensors.
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
Part 1: ADC
2|26
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
int main(void)
{
int result = 0;
float voltage = 0;
float Ev = 0;
float VREF = 3.3;//Reference Voltage = 3.3V
char str[80];
while (1)
{
//Configure ADC clk frequency CLKDIV = 14, assuming PCLK=CCLK/4=15MHz,
Modify the following statement (????????) to configure ADOCR for: software mode; AD0.6
channel; 1MHz ADC clk and to power UP and then start conversion in second line.
AD0CR =????????;
AD0CR |= (1 << 24)); // start conversion
//Switching LEDs
if (Ev < 100) {
IO1SET = 0x00FF0000;
}
else {
IO1CLR = 0x00FF0000;
}
//Displaying Reading on LCD
LCD_CMD(0x89);
sprintf(str, "%i mV ", (int)(voltage * 1000));
LCD_Write(str);
LCD_CMD(0xC9);
sprintf(str, "%i Lux ", (int)(Ev));
LCD_Write(str);
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3|26
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
Q Configure AD0CR register appropriately for software controlled ADC mode in the above code.
3. Create header file with name initClock.h; copy and paste the following code in it save. Then
build your project. If initClock.h doesn’t appear under “Source Group 1”, try adding manually.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <lpc214x.h>
void initClocks(void)
{
//setupPLL0 Assuming 12Mhz Xtal is connected to LPC2148.
PLL0CON = 0x01;
PLL0CFG = 0x24;
//connect PLL0
while (!(PLL0STAT & PLOCK));
PLL0CON = 0x03;
while (T0TC < milSec); //wait until timer counter reaches the desired delay
4|26
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
1. Add the following functions into ADC_UART_PMW.C and declare both function above main.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void initUART0(void)
{
//Select pins P0.0 as UART0 TxD and P0.1 as UART0 RxD
PINSEL0 |= (PINSEL0 & 0xFFFFFFF0) | 0x00000005
U0LCR = 0x83; // 8 bits, no Parity, 1 Stop bit | DLAB set to 1
//[U0DLM:U0DLL] holds 16bit [8:8] used to divide clock and generator baud
U0DLL = 0x62;
U0DLM = 0x00; // for 9600 Baud Rate
U0FDR = (1 << 4) | 0x0; // MULVAL=1(bits - 7:4) , DIVADDVAL=0(bits - 3:0)
5|26
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
3. Start debugging and run your code (F5); from Peripherals, open UART0 and check correctness
of fields: (a) “line control” values (b) Baudrate =9566~9600.
4. From View->Serial Windows menu open UART #1; you shall see
what ever value you set in AD06 analog input under A/D Converter
0 peripheral window.
Q Write a function “receiveUART0(char * str, int size)” to receive
data from UART0 and print to the LCD screen. It should be very
similar to the transmit function except it additionally takes size of
the character array as argument.
Q Try to discuss at least 2 ways to double the baudrate of your
UART, show the calculation. Note that Peripheral Clock (PCLK) can
be increased up to CPU Clock (CCLK) frequency.
Q What is the purpose of the 10µF capacitor and the 3.3V Zener Diode?
3. Run simulation and observe the LCD output by varying photo sensor value.
4. Connect Virtual terminal and COMPIM from parts to UART0 pins as shown in the figure below.
5. Double click on both Virtual terminal and COMPIM and configure baudrate = 9600, data bit = 8,
stop bit =1 and set the rest to none. For COMPIM set com port to COM4 or the last available
from the list.
6. Run simulation and, From Debug menu open Virtual terminal window; you shall see UART
output on the terminal window.
6|26
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
The COMPIM sends the digital data to the serial port of your computer. You can read this data from serial
port of your computer (COM5) using any programming language. Now, lest use terminal emulators to
read the data.
7. Download terminal emulators of your preference and open. Configure the terminal with the
appropriate line control parameter values (baudrate = 9600, data bit = 8, stop bit =1 and set
the rest to none)
8. Download Virtual Serial Port Driver and pair COM4 and COM5
9. Connect the terminal on COM5 and observe.
Q Wright a python or c++ code that sends data to be displayed into LCD within you Proteus
project.
7|26
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
8|26
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
9|26
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
10 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
11 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
12 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
13 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
:
:
14 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
Interface
Diagram
(*) I2C sends devise and register address together with data serially (only two wire required) while SPI uses
extra CS (Chip Select) wiring which number of wire depends on number of slaves.
15 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
In this lab, however, we will cover only UART for its simplicity and wide usage.
16 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
17 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
UART
UART (Universal Asynchronous Receiver Transmitter) is a serial communication which provide
a cost effective simple and reliable communication between one microcontroller to another or
between a microcontroller and PC. It is also called RS-232 (except for logic level mismatch).
In UART there is no synchronizing clock, however
any rising or falling edge can be used to sync the
communicating devises; to the worst, the data being
sent could be all zeros, in this case a logic 1 stop bit
and a logic 0 start bit creates falling edge for sync.
Baud rate
Baud rate is the number of symbols transmitted per second. If number of bits per symbol (baud)
is one, then baud rate and bit rate (bps) will be the same. If number of bits per symbol is say
two, then baud rate will be half of bit rate; bits per symbol depends on modulation schemes.
Baud rate doesn’t refer to the actual data transfer rate. In a protocol frame, the data is called
the payload and non-data bits are known as the overhead. Overload includes bits representing
source address, destination address, error detection and correction codes, and other
information or control bits. Hence baud rate refers to the rate for the whole frame, payload and
overload.
One of very important requirement to communicate using UART is to configure both ends to
use the same baud rate out of the standard baud rates: 1200, 2400, 4800, 9600, 19200, 38400,
57600, 115200, 230400, etc. (bauds). 9600 is usually used in embedded systems.
18 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
UART in LPC2148
There are two UART modules in LPC2148, UART0 and UART1.
Both UART0 & UART1 blocks internally have a 16-byte FIFO structure to hold the Rx and Tx
data in 2 registers each THR (Transmit Holding Register) and TSR (Transmit Shift Register).
When we Data to be sent is write into THR, it is then transferred to TSR which assembles the
data to be transmitted via Tx Pin.
Similarly, Rx has RSR (Receive Shift Register) and RBR (Receive Buffer Register). When a
valid data is Received at Rx Pin, it is first assembled in RSR and then passed in to Rx FIFO
which can be then accessed via RBR
UARTx pin description
Pin UART0 UART1 Type Description
RXD: P0.1 P0.9 Input Serial Input. Serial receive data.
TXD: P0.0 P0.9 Output Serial Output. Serial transmit data.
UART1 is identical to UART0, with the addition of a modem interface hence has 6 additional pins.
19 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
0 < MULVAL ≤ 15; 0 < DIVADDVAL≤ 15; PCLK is the peripheral clock
Example 2: Using UART0 baud rate formula from above, it can be determined that system
20 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
21 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
22 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
What is PWM
Pulse Width Modulation (PWM), a type of digital signal, is a technique that modulates the width
of a rectangular pulse wave in order to get a variation in the average value of the resulting wave
in a fixed period of time and having fixed frequency.
PWM is a powerful technique for controlling analog circuits with a microprocessor's digital
outputs. PWM is employed in a wide variety of applications, ranging from measurement and
communications to power control and conversion.
power control
In power control application, PWM is used to control the amount of power delivered to a load
without incurring the losses that would result from linear power delivery by resistive means. For
example, to control power delivered to LEDs, you can use variable current limiting or voltage
divider resistor which dissipate power while controlling power delivered to LEDs; however, if
you use PWM this power dissipation will no longer be there. Motor speed control is also another
power control application. In this lab we will focus on power control application aspect of PWM.
Communication
In communication, the widths of the pulses are used to encode specific data values. For
example, one may encode ‘0’ as 25% duty cycle signal and ‘1’ as 75% duty cycle; this may
have synchronization advantage over UART which encodes ‘0’ as 0V and ‘1’ as 5V while
transmitting continuous ‘0’s and very close to Manchester Encoding (that your LAN cables use)
in which ‘0’ is encoded with 5v to 0v transition and the reverse for ‘1’.
Voltage Regulation
In voltage regulation (as in switched-mode power supply, the one your desktop PC power
supply uses), PWM is used to maintain constant output voltage by controlling an inverter by
varying duty cycle based on feedback from the output.
In the graphic below, you can see PWM signals with different duty cycle all representing a fixed
frequency square wave signal; the green lines represent a regular time period.
𝑡𝑜𝑛
DutyCycle = × 100% ; 𝑇 = 1/𝑓
𝑇
23 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
PWM in LPC2148
LPC2148 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. It uses a 32-bit
Timer/Counter which can be used as a standard timer (third timer) if the PWM mode is not
enabled.
Below block diagram shows the PWM pins and the associated Match (Duty Cycle) registers.
PWM_2 P0.7 0-GPIO, 1-SSEL0, 2-PWM2, 3-EINT2 14,15 bits of PINSEL0 PWMMR2
PWM_3 P0.1 0-GPIO, 1-RXD0, 2-PWM3, 3-EINT0 2,3 bits of PINSEL0 PWMMR3
PWM_6 P0.9 0-GPIO, 1-RXD1, 2-PWM6, 3-EINT3 18,19 bits of PINSEL0 PWMMR6
Source: https://www.arduino.cc/en/tutorial/PWM
PWM Registers
The below table shows the registers associated with LPC2148 PWM.
REGISTER DESCRIPTION
PWM Interrupt Register. The PWMIR can be written to clear interrupts. The
PWMIR PWMIR can be read to identify which of the possible interrupt sources are
pending.. Writing Logic-1 will clear the corresponding interrupt.
Timer Control Register: The PWMTCR is used to control the Timer Counter
PWMTCR functions(enable/disable/reset).
Timer Counter: The 32-bit TC is incremented every PR+1 cycles of PCLK.
PWMTC The PWMTC is controlled through the PWMTCR.
PWMMR0 Match Register: This register hold the max cycle Time(Ton+Toff).
PWMMR1- Match Registers: These registers holds the Match value(PWM Duty) for
PWMMR6 corresponding PWM channels(PWM1-PWM6).
PWM Control Register: PWM Control Register. Enables PWM outputs and
PWMPCR selects PWM channel types as either single edge or double edge controlled.
PWMLER Load Enable Register: Enables use of new PWM values once the match
24 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
occurs.
Refer LPC2148 user manual pp 263 for detail of these registers.
PWMTCR
31:4 3 2 1 0
Reserved PWM Enable Reserved Counter Reset Counter Enable
PWMMCR
31:21 20 19 18 - 2 1 0
PWMMRxI
This bit is used to Enable or Disable the PWM interrupts when the PWMTC matches
PWMMRx (x:0-6)
0- Disable the PWM Match interrupt
1- Enable the PWM Match interrupt.
PWMMRxR
This bit is used to Reset PWMTC whenever it Matches PWMRx(x:0-6)
0- Do not Clear.
1- Reset the PWMTC counter value whenever it matches PWMRx.
PWMMRxS
This bit is used to Stop the PWMTC,PWMPC whenever the PWMTC matches
PWMMRx(x:0-6).
0- Disable the PWM stop o match feature
1- Enable the PWM Stop feature. This will stop the PWM whenever the PWMTC reaches
the Match register value.
PWMPCR
25 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
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.
PWMLER
31-7 6 5 4 3 2 1 0
Unused LEN6 LEN5 LEN4 LEN3 LEN2 LEN1 LEN0
LENx
This bit is used Enable/Disable the loading of new Match value whenever the PWMTC is
reset(x:0-6)
PWMTC will be continously incremented and whenever it reaches the PWMMRO, timer
will be reset depeding on PWMTCR configuraion. Once the Timer is reset the New Match
values will be loaded from MR0-MR6 depending on bits set in this register.
0- Disable the loading of new Match Values
1- Load the new Match values from MRx when the timer is reset
The match register selections for various PWM outputs is shown in Table 246. And a sample
of how PWM values relate to waveform outputs is shown in Figure bellow. This implementation
supports up to N-1 single edge PWM outputs or (N-1)/2 double edge PWM outputs, where N is
the number of match registers that are implemented. PWM types can be mixed if desired.
26 | 2 6
AAiT, SECE
Microcomputers and Interfacing - Lab Set IV - Draft II- Kinde M. 2021
Single Edge
Double Edge
27 | 2 6