Microcontroller Simulator Task 5 - Electronics Tutorials

Simulator Traffic LightsWhen you CALL a subroutine, the processor needs to keep track of the address it has to return to when the subroutine is finished. The Stack is used to save these Return Addresses.

The stack is an area of memory used to store the return addresses of subroutine calls. The stack pointer is a register which points to the current stack position. Data is added and removed from the stack in a strict Last In First Out (LIFO) order. The stack pointer is adjusted to keep track of these operations.

Imagine a tall pile of dinner plates. It is easy to add to the top of the pile. Also it's easy to remove from the top of the pile. Removing a middle or bottom plate is not a good idea. The plate pile is a stack and obeys the LIFO rule.

When data is added to the stack this is called a PUSH. When data is removed from the stack it's called a POP. If you write bad code, the stack can grow so big that it "eats" your program. That would be a stack overflow. If you write your code correctly, each CALL has a matching RETurn. If you have more calls than returns, your stack will grow and eat your program. If you have more returns than calls, you will get a Stack Underflow. This might not destroy your program but something is going to break.

The Task ...

Open a browser window or tab containing the microcontroller simulator.

Copy and paste this program into the code editing area. Press F8 to assemble the code.

Click the TRAFFIC button on the highlighted "C" to connect the traffic Lights to PORTC.
If necessary, drag the lights to a more visible position. Don't cover SP or the stack, indicated by the blue highlight.

Step the program and watch SP and the Stack. Answer the reviseOmatic questions on this task.

  • CALL alters PC to the address labeled by DELAY:
  • CALL also saves a "return address" on the Stack and SP is altered.
  • SP is the stack pointer register. SP points to the next free stack location.
  • SUBW 0x01 subtracts one from (W).
  • JPZ jumps if the Z flag is set to 1.
  • The Z flag is set if a move or a calculation leaves a Zero in W.
  • JMP jumps whatever state the flags are in.
  • RET sets PC to the address saved on the stack earlier.
; ===========================================
;  TRAFFIC LIGHTS WITH TIME DELAY SUBROUTINE
; ===========================================
        MOVW    0X00    ; 0: Output  1: Input
        MOVWR   TRISC   ; PORTC 8 INPUTS
START:
        MOVW    0X21    ; GARxxRAG - 00100001
        MOVWR   PORTC   ;   X    X
        MOVW    0X20    ; DELAY DURATION
        CALL    DELAY

        MOVW    0X62    ; GARxxRAG - 01100010
        MOVWR   PORTC   ;  XX   X 
        MOVW    0X05    ; DELAY DURATION
        CALL    DELAY

        MOVW    0X84    ; GARxxRAG - 10000100
        MOVWR   PORTC   ; X    X  
        MOVW    0X20    ; DELAY DURATION
        CALL    DELAY

        MOVW    0X46    ; GARxxRAG - 01000110
        MOVWR   PORTC   ;  X   XX 
        MOVW    0X05    ; DELAY DURATION
        CALL    DELAY

        JMP     START
; ===========================================
; ===== TIME DELAY SUBROUTINE ===============
; ===========================================
DELAY:
        SUBW    0X1     ; SUBTRACT ONE FROM W
        JPZ     DONE    ; JUMP IF Z FLAG IS SET
        JMP     DELAY   ; CARRY ON COUNTING
DONE:
        RET             ; SUBROUTINE RETURN
; ===========================================

Topics:

01-nop-jmp   02-LEDs   03-Heater   03-Tlight   04-SevSeg   05-Delay   06-H-Bridge   07-Interrupts   08-Gray   09-Stepper   doc   hex-num   index  

Flash:

sim  

PDF:

Microcontroller Simulator  

DOC:

Microcontroller Simulator   Microcontroller Simulator.   Microcontroller with Harvard Architecture  

Images:

architecture   assem-H-Bridge   assem-Heater-FC   assem-Heater   assem-Stepper   assem-TLIGHT-grid   Gray   pinout   sim-leds   sim-sev-seg-design   sim-sev-seg   sim-traffic  

 

Privacy Policy, © Copyright, Contact Information and Disclaimers

Site highly rated by Schoolzone.co.uk

Valid XHTML 1.0 Transitional

Valid CSS!