Vous êtes sur la page 1sur 33

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  
#include <stdio.h>
Source  code:   int main(int argc, char** argv)
Plain  text  file  created  in  a  high-­‐ {
level  programming  language   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  
#include <stdio.h>

Source  code:   int main(int argc, char** argv)


{
Plain  text  file  created  in  a  high-­‐ printf("Hello world!\n");
return 0;
level  programming  language   }

HelloWorld.c  

% gcc -S HelloWorld.c -o HelloWorld.s

_main:
Assembly  language:   pushl
movl
%ebp
%esp, %ebp
Low-­‐level,  but  s=ll  human   subl $8, %esp

readable/writeable  
HelloWorld.s  
% gcc HelloWorld.s -o HelloWorld

Machine  language:  
Actual  binary  run  by  a  
par=cular  processor,  not  
human  readable/writeable  
HelloWorld.exe   11  
Assembly  language   C  

Keil  IDE  

Keil  IDE  
Hex  file  

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-­‐ Instruc=ons,  register  
sensi=ve,  MYLABEL   names,  etc.  are  not  
not  the  same  place  in   case-­‐sensi=ve  
memory  

; 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  
Number  bases  
•  Literal  values  
–  Fix  numeric  used  by  your  code  
–  Start  with  #  
–  Decimal:  normal  
–  Hex:  trailing  H  
MOV A, #30 ; decimal
–  Binary:  trailing  B   MOV A, #11110b ; binary
MOV A, #1Eh ; hex
–  Octal:  trailing  Q   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  
–  Accumulator  (8-­‐bit)  
–  Half  of  255  opcodes  use  A  
•  B  
–  Used  by  mul=ply  (MUL)  and  division  (DIV)  (8-­‐bit)  
–  Also  can  be  used  as  general  purpose  register  
•  PC  
–  Program  counter  (16-­‐bit)  
–  Not  accessible,  changed  via  jump  instruc=ons  
17  
Basic  registers  
•  DPTR  
–  Data  pointer  (16-­‐bit)  
–  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  
•  SP  
–  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  
Port  0  
•  P0  
–  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: START:
SETB P0.0 ; Turn LED D0 off MOV P0, #01010101b
CLR P0.1 ; Turn LED D1 on
SETB P0.2 ; Turn LED D2 off SJMP START ; Loop forever
CLR P0.3 ; Turn LED D3 on END
SETB P0.4 ; Turn LED D4 off binary  literal  
CLR P0.5 ; Turn LED D5 on
SETB P0.6 ; Turn LED D6 off START:
CLR P0.7 ; Turn LED D7 on MOV P0, #55h
SJMP START ; Loop forever SJMP START ; Loop forever
END END

hex  literal  
START: START:
MOV P0, #85 MOV P0, #125q

SJMP START ; Loop forever SJMP START ; Loop forever


END END

decimal  literal   octal  literal  


24  
Using  memory  address  
START: START:
SETB P0.0 ; Turn LED D0 off MOV 80h, #01010101b
CLR P0.1 ; Turn LED D1 on
SETB P0.2 ; Turn LED D2 off SJMP START ; Loop forever
CLR P0.3 ; Turn LED D3 on END
SETB P0.4 ; Turn LED D4 off binary  literal  
CLR P0.5 ; Turn LED D5 on
SETB P0.6 ; Turn LED D6 off START:
CLR P0.7 ; Turn LED D7 on MOV 80h, #55h
SJMP START ; Loop forever SJMP START ; Loop forever
END END

hex  literal  
START: START:
MOV 80h, #85 MOV 80h, #125q

SJMP START ; Loop forever SJMP START ; Loop forever


END END

decimal  literal   octal  literal  


25  
Port  1,  2,  3  
•  P1,  P2,  P3  
–  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  
Sepng,  clearing,  flipping  bits  
•  SETB  
–  Sets  a  bit  to  1  
–  Specify  SFR  like  P0.1  
–  Bit  addressable  memory  
•  20h  –  7Fh  
•  CLR  
–  Sets  a  bit  to  0  
•  CPL  
–  Flips  a  bit  

27  
Moving  a  bit  
•  MOV  
–  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  
Register  movement  
•  MOV  
–  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 ; INVALID!

MOV A, R1 ; First, move R1 to accumulator


MOV R2, A ; Second, copy accumulator to R2

29  
Increment  and  decrement  
•  INC,  DEC  
–  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  
Program  loops  
•  DJNZ  
–  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  
Summary  
•  Overview  of  8052    
–  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  

Vous aimerez peut-être aussi