You are on page 1of 57

PROGRAMMING EMBEDDED SYSTEMS

THE 8051 PROGRAMMING &


INSTRUCTION SET
PROGRAMMING
 What is programming ?
 Software eng’g
 Application Programm.
 Embedded Sys. Prog.
 Programming what ?
 A computer ? A machine ?
 In a server , multi-client , standalone ?
PROGRAMMING ENVIRONMENT
 Programming Environment causes :

The difference in software programming and


embedded systems programming
Life As an Embedded
Software Developer
 Some qualities of embedded software differ
embedded developers from other software
developers.

 An embedded software developer is the one


who gets his/her hands dirty by getting down
close to the hardware.
Life As an Embedded
Software Developer

 Embedded software development, in most


cases, requires close interaction with the
physical world the hardware platform.
Life As an Embedded
Software Developer

 Large embedded systems require individuals to


work solely on the application-layer software
for the system.

 These application developers typically do not


have any interaction with the hardware.
Life As an Embedded
Software Developer

 When designed properly, the hardware device


drivers are abstracted away from the actual
hardware 
developer writing software doesn't know
how a string gets output to the display, just that
it happens
Yeni Mezun Bilgisayar Mühendis Maaşları

 “Yeni mezun bilgisayar mühendisi …. minimum ………………. Java yada Oracle biliyorsanız ….
minimum 1400$ istemelisiniz. ……….Eğer Gömülü Sistem işleriyle uğraşacak yeni birbilgisayar
mühendisliği mezunu iseniz 1300 dolar isteyin. Özellikle C++ ile program yazacaksanız
1500 dolar minimum ücret isteyebilirsiniz. ………………Normalde yeni mezunlara
Veritabanı Yöneticiliği zor yaptırırlar, eğer böyle bir sorumluluğun altına giriyorsanız ve ayrıca
program yazacaksanız bunun bir bedeli Minimum 1750$ 'dır bu bedel.”
(http://gomsis.net/?p=279)
 Yeni mezun bilgisayar mühendislerinin maaşları 2.700₺ ile 3.450₺ aralığında … Ancak alanında
kendini geliştirmiş bir bilgisayar mühendisi doğal olarak 3.500₺ ile 5.200₺ arasında bir maaş..
(2020 www.nkariyer.com)
 Alanında kendini geliştiren bir bilgisayar mühendisi 3.500 - 5.200 TL arası maaş almaktadır.
Kariyer sitelerinden iş bulan bir bilgisayar mühendisi ortalama 4.675 TL maaş almaktadır.
(2020 www.univerlist.com)
 Bilgisayar Mühendisi en düşük 2320 TL, ortalama 5010 TL, en yüksek 7950 TL (2020
www.kariyet-r.net)
 Yeni mezun …… kamuda işe başladığında ortalama 5.965 TL … tecrübe ve çalışma süresi
arttığında 10.000 TL ye kadar ….. Özel sektörde yeni mezun bir bilgisayar mühendisi 2.500 TL
ile 6.080 TL arasında maaş alıyor … kendini geliştiren bilgisayar mühendisleri yaklaşık 10.000
TL maaş kazanabiliyor. (2021 www.unikesfet.com)
REQUIRED
 Embedded system programmers need
to have the following skills:
Hardware knowledge
 Must become familiar with the ICs, the boards
and buses, and the attached devices used in
order to write embedded software (firmware).

 Shouldn't be afraid to dive into the schematics,


grab an oscilloscope probe, and start poking
around the circuit to find out what is going on.
Efficient code
 Because embedded systems are designed with
the least powerful and most cost-effective
processor, embedded software developers must
make every line of code count.

 The ability to write efficient code is a great


quality to possess as a firmware developer.
Robust code
 There are expectations that embedded systems
will run for years.

 This is not a typical requirement for software


applications written for a PC or Mac.

 If you had to keep reseting your device, it


would be the last time you purchased a product
from that company.
Peripheral interfaces
 At the lowest level, firmware is very specialized,
because each component or circuit has its own
way of performing its function.

 Embedded developers need to know how to


communicate with the different devices or
peripherals in order to have full control of the
devices in the system.
EMBEDDED PROGRAMMING
ENVIRONMENT: Minimal resources
 Resource constraints: The rules for writing
firmware are different from the rules for writing
software for a PC.

 Memory allocation: An application for a modern


PC can take for granted that it will have access
to limitless resources. But in an embedded
system, you will run out of memory if you do
not plan ahead and design the software.
Minimal resources
 Developer must closely manage memory and
processing power.
 Using standard dynamic memory allocation
functions can cause fragmentation and overlap.
 Allocate all memory needed by the system at
initialization time. This is safer than using
dynamic memory allocation.
Reusable software
 Code portability software so that it can be
moved to a new hardware is important in
transition to new projects.
 There are basic methods to ensure that your
embedded code can be moved easily from
project to project.
 An LCD for which you've previously developed
a driver, drop in the old code and save some
precious time in the schedule.
Development tools
 The tools used throughout a career as an
embedded developer will vary from company to
company and often from project to project.
 Need to learn new tools in a career.
 Typically, these tools are not as powerful or as
easy to use as those used in PC software
development.
Development tools
 The debugging tools could vary from a simple
LED to a full-blown emulator.
 This requires one to be very resourceful and
have a bag of techniques to call upon when the
debug environment is lacking.
 The C language. The common denominator.
E.S. Programming
 Programming an E.S. is more difficult than a PC
 A system with finite and well defined states is
defined as a whole
 There is no operating system or only RTOS
 Loop forever structure
S1 S2 S3
System 1 System 2
S4
S10 S1 S2 S3
S6 S5 S4
S9
S8 S7 S6 S5
E.S. Programming Language
 What is the lowest level ? Machine Language !
 Machine Language
  001001101010
 101010101011
 111000110100
 Assembly Language  One higher level Langauge
 ADD A, B CHAPTER 2 pp 38,39,
 MOV A, #67
 SJMP HERE
 Embedded C  One higher ! High Level Programming
 TH0 = 0x35
 TR1 = 1
 While ( TF == 0 )
 Which one to choose ? Only one ? Two ? All ?
Types of Code
Machine Language, Assembly Language, High Level Language

Assembly is a symbolic language that is assembled into machine language by an assembler. Assembly is a
mnemonic statement that corresponds directly to processor-specific instructions. Each type of processor
has its own instruction set and thus its own assembly language. Assembly deals directly with the registers
of the processor and memory locations.
Language Description Example

What the computer actually sees and deals with. 83 ec 08 -> sub $0x8,%esp
Machine Every command is given as a number or 83 e4 f0 -> and $0xfffffff0,%esp
Language sequence of numbers. It is in binary, presented b8 00 00 00 00 -> mov $0x0,%eax
in hex to simplify and be more readable. 83 c0 0f -> add $0xf,%eax

push %ebp
sub $0x8,%esp
movb $0x41,0xffffffff(%ebp)
This is the same as machine language, except the
HLA (High Level Assembly):
Assembly command numbers have been replaced by letter
Language sequences which are more readable and easier to program HelloWorld;
memorize. #include( "stdlib.hhf" )
begin HelloWorld;
stdout.put( "Hello, World of Assembly Language", nl );
end HelloWorld;
Compiling-Assembling-Linking
 Stages until running a program
 pp 44-47
Which Language ?
 There are books on this topic !
 Different views !
 The reality is you need to “know” all three
1. Use a high level language to define the overall system
2. Use assembly language to refine the code and obtain
“granularity”
3. Use machine language to find out bugs and pin down erronous
code segments or hardware
ALWAYS REMEMBER
 The heart of computer engineering and “art” of
digital design is the knowhow of representing a
whole universe in terms of “1”s and “0”
 Everything and anything must be and can be
represented in 1s and 0s
 Beleive it ?
 No ?    DO NOT CHOOSE HARDWARE !
PROGRAMMING INTEL 8051
 The basics of learning a new microcontroller
architecture
 Choosing a level

 We choose C level coding but


There is always a chance to do the programing in a
high level language then add lines in assembly
language
Figure 0–11 Internal Block Diagram of a CPU (page 16)
ASSEMBLY LANGUAGE
The format :
[LABEL :] mnemonic[operands] [;comment]
 Low level language

 Converted into machine code by an assembler

 Programmer must know all the registers &


their sizes
 Assembling and running a 8051 program 

READING ASSIGNMENT
Mazidi......Chapters 2-3. PAGE 44-45
Carry Bit-Related Instructions

© 2006 Pearson Education, Inc.


The 8051 Microcontroller and Embedded Systems, 2e Pearson Prentice Hall
By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay Upper Saddle River, NJ 07458
Single-Bit Instructions

© 2006 Pearson Education, Inc.


The 8051 Microcontroller and Embedded Systems, 2e Pearson Prentice Hall
By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay Upper Saddle River, NJ 07458
Instructions For Reading an Input Port

© 2006 Pearson Education, Inc.


The 8051 Microcontroller and Embedded Systems, 2e Pearson Prentice Hall
By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay Upper Saddle River, NJ 07458
Instructions Reading a Latch (Read-Modify-Write)

© 2006 Pearson Education, Inc.


The 8051 Microcontroller and Embedded Systems, 2e Pearson Prentice Hall
By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay Upper Saddle River, NJ 07458
Assembling and Running a program

 Use an editor to write your program


 Source file .asm extension (or src)
 Assembler produces a
 List file  .lst
 Object file  .obj
 Linker produces an absolute object file
from one or more object files  .abs
 OH  Object to hex converter , hex file
 Ready to burn into a ROM  .hex

 PP 44-51
Figure 2–1a Some 8-bit Registers of the 8051
Figure 2–1b Some 8051 16-bit Registers
THE 8051 PROGRAMMING & INSTRUCTION SET
ASSEMBLY LANGUAGE
Addressing Modes

 An "addressing mode" refers to how a given data is accessed .


There are various ways typical of each brand
 Addressing modes of 8051, with an example of each:
 Immediate Addressing MOV A,#20h
 Register Addressing MOV A, R0
 Direct Addressing MOV A,30h
 Indirect Addressing MOV A,@R0
 External Direct MOVX A,@DPTR
 Code Indirect MOVC A,@A+DPTR
Figure 2–5 RAM Allocation in the 8051
Immediate Addressing
 Source Operand is a constant.
 Constant must be preceded by a pound sign “ # “.
 This mode can be used to load a constant into any
register including DPTR
 Some examples
 MOV A, #25H
 MOV R3, #62
 MOV B, #41H
 MOV DPTR, #4521H  same as
 MOV DPL, #21H
 MOV DPH, #45H
Immediate Addressing

 The value to be stored into Acc is immediately


following the instruction.
 Value that immediately follows; in this case
20H , is loaded into Acc.  MOV A, #20H
 Immediate addressing is very fast since the
value to be loaded is included in the
instruction.
 However, since the value to be loaded is fixed
at compile-time it is not very flexible.
Register Addressing

 The register banks, containing registers R0


through R7, can be accessed by certain
instructions
 The instruction opcode carry a 3-bit register
specification
 Register instructions are code efficient, since
this mode eliminates an address byte.
Register Addressing

When the instruction is executed, one of the


eight registers in the selected bank is accessed.
 One of four banks is selected at execution time

by the two bank select bits in the PSW.


 Some instructions are specific to a certain register.
 For example, some instructions always operate on the
Accumulator, or Data Pointer, no address byte is needed to
point to it. The opcode does this itself. Instructions that
refer to the Accumulator as ‘A’ assemble as accumulator-
specific opcodes.
Register Instructions

 Source and destination register sizes must


match.
 MOV DPTR, A  will give an error
 Correct way
 MOV DPTR, #25F5
 MOV DPL, #F5
 MOV DPL, #25
 Movement of data between registersNOT
allowed
 MOV R4, R7  INVALID
Direct Addressing
 Operand is an 8-bit address field.
 Only 128 Lowest bytes of internal Data RAM
and SFRs can be directly addressed.
 So-named : the value to be operated upon is
obtained by directly retrieving it from a
memory location.
Example: MOV A,30h
 Read the data out of Internal RAM address

30 H and store it in the Accumulator.


Direct Addressing
Direct addressing is generally fast : although the
value isn’t included in the instruction, it is
quickly accessable from the Internal RAM.
 Flexible than Immediate Addressing since
value to be loaded is at the given address--
which may be a variable.
 Any instruction refering to 80H - FFH is
referring to the SFR.
Indirect Addressing

 The instruction specifies a register which


contains the address of the operand.
 Both internal and external RAM can be
indirectly addressed.
 The address register for 8-bit addresses can
be R0 or R1 of the selected register bank, or
the Stack Pointer.
 The address register for 16-bit addresses can
only be the 16-bit “data pointer” register,
DPTR.
Indirect Addressing

 Example : MOV A,@R0


  analyze the value of R0 . Then load the
accumulator with the value from address
indicated by R0. If R0 holds the value 40h and
RAM address 40h holds value 67h. Accumulator
ends up holding 67h.
 Indirect addressing always refers to Internal
RAM; it never refers to an SFR.
Indexed Addressing

 Only Program Memory can be accessed with indexed


addressing, and it can only be read.
 This addressing mode is intended for reading look-up
tables in Program Memory.
 A 16-bit base register (either DPTR or the PC) points
to the base of the table and the Accumulator is set up
with the table entry number.
 The address of the table entry in Program Memory is
formed by adding the Accumulator data to the base
pointer.
Indexed Addressing

 Another type of indexed addressing is used in


the “case jump” instruction. In this case the
destination address of a jump instruction is
computed as the sum of the base pointer and
the Accumulator data.
 “External Direct” addressing mode: Only two
instructions  both utilize DPTR
MOVX A,@DPTR
MOVX @DPTR,A
Indexed Addressing

DPTR must first be loaded with the address of external


memory that you wish to read or write. First instr. will
move the contents of that external memory address
into the Accumulator. Second will do the opposite.
 “External Indirect” : External memory can also be
accessed using indirect addressing  only used in
small projects that have small external RAM :
MOVX @R0,A
 Since the value of @R0 can only be 00h through FFh
the project would effectively be limited to 256 bytes of
External RAM.
READING ASSIGNMENT
 CHAPTER 5,6 of Mazidi’s Book...

ASSEMBLY LANGUAGE chapter 2 all of


it.
Program & Data Memory

Data Memory
Program Memory
Figure 2–3 8051 On-Chip ROM Address Range
128 Bytes of RAM
Direct and Indirect Addressable
See also Fig in book
(a) Ram bit addresses
(b) SFR bit addresses

See also Table of


Predefined bit addr.

See also Table of


Predefined data addr.
INSTRUCTION GROUPS
 111 Instruction types
 49 one-byte
 45 two-byte
 17 three-byte
 Total of 255 separate instructions
 Every HEX CODE from 00-FF ( except A5) is a valid instruction
 Five Groups :
 Aritmetic ADD,ADDC,MUL AB
 Logic ANL,ORL,XRL,RL,RLC….SWAP A
 Data Transfer MOV,MOVC,MOVX,PUSH,POP,CXH
 Boolean Next Slide
 Branching SJMP,LJMP,AJMP,CJNE
SINGLE BIT BOOLEAN PROCESSOR

 Built-inbit-level “co-processor”
 All port lines, 128 bits in RAM, many bits in
SFR have bit addresses
 A set of Boolean Instructions
   WELL SUITED FOR CONTROL APPLICATIONS
 CY ( carry Bit = PSW.7 ) “Acc” of boolean processor
 CY is at D7Hex

 STUDY THE EXAMPLE PROGRAMS


The Old Lab Kit Set-Up
Programming Arduino

You might also like