Programming the Finite State Machine (Extract)

Page 1

Programming the Finite State Machine

Programming the Finite State Machine

Andrew Pratt served for 25 years in the Royal Air Force as an Aircraft Technician. He holds a Higher National Certificate in electrical and electronic engineering and an Honours Degree from the Open University. He continued his career working in industrial controls.

ISBN 978-1-907920-92-9

Elektor International Media BV

www.elektor.com

Andrew Pratt provides a detailed introduction to programming PIC microcontrollers, as well as a thorough overview of the Finite State Machine (FSM) approach to programming. Most of the book uses assembly programming, but do not be deterred. The FSM gives a structure to a program, making it easy to plan, write, and modify. The last two chapters introduce programming in C, so you can make a direct comparison between the two techniques. The book references the relevant parts of the Microchip datasheet as familiarity with it is the best way to discover detailed information. This book is aimed at Microsoft Windows and Linux users. To keep your costs to a minimum and to simplify the toolchain, specific applications are provided as a free download to enable you to use an FTDI serial lead as the programmer. The assembler used is the open-source "gpasm". All programming can be done in a text editor. There are detailed instructions on how to perform the necessary installations on Windows, Linux Debian, and derivatives such as Ubuntu and Fedora. For programming in C, Microchip's XC8 compiler is used from the command line. In addition to the programming applications, two serial read and serial write applications can be used for communicating with the PICs from a computer. A voltmeter project including practical instructions on building a circuit board from scratch is included. All theory is covered beforehand, including how to do integer arithmetic in assembly. Two PICs are covered: the PIC12F1822 and the PIC16F1823. Both can run at 32 MHz with an internal oscillator. You do not need to buy a factory-made development board and programmer. With relatively inexpensive parts including a serial lead, microcontroller, a few resistors, and LEDs, you can get started exploring embedded programming.

LEARN DESIGN SHARE

Programming the Finite State Machine ● Andrew Pratt

Andrew Pratt

with 8-Bit PICs in Assembly and C

with 8-Bit PICs in Assembly and C

Enhanced 2nd Edition ;-----------Machine States. S0 MOVLW 3 SUBWF TICK_COUNTER, W BTFSS STATUS, C GOTO S0 BCF PORTA, 2 CLRF TICK_COUNTER INCF PULSE_COUNTER, F S1

S2

S3

MOVLW 10 SUBWF PULSE_COUNTER, W BTFSS STATUS, C GOTO $+3 CLRF PULSE_COUNTER GOTO S3 MOVLW 9 SUBWF TICK_COUNTER, W BTFSS STATUS, C GOTO S1 CLRF TICK_COUNTER BSF PORTA, 2 GOTO S0 MOVLW 31 SUBWF TICK_COUNTER, W BTFSS STATUS, C GOTO S2 BCF PORTA, 2 CLRF TICK_COUNTER INCF PULSE_COUNTER, F

MOVLW 3 SUBWF PULSE_COUNTER, W BTFSS STATUS, C GOTO $+3 CLRF PULSE_COUNTER GOTO S1 MOVLW 91 SUBWF TICK_COUNTER, W BTFSS STATUS, C GOTO S3 CLRF TICK_COUNTER BSF PORTA, 2 GOTO S2

; LED on. Fast cycle. ; ; ; ; ; ; ;

C in STATUS is set when TICK_COUNTER >= to 3. Skip the next instruction if C in STATUS is found set. C in STATUS found not set. Stay in this state. Turn LED off Reset the tick counter. Add 1 to PULSE_COUNTER and put the result in PULSE_COUNTER. Continue to S1

; LED off. Fast cycle. ; ; ; ; ; ; ; ; ;

C in STATUS is set when PULSE_COUNTER >= to 10. Skip the next instruction if C in STATUS is found set. Have not reached 10 pulses jump to ticks check. 10 Pulses have been counted, reset PULSE_COUNTER. Transition to S3. Load W with 9 for ticks check. C in STATUS is set when TICK_COUNTER >= to 9. Skip the next instruction if C in STATUS is found set. C in STATUS found set, 9 ticks counted.

; Turn LED on.

; ; ; ; ; ; ;

C in STATUS is set when TICK_COUNTER >= to 3. Skip the next instruction if C in STATUS is found set. C in STATUS found not set. Turn LED off Reset the tick counter. Add 1 to PULSE_COUNTER and put the result in PULSE_COUNTER. Continue to S3

; LED on. Slow cycle. ; ; ; ; ;

C in STATUS is set when PULSE_COUNTER >= to 3. Skip the next instruction if C in STATUS is found set. ave not reached 3 pulses jump to ticks check. Reset PULSE_COUNTER. Transition to S1.

; C in STATUS is set when TICK_COUNTER >= to 9. ; Skip the next instruction if C in STATUS is found set. ; C in STATUS found set. ; Turn LED on.

Andrew Pratt LEARN DESIGN SHARE

LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHAR ESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHA COVER EN template pratt.indd All Pages

28/05/2020 12:06:56


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.