Microcontroller 8051 v1
Microcontroller 8051 v1
Microcontrollers
Microcontroller vs General-purpose Microprocessor
Address bus
Microcontroller
The 8051 Family
In 1981, Intel Corporation introduced an 8-bit microcontroller called the 8051
4K bytes of on-chip
ROM
Two timers
Port 1 Port 0
Port 3
Port 2
Intel MCS-51 family
Address/data b
Serial port
interrupts
Timer inputs
The most widely used registers of the 8051 are A (accumulator), B, R0, R1, R2,
R3, R4, R5, R6, R7 and DPTR (data pointer) and PC (program counter). All of the
above registers are 8-bits except DPTR and program counter.
A
B
R0 DPTR DPH DPL
R1
R2 PC PC (program counter)
R3
R4
The point to remember is that no member of the 8051
family can access more than 64K bytes of opcode since the
R5
program counter in the 8051 is a 16-bit register (0000 to
R6
FFFF address range).
R7
Assembling and Running an 8051 Program
Editor Ex. MS-DOS EDIT, Notepad
Program
myfile.asm
The assembler converts the instructions
Assembler into machine code. It will produce an
The lst file lists all the Program object file and a list file
opcodes and addresses
as well as errors that the myfile.lst
assembler detected. This myfile.obj Other obj files
file can be accessed by
Linker Assemblers requires a third step called
an editor such as DOS
EDIT Program linking. The link program takes one or
more object files and produces an
absolute object file with the extension
myfile.abs “abs”.
The program counter in the 8051 is 16 bits wide. This means that the 8051 can access
program addresses 0000 to FFFF, a total of 64K bytes of code. However, not all
members of the 8051 have the entire 64K bytes of on chip ROM installed.
Address code
0000 7D
0001 25 Note:
0002 7F MOV R6,#12 ; load 12 decimal (0CH)
0003 34 MOV R5,#0F9H; that a 0 is used between the #
0004 74 and F to indicate that F is a hex number and not
0005 00 a letter
0006 2D
MOV R5,#F9H; will cause an error
0007 2F
0008 24
MOV A,#7F2H ; ILLEGAL: 7F2H>8bits (FFH)
0009 12 MOV R2,#456;ILLEGAL: 456>255 decimal (FFH)
000A 80
000B FE
MCU 8051 IDE
128 Byte
Memory Space Main Registers Special Function Registers SFR
ORG 0H ;START AT LOCATION 0
MOV R5,#25H ;LOAD 25h INTO R5
MOV R7,#34H ;LOAD 34H INTO R7
MOV A,#0 ;LOAD 0 INTO A
ADD A,R5 ;ADD CONTENTS OF R5 TO A
ADD A,R7 ;ADD CONTENTS OF R7 TO A
ADD A,#12H ;A=A+12H
END ;END OF ASM SOURCE FILE
RAM memory space allocation in the 8051
The 128 bytes of RAM inside the 8051 are assigned addresses 00 to 7FH. These 128
bytes are divided into three different groups as follows.
7F
30
2F
Bit-Addressable RAM
20
1F
Register Bank 3
18
17
Register Bank 2
10
0F
Register Bank 1 (Stack)
08
07
Register Bank 0 PSW (program status word) register
00
By default RS1 = 0 and RS0 = 0
Register Bank 0
PSW (program status word) register
PSW (program status word) register
PSW (Program Status Word) Register
The PSW register is an 8-bit register. It is also referred as the flag register.
Although the PSW register is 8 bits wide, only 6 bits of it are used by the 8051. The
two unused bits are user-definable flags.
CY AC F0 RS1 RS0 OV - P
ORG 0H
SETB PSW.4 ;MAKE RS1=1 FOR SELECTING BANK3
SETB PSW.3 ;MAKE RS0=1 FOR SELECTING BANK3
MOV R0,#05H ;LOAD R0 WITH VALUE 05H
MOV R1,#35H ;LOAD R1 WITH VALUE 05H
MOV R2,#21H ;LOAD R2 WITH VALUE 05H
MOV R3,#85H ;LOAD R3 WITH VALUE 05H
END
1 2
3 4
5 6
PSW Continue …
1. Use assembler directives to place constants 0FCH, 05H,76H,28D and character string
“SAM” in consecutive program memory locations beginning from location 0005H.
ORG 0005H
DB 0FCH,05H,76H,28
DB 'S','A','M'
Instruction Action
JZ Jump if A=0
JNZ Jump if A≠0
DJNZ Decrement and jump if register ≠0
If the stack is a section of RAM, there must be a registers inside the CPU to
point to it. The register used to access the stack is called the SP (stack pointer)
register. The stack pointer in the 8051 is only 8 bits wide, which means that is
can take values of 00 to FFH.
When the 8051 powered up, the SP register contains value 07. This means that
RAM location 08 is the first location used for the stack by the 8051.
In the 8051 the stack pointer (SP) points to the last used location of the stack.
As we push data onto the stack, the stack pointer is incremented by one.
ORG 0
MOV R0,#25H
MOV R1,#12H
MOV R2,#0F3H
PUSH 2
PUSH 1
PUSH 0
POP 7
POP 6
POP 5
END
The Special
Function
Registers (SFRs)
Problem3. Write a program to toggle all the bits of port 1 by sending to it the value 55H and
AAH continuously. Put a time delay in between each issuing of data to port 1.
ORG 0100H
BACK: MOV A,#55H ;LOAD A WITH 55H (0100 0101)
MOV P1,A ;SEND 55H TO PORT1 (0102 0103)
LCALL DELAY1 ;TIME DELAY (0104 0105 0106)
MOV A,#0AAH ;LOAD A WITH AA IN HEX (0107 0108)
MOV P1,A ;SEND AAH TO PORT1 (0109 010A)
LCALL DELAY1 ;TIME DELAY (010B 010C 010D)
SJMP BACK ;KEEP DOING THIS INDEFINITELY (010E 010F) SHORT JUMP
; THIS IS THE DELAY SUBROUTINE
ORG 300H ;PUT TIME DELAY AT ADDRESS 300H
DELAY1: MOV R5,#05H ;R5=5 THE COUNTER1
DELAY2: MOV R6,#03H ;R6=3 THE COUNTER2
AGAIN: DJNZ R6,AGAIN ;STAY HERE UNTILL R6 BECOMES 0
DJNZ R5,DELAY2
RET ;RETURN TO CALLER (WHEN R5=0)
END
ORG 0100H
BACK: MOV A,#55H ;LOAD A WITH 55H (0100 0101)
MOV P1,A ;SEND 55H TO PORT1 (0102 0103)
LCALL DELAY1 ;TIME DELAY1 (0104 0105 0106)
MOV A,#0AAH ;LOAD A WITH AA IN HEX (0107 0108)
MOV P1,A ;SEND AAH TO PORT1 (0109 010A)
LCALL DELAY1 ;TIME DELAY1 (010B 010C 010D)
SJMP BACK ;KEEP DOING THIS INDEFINITELY (010E 010F) SHORT JUMP
; THIS IS THE TIME DELAY1 SUBROUTINE
ORG 300H ;PUT TIME DELAY1 AT ADDRESS 300H
DELAY1: MOV R5,#05H ;R5=5 THE COUNTER1 (0300 0301)
AGAIN1: DJNZ R5,AGAIN1 ;STAY HERE UNTILL R5 BECOMES 0 (0302 0303)
LCALL DELAY2 ;TIME DELAY2 (0304 0305 0306)
RET ;RETURN TO CALLER (WHEN R5=0) (0307)
; THIS IS THE TIME DELAY2 SUBROUTINE
ORG 400H ;PUT TIME DELAY1 AT ADDRESS 400H
DELAY2: MOV R6,#03H ;R6=3 THE COUNTER2 (0400 0401)
AGAIN2: DJNZ R6,AGAIN2 ;STAY HERE UNTILL R6 BECOMES 0 (0402 0403)
RET ;RETURN TO CALLER (WHEN R6=0) (0404)
END
Time Delay for Various 8051 chips
The CPU takes a certain number of clock cycles to execute an instruction. Theses clock
cycles referred to as machine cycles. The length of the machine cycle depends on the
frequency of the crystal oscillator connected to the 8051 system. The crystal oscillator,
along with on-chip circuitry, provide the clock source for the 8051 CPU. Very often the
11.0592 MHz crystal oscillator is used to make the 8051 based system compatible with
the serial port of the IBM PC. In the original 8051, one machine cycle lasts 12 oscillator
periods. Therefore, to calculate the machine cycle for the 8051, we take 1/12 of the
crystal frequency, then takes its inverse.
The following shows crystal frequency for three different 8051 based systems. Find the
period of the machine cycle in each case.
Problem 4. For an 8051 system of 11.0592 MHz, find the time delay for the following
subroutine:
machine cycles
The time delay inside the HERE loop is
DELAY: MOV R3,#250 1
[250(1+1+1+1+2)]x1.085 µs = 1627. 5 µs
HERE: NOP 1
NOP 1
Adding the two instructions outside the loop
NOP 1
we have
NOP 1
1627.5 µs + 3x1.085 µs = 1630.755 µs
DJNZ R3,HERE 2
RET 2
I/O Port Programming
In the 8051 there are a total of four ports for I/O operations.
Port 3
Port 2
Port 0
Port 0 occupies a total of 8 pins(pin32-39). It can be used for input or output. To use the
pins of port 0 as both input and output ports, each pin must be connected externally to a
10Kohms pull-up resistor. This is due to the fact that P0 is an open drain, unlike P1,P2 and
P3. Open drain is a term used for MOS chips in the same way that open collector is used for
TTL chips.
ORG 0H
MOV A,#0FFH
MOV P0,A
MOV A,#00H
MOV P1,A
BACK: MOV A,P0
MOV P1,A
SJMP BACK
I/O ports and bit-addressability
Sometimes we need to access only 1 or 2 bits of the port instead of the entire 8
bits. A powerful feature of 8051 I/O ports is their capability to access
individual bits of the port without altering the rest of the bits in that port.
Solution
(a) The 50% duty cycle means that the “on” and “off” states (or the high and low
portion of the pulse) have the same length. Therefore, we toggle P1.0 with a time
delay in between each state.
ORG 0
HERE: SETB P1.0 ;SET TO HIGH BIT 0 OF PORT 1
LCALL DELAY ;CALL THE DELAY SUBROUTINE
CLR P1.0 ;P1.0=0
LCALL DELAY
SJMP HERE ;KEEP DOING IT
ORG 300H
DELAY: MOV R5,#5H ;R5=5 THE COUNTER (0300H 0301H)
AGAIN: DJNZ R5,AGAIN ;STAY HERE UNTILL R5 BECOMES 0 (0302H 0303H)
RET ;RETURN TO CALLER (WHEN R5=0) (0304H 0305H)
END
(b) The 66% duty cycle means the “on” state is twice the “off” state.
ORG 0
HERE: SETB P1.3 ;SET TO HIGH BIT 0 OF PORT 1
LCALL DELAY ;CALL THE DELAY SUBROUTINE
LCALL DELAY ;CALL THE DELAY SUBROUTINE
CLR P1.3 ;P1.0=0
LCALL DELAY
SJMP HERE ;KEEP DOING IT
ORG 300H
DELAY: MOV R5,#5H ;R5=5 THE COUNTER (0300H 0301H)
AGAIN: DJNZ R5,AGAIN ;STAY HERE UNTILL R5 BECOMES 0 (0302H 0303H)
RET ;RETURN TO CALLER (WHEN R5=0) (0304H 0305H)
END
Write a program to perform the following.
(a) Keep monitoring pin P0.1 until it becomes high
(b) When P0.1 becomes high, read in the data from P1
(c) Send a low to high pulse on P0.2 to indicate that the data has been read
Solution
Philips P89C51RA2/RB2/RC2/RD2xx
80C51 8-bit Flash microcontroller family
8KB/16KB/32KB/64KB ISP/IAP Flash with 512B/512B/512B/1KB RAM
In-System Programming capability (ISP)
Parallel programming with 87C51 compatible hardware
interface to programmer
Four interrupt priority levels
Programmable Counter Array (PCA) – PWM and Capture/compare
Power control modes: stopped clock, idle, power down
In-System Programming (ISP)
also called In-Circuit Serial Programming (ICSP)
In-System Programming eliminates the physical removal of chips from the system. This
will save time, and money, both during development in the lab, and when updating the
software or parameters in the field.
Microcontrollers with 8051 Core
Atmel AT89S51
Philips P8xC591
Single-chip 8-bit microcontroller with CAN controller
Power consumption is one of the big issues on mobile or battery powered devices.
Some microcontrollers allow you to reduce the clock frequency, for example by
switching to an internal clock, to reduce power consumption when only little work
has to be done.
Programming mode
Fetch instruction
Decode instruction
Read data
Process data
Write data
The pipeline will only work at full efficiency if it can fetch instructions
from sequential locations in memory.
Part Num. Code Data Data I/O pins ADC Timers Pin numbers &
ROM RAM EEPROM Package
The vast majority of AVR registers are 8-bit registers. In AVR there are 32 general
purpose registers (GPRs). They are R0-R31 and are located in lowest location of memory
address 00-1F.
AVR microcontrollers are Harvard architecture. This means, that in this architecture are
separate memory types (program memory and data memory) connected with distinct
buses. Such memory architecture allows processor to access program memory and data
memory at the same time. This increases performance of MCU comparing to CISC
architecture, where CPU uses same bus for accessing program memory and data
memory.
Each memory type has its own address space:
For instance few Atmega series memory map examples:
Ex:
LDI R20, 0x25 ; load R20 with 0x25 (R20 = 0x25 -- 25 in hex)
Note : If we want to present a number in hex, we put a dollar sign ($) or a 0x in front of it.
If we put nothing in front of a number, it is in decimal.
ADD instruction
Ex :
LDI R16, 0x25 ; load 0x25 into R16
LDI R17, 0x34 ; load 0x34 into R17
ADD R16,R17 ; add value R17 to R16 (R16 = R16 + R17)
Ex:
For In-System Programming, the programmer is connected to the target using as few wires
as possible. To program any AVR microcontroller in any target system, a simple Six-wire
interface is used to connect the programmer to the target PCB. Below shows the
connections needed.
To assure proper communication on the three SPI lines, it is necessary to connect ground
on the programmer to ground on the target (GND).
The Serial Peripheral Interface (SPI) consists of three wires: Serial ClocK (SCK), Master In –
Slave Out (MISO) and Master Out – Slave In (MOSI). When programming the AVR, the In-
System Programmer always operate as the Master, and the target system always operate as
the Slave.
The In-System Programmer (Master) provides the clock for the communication on the
SCK Line. Each pulse on the SCK Line transfers one bit from the Programmer (Master) to
the Target (Slave) on the Master Out – Slave In (MOSI) line. Simultaneously, each pulse
on the SCK Line transfers one bit from the target (Slave) to the Programmer (Master) on
the Master In – Slave Out (MISO) line.
The SCK pin is the clock. It is essential because in order for the master and slave device to communicate, they
need to have a time signal to communicate data in synchrony. The common clock signal shared between the
master and slave device allow for efficient communication.
The RST pin is an essential connection because it must be put to an active low connection in order for
programming to occur between the master and slave device. It is normally held high, but for programming to
occur, it must be put low. It is an active low pin. When the RST pin is put low, the master slave can
communicate on the SCK, MISO, and MOSI lines.
The remaining 2 connections, VCC and GND, are the simplest; they are for power. The AVR chip being
programmed and the master device doing the programming both need to power in order to operate.
End of Part 1
Atmega 32 C programming using Atmel Studio
Step 01.
install USBASP driver
http://www.protostack.com/accessories/usbasp-avr-programmer
Download the driver and save it
Plug the USBASP
Mycomputer -> Manage -> update the driver software browse the above folder
Step 02.
Install AVRDUDE
The programmer come with WINAVR . Install WINAVR
Step 03.
Test the connections and response
Step 4
Command Window -> avrdude
Connect the atmega32 with usbasp
Then command and arguments in command window
Step 05.
Install Atmel Studio
Step 06.
Settings of Atmel Studio
Tools -> external tools