Programming the 8051/8052 CSCI 255: Introduc/on to Embedded Systems Keith Vertanen Copyright 2011
Course outcomes (what you'll learn) ü Number systems ü binary, octal, decimal, hexadecimal How computers represent numbers ü integers (posi=ve/nega=ve) floa=ng- point ü Boolean algebra, bit manipula=ons ü Combina=onal and sequen=al circuits ü How these things build up to a computer Embedded programming In assembly language and C Subrou=nes, =mers, interrupts 2
Today 8051/8052 microcontroller overview Assembly language programming in general for 8051 microcontroller 3
8051/8052 microcontroller 8051/8052 Microcontroller with instruc=on set compa=ble with the MCS- 51 standard Developed by Intel in 1980 Compa=ble chips made by a variety of other companies Harvard architecture Code and data stored separately 8052 is enhanced version of 8051 256 bytes of RAM instead of 128 bytes Adds a third =mer 4
8051 microarchitecture 5
8051 educa=onal board Amtel AT89S8253 microcontroller 6
Port 0 LEDs monitoring logic levels on port 0 7
Port 2 Push buyons and DIP switches hooked to port 2 8
8051 pin outs 9
Compiling: 1- step, desktop Source code: Plain text file created in a high- level programming language #include <stdio.h> int main(int argc, char** argv) { printf("hello world!\n"); return 0; } HelloWorld.c % gcc HelloWorld.c -o HelloWorld Machine language: Actual binary run by a par=cular processor, not human readable/writeable HelloWorld.exe 10
Compiling: 2- step, desktop Source code: Plain text file created in a high- level programming language #include <stdio.h> int main(int argc, char** argv) { printf("hello world!\n"); return 0; } HelloWorld.c % gcc -S HelloWorld.c -o HelloWorld.s Assembly language: Low- level, but s=ll human readable/writeable _main: % gcc HelloWorld.s -o HelloWorld pushl %ebp movl %esp, %ebp subl $8, %esp HelloWorld.s Machine language: Actual binary run by a par=cular processor, not human readable/writeable HelloWorld.exe 11
Assembly language C Keil IDE Hex file Keil IDE MRT loader 12
Assembly language Assembly language Very processor specific Different instruc=on sets Different forms of addressing Requires understanding of underlying hardware Stored in a human "readable" text file: HandleFreqOutput: CLR A MOV TL0,A MOV TH0,A ;clear the counter MOV Overflow,A MOV GateTime,#20 MOV TL2, # Low (-49999) ;V0.2 new ReloadValue -49999 MOV TH2, # High(-49999) SETB T2I0 SETB TR0 ;restart both Timers CLR bready 13
Assembly syntax Each line of assembly code can have: Label Programmer chosen symbol naming this instruc=on's address in memory. Instruc=on Instruc=on to execute along with any parameter(s) Comment Text so you can remember what the heck this does MyLabel: MOV A, #25h ; This is a sample comment 14
Assembly syntax MyLabel: MOV A, #25h ; This is a sample comment Labels are case- sensi=ve, MYLABEL not the same place in memory Instruc=ons, register names, etc. are not case- sensi=ve ; You can have lines with only a comment ; Probably a good idea to use lots of comments ; Your programs will be graded on: ; functionality, style and elegance. 15
Literal values Number bases Fix numeric used by your code Start with # Decimal: normal Hex: trailing H Binary: trailing B Octal: trailing Q MOV A, #30 ; decimal MOV A, #11110b ; binary MOV A, #1Eh ; hex MOV A, #36q ; octal 4 different ways to load the accumulator with the value 30 (decimal). 16
Basic registers R0 R7 Bank of 8 general purpose 8- bit registers A B PC Accumulator (8- bit) Half of 255 opcodes use A Used by mul=ply (MUL) and division (DIV) (8- bit) Also can be used as general purpose register Program counter (16- bit) Not accessible, changed via jump instruc=ons 17
Basic registers DPTR Data pointer (16- bit) SP Only user- accessible 16- bit register Used to access external memory 16- bits allows addressing of 64K of memory Ojen used by programmers for two- byte variable Stack pointer (8- bit) Indicates next value in RAM to remove from stack Ini=alized to 07h 18
8052 memory architecture 128 + 128 bytes Port 0-3, serial port, =mers, interrupts AT89S8253 12KB flash 19
8052 memory addresses 20
8052 SFR memory 21
P0 Port 0 General purpose input/output port Lives at memory address 80h Bit addressable P0.0, P0.1,, P0.7 Wri=ng 1 to bit brings I/O high Wri=ng 0 brings I/O low Bring pin high before reading Also hooked up to 8 onboard LEDs Set bit to 1 to turn off LED (instruc=on SETB) Set bit to 0 to turn on LED (instruc=on CLR) 22
Simple LED START: SETB P0.0 ; Turn LED D0 off CLR P0.1 ; Turn LED D1 on SETB P0.2 ; Turn LED D2 off CLR P0.3 ; Turn LED D3 on SETB P0.4 ; Turn LED D4 off CLR P0.5 ; Turn LED D5 on SETB P0.6 ; Turn LED D6 off CLR P0.7 ; Turn LED D7 on SJMP START ; Loop forever END 23
Simple LED - loading all bits START: SETB P0.0 ; Turn LED D0 off CLR P0.1 ; Turn LED D1 on SETB P0.2 ; Turn LED D2 off CLR P0.3 ; Turn LED D3 on SETB P0.4 ; Turn LED D4 off CLR P0.5 ; Turn LED D5 on SETB P0.6 ; Turn LED D6 off CLR P0.7 ; Turn LED D7 on SJMP START ; Loop forever END START: MOV P0, #85 SJMP START ; Loop forever END decimal literal START: MOV P0, #01010101b SJMP START ; Loop forever END START: MOV P0, #55h SJMP START ; Loop forever END START: MOV P0, #125q binary literal hex literal SJMP START ; Loop forever END octal literal 24
Using memory address START: SETB P0.0 ; Turn LED D0 off CLR P0.1 ; Turn LED D1 on SETB P0.2 ; Turn LED D2 off CLR P0.3 ; Turn LED D3 on SETB P0.4 ; Turn LED D4 off CLR P0.5 ; Turn LED D5 on SETB P0.6 ; Turn LED D6 off CLR P0.7 ; Turn LED D7 on SJMP START ; Loop forever END START: MOV 80h, #85 SJMP START ; Loop forever END decimal literal START: MOV 80h, #01010101b SJMP START ; Loop forever END START: MOV 80h, #55h binary literal SJMP START ; Loop forever END hex literal START: MOV 80h, #125q SJMP START ; Loop forever END octal literal 25
P1, P2, P3 Port 1, 2, 3 Bit addressable like P0 P1, some pins used by =mer 2, external memory access P2, hooked to push buyons, dip switches P3, some pins used by serial port, interrupt, =mer 0, =me 1, external memory access 26
SETB Sepng, clearing, flipping bits Sets a bit to 1 Specify SFR like P0.1 Bit addressable memory CLR 20h 7Fh Sets a bit to 0 CPL Flips a bit 27
MOV Moving a bit Only available to/from carry bit MOV C, P0.0 ; Move value of P0.0 to the carry bit MOV 25h, C ; Copy result to user bit 25h 28
MOV Register movement 57 of the 255 opcodes MOV des=na=on register, source value Des=na=on always a register or memory address Source value may be register, memory address, or literal Use # to indicate literal MOV R2, R1 MOV A, R1 MOV R2, A ; INVALID! ; First, move R1 to accumulator ; Second, copy accumulator to R2 29
INC, DEC Increment and decrement Add/subtract one from a register or memory address Incremen=ng 255 results in 0 Decremen=ng 0 results in 255 Overflow, underflows don t set any flags (such as the carry bit) INC A ; Increment accumulator by 1 DEC A ; Decrement accumulator by 1 INC 40h ; Increment RAM address 40h by 1 30
DJNZ Program loops Decrement then jump if resul=ng value not zero Most common way to do a loop execu=ng a specific number of =mes (i.e. for- loop) MOV R0, #08h ; Loop for 8 times LOOP: NOOP ; Do whatever loop is suppose to do NOOP DJNZ R0, LOOP ; Increment RAM address 40h by 1 31
Goto considered necessary LJMP, SJMP, AJMP Change execu=on to given loca=on Normally a label in your program LJMP, can jump anywhere (3 bytes) SJMP, can jump within 128 bytes before or 127 byte ajer current loca=on (2 bytes) AJMP, can jump within same 2K block (2 bytes) Keil lets us just use JMP and it will use the most memory efficient version 32
Overview of 8052 Summary Memory architecture Important registers Import special func=on registers (SFRs) Assembly language introduc=on Sepng, clearing, moving bits Moving 8- bit values Incremen=ng, decremen=ng Crea=ng a basic loop 33