Temp IOT Lab Manual
Temp IOT Lab Manual
IV SEMESTER B.E.
LABORATORY RECORD
[Autonomous Scheme 2022]
2023-2024
2023 - 2024
LABORATORY CERTIFICATE
Marks
Maximum Obtained
50
Date:
R. V. COLLEGE OF ENGINEERING
[Autonomous Institution Affiliated to VTU, Belagavi] Department of
Computer Science & Engineering Bengaluru-560059
VISION
MISSION
PEO2: To develop the ability among graduates to analyze and understand current pedagogical
techniques, industry accepted computing practices and state-of-art technology.
PEO3: To develop graduates who will exhibit cultural awareness, teamwork with professional
ethics, effective communication skills and appropriately apply knowledge of societal impacts
of computing technology.
PEO4: To prepare graduates with a capability to successfully get employed in the right role /
become entrepreneurs to achieve higher career goals or take up higher education in pursuit of
lifelong learning.
Program Outcomes
PO1: Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization for the solution of complex engineering
problems
PO2: Problem analysis: Identify, formulate, research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences and engineering sciences.
PO5: Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools, including prediction and modeling to complex engineering
activities, with an understanding of the limitations.
PO6: The engineer and society: Apply reasoning informed by the contextual knowledge to
assess Societal, health, safety, legal, and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
PO8: Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
PO9: Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
PO11: Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
PO12: Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological change.
Course Outcomes
After completing the course, the student will be able to:
CO1: Acquire the knowledge of architecture of Microprocessors and Microcontrollers for the
different applications.
CO2: Develop skill in simple program writing for micro controllers for applications in
assembly level language and Embedded C.
CO3: Design system configuration for a given application.
CO4: Integrate, implement and test the design in applications.
Do’s and Don’ts in the Laboratory
Do’s……………...
• Come prepared to the lab with the program logic.
• Use the computers and controller kit for academic purposes only.
• Following the lab exercise cycles as per the instructions
given by the department.
• Keep the chairs back to their position before you leave.
• Handle the computer and the kits with care.
• Keep your lab clean.
Don’ts……………...
• Coming late to the lab and leaving the lab early.
• Move around in the lab during the lab session.
• Download or install any software onto the computers.
• Tamper system files or try to access the server.
• Write record in lab.
• Change the system assigned to you without the notice of lab
staff.
• Carrying CD’s, Floppy’s, Pen Drives and other storage devices
into lab.
• Using others login id’s.
PARTICULARS OF THE EXPERIMENT
Page No
Marks Split as per rubrics
Prog Marks
No. Program Execution Viva (10)
2 2 2 2 2
Interface Logic Controller and write Embedded C 31
1 programs to generate BCD up / down and Ring counters.
Input is read from the DIP switch.
Seven Segment Display Interface: Write a C program to 34
2 display messages “FIRE” & “HELP” on 4 digit seven
segment display alternately with a suitable delay.
Stepper Motor Interface: Write an Embedded C program 39
3 to rotate stepper motor in clockwise direction for “M”
steps, anti-clock wise direction for “N” steps.
DAC Interface : Write an Embedded C program to 42
4 generate sine , full rectified ,triangular, sawtooth and
square waveforms using DAC module
Matrix Keyboard Interface : Write an mbedded C 49
5 program to interface 4 X 4 matrix keyboard using
lookup table and display the key pressed on the
Terminal.
DC Motor Interface: Write an Embedded C program to 53
6 generate PWM wave to control speed of DC motor.
Control the duty cycle by analog input.
Character LCD Interface : Write an Embedded C 57
7 program to display text messages on the multiple lines
of the display.
Total for 70 Marks
LAB INTERNALS
TOTAL / 50 Marks
Lab Write-up and Execution Rubrics (Max: 6 marks)
Sl
Criteria Excellent Good Poor Score
no
Student exhibits Student does not
Student has sufficient
thorough have clear
understanding of
understanding of understanding of
Understanding program
program program
of problem and requirements and
requirements and requirements and is
1 requirements applies ALP /
applies ALP / unable to apply
(2 Marks) Embedded C for
Embedded C for ALP / Embedded C
CO1 ARM concepts.
ARM concepts. for ARM concepts.
(1.5M - 1M)
(2M) (0M)
Student demonstrates
Student demonstrates
the design &
the design &
execution of the
execution of the
Design & program without Student has not
program with
Execution optimization of the executed the
optimized code with
2 (2Marks) code and handles program.
all the modifications
only few (0M)
CO 2, 3 and test cases
modifications and
handled.
few test cases.
(2M)
(1.5M - 1M)
Documentation with
Documentation with Documentation
appropriate
Results and only few comments with no comments
comments and output
Documentation and only few output and no output cases
3 with observations is
(2Marks) cases is covered in covered in manual.
covered in manual.
CO 1, 4 manual. (1.5M - 1M) (0M)
(2M)
Total Marks
Staff Signature:
Microcontroller & Embedded Systems Lab - 21CS44
ARM – stands for “Advanced RISC Machine” , ARM based microcontrollers are very
popular in 32 bit embedded market, occupying the major share of the market. ARM7 was
the first commercial success, used extensively in products like PDAs, IPods, hard disks, set-
top boxex, mobile phones etc.
Operating Modes : ARM has seven operating modes, i.e it exists in any one of these modes
when the processor is running,
i) User mode: simplest mode with least privileges (or also referred as Unprivileged mode),
this is the mode under which most applications run, User mode is used for the execution
of programs and applications.
ii) FIQ Mode (Fast interrupt request): Entered this mode on request from FIQ interrupts
iii) IRQ Mode (Interrupt Request): Entered this mode on IRQ request
iv) Supervisor: Entered on Reset and when a software interrupt instruction (SWI) is
executed, and is generally the mode in which the operating system kernel operates in.
v) Abort: Used to handle memory access violations, Abort is entered after a failed
memory access
vi) Undef: Used to handle undefined instructions, Undefined is entered if the instruction
is not defined (invalid opcodes)
vii) System: This is highly privileged mode used by operating systems to manipulate and
control the activities of the processor, System mode is a special version of user mode that
allows full read write access to the CPSR
1
Microcontroller & Embedded Systems Lab - 21CS44
Register Set: ARM has 37 registers of size 32bits each, they are
i) PC - 1 dedicated program counter
ii) CPSR – 1 dedicated program status register, (like flag register of 8086)
iii) 5 dedicated saved program status registers (SPSR)
iv) 30 general purpose registers
Shift and Rotate operations can be part of other Register Transfer / Arithmetic / Logic
operations. One of the operand can be operated by shift/rotate operations using barrel
shifter.
MOVS R0, #0
Conditional Execution : Suffixing condition codes is possible for any data processing and
branch instructions, if condition code satisfies instruction works, else it is a NOP
instruction. There are 15 such condition codes.
Ex: MOVEQ R0, #10 ; 10 is moved to R0, if Zero flag is set else it is a NOP
Other Commonly used Condition codes :
EQ Z=1 zero flag set
NE Z=0 zero flag clear
CS C=1 carry flag set
CC C=0 carry flag clear
2
Microcontroller & Embedded Systems Lab - 21CS44
ARITHMETIC INSTRUCTIONS
Format:
ADD REG, REG, (REG/IMM)
SUB REG, REG, (REG/IMM)
RSB REG, REG, (REG/IMM)
LOGICAL INSTRUCTIONS
Format:
AND REG, REG, (REG/IMM)
EOR REG, REG, (REG/IMM)
ORR REG, REG, (REG/IMM)
BIC REG, REG, (REG/IMM)
COMPARE instructions
The CPSR register contains four flags Negative(sign flag),Zero,Carry and Overflow flags,
which are affected by the execution of following instructions. Flags are also affected by
using suffix S to the other data processing instructions.
Format:
CMP REG, (REG/IMM)
TST REG, (REG/IMM)
TEQ REG, (REG/IMM)
MULTIPLICATION
Format:
MUL REG , REG, (REG/IMM)
MLA REG , REG, REG, REG
3
Microcontroller & Embedded Systems Lab - 21CS44
Examples:
MUL R1 , R2 , R3 - Multiply R1 R2 x R3
MLA R4 , R3 , R2 , R1 - Multiply and Accumulate; R4 (R3 x R2) + R1
BRANCH INSTRUCTIONS
Ex:
LDR R1, [R0] ;contents of memory(32 bit number – 4 bytes) pointed by
R0 is loaded into R1
STR R1, [R0] ; contents of R1(32bit number-4 bytes) is stored in memory
pointed by R0.
4
Microcontroller & Embedded Systems Lab - 21CS44
AIM: Translate the following code in C to the ARM instruction set. Assume variables are
32bit integers represented in Registers.
A=B+ C-D
MOV R0,#00 ; A
MOV R1,#NUM1 ; B
MOV R2,#NUM2 ; C
MOV R3,#NUM3; D
ADD R0,R2,R1
SUB R0,R0,R3
STOP B STOP
END
A = 2*A + B
MOV R0,#NUM1 ; A
MOV R1,#NUM2; B
ADD R0,R1,R0,LSL #2
STOP B STOP
END
5
Microcontroller & Embedded Systems Lab - 21CS44
Sample Output:
Before Execution After Execution
6
Microcontroller & Embedded Systems Lab - 21CS44
AIM: Write an ARM ALP to perform addition and subtraction of two 32-bit and 64bit
numbers.
MSB:LSB
Value1 = R1 : R2
Value2 = R3 : R4
Result = R5 : R6
Program:
AREA RESET, CODE
ENTRY
LDR R0,=VALUE1
LDR R1,[R0]
LDR R2,[R0,#4]
LDR R0,=VALUE2
LDR R3,[R0]
LDR R4,[R0,#4]
ADDS R5,R1,R3
ADC R6,R2,R4
LDR R0,=RESULT
STR R5,[R0]
STR R6,[R0,#4]
STOP S STOP
7
Microcontroller & Embedded Systems Lab - 21CS44
Algorithm:
1) Initialize first element as smallest [R1] and number of elements n = n-1
2) Loop through all the n [R4] elements. If the current element is smaller than the smallest, then
update smallest.
AIM: Write an ARM ALP to find smallest and largest of N- 32 bit numbers.
AREA RESET,CODE
ENTRY
LDR R0,=DATA1
LDR R3,=0X40000000 ; memory location for storing answer
MOV R4,#05 ; //N- number of elements
LDR R1,[R0],#04; assume first no. as smaller no & increment R0 by 4
SUB R4,R4,#01 ; compare with n-1 elements
BACK
LDR R2,[R0] ; get next number & compare with small
CMP R1,R2
BLS LESS ; // If R1 < R2 , BRANCH
MOV R1,R2 ; update with new smaller no
LESS
ADD R0, R0,#04 ; increment pointer to next number
SUB R4,R4,#01
CMP R4,#00
BNE BACK
STR R1, [R3] ; // SMALLEST VALUE STORED IN MEMORY LOCATION
STOP B STOP
AREA DATA,CODE
DATA1 DCD &64,&05,&96,&10,&65
END
Sample Output:
8
Microcontroller & Embedded Systems Lab - 21CS44
Algorithm:
1) Initialize sum = 0
2) Loop through all the n elements. Add the current element to sum.
Program:
MOV R3,#0
MOV R4, #0
LDR R0, =INPUTS
LDR R1, =OUTPUTS
CONT
LDR R2, [R0]
ADD R4, R4, R2
ADD R0, R0, #4
ADD R3, R3, #1
CMP R3, #5
BNE CONT
MOV R2, #5
MOV R3, #0
REPT SUBS R4, R4, R2
ADDPL R3, R3, #1
BPL REPT
ADDMI R4, R4, R2
STR R3, [R1]
STOP B STOP
END
9
Microcontroller & Embedded Systems Lab - 21CS44
Sample Output:
10
Microcontroller & Embedded Systems Lab - 21CS44
}
Program:
AREA RESET, CODE, READWRITE
ENTRY
LDR R0,=ARR
MOV R1, #0 ; Loop Iterator
MOV R7, #0 ; Number Of occurrences in The Array
MOV R4, #4 ; key
CONT
LDR R3,[R0]
CMP R3, R4
BNE SKIP
ADD R7, R7,#1
SKIP
ADD R0, #4
ADD R1, #1
CMP R1, #10 ; no of elements
BNE CONT
STOP B STOP
ARR DCD 0,5,1,4,100,4,0,8,7,20
END
Sample Output:
11
Microcontroller & Embedded Systems Lab - 21CS44
AIM: Write an ARM ALP to compute number of 1’s in a given 32 bit number and check
the parity of the given number
Program:
AREA RESET,CODE
ENTRY
LDR R0,=DATA1
LDR R1,[R0] ;Stores 32bit number-16 ones
MOV R2,#0 ;loop counter, 32 times(32bits)
MOV R4,#0 ;number of 1's counter
MOV R3,#1
LOOP
MOVS R1,R1,LSR #1
ADDCS R4,R4,#1
ADD R2,#1
CMP R2,#32 ; check for 32 bits
BNE LOOP
STOP B STOP
AREA DATA,CODE
DATA1 DCD 0xAAAAAAAA
END
Sample Output:
12
Microcontroller & Embedded Systems Lab - 21CS44
AIM: Write an ARM ALP to compute GCD of two given 32-bit numbers.
Algorithm:
int gcd(int x,int y){
while(x!=y)
{
if(x>y)
return gcd(x-y,y);
else
return gcd(x,y-x);
}
return x;
}
Program:
AREA RESET,CODE
ENTRY
MOV R0,#30 ; test values
MOV R1,#45 ; test values
LOOP CMP R0,R1
BEQ EXIT
BGT COND1
SUB R1,R0
B LOOP
COND1 SUB R0,R1
B LOOP
EXIT LDR R0,=GCD
STR R1,[R0]
STOP B STOP
AREA RESULT,DATA
GCD SPACE 4
END
Sample Output:
13
Microcontroller & Embedded Systems Lab - 21CS44
AIM: Write an ARM ALP to compute the factorial of a given 32-bit number using procedure.
Algorithm:
fact(int n)
{
Read number n.
Initialize i =n and fact to 1.
Repeat while i is not equal to 0.
{
fact = fact * i
i = i -1
}
Return fact
}
Program:
AREA RESET,CODE
ENTRY
LDR R0,=INPUT
BL FACT ; CALL SUBROUTINE FACT
LDR R1,=0X40000000 ; RAM area
STR R3,[R1] ; store result in R3 to RAM
STOP B STOP
;subroutine-begin
FACT
LDR R2,[R0] ; get num in R2
CMP R2,#00
BEQ END1
MOV R3,R2 ; result = num
LOOP
SUB R2,#01 ; num = num - 1
CMP R2,#00
MULNE R3,R2,R3 ;result = result * num
BNE LOOP
MOV PC,LR
END1
MOV R3,#01 ; return R3=1, if num=0
END2
MOV PC,LR ; return from subroutine
;subroutine-end
14
Microcontroller & Embedded Systems Lab - 21CS44
Sample Output:
15
Microcontroller & Embedded Systems Lab - 21CS44
AIM: Write an ARM ALP to sort the given list of 32-bit numbers using Bubble sort.
Algorithm:
bubbleSort(list of n elements)
{
for all n elements
n = n-1;
for (i=0 to n, i++)
if ( A[i] > A[i+1] )
temp = A[i]
A[i] = A[i+1]
A[i+1] = temp
}
Program:
BNE LOOP1
STOP B STOP
16
Microcontroller & Embedded Systems Lab - 21CS44
Sample Output:
17
Microcontroller & Embedded Systems Lab - 21CS44
PART – A
Interfacing
Programs using
ARM LPC 2148
18
Microcontroller & Embedded Systems Lab - 21CS44
19
Microcontroller & Embedded Systems Lab - 21CS44
20
Microcontroller & Embedded Systems Lab - 21CS44
//Sample Program 1: Interfacing LED and Switch to LPC2148 using GPIO pins
//P0.31 connected to LED - D7 in CPU board(common anode)
//P1.14 connected to Switch - SW2 in CPU board
#include <lpc214x.h>
#define LED_OFF (IO0SET = 1U << 31)
#define LED_ON (IO0CLR = 1U << 31)
#define SW2 (IO0PIN & (1 << 14))
void delay_ms(unsigned int j);
int main( )
{
IO0DIR = 1U << 31;
IO0SET = 1U << 31;
while(1)
{
if (!(IO0PIN & (1 << 14)))//(if(!SW2 )
{
IO0CLR = 1U << 31; //LED_ON
delay_ms(250);
IO0SET = 1U << 31; //LED_OFF
delay_ms(250);
}
}
}
void delay_ms(unsigned int j)
{
unsigned int x, i;
for(i=0; i<j; i++)
{
for(x=0; x<10000; x++); /* loop to generate 1 milisecond delay with CCLK = 60MHz */
}
}
21
Microcontroller & Embedded Systems Lab - 21CS44
Interfacing Diagram
//Elevator Program:
// P0.16 - P0.19 are connected to decoder inputs, it makes one of the o/p LEDs 0 to 9 on
// P0.20-P0.23 are connected to *CLR pins of latches: make it '0' and then '1' to clear
// elevator keys: *Q outputs of latches connected to P1.16 TO P1.19
22
Microcontroller & Embedded Systems Lab - 21CS44
#include<lpc214x.h>
#define IS_ON(pin) (IO1PIN & (1U << (pin)))
int contUP = 0;
int contDN = 99;
unsigned int rightSFT = 1U<<7;
unsigned int leftSFT = 1;
const int key0 = 16;
const int key1 = 17;
const int key2 = 18;
const int key3 = 19;
int main()
{
IO0DIR |= 0xFF;
while(1)
{
if(IS_ON(key0))
{
reset_values(0);
IO0CLR = 0xFF<<16;
IO0SET |= ((contUP/10)<<4 | (contUP%10))<<16;
contUP++;
if(contUP > 99) contUP = 0;
}
else if(IS_ON(key1))
{
reset_values(1);
IO0CLR = 0xFF<<16;
IO0SET |= ((contDN/10)<<4 | (contDN%10)) << 16;
contDN--;
if(contDN < 0) contDN = 99;
}
else if(IS_ON(key2))
{
reset_values(2);
IO0CLR = 0xFF<<16;
IO0SET |= leftSFT<<16;
leftSFT<<=1;
if(leftSFT > 1U<<7) leftSFT = 1;
}
else if(IS_ON(key3))
{
reset_values(3);
IO0CLR = 0xFF<<16;
IO0SET |= rightSFT<<16;
23
Microcontroller & Embedded Systems Lab - 21CS44
rightSFT>>=1;
if(rightSFT < 1) rightSFT = 1U<<7;
}
delay_ms(100);
}
}
void reset_values(int y)
{
switch(y)
{
case 0: contDN = 99;
rightSFT = 1U<<7;
leftSFT = 1;
break;
case 1: contUP = 0;
rightSFT = 1U<<7;
leftSFT = 1;
break;
case 2: contUP = 0;
contDN = 99;
rightSFT = 1U<<7;
break;
case 3: contUP = 0;
contDN = 99;
leftSFT = 1;
break;
}
}
24
Microcontroller & Embedded Systems Lab - 21CS44
Output Observation:
25
Microcontroller & Embedded Systems Lab - 21CS44
Serial In Parallel Out mode of Shift Register (74HC4094) is used to send 8 bits of data to
seven segment display. Seven segment display used is of common anode type i.e. we have to
send 0 to make corresponding segment ON and 1 to make it OFF.
DP G f e d c b a
1 0 1 1 0 0 0 0
This is B0 in hexadecimal. To send B0H we have to start sending the bits from MSB onwards
i.e D7 first, D6 next and so on with D0 being the last
Clock pulses are required to clock in the data, 8 clock pulses for one byte of data. As shift
registers are cascaded, 8*4=32 clocks are required to clock in 4 bytes of data. To send “12345”,
first we have to send ‘1’, then ‘2’,‘3’ ,’4’ and lastly ‘5’. All the shift registers are cascaded, the
data is fed to the shift register using serial in parallel out method. Strobe is used to copy the
shifted data to the output pins. STB is generated after shifting is comleted.
26
Microcontroller & Embedded Systems Lab - 21CS44
Interfacing Diagram
#include <lpc214x.h>
#define LED_OFF (IO0SET = 1U << 31)
#define LED_ON (IO0CLR = 1U << 31)
#define PLOCK 0x00000400
void delay_ms(unsigned int j);
void SystemInit(void);
unsigned char getAlphaCode(unsigned char alphachar);
void alphadisp7SEG(char *buf);
int main()
{
IO0DIR |= 1U << 31 | 1U << 19 | 1U << 20 | 1U << 30 ; // to set as o/ps
LED_ON; // make D7 Led on .. just indicate the program is running
SystemInit();
while(1)
{
alphadisp7SEG("fire ");
delay_ms(500);
alphadisp7SEG("help ");
delay_ms(500);
}
27
Microcontroller & Embedded Systems Lab - 21CS44
28
Microcontroller & Embedded Systems Lab - 21CS44
29
Microcontroller & Embedded Systems Lab - 21CS44
Output Observation:
30
Microcontroller & Embedded Systems Lab - 21CS44
▪ Total number of steps for one revolution = 200 steps (200 teeth shaft)
Step angle = 360°/200 = 1.8°
▪ Use appropriate delay in between consequent steps
▪ 2Phase, 4winding stepper motor is used, along with driver circuit(ULN 2803) built on
the RV All-In- One Card, 12v power is used to drive the stepper motor. Digital input
generated by the microcontroller, is used to drive and control the direction and rotation
of stepper motors. If it is required to drive bigger/higher torque stepper motors only
change is- use MOSFETS or higher power stepper driver ICs to drive motors
31
Microcontroller & Embedded Systems Lab - 21CS44
#include <lpc214x.h>
#define LED_OFF (IO0SET = 1U << 31)
#define LED_ON (IO0CLR = 1U << 31)
#define PLOCK 0x00000400
void delay_ms(unsigned int j);
void SystemInit(void);
int main()
{
unsigned int no_of_steps_clk = 100, no_of_steps_aclk = 100;
IO0DIR |= 1U << 31 | 0x00FF0000 | 1U << 30; // to set P0.16 to P0.23 as o/ps
LED_ON; delay_ms(500);LED_OFF; // make D7 Led on .. just indicate the program is running
SystemInit( );
do{
IO0CLR = 0X000F0000;IO0SET = 0X00010000;delay_ms(10);if(--no_of_steps_clk == 0) break;
IO0CLR = 0X000F0000;IO0SET = 0X00020000;delay_ms(10);if(--no_of_steps_clk == 0) break;
IO0CLR = 0X000F0000;IO0SET = 0X00040000;delay_ms(10);if(--no_of_steps_clk == 0) break;
IO0CLR = 0X000F0000;IO0SET = 0X00080000;delay_ms(10);if(--no_of_steps_clk == 0) break;
}while(1);
do{
IO0CLR = 0X000F0000;IO0SET = 0X00080000;delay_ms(10);if(--no_of_steps_aclk == 0) break;
IO0CLR = 0X000F0000;IO0SET = 0X00040000;delay_ms(10);if(--no_of_steps_aclk == 0) break;
IO0CLR = 0X000F0000;IO0SET = 0X00020000;delay_ms(10);if(--no_of_steps_aclk == 0) break;
IO0CLR = 0X000F0000;IO0SET = 0X00010000;delay_ms(10);if(--no_of_steps_aclk == 0) break;
}while(1);
IO0CLR = 0X00FF0000;
while(1);
}
32
Microcontroller & Embedded Systems Lab - 21CS44
Output Observation:
33
Microcontroller & Embedded Systems Lab - 21CS44
• DAC module of LPC 2148 is 10 bit Digital to Analog converter used to convert 10 bit
Digital data to corresponding Analog voltage.
• Digital I/P : 000 to 3FF (0 to 1023), corresponding Analog O/P : 0V to 3.3V
• Resolution = (3.3/1024) ≈ 3.2mili volts
Look up Table Creation: Look up tables are used extensively in embedded systems, to store
precomputed digital values, corresponding to analog voltages and used to generate different
waveforms using DAC Module. Here the explanation about creating sine table is given.
Formula for calculation of the sine table entries: 512 + 511 x Sin Ѳ
(512 Corresponds to 1FFh, i.e. 3.3/2 V, 511 x SIN 90 gives 511, so 512 + 511 = 1023 (for
3.3V). Calculate the digital values to be outputted to DAC for angles in the steps of 6 o,
511 x sin 0 = 0 511 x sin 48 = 380
511 x sin 6 = 53 511 x sin 54 = 413
511 x sin 12 = 106 511 x sin 60 = 442
511 x sin 18 = 158 511 x sin 66 = 467
511 x sin 24 = 208 511 x sin 72 = 486
511 x sin 30 = 256 511 x sin 80 = 503
511 x sin 36 = 300 511 x sin 86 = 510
511 x sin 42 = 342 511 x sin 90 = 511
34
Microcontroller & Embedded Systems Lab - 21CS44
Output the above values in the reverse order to get other portion of the top half cycle, (add
512 for top half cycle, and subtract from 512 for the lower half cycle, refer the table
declaration).
35
Microcontroller & Embedded Systems Lab - 21CS44
#include <lpc214x.h>
#include <stdio.h>
#define PLOCK 0x00000400
#define LED_OFF (IO0SET = 1U << 31)
#define LED_ON (IO0CLR = 1U << 31)
#define SW2 (IO0PIN & (1 << 14))
#define SW3 (IO0PIN & (1 << 15))
#define SW4 (IO1PIN & (1 << 18))
#define SW5 (IO1PIN & (1 << 19))
#define SW6 (IO1PIN & (1 << 20))
void SystemInit(void);
static void delay_ms(unsigned int j);//millisecond delay
short int sine_table[ ] =
{512+0,512+53,512+106,512+158,512+208,512+256,512+300,512+342,512+380,512+413,
512+442,512+467,512+486,512+503,512+510,512+511,
512+510,512+503,512+486,512+467,512+442,512+413,512+380,512+342,512+300,512+25
6,512+208,512+158,512+106,512+53,512+0,
512-53,512-106,512-158,512-208,512-256,512-300,512-342,512-380,512-413,512-442,512-
467,512-486,512-503,512-510,512-511,
512-510,512-503,512-486,512-467,512-442,512-413,512-380,512-342,512-300,512-
256,512-208,512-158,512-106,512-53};
short int sine_rect_table[ ] =
{512+0,512+53,512+106,512+158,512+208,512+256,512+300,512+342,512+380,512+413,
512+442,512+467,512+486,512+503,512+510,512+511,
512+510,512+503,512+486,512+467,512+442,512+413,512+380,512+342,512+300,512+25
6,512+208,512+158,512+106,512+53,512+0};
int main()
{
short int value,i=0;
SystemInit();
PINSEL1 |= 0x00080000; /* P0.25 as DAC output :option 3 - 10 (bits18,19)*/
IO0DIR |= 1U << 31 | 0x00FF0000 ; // to set P0.16 to P0.23 as o/ps
while(1)
{
if (!SW2) /* If switch for sine wave is pressed */
{
while (i!=60 )
{
value = sine_table[i++];
DACR = ( (1<<16) | (value<<6) );
delay_ms(1);
}
i=0;
}
36
Microcontroller & Embedded Systems Lab - 21CS44
else if (!SW3)
{
while ( i!=30 )
{
value = sine_rect_table[i++];
DACR = ( (1<<16) | (value<<6) );
delay_ms(1);
}
i=0;
}
else if ( !SW4) /* If switch for triangular wave is pressed */
{
value = 0;
while ( value != 1023 )
{
DACR = ( (1<<16) | (value<<6) );
value++;
}
while ( value != 0 )
{
DACR = ( (1<<16) | (value<<6) );
value--;
}
}
else if ( !SW5 ) /* If switch for sawtooth wave is pressed */
{
value = 0;
while ( value != 1023 )
{
DACR = ( (1<<16) | (value<<6) );
value++;
}
}
else if ( !SW6 ) /* If switch for square wave is pressed */
{
value = 1023;
DACR = ( (1<<16) | (value<<6) );
delay_ms(1);
value = 0;
DACR = ( (1<<16) | (value<<6) );
delay_ms(1);
}
else /* If no switch is pressed, 3.3V DC */
{
value = 1023;
DACR = ( (1<<16) | (value<<6) );
}
}
}
37
Microcontroller & Embedded Systems Lab - 21CS44
void SystemInit(void)
{
PLL0CON = 0x01;
PLL0CFG = 0x24;
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
while( !( PLL0STAT & PLOCK ))
{ ; }
PLL0CON = 0x03;
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
}
void delay_ms(unsigned int j)
{
unsigned int x,i;
for(i=0;i<j;i++)
{
for(x=0; x<10000; x++);
}
}
38
Microcontroller & Embedded Systems Lab - 21CS44
Output Observation:
39
Microcontroller & Embedded Systems Lab - 21CS44
Interfacing Diagram
Working method:
➢ If no key is pressed, we will have on columns 0-3, ‘1111’ on P1.16 to P1.19, as all the
inputs are pulled up by pull up resistors.
➢ If we press any key, let ‘0’ key be pressed, it will short row0 and col0 lines (P0.16 &
P1.19), so whatever data (0 or 1) available at row0 (P0.16) is available at col0 (P1.19).
Since already columns are pulled high, it is required to apply logic ‘0’ to see change
in col0 when the key is pressed.
➢ To identify which key is pressed,
▪ Check for a key press in first row by out putting – ‘0111’on row’s, check which
column data is changed, if no key press go for next row
▪ Check for a key press in second row by out putting – ‘1011’on row’s, check which
column data is changed, if no key press go for next row
▪ Check for a key press in third row by out putting – ‘1101’on row’s, check which
column data is changed, if no key press go for next row
▪ Check for a key press in last row by out putting – ‘1110’on row’s, if no key is
pressed go for the first row again
➢ Once the key press is found, use the row number and column number and look up table
to convert the key position corresponding to ascii code. Use appropriate delay for
debouncing.
40
Microcontroller & Embedded Systems Lab - 21CS44
//Matrix 4 x 4 Keyboard
//Columns & Rows are pulled to +5v,if dont press key, we receive '1' on columns
//Method: Sending '0' to a selected row, checking for '0' on each column
//ROWS - ROW0-ROW3 -> P0.16,P0.17,P0.18,P0.19
//COLS - COL0-COL3 -> P1.19,P1.18,P1.17,P1.16
#include <lpc214x.h>
#define PLOCK 0x00000400
#define LED_OFF (IO0SET = 1U << 31)
#define LED_ON (IO0CLR = 1U << 31)
#define COL0 (IO1PIN & 1 <<19)
#define COL1 (IO1PIN & 1 <<18)
#define COL2 (IO1PIN & 1 <<17)
#define COL3 (IO1PIN & 1 <<16)
void SystemInit(void);
void delay_ms(unsigned int j);
void uart_init(void);
unsigned char lookup_table[4][4]={ {'0', '1', '2','3'},
{'4', '5', '6','7'},
{'8', '9', 'a','b'},
{'c', 'd', 'e','f'}};
unsigned char rowsel=0,colsel=0;
int main( )
{
SystemInit();
uart_init();//initialize UART0 port
IO0DIR |= 1U << 31 | 0x00FF0000; // to set P0.16 to P0.23 as o/ps
//make D7 Led on off for testing
LED_ON; delay_ms(500);LED_OFF;delay_ms(500);
do
{
while(1)
{
//check for keypress in row0,make row0 '0',row1=row2=row3='1'
rowsel=0;IO0SET = 0X000F0000;IO0CLR = 1 << 16;
if(COL0==0){colsel=0;break;};if(COL1==0){colsel=1;break;};
if(COL2==0){colsel=2;break;};if(COL3==0){colsel=3;break;};
//check for keypress in row1,make row1 '0'
rowsel=1;IO0SET = 0X000F0000;IO0CLR = 1 << 17;
if(COL0==0){colsel=0;break;};if(COL1==0){colsel=1;break;};
if(COL2==0){colsel=2;break;};if(COL3==0){colsel=3;break;};
//check for keypress in row2,make row2 '0'
rowsel=2;IO0SET = 0X000F0000;IO0CLR = 1 << 18;//make row2 '0'
if(COL0==0){colsel=0;break;};if(COL1==0){colsel=1;break;};
41
Microcontroller & Embedded Systems Lab - 21CS44
if(COL2==0){colsel=2;break;};if(COL3==0){colsel=3;break;};
//check for keypress in row3,make row3 '0'
rowsel=3;IO0SET = 0X000F0000;IO0CLR = 1 << 19;//make row3 '0'
if(COL0==0){colsel=0;break;};if(COL1==0){colsel=1;break;};
if(COL2==0){colsel=2;break;};if(COL3==0){colsel=3;break;};
};
delay_ms(50); //allow for key debouncing
while(COL0==0 || COL1==0 || COL2==0 || COL3==0);//wait for key release
delay_ms(50); //allow for key debouncing
IO0SET = 0X000F0000; //disable all the rows
U0THR = lookup_table[rowsel][colsel]; //send to serial port(check on the terminal)
}
while(1);
}
void uart_init(void)
{
//configurations to use serial port
PINSEL0 |= 0x00000005; // P0.0 & P0.1 ARE CONFIGURED AS TXD0 & RXD0
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U0DLM = 0; U0DLL = 8; // 115200 baud rate
U0LCR = 0x03; /* DLAB = 0 */
U0FCR = 0x07; /* Enable and reset TX and RX FIFO. */
}
void SystemInit(void)
{
PLL0CON = 0x01;
PLL0CFG = 0x24;
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
while( !( PLL0STAT & PLOCK ))
{;}
PLL0CON = 0x03;
PLL0FEED = 0xAA; // lock the PLL registers after setting the required PLL
PLL0FEED = 0x55;
VPBDIV = 0x01; // PCLK is same as CCLK i.e 60Mhz
}
void delay_ms(unsigned int j)
{
unsigned int x,i;
for(i=0;i<j;i++)
{
for(x=0; x<10000; x++);
}
}
42
Microcontroller & Embedded Systems Lab - 21CS44
Output Observation:
43
Microcontroller & Embedded Systems Lab - 21CS44
Interfacing Diagram
44
Microcontroller & Embedded Systems Lab - 21CS44
#include <lpc214x.h>
#define LED_OFF (IO0SET = 1U << 31)
#define LED_ON (IO0CLR = 1U << 31)
#define PLOCK 0x00000400
void delay_ms(unsigned int j);
void SystemInit(void);
void runDCMotor(int direction,int dutycycle);
unsigned int adc(int no,int ch);
int main()
{
int dig_val;
IO0DIR |= 1U << 31 | 0x00FF0000 | 1U << 30; // to set P0.16 to P0.23 as o/ps
LED_ON; delay_ms(500);LED_OFF; // make D7 Led on / off for program checking
SystemInit( );
do{
dig_val = adc(1,2) / 10;
if(dig_val > 100) dig_val =100;
runDCMotor(2,dig_val); // run at 10% duty cycle
}
while(1);
}
45
Microcontroller & Embedded Systems Lab - 21CS44
void SystemInit(void)
{
PLL0CON = 0x01;
PLL0CFG = 0x24;
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
while( !( PLL0STAT & PLOCK ))
{;}
PLL0CON = 0x03;
PLL0FEED = 0xAA; // lock the PLL registers after setting the required PLL
PLL0FEED = 0x55;
VPBDIV = 0x01; // PCLK is same as CCLK i.e 60Mhz
}
void delay_ms(unsigned int j)
{
unsigned int x,i;
for(i=0;i<j;i++)
{
for(x=0; x<10000; x++);
}
}
46
Microcontroller & Embedded Systems Lab - 21CS44
Output Observation:
47
Microcontroller & Embedded Systems Lab - 21CS44
LCD’s are preferred to seven segment displays because of their versatility and capability to
house more information.2 line (2 x 6) and 4 line (4x20) is the most popular, low cost character
oriented LCD, suitable for understanding the working and programming of LCD. You have
seen LCD modules used in many of the electronics devices like coin phone, billing machine
and weighing machines. It is a powerful display options for stand-alone systems. Because of
low power dissipation, high readability, flexibility for programmers, LCD modules are
becoming popular.
LCD consists of DDRAM, CGROM, Shift registers, bit/pixel drivers, refreshing logics and lcd
controller. The data to be displayed on lcd, is to be written on to the DDRAM-display data Ram
using the ascii format. CGROM-Character generator rom, contains dot/pixel patterns for every
character to be displayed (pre programmed). Shift registers are used to convert CGROM
parallel data to serial data(serializing), drivers are required to drive (ON/OFF) the bits,
refreshing logics are required to hold the display data, as the dots are displayed row by row
basis continuously, like in CRT.
communicate, whatever the data we write to LCD is of two types, either it is a command to the
LCD(to configure) or ASCII code of character to be displayed on LCD (to DDRAM). RS signal
is used for this,
RS (Register Select): 0 or 1
0 - writing command byte into command register of LCD
1 - writing data (ASCII code) into Data register of LCD
R/W : (Read/Write) (In our kit, R/W is grounded)
0- Write to LCD (Data/Command)
1- Read from the LCD
E (Enable) : 1 to 0 pulse
- Enable is required to perform the writing/reading to LCD, E – ‘1’ (for 450nsec) & then ‘0’
(High to Low Pulse)
D0-D7 - It is a bidirectional data bus, used to write data/command to LCD or reading status. In
4bit nibble mode, only lines D4 – D7 are used for communication.
48
Microcontroller & Embedded Systems Lab - 21CS44
Interfacing Diagram
Instructi D7 D6 D5 D4 D3 D2 D1 D0 Descripti
on on
Clear Clears Display and returns cursor to home
0 0 0 0 0 0 0 1
display position.
Returns cursor to home position. Also
Cursor 0 0 0 0 0 0 1 X returns display being shifted to the original
home
position.
I/D = 0 - cursor is in decrement position.
Entry I/D = 1 - cursor is in increment position.
mode set 0 0 0 0 0 1 I/D S S = 0 - Shift is invisible.
S = 1 - Shift is visible
49
Microcontroller & Embedded Systems Lab - 21CS44
2. Writing actual string data to LCD, character by character, (by default characters are
displayed from line1 first column position, we can issue DDRAM address command -
0x80 + char pos, for first line, 0xc0 + char pos, for second line,0x94+char pos, for third
line, 0xD4+char pos, for fourth line).
#include <lpc214x.h>
#define PLOCK 0x00000400
#define LED_OFF (IO0SET = 1U << 31)
#define LED_ON (IO0CLR = 1U << 31)
#define RS_ON (IO0SET = 1U << 20)
#define RS_OFF (IO0CLR = 1U << 20)
#define EN_ON (IO1SET = 1U << 25)
#define EN_OFF (IO1CLR = 1U << 25)
void SystemInit(void);
static void delay_ms(unsigned int j);//millisecond delay
static void delay_us(unsigned int count);//microsecond delay
static void LCD_SendCmdSignals(void);
static void LCD_SendDataSignals(void);
static void LCD_SendHigherNibble(unsigned char dataByte);
static void LCD_CmdWrite( unsigned char cmdByte);
static void LCD_DataWrite( unsigned char dataByte);
static void LCD_Reset(void);
static void LCD_Init(void);
void LCD_DisplayString(const char *ptr_stringPointer_u8);
int main()
{
SystemInit();
IO0DIR |= 1U << 31 | 0x00FF0000 ; // to set P0.16 to P0.23 as o/ps
IO1DIR |= 1U << 25; // to set P1.25 as o/p used for EN
// make D7 Led on off for testing
LED_ON; delay_ms(500);LED_OFF;delay_ms(500);
LCD_Reset();
LCD_Init();
delay_ms(100);
50
Microcontroller & Embedded Systems Lab - 21CS44
51
Microcontroller & Embedded Systems Lab - 21CS44
52
Microcontroller & Embedded Systems Lab - 21CS44
Output Observation:
53
Microcontroller & Embedded Systems Lab - 21CS44
Mini Project
54
Microcontroller & Embedded Systems Lab - 21CS44
55
Microcontroller & Embedded Systems Lab - 21CS44
void PWM_RGBLed_test(void)
{
unsigned int i;
PWM_Init();
for(i=0;i<=100;i=i+1)
{
PWMMR4 = 100; PWMMR5 = i; PWMMR6 = 100;
PWMLER = 0X70; // to enable copy to Match registers from shadow regs. 1110000
delay_ms(1000);
}
for(i=0;i<=100;i=i+1)
{
PWMMR4 = 100; PWMMR5 = 100; PWMMR6 = i;
PWMLER = 0X70; // to enable copy to Match registers from shadow regs. 1110000
delay_ms(1000);
}
for(i=0;i<=100;i=i+1)
{
PWMMR4 = i PWMMR5 = 100; PWMMR6 = 100;
PWMLER = 0X70; // to enable copy to Match registers from shadow regs. 1110000
delay_ms(1000);
}
}
void PWM_Init(void)
{
PINSEL0 |= 2 << 18 | 2 << 16 ;
//SELECT P0.8 PWM4 AND P0.9PWM6 AS 2ND OPTION FOR PWM OPERATION
PINSEL1 |= 1 << 10; //SELECT P0.21 PWM5 AS OPTION 1 FOR PWM OPERATION
PWMPCR = (1 << 12 | 1 << 13 | 1 << 14); //Enable PWM4,PWM5 and PWM
PWMMR0 = 100; // load the value to MR0 to fix the pulse rate
PWMTCR = 0x00000009; // bit D3 = 1 (enable PWM), bit D0=1 (start the timer)
}
56
Microcontroller & Embedded Systems Lab - 21CS44
Joystick Interface
char Joystick_position(void)
{
// returns joystic position
// Up , Left , Down, Right & Enter
unsigned int adc_result=0;
char res =6;
PINSEL0|= 0x0C000000; /* Select the P0_13 AD1.4 for ADC function */
adc_result = adc(1,4);
if(adc_result == 0 && adc_result < 5) res =6;
else if(adc_result > 180 && adc_result < 185) res = 0; // Up
else if(adc_result > 315 && adc_result < 325) res = 1; // Left
else if(adc_result > 460 && adc_result < 470) res = 2; // Down
else if(adc_result > 614 && adc_result < 624) res = 3; // Right
else if(adc_result > 770 && adc_result < 800) res = 4; // Enter
return res;
57
Microcontroller & Embedded Systems Lab - 21CS44
58
Microcontroller & Embedded Systems Lab - 21CS44
59