Myke's Home Page

Book CD-ROM Home

File Copying/Harddrive Setup

Development Tools

Experiments

Projects

Useful Code Snippets and Macros

Introduction to Electronics

Introduction to Programming

Datasheets

PCBs

Links

McGraw-Hill Professional Publishing

"StateMC" Experiment

The 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.

Address In 1 Second O/F 15 Second O/F Address Out 1 Second Reset 15 Second Reset Lights
Start x x Start + 1 1 0 None
Start + 1 0 0 Start + 1 0 0 None
Start + 1 1 0 Green 0 1 Green
Green 0 0 Green 0 0 Green
Green 0 1 Yellow 1 0 Yellow
Yellow 0 0 Yellow 0 0 Yellow
Yellow 1 0 Red 0 1 Red
Red 0 0 Red 0 0 Red
Red 0 0 Green 0 1 Green

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
              

Click Here to look at the eighteenth experiment - LEDOn