Myke's Home Page
|
"StateMC" ExperimentThe PICmicro can implement State machines very effectively as a method of "non-linear programming". In this experiment, the simulated PICmicro® microcontroller is used to behave like the hardware state machine shown in the diagram below:
The state table I came up with for implementing the application is shown in the table below. Note that the states can be followed as the light color as well as the timer overflow values. When the timer that is reset in the previous operation overflows, then the application moves on to the next state.
The source code listed below can be accessed from the CD-ROM by clicking Here.
title "StateMC - Demonstrating a State Machine"
;
; This Program is Demonstrates how a State Machine could work with
; the PICMicro Archictecture.
;
; 99.11.14 - Updated for Second Edition
;
; Myke Predko
; 96.05.14
;
LIST P=16F84, F=INHX8M, R=DEC
INCLUDE "P16F84.inc"
; Registers
CBLOCK 0x020
i ; General Counter
state ; Returned Value
Temp ; Temporary Storage Variable
ENDC
__CONFIG _CP_OFF & _WDT_OFF & _RC_OSC
PAGE
; Mainline of StateMC
org 0
clrf i ; Initialize Variables
clrf state
clrf PORTB ; Setup PortB
bsf STATUS, RP0
clrf TRISB ^ 0x080
bcf STATUS, RP0
; Now, Execute the Program
Loop ; Return Here after every Execution
movlw 1 ; Check the Least Significant Bit of PORTB
andwf PORTB, w
movwf Temp
bcf STATUS, C ; Now, Shift over the state Variable
rlf state, w
addwf Temp, w ; Add the Least Significant Bit of PORTB
addwf PCL, f ; Jump to the Correct State Execution Vector
goto State0
goto State0
goto State10
goto State11
goto State2
goto State2
; State Routines...
State0 ; Increment i to 4
incf i, f
movlw 4
subwf i, w ; is "i" greater than 3?
btfsc STATUS, C
incf state, f ; Yes, Increment the State Variable
goto Loop ; Execute the State value again
State10 ; Increment the LSB of PORTB if it's == 0
movlw 1
addwf PORTB, f
goto Loop
State11 ; Shift Over PORTB by one until Carry Set
bcf STATUS, C
rlf PORTB, f
btfsc STATUS, C ; Is the Carry Set?
incf state, f ; Yes, Go to the Next State
goto Loop
State2 ; Reset Everything and Restart the Program
clrf i
clrf state
goto Loop
end
|