Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microprocessor Systems Design I

Similar presentations


Presentation on theme: "Microprocessor Systems Design I"— Presentation transcript:

1 16.317 Microprocessor Systems Design I
Instructor: Dr. Michael Geiger Summer 2012 Lecture 10 PIC16F684 programming examples Exam 3 Preview

2 Microprocessors I: Lecture 10
Lecture outline Announcements/reminders Lab 4 due today HW 3 due Friday, 8/10 Do not do problem 2j (JL instruction) Turn in to my office by 1:45, or Exam 3: Monday, 8/13 Exam 2 regrade requests also due that day Lab 5 due Tuesday, 8/14 Same turn-in procedure as HW 3 Today’s lecture PIC programming examples Exam 3 Preview 10/27/2017 Microprocessors I: Lecture 10

3 Review: PIC instructions
Logical operations andlw/andwf iorlw/iorwf xorlw/xorwf Rotates rrf rlf Jumps/calls/return goto call return/retlw/retfie Miscellaneous nop sleep/clrwdt Conditional execution Test bit and skip next instruction if clear/set: btfsc/btfss Increment/decrement register and skip next instruction if zero: incfsz/decfsz Example use: combined with goto to create conditional jump 10/27/2017 Microprocessors I: Lecture 10

4 Microprocessors I: Lecture 10
10/27/2017 Working with 16-bit data Assume a 16-bit counter, the upper byte of the counter is called COUNTH and the lower byte is called COUNTL. Decrement a 16-bit counter movf COUNTL, F ; Set Z if lower byte == 0 btfsc STATUS, Z decf COUNTH, F ; if so, decrement COUNTH decf COUNTL, F ; in either case decrement COUNTL Test a 16-bit variable for zero btfsc STATUS, Z ; If not, then done testing movf COUNTH, F ; Set Z if upper byte == 0 btfsc STATUS, Z ; if not, then done goto BothZero ; branch if 16-bit variable == 0 CarryOn 10/27/2017 Microprocessors I: Lecture 10 Chapter 9

5 Microprocessors I: Lecture 10
10/27/2017 A Delay Subroutine ; *********************************************************************************** ; TenMs subroutine and its call inserts a delay of exactly ten milliseconds ; into the execution of code. ; It assumes a 4 MHz crystal clock. One instruction cycle = 4 * Tosc. ; TenMsH equ 13 ; Initial value of TenMs Subroutine's counter ; TenMsL equ 250 ; COUNTH and COUNTL are two variables TenMs nop ; one cycle movlw TenMsH ; Initialize COUNT movwf COUNTH movlw TenMsL movwf COUNTL Ten_1 decfsz COUNTL,F ; Inner loop goto Ten_1 decfsz COUNTH,F ; Outer loop goto Ten_1 return 10/27/2017 Microprocessors I: Lecture 10 Chapter 9

6 Microprocessors I: Lecture 10
Blinking LED example Assume three LEDs (Green, Yellow, Red) are attached to Port D bit 0, 1 and 2. Write a program for the PIC16F874 that toggles the three LEDs every half second in sequence: green, yellow, red, green, …. For this example, assume that the system clock is 4MHz. 10/27/2017 Microprocessors I: Lecture 10

7 Microprocessors I: Lecture 10
Top Level Flowchart Initialize: Initialize port D, initialize the counter for 500ms. Blink: Toggle the LED in sequence, green, yellow, red, green, …. Which LED to be toggled is determined by the previous state. Wait for 500ms: Keep the LED on for 500ms and then toggle the next one. 10/27/2017 Microprocessors I: Lecture 10

8 Microprocessors I: Lecture 10
Strategy to “Blink” The LEDs are toggled in sequence - green, yellow, red, green, yellow, red… Let’s look at the lower three bits of PORTD 001=green, 010=yellow, 100=red The next LED to be toggled is determined by the current LED. 001->010->100->001->… 10/27/2017 Microprocessors I: Lecture 10

9 Microprocessors I: Lecture 10
“Blink” Subroutine Blink btfsc PORTD, 0 ; is it Green? goto toggle1 ; yes, goto toggle1 btfsc PORTD, 1 ; else is it Yellow? goto toggle2 ; yes, goto toggle2 ;toggle0 bcf PORTD, 2 ; otherwise, must be red, change to green bsf PORTD, 0 ; 100->001 return toggle1 bcf PORTD, 0 ; change from green to yellow bsf PORTD, 1 ; 001->010 toggle2 bcf PORTD, 1 ; change from yellow to red bsf PORTD, 2 ; 010->100 10/27/2017 Microprocessors I: Lecture 10

10 Another way to code “Blink” ---- Table Use
movf PORTD, W ; Copy present state of LEDs into W andlw B' ' ; and keep only LED bits addwf PCL,F ; Change PC with PCLATH and offset in W retlw B' ' ; (000 -> 001) reinitialize to green retlw B' ' ; (001 -> 010) green to yellow retlw B' ' ; (010 -> 100) yellow to red retlw B' ' ; (011 -> 001) reinitialize to green retlw B' ' ; (100 -> 001) red to green retlw B' ' ; (101 -> 001) reinitialize to green retlw B' ' ; (110 -> 001) reinitialize to green retlw B' ' ; (111 -> 001) reinitialize to green In calling program call BlinkTable ; get bits to change into W xorwf PORTD, F ; toggle them into PORTD 10/27/2017 Microprocessors I: Lecture 10

11 Lab 5 Intro: Stepper motors
Magnet attached to shaft Current through coil  magnetic field Reverse current  reverse field Pair of coils used to attract magnet to one of 4 different directions Unipolar stepper motor: center taps on coil make current reversal easy Microcontroller can activate drive transistors Major differences between uni-polar and bi-polar. Uni-polar has logically two windings per phase, so a magnetic pole can be reversed without switching the direction of current, A microcontroller or stepper motor controller can be used to activate the drive transistors in the right order, Less torque bi-polar has logically a single winding per phase, so current in a winding needs to be reversed in order to reverse a magnetic pole, driving circuit must be more complicated, typically with an H-bridge arrangement More torque Magnet rotor coil 10/27/2017 Microprocessors I: Lecture 10

12 How Bi-polar Stepper Motor Works
More torque than unipolar motor Similar principle, but no center taps Need glue circuitry (use H-bridge) 10/27/2017 Microprocessors I: Lecture 10

13 Sequences (1 = phase activated)
Table of Stepping Sequences Sequences (1 = phase activated) Sequence Polarity Name Description Wave Drive, One-Phase Consumes the least power. Only one phase is energized at a time. Assures positional accuracy regardless of any winding imbalance in the motor. Hi-Torque, Two-Phase Hi Torque - This sequence energizes two adjacent phases, which offers an improved torque-speed product and greater holding torque. Half-Step Half Step - Effectively doubles the stepping resolution of the motor, but the torque is not uniform for each step.  (Since we are effectively switching between Wave Drive and Hi-Torque with each step, torque alternates each step.)  Note that this sequence is 8 steps. 10/27/2017 Microprocessors I: Lecture 10

14 Microprocessors I: Lecture 10
The Schematic Because of power requirements, induction of the windings, and temperature management, motors cannot be directly powered by most digital controllers. Some "glue circuitry," such as a motor controller (H-bridge) is necessary between digital controller and motor. The above image shows the basic circuit of a motor controller which can also sense motor current. (One wire of the motor is shown; a stepper motor would require such a circuit for four wires, and a normal DC motor for two. This circuitry is typically all included in an integrated H-bridge chip. 10/27/2017 Microprocessors I: Lecture 10

15 Our energization pattern
Step Up-down Coil East-West Coil 1 South Off 2 3 4 North 5 6 7 8 10/27/2017 Microprocessors I: Lecture 10

16 Microprocessors I: Lecture 10
Our control sequence RC5 RC4 RC3 RC2 1 10/27/2017 Microprocessors I: Lecture 10

17 Microprocessors I: Lecture 10
Sequence 0111 OFF 1 1 1 10/27/2017 Microprocessors I: Lecture 10

18 Microprocessors I: Lecture 10
Sequence 0101 1 1 10/27/2017 Microprocessors I: Lecture 10

19 The code (comments, directives)
title "asmStepper - PIC16F684 Bipolar Stepper Motor Control" ; ; This Program Outputs a new Bipolar Stepper Motor Sequence ; once every 250 ms. ; Hardware Notes: ; PIC16F684 running at 4 MHz Using the Internal Clock ; Internal Reset is Used ; RC5:RC2 - L293D Stepper Motor Control ;; ; Myke Predko ; LIST R=DEC ;yluo note: list directive to specify assembler options INCLUDE "p16f684.inc" List directive is used to specify different assembly and listing commands to the assembler program. R=DEC means the default number base in 10. _CONFIG directive is used to specify the configuration word bits. Each parameter is ANDed together to specify which bits are reset and set. 10/27/2017 Microprocessors I: Lecture 10

20 The Code (configuration code and data variables)
__CONFIG _FCMEN_OFF & _IESO_OFF & _BOD_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO ; Variables CBLOCK 0x20 Dlay, i ENDC PAGE ; Mainline org 0 nop ; For ICD Debug 10/27/2017 Microprocessors I: Lecture 10

21 The code (initialization)
movlw 1 << ; Start with Bit 2 Active movwf PORTC movlw ; Turn off Comparators movwf CMCON0 bsf STATUS, RP ; Execute out of Bank 1 clrf ANSEL ^ 0x ; All Bits are Digital movlw b'000011' ; RC5:RC2 are Outputs movwf TRISC ^ 0x080 bcf STATUS, RP ; Return Execution to Bank 0 clrf i 10/27/2017 Microprocessors I: Lecture 10

22 Microprocessors I: Lecture 10
The code (main loop) Loop: ; Return Here for Next Value movlw HIGH (( / 5) + 256) movwf Dlay movlw LOW (( / 5) + 256) addlw ; 250 ms Delay btfsc STATUS, Z decfsz Dlay, f goto $ - 3 movf i, w call SwitchRead movwf PORTC incf i, f ; i = (i + 1) % 8; bcf i, 3 goto Loop SwitchRead: addwf PCL, f ; Staying in First 256 Instructions dt b'011100', b'010100', b'000100', b'100100' dt b'100000', b'101000', b'111000', b'011000' end 10/27/2017 Microprocessors I: Lecture 10

23 Microprocessors I: Lecture 10
Exam 3 notes Allowed One 8.5” x 11” double-sided sheet of notes Calculator No other notes or electronic devices (phone, laptop, etc.) Exam will last three hours Will be written for ~50 minutes, but you’ll have whole exam period Covers all lectures after Exam 2 Format similar to previous exams 1 multiple choice question 2 short problems to solve/code sequences to evaluate 10/27/2017 Microprocessors I: Lecture 10

24 Review: Microcontroller/PIC basics
Microcontrollers: CPU integrated with storage, I/O devices Examples Timers/event counters Parallel & serial ports Clock generator Analog to digital converter Benefits: low cost/low power, easy to program Limitations: storage, computational power Introduced PIC 16F684 microcontroller 14 pins—12 multiplexed I/O + power/ground All computations using 2 values use accumulator Harvard memory architecture Memory divided into SFR / GPR Dedicated 8-entry system stack for return addresses (subroutines/interrupts) 10/27/2017 Microprocessors I: Lecture 10

25 Review: PIC Data Memory Organization
Made up of SFRs and GFRs Banking: 128 byte chunks Max offset within bank 0x7f Controller may have 2 or 4 banks Addressing Modes Direct addressing: 7 bit address (within bank) RP1:RP0 selects bank Indirect addressing: Access to INDF causes indirect addressing Actual memory address in IRP+FSR I/O ports Control register (e.g. TRISA) controls direction of each pin Bit = 1  input, Bit = 0  output Data register (e.g. PORTA) contains actual port state 10/27/2017 Microprocessors I: Lecture 10

26 Review: PIC instructions
Four instruction formats Upper bits of all hold opcode Byte-oriented includes 1 bit destination, 7 bit direct address Bit-oriented includes 3 bit position (0-7), 7 bit direct address Literal/control includes 8 bit literal CALL/GOTO includes 11 bit literal Variable declarations cblock <start_address>: start of variable declarations All names between cblock/endc directives assigned to consecutive bytes starting at <start_address> 10/27/2017 Microprocessors I: Lecture 10

27 Microprocessors I: Lecture 10
10/27/2017 Microprocessors I: Lecture 10

28 Review: PIC instructions (cont.)
Clearing register: clrw/clrf Moving values: movlw/movwf/movf Swap nibbles: swapf Single bit manipulation: bsf/bcf Unary operations: incf/decf/comf Arithmetic: addlw/addwf/sublw/subwf 10/27/2017 Microprocessors I: Lecture 10

29 Review: PIC instructions (cont.)
Logical operations andlw/andwf iorlw/iorwf xorlw/xorwf Rotates rrf rlf Jumps/calls/return goto call return/retlw/retfie Miscellaneous nop sleep/clrwdt Conditional execution Test bit and skip next instruction if clear/set: btfsc/btfss Increment/decrement register and skip next instruction if zero: incfsz/decfsz Example use: combined with goto to create conditional jump 10/27/2017 Microprocessors I: Lecture 10

30 Review: A Delay Subroutine
10/27/2017 Review: A Delay Subroutine ; *********************************************************************************** ; TenMs subroutine and its call inserts a delay of exactly ten milliseconds ; into the execution of code. ; It assumes a 4 MHz crystal clock. One instruction cycle = 4 * Tosc. ; TenMsH equ 13 ; Initial value of TenMs Subroutine's counter ; TenMsL equ 250 ; COUNTH and COUNTL are two variables TenMs nop ; one cycle movlw TenMsH ; Initialize COUNT movwf COUNTH movlw TenMsL movwf COUNTL Ten_1 decfsz COUNTL,F ; Inner loop goto Ten_1 decfsz COUNTH,F ; Outer loop goto Ten_1 return 10/27/2017 Microprocessors I: Lecture 10 Chapter 9

31 Review: Strategy to “Blink”
The LEDs are toggled in sequence - green, yellow, red, green, yellow, red… Let’s look at the lower three bits of PORTD 001=green, 010=yellow, 100=red The next LED to be toggled is determined by the current LED. 001->010->100->001->… 10/27/2017 Microprocessors I: Lecture 10

32 Microprocessors I: Lecture 10
“Blink” Subroutine Blink btfsc PORTD, 0 ; is it Green? goto toggle1 ; yes, goto toggle1 btfsc PORTD, 1 ; else is it Yellow? goto toggle2 ; yes, goto toggle2 ;toggle0 bcf PORTD, 2 ; otherwise, must be red, change to green bsf PORTD, 0 ; 100->001 return toggle1 bcf PORTD, 0 ; change from green to yellow bsf PORTD, 1 ; 001->010 toggle2 bcf PORTD, 1 ; change from yellow to red bsf PORTD, 2 ; 010->100 10/27/2017 Microprocessors I: Lecture 10

33 Another way to code “Blink” ---- Table Use
movf PORTD, W ; Copy present state of LEDs into W andlw B' ' ; and keep only LED bits addwf PCL,F ; Change PC with PCLATH and offset in W retlw B' ' ; (000 -> 001) reinitialize to green retlw B' ' ; (001 -> 010) green to yellow retlw B' ' ; (010 -> 100) yellow to red retlw B' ' ; (011 -> 001) reinitialize to green retlw B' ' ; (100 -> 001) red to green retlw B' ' ; (101 -> 001) reinitialize to green retlw B' ' ; (110 -> 001) reinitialize to green retlw B' ' ; (111 -> 001) reinitialize to green In calling program call BlinkTable ; get bits to change into W xorwf PORTD, F ; toggle them into PORTD 10/27/2017 Microprocessors I: Lecture 10

34 Microprocessors I: Lecture 10
Final notes Next time Exam 3, Monday, 8/13 Reminders Lab 4 due today HW 3 due Friday, 8/10 Do not do problem 2j (JL instruction) Turn in to my office by 1:45, or Exam 2 regrade requests due Monday, 8/13 Lab 5 due Tuesday, 8/14 Same turn-in procedure as HW 3 10/27/2017 Microprocessors I: Lecture 10

35 Microprocessors I: Lecture 10
References Myke Predko, "Programming and Customizing PICmicro Microcontrollers" 2nd Ed, McGrawHill, 2002, ISBN R. Laidman, Stepper Motors and Control, Part II - Bipolar Stepper Motor and Control, D. Jones, Stepping Motor Types, 10/27/2017 Microprocessors I: Lecture 10


Download ppt "Microprocessor Systems Design I"

Similar presentations


Ads by Google