Microcontroller and Applications
Microcontroller and Applications
2. CIP-51 Architecture
2. Skills:
To write assembly language and embedded C programs.
To apply knowledge for the development of multidisciplinary projects.
3.Attitude:
To select the appropriate microcontroller for the desired application.
To develop real-world applications using a microcontroller.
Course Objectives & Course Outcomes:
Course Outcomes: After completion of this course students will be able to
7. Interfacing DC motor and control its speed using PWM with C8051F340.
Introduction to Microcontroller
• Microprocessor and Microcontroller comparison,
• Memory organization
• Instruction set
• Assembly programming
Terminologies:
• Integrated Circuit (IC): A miniaturized electronic circuit that consists of
semiconductor devices and passive components contained in a package
• Central Processing Unit (CPU): This refers to the core of the MCU that
executes code
• “n-bit” – the “n” refers to the data bus width of the CPU, and is the maximum
width of data it can handle at a time
• Examples: 8-bit MCU, 32-bit MCU
Micro-processors and Microcontrollers
https://raspberrytips.com/is-raspberry-pi-a-microcontroller/
Microprocessor Vs Microcontroller
Microprocessor Microcontroller
Eg. Intel 8085, 8086, Motorola’s Eg. Intel’s MCS-51, Atmel’s AT89c51
680x
Role of microcontroller in Embedded System
• A combination of hardware and software designed to perform a dedicated
function
• Embedded systems are computing systems with tightly coupled hardware
and software integration.
• Designed to perform dedicated function
• Embedded systems are part of a larger system or product,
-e.g., antilock braking system in a car
• Embedded systems are tightly coupled to their environment → imposes real-
time constraints by the need to interact with the environment
Embedded Products Using Microcontrollers (Applications)
• Home
• Appliances, intercom, telephones, security systems, garage door openers, answering
machines, fax machines, home computers, TVs, cable TV tuner, VCR, camcorder,
remote controls, video games, cellular phones, musical instruments, sewing machines,
lighting control, paging, camera, pinball machines, toys, exercise equipment
• Office
• Telephones, computers, security systems, fax machines, microwave, copier, laser
printer, color printer, paging
• Auto
• Trip computer, engine control, air bag, instrumentation, security system, transmission
control, entertainment, climate control, cellular phone, keyless entry
Is 8-bit Still Relevant?
• “n-bit” – the “n” refers to the data bus width of the CPU, and is the
maximum width of data it can handle at a time
• More and more advanced features and peripherals are added to 8-bit
processors by various vendors
https://www.manualslib.com/manual/152880/Silicon-Laboratories-
C8051f341.html?page=73#manual
Important Basics Microcontroller
(Small sized Computer)
Differences between Von Neumann and Harvard
architecture
Comparison of Von Neumann and Harvard
Memory Classes and Layout introduces the different memory types used to programming the 8051 microcontroller and
variants. Memory classes identify distinct physical memory regions, which can be represented in a memory layout. Physical
memory regions in an A51 system include:
Program Memory: in the classic 8051, this is a 64KB space called CODE. Typically, this region is a ROM space that used for
the program code and constants. Constants are fetched with the MOVC instruction.
Internal Data Memory: in the classic 8051, this is the on-chip RAM space with a maximum of 256 Bytes containing register
banks, BIT space, direct addressable DATA space, and indirect addressable IDATA space. This region should be used for
frequently used variables.
External Data Memory: in classic 8051 devices, this area, called XDATA, is off-chip RAM with a space of up to 64KB.
However, several new 8051 devices have additional on-chip RAM that is mapped into the XDATA space. Usually, this
additional on-chip RAM has to be enabled via dedicated SFRs.
1. First 32 bytes of memory
location in RAM are used as
registers.(R0 to R7)
https://www.manualslib.com/manual/152880/Silicon-Laboratories-
C8051f341.html?page=78#manual
Instructions Format
Assembly language instructions specify the program code that is to be assembled by the Ax51 assembler.
The Ax51 assembler translates the assembly instructions in your program into machine code and stores the resulting code
in an object file.
Assembly instructions have the following general format:
mnemonic is the ASCII text string that symbolically represents a machine language instruction.
is an optional description or explanation of the instruction. A comment may contain any text
comment
you wish. Comments are ignored by the assembler.
Using Operands and Expressions
Assembler instructions support a wider variety of operands than do directives. Some instructions require no
operands and some may require up to 3 operands. Multiple operands are separated by commas. For example:
MOV R2, #0
The number of operands that are required and their types depend on the instruction or directive that is
specified. In the following table, the first four operands can also be expressions. Instruction operands can be
classified as one the following types:
Addressing Modes
• Five modes of addressing
• The different addressing modes determine how the
operand byte is selected
Register MOV A, B
Operand is in register
• Assembler Directives
• ORG
• EQU
• END
• Data Type:
• DB
Note: For the mnemonic, number of bytes, and number of clock cycles for
each instruction refer Table 9.1 in the datasheet
Many instructions have required arguments that are described in the following table:
Argument Description
Addr11 An 11-bit address destination. This argument is used by ACALL and AJMP instructions. The target of the
(11 bit address) CALL or JMP must lie within the same 2K page as the first byte of the following instruction.
Addr16
A 16-bit address destination. This argument is used by LCALL and LJMP instructions.
(16 bit address)
bit A direct addressed bit in internal data RAM or SFR memory.
A signed (two's complement) 8-bit offset (-128 to 127) relative to the first byte of the following
offset
instruction.
@Ri An internal data RAM location (0-255) addressed indirectly through R0 or R1.
Rn Register R0-R7.
Arithmetic Flags
Flag: It is a 1-bit register that indicates the status of
the result from an operation
0V Overflow flag
UD User definable flag
P Parity flag; P=1 for Odd no. of 1’s; P=0 for Even no.
of 1’s
Related Terms
Micro Controller performs all operations in hex
https://developer.arm.com/documentation/101655/0961/8051-Instruction-Set-Manual/Instructions/MOV?lang=en
X can be 0 or 1
Program Branching Instructions
Sr. No Mnemonic Description
95 ACALL addr11 Absolute subroutine call
96 LCALL addr16 Long subroutine call
97 RET Return from subroutine
98 RETI Return from interrupt
99 AJMP addr11 Absolute Jump
100 LJMP addr16 Long Jump
101 SJMP rel Short Jump (relative addr)
102 JMP @A+DPTR Jump indirect relative to DPTR
103 JZ rel Jump is accumulator is zero
104 JNZ rel Jump if accumulator is not zero
105 CJNE A, direct, rel Compare direct byte to accumulator and jump if not equal
106 CJNE A, #data, rel Compare immediate to accumulator and jump if not equal
107 CJNE Rn, #data, rel Compare immediate to register and jump if not equal
108 CJNE @Ri, #data, rel Compare immediate to indirect and jump if not equal
109 DJNZ Rn, rel Decrement register and jump if not zero
110 DJNZ direct, rel Decrement direct byte and jump if not zero
Carry
Some instructions related to carry Flag
ADDC
The ADDC instruction adds a byte value and the value of the carry flag to the
accumulator. The results of the addition are stored back in the accumulator.
Several of the flag registers are affected.
Example: ADDC A, #23h
CLR
The CLR instruction sets the specified destination operand to a value of 0.
Example: CLR C
SETB
• The SETB instruction sets the bit operand to a value of 1. This instruction can
operate on the carry flag or any other directly addressable bit. No flags are
affected by this instruction.
Example: SETB C
Program1: Addition of two 16 bit Numbers
ORG 0000H ; Operand 1 (AB20H) Operand 2 (65DE)
CLR C ;MAKE CY=0
MOV A,#020H ;LOWER BYTE OF OPERAND 1(20H) IN A
ADD A,#0DEH ;ADD LOWER BYTE OF OPERAND (DE)2 WITH A
MOV R1,A ;STORE Lower byte OF RESULT IN R1
MOV A,#65H ;HIGHER BYTE OF OPERAND 2 IN A
ADDC A,#0ABH ; ADD WITH HIGHER BYTE OF OPERAND 1
MOV R0,A ;STORES MSB OF RESULT IN R0
END
Increment /Decrement
INC
The INC instruction increments the specified operand by 1. An original
value of 0FFh or 0FFFFh overflows to 00h or 0000h. No flags are
affected by this instruction.
Example: INC A(A=A+1) register content increment
INC 34h Address increment
INC DPTR
INC Rn
Logical: OR, XOR, AND, SWAP
MOV A, #20H //0010 0000//
MOV R0, #03H //0000 0011//
XRL A, R0 // 0010 0011//
DJNZ
The DJNZ instruction decrements the byte indicated by the first
operand and, if the resulting value is not zero, branches to the address
specified in the second operand.
Addition of 5(N), 8 bit numbers stored in
memory Memory Location Data Values
40H 01H
ORG 0000H
MOV R0, #40H 41H 02H
MOV R2, #05H
MOV 46H,#00H 42H 03H
MOV 45H,#00H
MOV A,#00H 43H 04H
LABEL2:ADD A,@R0
JNC LABEL1 44H 05H
INC 45H
LABEL1:INC R0 45H 00H(MSB of result)
DJNZ R2, LABEL2
46H 00H(LSB of result)
MOV 46H,A
END
Special Insructions
DA
The DA instruction adjusts the eight-bit value in the Accumulator resulting from the earlier addition of two
variables (each in packed-BCD format), producing two four-bit digits. Any ADD or ADDC instruction may have
been used to perform the addition.
If Accumulator bits 3-0 are greater than nine (xxx1010-xxx1111), or if the AC flag is one, six is added to the
Accumulator, producing the proper BCD digit in the low-order nibble. This internal addition would set the
carry flag if a carry-out of the low-order four-bit field propagated through all high-order bits, but it would
not clear the carry flag otherwise.
If the carry flag is now set, or if the four high-order bits now exceed nine (1010xxx-111xxxx), these high-
order bits are incremented by six, producing the proper BCD digit in the high-order nibble. Again, this would
set the carry flag if there was a carry-out of the high-order bits, but would not clear the carry. The carry flag
thus indicates if the sum of the original two BCD variables is greater than 100, allowing multiple precision
decimal addition. OV is not affected.
All of this occurs during the one instruction cycle. Essentially, this instruction performs the decimal
conversion by adding 00H, 06H, 60H, or 66H to the Accumulator, depending on initial Accumulator and PSW
conditions.
Find square of a number using DPTR
ORG 0000H
1.Initialize R0 with memory address where all squares are stored and R1 with 0.
2.Increment R1 to find the square of a number.
3.Find the square of a number by multiplying A and B with same number.
4.Store the result with the assigned address array.
ORG 0000h
LJMP MAIN
ORG 40h
MAIN: MOV R0,#10H ; memory address where all the squares will be stored
MOV R1,#00
LOOP: INC R1 ;incrementing each time to generate a new square
MOV A,R1
MOV B,R1
MUL AB ;finding the square of a number
MOV @R0,A ;storing the square of all the numbers in an array
INC R0
CJNE R1,#10,LOOP ; checking the counter value whether it has reached zero
END
Rotate Instructions
RL
The RL instruction rotates the eight bits in the accumulator left one bit
position. Bit 7 of the accumulator is rotated into bit 0, bit 0 into bit 1,
bit 1 into bit 2, and so on. No flags are affected by this instruction.
Example RL A
RR
The RR instruction rotates the eight bits in the accumulator right one
bit position. Bit 0 of the accumulator is rotated into bit 7, bit 7 into bit
6, and so on. No flags are affected by this instruction.
Example RR A
MOV A, #25h
Rotate RR A
Instructions
What will be the content in A ?
MIT-WPU ECE
Program to count number of 1’s in a given
byte describes RRC Instruction
https://www.silabs.com/documents/public/datasheets/C8051F34x.pdf
Instruction set
http://www.keil.com/support/man/docs/is51/is51_xch.htm