Flow Charts and Assembler Programs

Similar documents
Binary Outputs and Timing

ECE Homework #3

Embedded Systems. PIC16F84A Sample Programs. Eng. Anis Nazer First Semester

The University of Texas at Arlington Lecture 5

Dept. of Computer Engineering Final Exam, First Semester: 2016/2017

Introduction. Embedded system functionality aspects. Processing. Storage. Communication. Transformation of data Implemented using processors

ECE Test #1: Name

Timer2 Interrupts. NDSU Timer2 Interrupts September 20, Background:

Input/Output Ports and Interfacing

University of Jordan Faculty of Engineering and Technology Department of Computer Engineering Embedded Systems Laboratory

EXPERIMENT 4: Parallel Input/Output. Objectives Introduction to the Parallel Input/Output (I/O) Familiarization to Interfacing

PIC PROGRAMMING START. The next stage is always the setting up of the PORTS, the symbol used to indicate this and all Processes is a Rectangle.

Embedded System Design

AN587. Interfacing to an LCD Module. Interfacing to an LCD Module INTRODUCTION OPERATION CONTROL SIGNAL FUNCTIONS TABLE 2: CONDITIONAL ASSEMBLY FLAGS

PIC 16F84A programming (II)

16.317: Microprocessor Systems Design I Fall 2013 Exam 3 Solution

Lesson 14. Title of the Experiment: Introduction to Microcontroller (Activity number of the GCE Advanced Level practical Guide 27)

Outlines. PIC Programming in C and Assembly. Krerk Piromsopa, Ph.D. Department of Computer Engineering Chulalongkorn University

Jordan University of Science and Technology Electrical Engineering Department Microcontrollers and Embedded Systems Spring 2011

D:\PICstuff\PartCounter\PartCounter.asm

SOLUTIONS!! DO NOT DISTRIBUTE PRIOR TO EXAM!!

Laboratory Exercise 5 - Analog to Digital Conversion

Lecture (04) PIC16F84A (3)

CONNECT TO THE PIC. A Simple Development Board

Hardware Interfacing. EE25M Introduction to microprocessors. Part V. 15 Interfacing methods. original author: Feisal Mohammed

CENG 336 INT. TO EMBEDDED SYSTEMS DEVELOPMENT. Spring 2006

EEE111A/B Microprocessors

/* PROGRAM FOR BLINKING LEDs CONEECTED TO PORT-D */

Interfacing PIC Microcontrollers. ADC8BIT2 Schematic. This application demonstrates analogue input sampling

Mechatronics and Measurement. Lecturer:Dung-An Wang Lecture 6

EE6008-Microcontroller Based System Design Department Of EEE/ DCE

When JP1 is cut, baud rate is Otherwise, baud rate is Factory default is that JP1 is shorted. (JP1 is jumper type in some model)

Weekly Report: Interactive Wheel of Fortune Week 4 02/014/07-02/22/07 Written by: Yadverinder Singh

Figure 1: Pushbutton without Pull-up.

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

Arithmetic,logic Instruction and Programs

Embedded Systems Programming and Architectures

Application Note - PIC Source Code v1.1.doc

APPLICATION NOTE Wire Communication with a Microchip PICmicro Microcontroller

Assembly Language Instructions

APPLICATION NOTE 2361 Interfacing an SPI-Interface RTC with a PIC Microcontroller

C and Embedded Systems. So Why Learn Assembly Language? C Compilation. PICC Lite C Compiler. PICC Lite C Optimization Results (Lab #13)

LPTCOM. Bruce Misner Lakehead University h d3 RD2 pin 21. RD3 pin h d4. RD4 pin 27 RD5 pin h d5. RD6 pin 29 RD7 pin H d6

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

which means that writing to a port implies that the port pins are first read, then this value is modified and then written to the port data latch.

Chapter 5 Sections 1 6 Dr. Iyad Jafar

Chapter 4 Sections 1 4, 10 Dr. Iyad Jafar

TOPIC 3 INTRODUCTION TO PIC ASSEMBLY LANGUAGE. E4160 Microprocessor & Microcontroller System. Prepared by : Puziah Yahaya JKE, POLISAS / DEC 2010

Micro II and Embedded Systems

Chapter 9. Input/Output (I/O) Ports and Interfacing. Updated: 3/13/12

NH-67, TRICHY MAIN ROAD, PULIYUR, C.F , KARUR DT. DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING COURSE MATERIAL

Chapter 3: Further Microcontrollers

LAB WORK 2. 1) Debugger-Select Tool-MPLAB SIM View-Program Memory Trace the program by F7 button. Lab Work

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

CHAPTER 6 CONCLUSION AND SCOPE FOR FUTURE WORK

Outline. Micriprocessor vs Microcontroller Introduction to PIC MCU PIC16F877 Hardware:

More (up a level)... Connecting the Nokia 3510i LCD to a Microchip PIC16F84 microcontroller

Physics 335 Intro to MicroControllers and the PIC Microcontroller

A Better Mouse Trap. Consumer Appliance, Widget, Gadget APPLICATION OPERATION: Ontario, Canada

movwf prevcod ; a new button is pressed - rcnt=3 movwf (mtx_buffer+1) movlw 3 movwf rcnt

CREATING FIRST PROJECT in mikropascal PRO for PIC32

Chapter 11: Interrupt On Change

S w e d i s h c r. w e e b l y. c o m j a l i l a h m e l i v e. c o m Page 1

16.317: Microprocessor-Based Systems I Summer 2012

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING. EE Microcontroller Based System Design

EECE.3170: Microprocessor Systems Design I Spring 2016

ME 515 Mechatronics. A microprocessor

Laboratory Exercise 7 - Extended I/O & Parallel Processing

Week1. EEE305 Microcontroller Key Points

Arithmetic and Logic Instructions. Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

ME 6405 Introduction to Mechatronics

PIC Discussion. By Eng. Tamar Jomaa

SOLAR TRACKING SYSTEM USING PIC16F84A STEPPER MOTOR AND 555TIMER

Inverted Pendulum Microprocessor and FPGA Manual Sheldon Logan July 3, 2006

ﻢﯿﺣﺮﻟا ﻦﻤﺣﺮﻟا ﷲا ﻢﺴﺑ

Chapter 5. Problems All programming problems should include design pseudo code either as a separate design document on embedded comments in the code.

Timer1 Capture Mode:

EECE.3170: Microprocessor Systems Design I Summer 2017

Discrete Logic Replacement A Keypad Controller for Bi-directional Key Matrix

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002 RESIT. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J1. Time allowed: 3 Hours

DESIGN AND FABRICATION OF FARE METER OF TAXICAB USING MICROCONTROLLER

Microcontroller Overview

Laboratory 10. Programming a PIC Microcontroller - Part II

Section 13. Timer0 HIGHLIGHTS. Timer0. This section of the manual contains the following major topics:

Experiment 7:The USART

Learning Objectives:

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002. Semester 2. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J2. Time allowed: 3 Hours

Performance & Applications

1 Introduction to Computers and Computer Terminology Programs Memory Processor Data Sheet Example Application...

ECE Homework #10

/*Algorithm: This code display a centrifuge with five variable speed RPM by increaseing */

PIC16F87X 13.0 INSTRUCTION SET SUMMARY INSTRUCTIONS DESCRIPTIONS

Timer0..Timer3. Interrupt Description Input Conditions Enable Flag

DERTS Design Requirements (1): Microcontroller Architecture & Programming

Interrupts. ELEC 330 Digital Systems Engineering Dr. Ron Hayne. Images Courtesy of Ramesh Gaonkar and Delmar Learning

16.317: Microprocessor-Based Systems I Spring 2012

ECE 354 Computer Systems Lab II. Memory and Indirect Addressing

C:\IDE\Brooke-16F88-LED\Brooke-16F88_LED.asm

SOLUTIONS!! DO NOT DISTRIBUTE!!

1 Introduction to Computers and Computer Terminology Programs Memory Processor Data Sheet... 4

Transcription:

Flow Charts and Assembler Programs Flow Charts: A flow chart is a graphical way to display how a program works (i.e. the algorithm). The purpose of a flow chart is to make the program easier to understand. Likewise, when you make a flow chart, try to Keep it simple (less than 20 blocks), but Keep it informative (more than one block) It also helps if you follow a few rules: Flow charts should start at the top of the page The program execution should move down towards the bottom of the page There should be a single exit point The main symbols for flow charts are as follows: Symbol Image Meaning Oval Start / End of routine or program Rectangle Function or operation Parallelogram Input / Output Diamond no Decision yes Ideally, your program should match up with the flow chart. Sort of like how your English term paper should match up with the outline. For complicated routines, it helps to write the flow chart first. For smaller programs, you can just write the program first then draw the corresponding flow chart. This again is sort of like how you write your English papers and the corresponding outline. 1 September 4, 2017

PIC I/O To illustrate some programs, it will help to use some of the I/O pins on the PIC. These are pins which allow you to detect when a button is pressed (input) or turn an LED on and off (output). We'll talk more about this later - but for now, understand that the PIC has five I/O ports: PORTA..PORTE. These are physically connected to the pins on your PIC chip as follows: PORTA PORTE PORTC PORTD MCLR RA0 RA1 RA2 RA3 RA4 RA5 RE0 RE1 RE2 +5 gnd OSC1 OSC2 RC0 RC1 RC2 RC3 RD0 RD1 PIC18F4626 RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0 +5 gnd RD7 RD6 RD5 RD4 RC7 RD6 RD5 RC4 RD3 RD2 PORTB PORTD PORTC PORTD The PIC18f4620 chip has 33 I/O lines split into five ports: PORTA PORTB PORTC PORTD PORTE Pins 2..7 33..40 15..18, 24..26 19..22, 27..30 3 Binary Input 5 8 8 8 3 Binary Output 5 8 8 8 3 Analog Input 5 5 - - 3 Setting Up I/O Ports for Binary I/O Three registers are associated with each port PORTx: Defines whether the pin is 0V (0) or 5V (1) TRISx: Defines whether the pin is input (1) or output (0) LATx: I don't understand what the latch does. The data sheets say "Read-modify-write operations on the LATC register read and write the latched output value for PORTC." To this, I say "huh?" So far, ignoring the LAT registers hasn't caused any problems for me. They're probably good for something though. In addition, you need to initialize ADCON1 to 15 movlw 15 load the number 15 to W movwf ADCON1 write W to ADCON1 This sets all I/O pins to binary. Some can be analog inputs as well - we'll cover this later when we get to A/D converters. 2 September 4, 2017

TRISx are 8-bit registers. Each bit defines whether a given pin is input (1) or output (0). You can write to all 8 bits at once or set and clear each bit one at a time. For example, the command movlw 0x0F binary 0000 1111 movwf TRISB sets RB0..3 to input (1) and RB4..7 to output (0). The commands bsf bcf TRISC,1 TRISD,2 sets PORTC pin 1 (making RC1 input) and clears PORTD pin 2 (making RD2 output) PORTx defines the value on each pin: logic 0 is 0V logic 1 is 5V. When a pin is input, the logic level is defined by the external voltage applied to the pin. Writing to an input has no affect. When a pin is output, the logic level is defined by your program. Writing a 1 outputs 5V, writing a 0 outputs 0V. note: Each I/O pin can source or sink up to 25mA. If you want to tie an I/O pin to 0V or 5V, you should use a 200+ Ohm resistor rather than a wire. (Your evaluation boards use 1k resistors). If you accidently set that I/O pin to an output opposite of the connection, the 1k resistor limits the current to 5mA, saving the PIC. If you set the pin to input, the current should be zero (inputs have high impedance) and the 1k resistor has no effect. 3 September 4, 2017

Example 1: Write a program which counts how many times you press RB0. Display this count on PORTC. Software: To count one time each button press, you need to Keep checking RB0 until it goes high, then Keep checking RB0 until it goes low, and Repeat If all you do is wait for RB0 to go high, you'll count really fast while RB0 is high rather than just once. Count Count Count #include <p18f4620.inc> ; --- COUNT_RB0.ASM ---- ; This program counts how many times ; RB0 is pressed and displays the result ; on PORTC Start ; Program Loop1: Loop2: org 0x800 clrf TRISA ;PORTA is output movlw 0xFF movwf TRISB ;PORTB is input clrf TRISC ;PORTC is output clrf TRISD ;PORTD is output clrf TRISE ;PORTE is output movlw 15 movwf ADCON1 ;everyone is binary clrf PORTC ; start from zero ; Start of main loop btfsc PORTB,0 ; Wait until RB0=0 goto Loop1 btfss PORTB,0 ; Wait until RB0=1 goto Loop2 Initialize Ports Wait until RB0=0 (falling edge) Wait until RB0=1 (rising edge) Increment PORTC incf PORTC,F ; Increment count goto Loop1 end 4 September 4, 2017

Example 2: Design a circuit which displays a random number from 0..7 every time RB0 is pressed and relased. Solution: One way to do this is to use the above program but count really fast while RB0 = 1. When you release, the resulting value will look like a random number. RB0 Count really fast Display Count Count really fast Display Count ; --- RANDOM.ASM ---- ; This program generates a random number 0..7 every time RB0 is pressed ; and sends the result to PORTC #include <p18f4620.inc> ; Variables DIE EQU 0 ; random number located at address 0 ; Program org 0x800 clrf TRISA ;PORTA is output movlw 0xFF movwf TRISB ;PORTB is input clrf TRISC ;PORTC is output clrf TRISD ;PORTD is output clrf TRISE ;PORTE is output movlw 15 movwf ADCON1 ;everyone is binary Start Initialize I/O Main: btfsc PORTB,0 ; if RB0=1 incf DIE,W ; add one to the die roll andlw 0x07 ; take the result mod 8 movwf DIE ; and save it movwf PORTC ; display the die value goto Main ; and repeat no RB0=1? yes Keep rolling the die Display Die Value 5 September 4, 2017

Top Down Programming: A better way to write this program uses subroutines and top-down programming. The idea is to identify the main functions you need to do to make the program work and make each of these a subroutine. You then fill in each subroutine one by one to get the program to work. For example, the previous program could be ; --- RANDOM.ASM ---- ; This program generates a random number 0..7 every time RB0 is pressed ; and sends the result to PORTC #include <p18f4620.inc> ; Variables DIE EQU 0 ; random number located at address 0 ; --- Main Routine --- Main: org 0x800 call Init btfsc PORTB,0 ; if RB0=1 call Roll ; keep rolling the die call Display ; display the die value goto Main ; and repeat Init: Start Initialize I/O ; --- Subroutines --- Init: Roll: clrf TRISA ;PORTA is output movlw 0xFF movwf TRISB ;PORTB is input clrf TRISC ;PORTC is output clrf TRISD ;PORTD is output clrf TRISE ;PORTE is output movlw 15 movwf ADCON1 ;everyone is binary incf DIE,W ; add one to the die roll andlw 0x07 ; take the result mod 8 movwf DIE ; and save it Display: movf DIE,W ; display die on PORTC movwf PORTC no Roll: Display: RB0=1? yes Keep rolling the die Display Die Value end Note that, with top-down programming, The main routine is much easier to understand Modifications can be made by changing one subroutine 6 September 4, 2017

Example 3: Design a circuit which displays a random number from 1..6 every time RB0 is pressed and relased. Solution: Change the subroutine Roll: to count from 1 to 6. Increment DIE by one If DIE == 7, reset it to 1 Roll: incf DIE,F ; add one to the die roll movlw 7 cpfseq DIE ; check if DIE = 7 ; if DIE < 7, movlw 1 ; if DIE=7, make DIE=1 movwf DIE 7 September 4, 2017