16.317: Microprocessor Systems Design I Spring 2015 Exam 3 Solution

Similar documents
PIC Programming in Assembly. (

Section 29. Instruction Set

Embedded C Programming

Analog-to-Digital Converters

SPI. Overview and Use of the PICmicro Serial Peripheral Interface. Getting Started: SPI

AN727. Credit Card Reader Using a PIC12C509 DATA ENCODING INTRODUCTION FIGURE 1: POSITION OF ISO TRACKS 1, 2 AND 3. Andrew M Errington

Using The PIC I/O Ports

How To Program A Microcontroller With Memory On A Microchip Microcontroller

Section 14. Compare/Capture/PWM (CCP)

10-bit Σ ADC from a PIC16F84

Debouncing Switches. Mechanical switches are one of the most common interfaces to a uc.

Four-Channel Digital Voltmeter with Display and Keyboard. 8 x 220W RB0 RB1 RB2 RB3 RB4 RB5 RB6 RB7 RA0 RA1 RA2 RA3 PIC16C71

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

Microcontroller Code Example Explanation and Words of Wisdom For Senior Design

Simple Alarm System WIRELESS AND REMOTE CONTROLLED PERSONAL APPLIANCE CODE WORD ORGANIZATION TRANSMISSION FORMAT

Programming PIC Microcontrollers in PicBasic Pro Lesson 1 Cornerstone Electronics Technology and Robotics II

AN857. Brushless DC Motor Control Made Easy INTRODUCTION S 001 B. Anatomy of a BLDC SIMPLIFIED BLDC MOTOR DIAGRAMS. Microchip Technology Inc.

AN857. Brushless DC Motor Control Made Easy INTRODUCTION S 001 B. Anatomy of a BLDC SIMPLIFIED BLDC MOTOR DIAGRAMS

How to use AVR Studio for Assembler Programming

PROGRAMMABLE LOGIC CONTROLLERS Unit code: A/601/1625 QCF level: 4 Credit value: 15 TUTORIAL OUTCOME 2 Part 1

PIC Lab Manual. PIC Lab Manual. 1 Include CD-ROM 2010/2011

Timer A (0 and 1) and PWM EE3376

Keil C51 Cross Compiler

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

8051 MICROCONTROLLER COURSE

AN730. CRC Generating and Checking INTRODUCTION THEORY OF OPERATION EXAMPLE 1: MODULO-2 CALCULATION. Example Calculation. Microchip Technology Inc.

Nemo 96HD/HD+ MODBUS

Implementing SPI Master and Slave Functionality Using the Z8 Encore! F083A

Lecture 22: C Programming 4 Embedded Systems

MODULE BOUSSOLE ÉLECTRONIQUE CMPS03 Référence :

Z80 Instruction Set. Z80 Assembly Language

RS-485 Protocol Manual

An Introduction to MPLAB Integrated Development Environment

Chapter 13. PIC Family Microcontroller

Step Motor Controller. Application Note. AVR360: Step Motor Controller. Theory of Operation. Features. Introduction

EMBEDDED SYSTEMS PROGRAMMING WITH THE PIC16F877

Interrupts and the Timer Overflow Interrupts Huang Sections What Happens When You Reset the HCS12?

AN617. Fixed Point Routines FIXED POINT ARITHMETIC INTRODUCTION. Thi d t t d ith F M k Design Consultant

I 2 C Master Mode Overview and Use of the PICmicro MSSP I 2 C Interface with a 24xx01x EEPROM

8-bit Microcontroller. Application Note. AVR222: 8-point Moving Average Filter

Microcomputers. Analog-to-Digital and Digital-to-Analog Conversion

Flow Charts & Assembly Language

PART B QUESTIONS AND ANSWERS UNIT I

Chapter 2: Assembly Language Programming. The PIC18 Microcontroller. Han-Way Huang

AN1229. Class B Safety Software Library for PIC MCUs and dspic DSCs OVERVIEW OF THE IEC STANDARD INTRODUCTION

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16

ELEG3924 Microprocessor Ch.7 Programming In C

SSPBUF. Shift Clock SSPSR. START bit, STOP bit, Acknowledge Generate

AN585. A Real-Time Operating System for PICmicro Microcontrollers INTRODUCTION. Why do I Need a Real-Time Kernel? What is Multitasking Anyway?

Memory organization. Memory blocks: Program memory (flash-type) 16 kword (32 kbyte) (instruction 16 bit wide) Data RAM 1536 byte (1.

DAC Digital To Analog Converter

Measuring Resistance Using Digital I/O

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

Section 29. Real-Time Clock and Calendar (RTCC)

INTERRUPTS. There are 2 methods for communicating between the microcontroller and the external system: POLLING INTERRUPTS

6. Control Structures

Scan a Keypad with the BS2 For Pushbutton User Input

Useful Number Systems

B Robo Claw 2 Channel 5A Motor Controller Data Sheet

Quick Reference. B.5 12-Bit Core Instruction Set APPENDICES

Lecture N -1- PHYS Microcontrollers

PIC in Practice. A Project-Based Approach. D. W. Smith

Using the HCS12 Serial Monitor on Wytec Dragon-12 boards. Using Motorola s HCS12 Serial Monitor on Wytec s Dragon-12 boards

STEPPER MOTOR SPEED AND POSITION CONTROL

Pololu P-Star 25K50 Micro User s

eztcp Technical Document Modbus/TCP of eztcp Caution: Specifications of this document may be changed without prior notice for improvement.

PROGRAMMABLE LOGIC CONTROLLERS Unit code: A/601/1625 QCF level: 4 Credit value: 15

EE282 Computer Architecture and Organization Midterm Exam February 13, (Total Time = 120 minutes, Total Points = 100)

HC12 Assembly Language Programming

Digital DC Motor Speed Regulator

Graded ARM assembly language Examples

PROGRAMMABLE LOGIC CONTROLLERS Unit code: A/601/1625 QCF level: 4 Credit value: 15 OUTCOME 3 PART 1

Lesson-16: Real time clock DEVICES AND COMMUNICATION BUSES FOR DEVICES NETWORK

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: EC Microprocessor & Microcontroller Year/Sem : II/IV

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Traditional IBM Mainframe Operating Principles

Section 28. In-Circuit Serial Programming (ICSP )

Introduction to PIC Programming

Sample EHG CL and EHG SL10 16-bit Modbus RTU Packet

c0003 A Simple PIC Application CHAPTER 3

Data Acquisition Module with I2C interface «I2C-FLEXEL» User s Guide

The goal is to program the PLC and HMI to count with the following behaviors:

USB Human Interface Joystick Demonstration Create a HID USB Device (sample Joystick) By Jared St.Clare, October 2009 Version 1

PIC12F508/509/16F505 Data Sheet

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

Table 1 below is a complete list of MPTH commands with descriptions. Table 1 : MPTH Commands. Command Name Code Setting Value Description

Section bit A/D Converter

Section 9. I/O Ports

EXPERIMENT NUMBER 5 BASIC OSCILLOSCOPE OPERATIONS

C Programming Structure of a C18 Program

Microcontroller Basics A microcontroller is a small, low-cost computer-on-a-chip which usually includes:

Section bit A/D Converter

EXERCISE 3: String Variables and ASCII Code

Asynchronous counters, except for the first block, work independently from a system clock.

MICROPROCESSOR AND MICROCOMPUTER BASICS

Exception and Interrupt Handling in ARM

WARRANTY AND DISCLAIMER

Introduction to Embedded Systems. Software Update Problem

Software Manual RS232 Laser Merge Module. Document # SU Rev A

Transcription:

16.317: Microprocessor Systems Design I Spring 2015 Exam 3 Solution 1. (20 points, 5 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling or underlining the single choice you think best answers the question. a. We discussed the following delay loop in class: Ten_1 decfsz COUNTL,F ; Inner loop goto Ten_1 decfsz COUNTH,F ; Outer loop goto Ten_1 return How many times will the first instruction in this loop (decfsz COUNTL,F) execute if COUNTH is initially equal to 0x02 and COUNTL is initially equal to 0x01? (All answers given below are in base 10, unless otherwise noted.) i. 1 ii. 101 iii. 201 iv. 257 (which is 0x101 in hexadecimal) v. 258 1

1 (continued) b. Under what conditions will the following code jump to the label L1? movf subwf btfss goto btfss goto END: x, W y, W STATUS, Z END STATUS, C L1 i. x y ii. x = y and C = 1 iii. x = y and C = 0 iv. x y and C = 1 v. x y and C = 0 c. You are given the following short PIC16F1829 assembly function: F: movf PORTC, W andlw B'00000010' addwf PCL, F retlw B'11110000' retlw B'00111100' retlw B'00001111' retlw B'11111111' If PORTC = 0xC3, what value is in the working register when this function returns? i. B'00000001' ii. iii. iv. B'00001111' B'00111100' B'11110000' v. B'11111111' 2

1 (continued) d. Circle one (or more) of the choices below that you feel best answers this question. i. Thanks for the free points. ii. iii. iv. I don t REALLY have to answer the last three questions, do I? This exam is the best final I ve taken all day. None of the above. All of the above answers are correct. 3

2. (12 points) General microcontroller programming a. (3 points) Explain how an interrupt service routine can prioritize one interrupt over another. Solution: Priority is implicitly created based on the order in which the ISR evaluates the interrupt flags the first one tested will essentially be treated as the highest priority interrupt. b. (3 points) What are the different factors that determine the total amount of delay in an instruction count-based delay loop? Solution: The system clock cycle time, the number of clock cycles per instruction, and the number of instructions executed in the loop. You could also consider the initial values in the delay counter, which determines the number of instructions executed in the loop. c. (3 points) Given a 10-bit result from an analog to digital converter, as in the PIC 16F1829, explain a case in which you might prefer a left-justified result (upper 8 bits) over a rightjustified result (lower 8 bits). Solution: Using a left-justified result essentially ignores the lowest two bits of the result, meaning that the result does not take small changes in voltage into account. You would likely prefer this type of result for any voltage measurement in which the voltage is humancontrolled for example, if the voltage is based on the position of a potentiometer because people typically aren t capable of making small enough changes that you d need the lowest bits of the ADC result. d. (3 points) Explain why button presses cannot accurately be detected by simply checking if the switch produces a low voltage. Solution: First, note that I determined this question was too vague and gave full credit to everyone who attempted it. Had I asked why presses couldn t accurately be counted, I think it would have been clearer. The solution below answers that question: Detecting and counting button presses accurately requires you to differentiate the point at which the button is pressed from points at which the button is simply being held down without having been released. The only way to detect the time when the button is pressed is to detect a falling edge (a high to low transition) on the button. Checking if the switch produces a low voltage simply allows you to recognize that the button is currently pressed and was pressed at some point before it was sampled. 4

3. (18 points) PIC C programming Complete the short function below by writing the appropriate line(s) of C code into each of the blank spaces. The purpose of each line is described in a comment to the right of the blank. This interrupt service routine works with the analog-to-digital converter, as well as a global variable, var, which determines what is written to the LEDs. The ISR does the following: On a switch interrupt, start an analog-to-digital conversion. On a timer interrupt, evaluate the value in ADRESH and change the LEDs as follows: o If the value in ADRESH is in the upper half of possible values (which you can test by checking if any of the top 4 bits are set (actually only the MSB see below)), increment the value on the LEDs. You must account for the rollover case when the LEDs go from 1111 to 0000. o Otherwise, decrement the value on the LEDs, accounting for the 0000 to 1111 case. Assume the LEDs are wired to the lowest four bits of Port C, as on the development board used in HW 6, and that SWITCH and DOWN are appropriately defined. NOTE: The description above about detecting if ADRESH is in the upper half of possible values is incorrect. Any 8-bit register has 256 possible values in the range 0-255. The upper half of that range starts at 128. Therefore, all values in the upper half of the range have bit 7 set to 1. void interrupt ISR(void) { if (IOCAF) { // SW1 was pressed IOCAF = 0; // Clear flag in software delay_ms(5); // Delay for debouncing if (SWITCH == DOWN) { // If switch still pressed GO = 1; // start ADC if (INTCONbits.T0IF) { // Timer 0 interrupt INTCONbits.T0IF = 0; // Clear flag in software if (ADRESH & 0x80) { // If A/D result is in LATC++; // upper half of possible if (LATC == 0x10) // results (any of upper LATC = 0; // four bits are set) // increment LED value // Must account for // 1111 à 0000 case else { // Otherwise, decrement LATC--; // LED value. Must if (LATC == 0xFF) // account for LATC = 0x0F; // 0000 à 1111 case 5

4. (50 points, 25 points per part) PIC assembly programming For each of the following complex operations, write a sequence of PIC 16F1829 instructions not C code that performs an equivalent operation. CHOOSE ANY TWO OF THE THREE PARTS and fill in the space provided with appropriate code. You may complete all three parts for up to 10 points of extra credit, but must clearly indicate which part is the extra one I will assume it is part (c) if you mark none of them. Assume that 8-bit variables TEMP and COUNT have been defined for cases where you may need extra variables. Finally, please note that you are not required to write comments describing each instruction. You are certainly welcome to do so if you feel it will make your solution clearer to the instructor. a. You are given two unsigned 16-bit values, X and Y. You can access individual bytes within each value X contains bytes XH and XL (XL is the least-significant byte) and Y contains bytes YH and YL. Write a sequence of instructions that jumps to location L1 if X is less than Y. Your solution should not change any of the bytes of X or Y. Again, assume X and Y are unsigned (i.e., non-negative). Solution: Other solutions may be acceptable. The key points are: Performing a 16-bit subtraction in which the result ends up in the working register, so neither X nor Y is changed. Evaluating the carry flag to determine whether a borrow was used movf YL, W ; W = YL subwf XL, W ; W = XL YL movf YH, W ; W = YH subwfb XH, W ; W = XH YH (~C) (account for borrow in lo byte) btfss STATUS, C ; If C == 0 (borrow occurred), then X < Y goto L1 ; and program should jump to L1 6

4 (continued) Remember, you can assume that 8-bit variables TEMP and COUNT have been defined for cases where you may need extra variables. b. Given two 8-bit variables, X and N, flip the lowest N bits of X (change 0 à 1, 1 à 0) while leaving the other bits unchanged. For example: If X = 0x00 and N = 0x02, flip the lowest two bits bits 0 and 1. o X = 0x00 = 0000 0000 2 originally à X will change to 0000 0011 2 = 0x03 If X = 0x0C and N = 0x06, flip the lowest six bits bits 0 through 5. o X = 0x0C = 0000 1100 2 originally à X will change to 0011 0011 2 = 0x33 Note that: Since X and N are not constants, you cannot use both values together in any PIC instruction (for example, btfsc X, N is not a valid instruction). Your code should not modify N. Solution: Other solutions may be acceptable. The key points are Using an XOR operation to flip the appropriate bit(s) Using the counter N to determine the number of bits to be flipped movf N, W ; COUNT = N movwf COUNT movlw 0x01 ; Initial bit mask for flipping will start with 1 ; in lowest bit position L: xorwf X, F ; Flip appropriate bit of x movwf TEMP ; TEMP = bit mask (1 << # iterations) lslf TEMP, W ; W = shifted bit mask (move everything to ; left, shifting 0 into lowest position) decfsz COUNT, F ; Decrement loop counter and return to goto L ; start of loop if result is not 0 7

4 (continued) Remember, you can assume that 8-bit variables TEMP and COUNT have been defined for cases where you may need extra variables. c. You are given two 16-bit values, X and Y, and an 8 bit value, ZL. You can access individual bytes within each value X contains bytes XH and XL (XL is the least-significant byte) and Y contains bytes YH and YL. Perform a 16-bit left rotate without carry: X = Y rotated by ZL. (Note that, because the rotate amount is not greater than 15, a single byte is sufficient to hold that value.) Do not change Y or ZL when performing this operation. At each step, note that the bit shifted out of the most significant bit should be shifted into the least significant bit. For example, if Y = 0xFF00 = 1111 1111 0000 0000 2 and ZL = 1, then the final result should have X = 1111 1110 0000 0001 2. Solution: movf YL, W ; Copy YL to XL movwf XL movf YH, W ; Copy YH to XH movwf XH movf ZL, W ; Copy ZL to COUNT movwf COUNT L: bcf STATUS, C ; Carry = 0 btfsc XH, 7 ; If MSB (bit to be rotated) is 1 bsf STATUS, C ; set carry to 1 before rotating rlf XL, F ; Rotate upper byte (C = bit to be shifted into XL) rlf XH, F ; Rotate lower byte decfsz COUNT, F ; Decrement loop counter and return to start goto L ; of loop if there are more iterations. 8