Model Answer to PartA of Examination Paper EEE305J1: Microcontroller Systems 2001/2 General Observations

Similar documents
PIC Programming in Assembly. (

Using The PIC I/O Ports

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

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

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

Section 29. Instruction Set

10-bit Σ ADC from a PIC16F84

How To Program A Microcontroller With Memory On A Microchip Microcontroller

An Introduction to MPLAB Integrated Development Environment

AUTOMATIC NIGHT LAMP WITH MORNING ALARM USING MICROPROCESSOR

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

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

Section 14. Compare/Capture/PWM (CCP)

c0003 A Simple PIC Application CHAPTER 3

Section 8. Interrupts

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

Real-Time Clock. * Real-Time Computing, edited by Duncan A. Mellichamp, Van Nostrand Reinhold

Section 9. I/O Ports

Interfacing Analog to Digital Data Converters

MICROPROCESSOR AND MICROCOMPUTER BASICS

Interfacing To Alphanumeric Displays

Embedded C Programming

Microprocessor & Assembly Language

8051 hardware summary

4 Character 5x7 LED Matrix Display

PART B QUESTIONS AND ANSWERS UNIT I

Analog-to-Digital Converters

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

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

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

Microcontroller Code Example Explanation and Words of Wisdom For Senior Design

MACHINE ARCHITECTURE & LANGUAGE

Keil C51 Cross Compiler

ARM Thumb Microcontrollers. Application Note. Software ISO 7816 I/O Line Implementation. Features. Introduction

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

The stack and the stack pointer

Lecture N -1- PHYS Microcontrollers

MODULE BOUSSOLE ÉLECTRONIQUE CMPS03 Référence :

EXERCISE 3: String Variables and ASCII Code

(Refer Slide Time: 00:01:16 min)

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

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

Systems I: Computer Organization and Architecture

PIC16F84A. 18-pin Enhanced Flash/EEPROM 8-Bit Microcontroller. Devices Included in this Data Sheet: Pin Diagrams. High Performance RISC CPU Features:

Serial Communications

Chapter 13. PIC Family Microcontroller

Accurate Measurement of the Mains Electricity Frequency

Small Hardware Development and Prototyping Board for the SX28

Flow Charts & Assembly Language

A Digital Timer Implementation using 7 Segment Displays

Section 19. Voltage Reference

8051 MICROCONTROLLER COURSE

Section 28. In-Circuit Serial Programming (ICSP )

PIC16F8X. 18-pin Flash/EEPROM 8-Bit Microcontrollers. Devices Included in this Data Sheet: Pin Diagrams. High Performance RISC CPU Features:

Measuring Resistance Using Digital I/O

MICROPROCESSOR. Exclusive for IACE Students iacehyd.blogspot.in Ph: /422 Page 1

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

AN108 IMPLEMENTING A REALTIME CLOCK. Relevant Devices. Introduction. Key Points. Overview

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

CHAPTER 11: Flip Flops

ETEC Digital Controls PIC Lab 10 Pulse Width Modulation

PICmicro tm Development Board

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

Microcontroller Based Low Cost Portable PC Mouse and Keyboard Tester

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

How To Use A Watt Saver On A Microcontroller (Watt Saver) On A Cell Phone Or Mp3 Player

Monitoring of Intravenous Drip Rate

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

The components. E3: Digital electronics. Goals:

LADDER LOGIC/ FLOWCHART PROGRAMMING DIFFERENCES AND EXAMPLES

BASIC COMPUTER ORGANIZATION AND DESIGN

EMBEDDED SYSTEMS PROGRAMMING WITH THE PIC16F877

8085 INSTRUCTION SET

Timer A (0 and 1) and PWM EE3376

HANDLING SUSPEND MODE ON A USB MOUSE

PROBLEMS (Cap. 4 - Istruzioni macchina)

Traditional IBM Mainframe Operating Principles

Key Words Student Paper, School of Professional Studies

Decimal Number (base 10) Binary Number (base 2)

Controller for AD9850 DDS Modules Andy Talbot G4JNT

Having read this workbook you should be able to: recognise the arrangement of NAND gates used to form an S-R flip-flop.

HD44780-Based LCD Modules. Introduction to the LM018L

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

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

STEPPER MOTOR SPEED AND POSITION CONTROL

Application Note AN-1187

- 35mA Standby, mA Speaking pre-defined phrases with up to 1925 total characters.

Chapter 2 Logic Gates and Introduction to Computer Architecture

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

RS232 Board datasheet

AVR151: Setup and Use of the SPI. Introduction. Features. Atmel AVR 8-bit Microcontroller APPLICATION NOTE

The 104 Duke_ACC Machine

How To Program A Microcontroller Board (Eb064) With A Psp Microcontroller (B064-74) With An Ios 2.5V (Power) And A Ppt (Power Control) (Power Supply) (

Lab 17: Building a 4-Digit 7-Segment LED Decoder

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

FEATURES DESCRIPTION. PT6321 Fluorescent Display Tube Controller Driver

HD44780U (LCD-II) (Dot Matrix Liquid Crystal Display Controller/Driver)

DS1821 Programmable Digital Thermostat and Thermometer

8-Bit Flash Microcontroller for Smart Cards. AT89SCXXXXA Summary. Features. Description. Complete datasheet available under NDA

Voltage boost and buck circuits using Atmel AVR Tiny13V for driving a white LED.

Transcription:

Model Answer to PartA of Examination Paper EEE305J1: Microcontroller Systems 2001/2 General Observations Design questions like the mock question below are extremely difficult to mark, not least because there are literally an infinity of answers and the required information may pop up in several places in your script. The student should be careful to maximise his/her marks by clearly setting out all required information and assumptions, and by using aids to clarification, such as task lists, flowcharts and program comments. Time is of the essence with a question like this. Taking the primary mark (ex. bonus) of 70% gives us a total of 125 minutes (two hours). Thus a 6 mark section should be completed in around ten minutes, or 1.8 minutes per mark! Do not write a two-page essay for six marks; it isn t cost effective. The Question A certain vending machine accepts 1p, 2p, 5p, 10p, 20p and 50p coins. The coin mechanism channels the coins past the appropriate six microswitches and generates a short pulse whenever any microswitch closes. A 2-digit common-cathode 7-segment display is to indicate the total up to 99p. The intelligence of this machine is to be implemented using a PIC16F87X-based system which is to read the six coin sensing switches each time it is interrupted and to manage the 2-digit multiplexed 7-segment display. Pertinent hardware details are: A coin passing by in the chute is to close the normally-open microswitch which taken as a group will give a one of six code output (only one switch closed at a time). When any switch is closed, a single high-going pulse on a separate line is generated to alert the PIC that a coin has been inserted. The two common-cathode 7-segment displays are multiplexed to the one port with each digit being enabled by a single NPN transistor. When enabled, each segment requires a nominal 20 ma current. The output signalling a passing coin is to be active-high at normal logic levels. The PIC is clocked using a 4 MHz crystal and powered at +5 V and Ground. You are asked to design both input and output interface and software for the system to implement the following scheme. Questions (1), (2) and (3) should be illustrated by diagrams and any software port configuration software should be shown. 1. The input interface for the six coil microswitches, including the necessary resistor. 9 minutes...[5 marks] 1

2. The single input line indicating activity in the money chute interrupting the PIC. Show how the interrupt is enabled in software. 4 minutes...[2 marks] 3. The output interface to the two common-cathode 7-segment displays and two NPN transistors. You may assume that the minimum current gain of these enabling transistors is 40. Determine the value of the segment resistors and base resistors. 22 minutes...[12 marks] 4. An algorithm and assembly-level listing as follows, assuming that file registers 20 2Fh are free: (a) An Interrupt Service Routine entered whenever a coin falls down the chute, that accumulates the input money microswitch state as a grand total of pence in a file register. 30 minutes...[16 marks] (b) A subroutine giving a nominal 1 ms delay. 9 minutes...[5 marks] (c) A subroutine converting from a binary byte in the Working register to binary-coded decimal, with two bytes giving Tens and Units. 14 minutes...[8 marks] (d) A subroutine converting a 4-bit code binary 0000 through 1001 to active-high 7- segment code. 10 minutes...[6 marks] (e) A main endless background loop to: Initialise the system and variables. Call up subroutine?? above to convert the total of money in?? to binary-coded decimal. Call up subroutine?? two times to convert each digit to 7-segment code. To activate each 7-segment digit in turn with the appropriate data using the delay subroutine of??. 30 minutes...[16 marks] For Bonus marks: 5. What additional hardware would be required if a PIC16F84 were to be used as the microcontroller? 10 minutes...[6 marks] 6. What additional software and hardware resources would be needed to extend your system to cope with money values up to 2.55? 7 minutes... [4 marks] 2

Overall Specifications Be sure to understand the question! There are six switches to be interfaced to the PIC, and the most convenient way to do this is to use Port B inputs for this purpose. The pulse / \ from the coin mechanism can be connected to RB0/INT and the two 7-segment displays connected to, say, Port D with two Port A lines being used to drive the two transistors which enable the displays. Basically when the processor is interrupted each switch is tested for 0 in turn and the appropriate amount is added to the total. In the background the contents of this moneybox is continually converted to two BCD digits and displayed, with alternate digits being displayed each millisecond. Worked Solution A possible overall circuit is shown in Fig.??. PIC16F87X +5V Logic 0/1 from PIC 0V/+5V RD7 RB7 1p RD6 RB6 2p RA? RD5 RB5 5p LED RD4 RB4 10p to other six segments Common cathode RD3 RB3 20p RB? Logic 0/1 from PIC 0V/+5V RD2 RB2 50p RD1 RB1 Typical segment RD0 RB0/INT Coin passing down chute MSD Common cathode LSD Common cathode TR1 RB1 RA1 RA0 TR2 RB2 Figure 1: Block diagram of system covering solutions 1.1, 1.2 and 1.3 3

1. Key points are: Connection of the switches to a Port B lines, RB[7:2], with one end of switches grounded 1 mark. The need for a pull-up resistor to give a logic 1 when the switch is open. This is typically around 10 kω. Alternatively (only one student got this) use the internal Port B weak pull-up resistors by clearing bit 7 of the Option register. This needs the instruction bsf OPTION_REG,7 whilst in Bank 1 2 marks. Setting up of Port B: (also shown are the settings of Port A and Port D) 2 marks. bsf STATUS,RP0 ; Change to Bank 1 movlw 0FFh ; PortB all inputs movwf TRISB ; Make PortB all inputs movlw b 111100 ; PortA, pins 0 & 1 are out put movwf TRISA ; connected to transistors clrf TRISD ; Port D all outputs to common displays bcf STATUS,RP0 ; Back to Bank 0 For Port B it would be sufficient to state that all ports are automatically set to input on reset. 2. Key points are: Line shown connected to RB0/INT 1 2 mark. Setting up of INTCON to enable the hardware interrupt 1 1 2 mark. bsf INTCON,INTE ; Setthe INTerruptEnable flag bsf INTCON,GIE ; Set the General Interrupt Enable flag 3. Diagram showing two 7-segment digits with series anode resistors for each segment to limit current from the seven Port D lines and a NPN transistor connecting the common cathodes to ground or off (see Fig.??) 6 marks. To calculate the segment resistors we know that a LED drops around 2 V when conducting and that the PIC outputs are 5 V (actually it drops a little when it supplies 20 ma) when logic 1. Thus V = IR; R = 5 2 150Ω 2 marks. 20 10 3 To calculate the base transistor resistor, we know to know what collector current the transistor will carry. Each on segment will need 20 ma, and if all segments are lit (i.e. ) that makes 140 ma. If the current gain of the transistor is 40 (rather low) then the base current is 1.3 kω 2 marks. Code above setting Port D and Port A[1:0] to outputs 2 marks. 4. It is possible to get 2 of the paper s marks from the software just in this question. Thus 3 you ignore software at your peril! Although marks are not specifically allocated for task lists, flowcharts etc. it is strongly recommended that you spend some time putting your thoughts down on paper before 4

doing any coding. Not only does this help clarify your thoughts but it makes it easier for us to follow your program and give marks. (a) A possible Interrupt Service routine follows the task list: i. Save the Status and Working registers. ii. Get the total, labelled MONEY, in the main routine and then test each switch in turn adding the appropriate amount if a 0 is found. When completed restore the new value back in MONEY. iii. Restore the registers saved above. ; * Interrupt Service Routine entered on rising edge on pin RB0 * ; * Interrogates 6 switches and adds appropriate amount to MONEY * ISR movwf _work ; Save working register in file called _work swapf STATUS,w ; Get Status register without changing flags movwf _status ; and save in file register called _status ; ----------------------------------------------------------------------- bcf INTCON,INTF ; Clear interrupt flag movf MONEY,w ; Getcurrenttotal money btfss PORTB,7 ; Check for 1p switch (== 0?) addlw 1 ; IF closed THEN add one p ; and so on btfss PORTB,6 ; Check for 2p switch (== 0?) addlw 2 ; IF closed THEN add two p btfss PORTB,5 ; Check for 5p switch (== 0?) addlw 5 ; IF closed THEN add five p btfss PORTB,4 ; Check for 10p switch (== 0?) addlw d 10 ; IF closed THEN add decimal ten p ; NOTE DECIMAL 10 not10 hex! btfss PORTB,3 ; Check for 20p switch (== 0?) addlw d 20 ; IF closed THEN add decimal twenty p btfss PORTB,2 ; Check for 50p switch (== 0?) addlw d 50 ; IF closed THEN add decimal fifty p movwf MONEY ; Putyour money away ; ------------------------------------------------------------------------ swapf _status,w ; Get original Status register put away above movwf STATUS ; Does notaffectstatus flags swapf _work,f ; Swapping the saved Working reg twice swapf _work,w ; into W is a way of not affecting Status flags retfie ; Exit 5

Marks for: Saving entry/exit (standard for most ISRs) 5 marks. Clearing INTF before leaving 2 marks. Checking each bit RB[7:2] and adding the appropriate constant 8 marks. Return from interrupt retfie 1 mark. Notes: i. A few students added the BCD amounts; e.g. 50h for 50p remember that 50h is the same as 50 BCD (0101 0000). They then corrected at the end, before exit by adding 6 to the low digit if it was over 9 and the same for the upper nybble. That meant that an additional 8 marks was allocated in place of section (4)(c). ii. One student checked for a Carry after an addition and if so cleared the total to show overflow (i.e. over 99p). (b) A nominal 1 ms delay at 4 MHz can be got by decrementing the Working register down from FFh (decimal 255) to zero. ; FUNCTION: Provides a nominal 1ms delay with a 4MHz crystal * ; ENTRY : None * ; EXIT : Working register zero * DELAY_1_S movlw d 255 ; : 1 DLOOP addlw -1 ; Decrementdown from 255 to 0: 255 * 1 btfss STATUS,Z ; Exit loop if zero : (254*1) + 2 goto DLOOP ; Repeat until skipped over : 254*2 return ; : 2 Total time in cycles ( ) is 2 (for the call) +1+255+255+508+2=1023. With a 4 MHz crystal one cycle equals 1 µs so 1023 µs is just over 1 ms. Marks for: Algorithm 2 marks Calculation 2 marks Use of return for subroutine 1 mark NOTE: Could have been done as a macro. (c) To convert from a naturally (8-4-2-1) coded binary number which you know will never be greater than 99 to tens and units, simply subtract ten repeatedly until the outcome underflows (i.e. generates a Borrow). The number of successful subtractions it the number of tens and the residue after the last successful subtraction is the units. A suitable task list is: i. Zero the Tens count. ii. Subtract ten from the binary number. iii. IF did not generate a borrow then increment TENS and repeat. iv. The residue is UNITS. 6

* ; * FUNCTION: Determines the number of tens and units in a byte < 100 * ; * ENTRY : Natural binary byte in Working register * ; * EXIT : TENS, UNITS updated. W changed * ; * EXAMPLE : W = 01000001 -> TENS = 00000110 (06), UNITS = 00000101 (05) * * BIN_2_BCD clrf TENS ; Zero the Tens count ; ------------------------------------------------------------------------- LOOP_10 addlw -d 10 ; Take away ten (00001010) ; ------------------------------------------------------------------------- btfss STATUS,C ; IF no Borrow (C = 1) THEN skip goto COMPLETE ; ELSE a borrow signals completion incf TENS,f ; IncrementTens count goto LOOP_10 ; and repeat ; -------------------------------------------------------------------------- COMPLETE addlw d 10 ; When complete then add on the last ten too many! movwf UNITS ; and this is the Units count return ; End of subroutine Marks for: Algorithm 2 marks. Loop structure 2 marks. Coding 4 marks. NOTE: See remark in ISR above. (d) This again is a standard classroom exercise: t ; * FUNCTION: Converts a binary nybble 0-9 o act ive-high 7-seg code * ; * ENTRY : Binary value 00000000-00001001 (0-9) in W * ; * EXIT : 7-segmentcode in W * ; * EXAMPLE : n = 06, ret urn in W = 1111101 * SVN_SEG addwf PCL,f ; Add n to PC to return with a code for n ; gfedcba retlw b 0111111 ; Code for 0 retlw b 0000110 ; Code for 1 retlw b 1011011 ; Code for 2 retlw b 1001111 ; Code for 3 retlw b 1100110 ; Code for 4 retlw b 1101101 ; Code for 5 retlw b 1111101 ; Code for 6 retlw b 0000111 ; Code for 7 retlw b 1111111 ; Code for 8 retlw b 1101111 ; Code for 9 7

Marks for: Code patterns 2 marks addition/retlw structure 4 marks Bonus marks were given if it was pointed out that it is possible for the addition to overflow the PC, aspcl is only eight bits. Also mention that for robustness the value sent out to this subroutine should be ANDed to remove upper four bits and dummy entries in the table for 1010b 1111b. (e) The examination paper question is essentially a task list. Depending on whether MONEY is kept as BCD or a subroutine used to convert on display, the listing may vary somewhat. Here is a typical listing. include "p16f87x.inc" org 0 ; The Resetvector goto MAIN ; Goto the main background program org 4 ; The Interrupt vector goto ISR ; Goto the Interrupt handler ; ------------------------------------------------------------------------ MONEY equ 20h ; The money box MAIN ; Port initialisation as given in response for (1), (2) \& (3) above. clrf MONEY ; Startoff on Resetwith zero money! ; ------------------------------------------------------------------------- M_LOOP movwf MONEY,w ; Get money total call BIN_2_BCD ; Convertto BCD for display ; ------------------------------------------------------------------------- movf UNITS,w ; The Units from BIN_2_BCD call SVN_SEG ; Convertto 7-segmentcode movwf PORTC ; Send to display bsf PORTA,0 ; Turn on the LSD display bcf PORTA,1 ; and off the MSD call DELAY_1_MS ; for one ms ; -------------------------------------------------------------------------- movf TENS,w ; The Tens from BIN_2_BCD call SVN_SEG ; Convertto 7-segmentcode movwf PORTC ; Send to display bcf PORTA,0 ; Turn off the LSD display bsf PORTA,1 ; and the MSD display on call DELAY_1_MS ; for one ms ; --------------------------------------------------------------------------- goto M_LOOP ; Repeat forever Marks for: 8

Reset/interrupt vectors 2 marks Correct sequencing of calls with data passing 8 marks Multiplexing of the two digits 6 marks Bonus marks 5. A serial scheme, such as in Fig. 12.2 of your book, could replace Port C by RA2 & RA3 (say) and an external serial-in/parallel-out shift register driving both displays in nonmultiplexed mode. All other hardware would remain the same 6 marks. 6. Initially subtract a hundred in the BIN_2_BCD subroutine incrementing a HUNDS location. A third 7-segment display would be needed with RA2 and another transistor used to enable this additional display. Additional 1 and 2 switches would be an advantage, but not essential 4 marks. eee305j1_first_a1_solution_01.tex L A T E X February 7, 2002 9