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

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

Lecture (04) PIC16F84A (3)

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

16.317: Microprocessor Systems Design I Fall 2013 Exam 3 Solution

Assembly Language Instructions

PIC 16F84A programming (II)

Flow Charts and Assembler Programs

Chapter 4 Sections 1 4, 10 Dr. Iyad Jafar

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

Chapter 5 Sections 1 6 Dr. Iyad Jafar

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

D:\PICstuff\PartCounter\PartCounter.asm

ECE Test #1: Name

Application Note - PIC Source Code v1.1.doc

EEE111A/B Microprocessors

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 5 Solution

Instuction set

16.317: Microprocessor-Based Systems I Summer 2012

16.317: Microprocessor-Based Systems I Spring 2012

SOLAR TRACKING SYSTEM USING PIC16F84A STEPPER MOTOR AND 555TIMER

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

CONNECT TO THE PIC. A Simple Development Board

Timer2 Interrupts. NDSU Timer2 Interrupts September 20, Background:

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

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.

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

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

Binary Outputs and Timing

CENG 336 INT. TO EMBEDDED SYSTEMS DEVELOPMENT. Spring 2006

ECE Homework #3

The University of Texas at Arlington Lecture 5

DERTS Design Requirements (1): Microcontroller Architecture & Programming

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

Chapter 13. PIC Family Microcontroller

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

SOLUTIONS!! DO NOT DISTRIBUTE!!

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

PIC Discussion. By Eng. Tamar Jomaa

Embedded System Design

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

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)

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

DESIGN AND FABRICATION OF FARE METER OF TAXICAB USING MICROCONTROLLER

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

PIC16F87X 13.0 INSTRUCTION SET SUMMARY INSTRUCTIONS DESCRIPTIONS

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

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

/ 40 Q3: Writing PIC / 40 assembly language TOTAL SCORE / 100 EXTRA CREDIT / 10

SOLUTIONS!! DO NOT DISTRIBUTE PRIOR TO EXAM!!

Arithmetic,logic Instruction and Programs

Week1. EEE305 Microcontroller Key Points

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING. EE6008 Microcontroller based system design

Performance & Applications

Chapter 3: Further Microcontrollers

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

Controller Provides Display of Monitored Process Deviation

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

ME 515 Mechatronics. A microprocessor

Input/Output Ports and Interfacing

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

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

CHAPTER 6 CONCLUSION AND SCOPE FOR FUTURE WORK

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

ECE 354 Introduction to Lab 2. February 23 rd, 2003

Learning Objectives:

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

PART TWO LISTING 8 PROGRAM TK3TUT8 MOVF PORTA,W ANDLW B ADDWF COUNT,F MOVF COUNT,W MOVWF PORTB GOTO LOOP

APPLICATION NOTE Wire Communication with a Microchip PICmicro Microcontroller

Embedded Systems. PIC16F84A Internal Architecture. Eng. Anis Nazer First Semester

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

Laboratory Exercise 5 - Analog to Digital Conversion

Embedded Systems Design (630470) Lecture 4. Memory Organization. Prof. Kasim M. Al-Aubidy Computer Eng. Dept.

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.

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

ME 6405 Introduction to Mechatronics

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

Experiment 7:The USART

16.317: Microprocessor-Based Systems I Fall 2012

President Alan VK6ZWZ Acting Sec. Don VK6HK Vice President Terry VK6ZLT Treasurer Ces VK6AO

ECE 354 Computer Systems Lab II. Interrupts, Strings, and Busses

Appendix D: Source Codes. PDF created with pdffactory Pro trial version

Section 31. Instruction Set

Beacon Keyers Using PIC s. Dave Powis, DL/G4HUP

Micro II and Embedded Systems

Chapter 11: Interrupt On Change

AN530. Interfacing 93CX6 Serial EEPROMs to PIC16C5X Microcontrollers INTRODUCTION THE HARDWARE CONNECTION THE SOFTWARE CONNECTION

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

Physics 335 Intro to MicroControllers and the PIC Microcontroller

16.317: Microprocessor Systems Design I Fall 2015

AN530. Interfacing 93 Series Serial EEPROMs. Interfacing 93CX6 Serial EEPROMs to PIC16C5X Microcontrollers INTRODUCTION THE HARDWARE CONNECTION

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

Embedded Systems Programming and Architectures

Figure 1: Pushbutton without Pull-up.

EE6008-Microcontroller Based System Design Department Of EEE/ DCE

CENG-336 Introduction to Embedded Systems Development. Timers

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

/ 28 HLL assembly Q4: Conditional instructions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

PTK8756B 8 Bit Micro-controller Data Sheet

Discrete Logic Replacement Message Dispatch Engine

Fortune. Semiconductor Corporation 富晶半導體股份有限公司. 8-bit MCU with 1k program ROM, 64-byte RAM, 1 R2F module and 3 13 LCD driver. TD Rev. 1.

Transcription:

Embedded Systems PIC16F84A Sample Programs Eng. Anis Nazer First Semester 2017-2018

Development cycle (1) Write code (2) Assemble / compile (3) Simulate (4) Download to MCU (5) Test

Inputs / Outputs PIC16F84A has 13 pins for inputs/outputs can be configured as inputs or outputs Input data from the outside world to the PIC Output data from the PIC to the outside world two ports: PORTA: 5 bits PORTB: 8 bits

Input / Output Special function registers TRISA and TRISB to configure the pin state as input or output 0 in TRIS register pin is output 1 in TRIS register pin is input Ex. if you store b'0110 1101' in TRISB then: RB0, RB2, RB3, RB5, RB6 are inputs RB1, RB4, RB7 are outputs Ex. What is the effect of the following instructions: BSF MOVLW MOVWF STATUS, RP0 0x3C TRISB

Inputs / outputs The state of the ports are mapped into registers PORTA and PORTB to read the state of an input pin, read the value stored in the register to write a value to an output pin, store the value in the register Address of PORTA: 0x05 (Bank 0) Address of PORTB: 0x06 (Bank 0) Address of TRISA: 0x85 (Bank 1) Address of TRISB: 0x86 (Bank 1) How do you select bank 0 or bank 1?

Example Write code to set the pins of port A as inputs and port B as outputs BSF STATUS, RP0 ; select bank 1 MOVLW 0xFF MOVWF TRISA CLRF TRISB

Example Write code to read the state of port A into W register BCF STATUS, RP0 ; select bank 0 MOVF PORTA, 0 Write code to put 0xA7 on port B BCF STATUS, RP0 ; select bank 0 MOVLW 0xA7 MOVWF PORTB

Example Assume you have a switch and an LED, the switch and LED are connected as shown, Write a program that turns the LED on if the switch is closed, and turns the LED off if the switch is not closed. Vdd RB0 RA0 LED PIC16F84A

Example Steps: 1) set RA0 as output, and RB0 as input 2) read value on RB0: if RB0 == 0 turn LED off if RB0 == 1 turn LED on 3) repeat (2)

Example Cont. BSF STATUS, RP0 ; select bank 1 BCF TRISA, 0 ; set RA0 as output BSF TRISB, 0 ; set RB0 as input BCF STATUS, RP0 ; select bank 0 loop BTFSC PORTB, 0 ; if B0 is 0 don't set A0 BSF PORTA, 0 BTFSS PORTB, 0 BCF PORTA, 0 GOTO loop

Example Same example but with two switches and two LEDs BSF STATUS, RP0 ; select bank 1 BCF TRISA, 0 ; set RA0, RA1 as output BCF TRISA, 1 BSF TRISB, 0 ; set RB0, RB1 as input BSF TRISB, 1 BCF STATUS, RP0 ; select bank 0 loop BTFSC PORTB, 0 ; if B0 is 0 don't set A0 BSF PORTA, 0 ; BTFSS PORTB, 0 ; if B0 is 1 don't clear A0 BCF PORTA, 0 BTFSC PORTB, 1 BSF PORTA, 1 BTFSS PORTB, 1 BCF PORTA, 1 ; same for B1 GOTO loop

Alarm System We want to design an alarm system, it is composed of a motion detector, an enable switch, and a buzzer. Assume that: The motion detector gives a logic 1 if motion is detected. The buzzer is turned on if it receives a logic 1 The system is enabled if the switch is closed.

Alarm System Vcc Motion detector RA0 RA1 RB0 Buzzer PIC16F84A

Alarm System Start Flow chart: is system enabled? false buzzer off true is motion detected? true false buzzer on

Alarm System - Program #include "p16f84a.inc" ORG 0x000 BSF STATUS, RP0 ; select inputs and outputs BSF TRISA, 0 BSF TRISA, 1 BCF TRISB, 0 BCF STATUS, RP0 BCF PORTB, 0 ; initially, turn the buzzer off main BTFSS PORTA, 1 ; is system enabled? GOTO buzzer_off ; no, buzzer off BTFSC PORTA, 0 ; is motion detected? BSF PORTB, 0 ; yes, turn buzzer on GOTO main buzzer_off BCF PORTB, 0 ; disable the system GOTO main END

Example Assume that 8 LEDs are connected to the pins of PORTB, study the following code and explain its function #include "p16f84a.inc" count EQU 0x33 ORG 0x000 main BSF CLRF BCF STATUS, RP0 TRISB STATUS, RP0 start MOVLW D'100' MOVWF count CLRF PORTB next INCF PORTB, 1 DECFSZ count, 1 GOTO next CLRF PORTB GOTO start end

Subroutines Like functions in high level language executed using a CALL instruction, a return will go back to the instruction written after the call instruction two return instructions: RETURN RETLW : returns and stores a value in W reg Why is it useful?

Subroutine Write a subroutine that multiplies the value stored in location 0x20 by 3, and store the result in W register mult3 MOVF 0x20, 0 ADDWF 0x20, 0 ADDWF 0x20, 0 RETURN

Calling the Subroutine A program that uses the previous subroutine X EQU 0x20 Y EQU 0x21 ORG 0x000 GOTO start mult3 MOVF X, 0 ADDWF X, 0 ADDWF X, 0 RETURN start MOVLW D'23' MOVWF X CALL mult3 MOVWF Y GOTO start END

Software Delay To achieve a delay, execute a number of instructions that do nothing Ex. counter EQU 0x0C MOVLW D'10' MOVWF counter loop NOP DECFSZ counter, 1 GOTO loop How many instruction cycles are executed by the code?

Example: Delay Write a delay subroutine that executes 200 instruction cycles delay MOVLW D'48' ; 1 instruction cycle MOVWF counter ; 1 loop NOP ; 1 DECFSZ counter, 1 ; 1 or 2 GOTO loop ; 2 NOP ; 1 NOP ; 1 NOP ; 1 RETURN ; 2 instruction cycle count = 1+1+ ( 1+1+2 )*47 + (1+2) + 1+1+1+2 = 198 loop last iteration NOPs

Delay Assume that the clock frequency is 10KHz, how much time does the above delay subroutine take? each pulse takes 1/10K = 0.1ms instruction cycle takes 4 clock cycles so, each instruction takes 4*0.1m = 0.4ms so the delay subroutine above will take 200*0.4=80ms

Example: Delay Write a program to result in the execution of 2000 instruction cycles call the above subroutine 10 times cnt EQU 0x0C cnt1 EQU 0x0D ORG 0x00 start MOVLW D'10' MOVWF cnt1 loop1 CALL delay DECFSZ cnt1,1 GOTO loop1 delay loop MOVLW D'48' MOVWF cnt NOP DECFSZ cnt, 1 GOTO loop NOP NOP NOP RETURN END

Moving light Example Assume that 8 LEDs are connected to PORTB Assume that the clock frequency is 10KHz Write code to light LED at RB0 for 0.5 seconds then LED at RB1 for 0.5 seconds, then LED at RB2 for 0.5 seconds, etc... hint: use rotate instruction

Moving light Example Circuit:

Moving light Example Steps: 1) Store 0x01 in PORTB this will turn on the LED connected to RB0 2) delay for 0.5 seconds 3) rotate PORTB to the left this will turn off the LED and will turn on the next LED 4) GOTO step (2)

Moving light Example Calculations: delay for 0.5 second clock frequency is 10kHz instruction frequency = 10k/4 = 2.5k IPS instruction cycle time = 1/2.5K = 0.4ms so, we need 0.5 / 0.4m = 1250 instructions to get a delay of 0.5 second

Moving light Example delay loop executes 1250 instruction cycles 1250 = 5 x 250 so, each iteration executes 5 instructions, and the loop repeats 250 times counter EQU 0x0c delay MOVLW D'250' ; delay loop MOVWF counter loop NOP NOP DECFSZ counter, 1 GOTO loop RETURN

Moving light Example #include "P16F84A.INC" counter EQU 0x0c ORG 0x00 BSF STATUS, RP0 ; select bank 1 CLRF TRISB ; set portb as output BCF STATUS, RP0 ; select bank 0 MOVLW 0x01 ; light up led at RB0 MOVWF PORTB BCF STATUS, C ; clear carry flag loop1 CALL delay RLF PORTB, 1 ; rotate left GOTO loop1 delay MOVLW D'250' ; delay loop MOVWF counter loop NOP NOP DECFSZ counter, 1 GOTO loop RETURN END

Moving light Example 2 Assume that 8 LEDs are connected to PORTB Assume that the clock frequency is 10KHz Write code to light LED at B0 then LED at B1 after 0.5 seconds, then light B2 after 0.5 seconds, etc, when the light reaches B7, delay for 0.5 seconds then light B6 then delay for 0.5 seconds, then light B5... etc i.e. make the light bounce...:)

Lookup Table Lookup table: is a block of data stored in program memory A lookup table can be used to implement a mathematical function, generate output patterns...etc In PIC a lookup table is implemented using a CALL and RETLW, with a computed jump PCL register holds the least significant byte of the program counter

Lookup Table what happens if we execute: ORG 0x111 MOVLW 0x02 ADDWF PCL, 1 What is the address of the instruction that will be executed after the ADDWF? PCL + 2 0x113 + 2 = 0x115 computed jump

Lookup tables Write a program that will take the input at PORTA as x (in binary) and calculate f(x) and display output on PORTB (in binary) where f x =x 2 2 x f(x) 0 2 1 3 2 6 3 11 4 18...... 15 227

Lookup table ORG BSF CLRF TRISB MOVLW 0xFF 0x00 STATUS, RP0 MOVWF TRISA BCF STATUS, RP0 loop MOVF PORTA, 0 ANDLW 0x0F CALL func MOVWF PORTB GOTO loop func ADDWF PCL, 1 RETLW D'2' RETLW D'3' RETLW D'6' RETLW D'11' RETLW D'18' RETLW D'27' RETLW D'38' RETLW D'51' RETLW D'66' RETLW D'83' RETLW D'102' RETLW D'123' RETLW D'146' RETLW D'171' RETLW D'198' RETLW D'227'