UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS 2008/9 Semester One Module Code: EEE305 CRN: 3712 Title: MICROCONTROLLER SYSTEMS Time Allowed: 3 Hours Use of Dictionaries: English-foreign language dictionaries are permitted Examination Aids: A PIC16FXXX instruction set appears at the end of this paper. A CCS C compiler reference card appears at the end of this paper Instructions to Candidates: Candidates should read this section carefully before commencement of the examination. Answer as many questions as you can Not more than TWO questions from Section B will contribute to the final mark Students are advised to write their registration number and desk number only on any attachment, e.g. graph paper, or any other documentation being submitted with their examination script book(s). Module Co-Ordinator: Dr S J Katzen 1/9
SECTION A Q.A1 A certain dishwasher has four wash programmes, which are determined on reset (the power ON/OFF switch) by the setting of four front-panel switches labelled AUTO, ECONO, QUICK and RINSE. To allow the user some time to make up his/her mind, these switches are continually scanned, nominally, every four milliseconds and if a switch is set, the pattern stored in a File register. After 256 scans (approximately one second), the PICI6F84 microcontroller lights a green LED above the last closed switch, a buzzer is sounded for around 100 ms and the processor calls the appropriate subroutine called AUTOMATIC, ECONOMY, QUICK_WASH and PRE_RINSE, unless no switch is closed in which case the scan continues until a switch is closed. Pertinent hardware details are: A closed switch gives a logic 0 Each LED requires a nominal current of 20 ma. The buzzer requires five volts to activate. The PIC16F84 is clocked using a 4 MHz crystal and powered with a 5V supply. You are asked to design both input and output interface circuitry for the switches, buzzer and LEDs, and software for the system to implement the following scheme. Questions (1), (2) and (3) should be illustrated with a diagram and any necessary parallel port software configuration shown at that point in your answer. (1) The input interface for the four Programme-Select switches, including any necessary resistors and their nominal value. [6 marks] (2) The output interface for the four green LEDs. Determine the value of the current-limiting resistors and state if the LED is illuminated by a logic 0 or logic 1. Use the value of current given above. [8 marks] (3) A single output line to drive the buzzer. [2 marks] (4) An algorithm and assembly-level code as specified below. You can assume that all File registers between h 20 and h 2F are free for general variable storage. (a) A main routine to: (i) (ii) initialise the system; loop every four milliseconds using subroutine (b) below, reading the Programme-Select switches and storing the pattern in a File register if any switch is closed; (iii) breaking out of the loop after 256 reads; 2/9
(iv) test each bit in turn in the File register of (ii). If a logic 0 is found, light the corresponding LED and call the BUZZER subroutine of (d) below before finally calling up the appropriate subroutine AUTOMATIC, ECONOMY, QUICK_WASH or PRE_RINSE. You can assume that these subroutines are in position; you do not have to write them; (v) if a logic 0 is not found, then read the switches until a closed switch is found, store in the File register and repeat (iv). [27 marks] (b) A subroutine to give a fixed nominal 4 ms delay. [9 marks] (c) A subroutine based on (b) above to give a nominal 4 n ms delay, where n is an integer (0-255) passed to the subroutine in the Working register. [5 marks] (d) A subroutine to turn on the buzzer for nominally 100 ms using subroutine (c) above. [3 marks] For bonus marks: (5) What change could you make to your software so the washing programme selected will be the one that the user selected the last time the dishwasher was switched on, if a new switch is not closed during the initial scan period? [5 marks] (6) Write a main() function in the C language to implement the code of 4(a) above. You can assume functions buzzer(), automatic(), economy(), quick_wash() and pre_rinse() are already available and in place; you don t need to write them. Also available is the function delay_ms(n), as listed in the CCS compiler reference card at the end of this paper. [25 marks] 3/9
SECTION B Q. B2 Figure Q.B2 shows a simplified model of the PIC16F84 microcontroller. (a) (b) Describe the operation of each of the functional blocks employing, where relevant, its role with respect to the fetch and execute Harvard cycle. [14 marks] Explain how Indirect addressing is implemented in the PIC MCU family of processors, and illustrate your answer by showing how you could clear all memory locations between File h 30 and File h 4F using Indirect addressing. [6 marks] Vdd 14 Vss 5 Program store Program data bus Flash ROM 1K x 14 14 Pipeline Instruction reg 1 Instruction reg 2 Program address bus 13 Program Counter PLATH F 0Ah 8-level stack PIC16F84 F 02h 8 RAM Register file 68 x 8 8 File data bus File address bus 8 Data store EEPROM data memory F 08h EEPROM data EEPROM data memory 64 x 8 F 09h EEPROM address 7 Direct address Address mux Indirect 8 address F 04h File select reg. Timer 8 Literal data MCLR 4 OSC1 16 OSC2 15 op-code 7 Instruction decoder & control Q1 Q2 Q3 Q4 Oscillator and timing generation Power-up timer Oscillator start-up Power-on Reset Watchdog timer SLEEP F 0Bh Int control Data mux ALU Working reg. F 86h TRISB F 06h Port B F 03h RP0 Z DC C Status register I/O port B F 81h Prescale:OPTION F 01h Timer counter I/O port A F 85h TRISA F 05h Port A RA4/TOCKI 3 RA3 2 RA2 1 RA1 18 RA0 17 RB7 13 RB6 12 RB5 11 RB4 10 RB3 9 RB2 8 RB1 7 RB0/INT 6 4/9
Q. B3 (a) The interaction of a computer with external events can be by polling or interrupt driven. Discuss each method as applied to the scenario when a particular program is to be executed in response to whenever a person presses a button, as compared to monitoring a biomedical signal to a time resolution of 100µs. [4 marks] (b) Describe how a PIC MCU handles interrupts. Your answer should cover the Interrupt vector, INTCON register, general and local masking and interrupt flags. [7 marks] (b) Design an interrupt service routine handling a pulse on the RB0/INT pin which will copy the state of the higher four bits of PortB into the lower nybble of File h 20. For full marks, show your initialization of the INTCON register and setting up of the Interrupt vector. [9 marks] Q. B4 (a) Listing the advantages and disadvantages of each, briefly discuss the differences between a high-level programming language, such as C or Java, and assembly level programming. [4 marks] (b) Write a function using the CCS C language that will continually display on LEDs the bit pattern in successive bytes of the integer array DISPLAY[0] through DISPLAY[15]. Assume that eight LEDs are connected to port B and there is to be a 250 millisecond delay between each byte s display. A CCS C reference set is appended to the end of this paper. You should assume a clock frequency of 20 MHz. [16 marks] 5/9
14-bit 16CXX Dest CCR op-code Instruction Mnemonic WFZD C Operation summary 11 1110 LLLL LLLL ADD Literal to W addlw LL w <- w + #LL 00 0111 dfff ffff ADD W and F addwf f,d d <- w + f 11 1110 LLLL LLLL AND Literal to W andlw LL w <- w #LL 00 0101 dfff ffff ANDWtoF andwf f,d d <- w f 01 00nn nfff ffff Bit Clear File bit n bcf f,n 01 01nn nfff ffff Bit Set File bit n bsf f,n 01 10nn nfff ffff Bit Test File bit n & Skip if Clear btfsc f,n 01 11nn nfff ffff Bit Test File bit n & Skip if Set btfss f,n fn <- 0 fn <- 1 pc++ IF fn == 0 pc++ IF fn == 1 10 0aaa aaaa aaaa CALL (jump to) subroutine call aaa TOS <- pc, pc <- aaa 00 0001 1fff ffff CLeaR File clrf f f <- 00 00 0001 0000 0011 CLeaR Working register clrw d <- 00 00 0000 0000 0100 CLeaR Watch Dog Timer clrwdt wdt <- 00 00 1001 dfff ffff COMplement File comf f,d d <- f 00 0011 dfff ffff DECrement File decf f,d d <- f-- 00 1011 dfff ffff DECrement File & Skip on Zero decfsz f,d d <- f--, pc++ IF f == 0 10 1aaa aaaa aaaa GOTO (jump to) aaa goto aaa pc <- aaa 00 1010 dfff ffff INCrement File incf f,d d <- f++ 00 1111 dfff ffff INCrement File & Skip on Zero incfsz f,d d <- f++, pc++ IF f == 0 11 1000 LLLL LLLL Inclusive OR Literal to W iorlw LL w <- w + #LL 00 0100 dfff ffff Inclusive OR W to F iorwf f,d d <- w + f 00 1000 dfff ffff MOVe in File (load) movf f,d d <- f 11 0000 LLLL LLLL MOVe Literal into W movlw LL w <- #LL 00 0000 1fff ffff MOVe W out to File (store) movwf f f <- w 00 0000 0000 0000 No OPeration nop Do nothing 11 0100 LLLL LLLL RETurn from subroutine with L in W retlw w <- #LL, pc <- TOS 00 0000 0000 1000 RETURN from subroutine return pc <- TOS 00 0000 0000 1001 RETurn From IntErrupt retfie GIE <- 1, pc <- TOS 00 1101 dfff ffff Rotate Left File rlf f,d b7 C 7 file 0 00 1100 dfff ffff Rotate Right File rrf f,d b0 7 file 0 C 00 0000 0110 0011 Sleep mode on sleep wdt <- 0, Clock off 11 1100 LLLL LLLL SUB W from Literal sublw LL w <- #LL - w 00 0010 dfff ffff SUBtract W from F subwf f,d d <- f - w 00 1110 dfff ffff SWAP File nybbles swapf f,d d <- f[7:4] <--> f[3:0] 11 1010 LLLL LLLL exclusive OR Literal to W xorlw LL w <- w #LL 00 0110 dfff ffff exclusive OR W to F xorwf f,d d <- w f : Flag operates in the normal manner : Not affected a : Address d : Destination; 0=w,1=f f : File register f n : File bit n L : Literal data pc : Program Counter w : Working register wdt : Watch Dog Timer/prescaler TOS : Top Of Stack pc++ : Jump over next instruction == : Equivalent to ++ : Add one -- : Subtract one GIE : Global Interrupt Enable mask # : Constant S.J. Katzen L A T E X2ε Version 2.1.0 November 13, 2001 pic_ins14.tex 6/9
PIC16F83/84 Special-Purpose Register file summary File Name 7 6 5 4 3 2 1 0 Power-on All other address Reset Resets Bank 0 00h INDF Uses contents of this to address Data memory (not a physical register) 01h TMR0 8-bit real-time clock/counter XXXX XXXX UUUU UUUU 02h PCL 1 Lower-order 8 bits of the Program Counter 0000 00000 0000 0000 03h STATUS 1 IRP RP1 RP0 TO PD Z DC C 0001 1XXX 000??UUU 04h FSR Indirect Data memory address pointer 0 XXXX XXXX UUUU UUUU 05h PORTA RA4 RA3 RA2 RA1 RA0 X XXXX U UUUU 06h PORTB RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0 XXXX XXXX UUUU UUUU 08h EEDATA Data EEPROM Data register XXXX XXXX UUUU UUUU 09h EEADR Data EEPROM Address register XXXX XXXX UUUU UUUU 0Ah PCLATH Write buffer for top 5 PC bits 0 0000 0 0000 0Bh INTCON GIE EEIE T0IE INTE RBIE T0IF INTF RBIF 0000 000X 0000 000U Bank 1 80h INDF Uses contents of this to address Data memory (not a physical register) 81h OPTION RBPU INTEDG T0CS T0SE PSA PS2 PS1 PS0 1111 1111 1111 1111 82h PCL 1 Lower-order 8 bits of the Program Counter 0000 00000 0000 0000 83h STATUS 1 IRP RP1 RP0 TO PD Z DC C 0001 1XXX 000??UUU 84h FSR Indirect Data memory address pointer 0 XXXX XXXX UUUU UUUU 85h TRISA Port A Direction Register 1 1111 1 1111 86h TRISB Port B Data Direction Register 1111 1111 1111 1111 88h EECON1 Data EEPROM Data register XXXX XXXX UUUU UUUU 89h EECON2 EEPROM Control register (not a physical register) 8Ah PCLATH Write buffer for top 5 PC bits 0 0000 0 0000 8Bh INTCON GIE EEIE T0IE INTE RBIE T0IF INTF RBIF 0000 000X 0000 000U X Not known U Unchanged? Value depends on whether a Watchdog Reset and if in Sleep mode before Reset. Unimplemented; reads as 0. Note 1: Next instruction address if PIC in Sleep mode. S.J. Katzen L A T E X2ε Version 2.1.1 November 13, 2001 pic_ins14.tex 7/9
CCS C Compiler Version 3 Pre-Processor: Standard C #define #undef #include #if #ifdef #ifndef #else #endif #list #nolist #error #pragma Function Qualifiers #inline #separate #int_xxxxx #int_global #int_default Standard C #device #id #fuses Libraries #use delay #use rs232 #use i2c #use standard_io #use fixed_io #use fast_io Standard C #byte #bit #locate #reserve #rom #zero_ram #asm #endasm Standard C #case #opt #priority _date device pcb pcm pch Standard C: if, else, while, do, switch, case, for, return, goto, break, continue! ~ ++ -- + -,& / % << >> ^ &&?: < <= > >= ==!= = += -+ *= /= %= >>= <<= &= ^= = typedef, static, auto, const, enum, struct, union Arrays up to 5 subscripts Structures and Unions may be nested. Custom bit fields (1-8 bits) within structures. ENUMurated types CONSTant variables, arrays and strings. Full function parameter support (any number). Some support for C++ reference parameters. 8/9
Standard C Char atoi() atol() atoi32() atof() tolower(), toupper() isalnum(), isalpha() isamoung(), isdigit() islower(), isspace() isupper(), isxdigit() strlen(), strcpy() stncpy(), strcopy() strcmp(), stricmp() strncmp(), strcat() strstr(), strchr() strrchr(), strtok() strspn(), strcspn() strpbrk(), strlwr() Delays delay_cycles() delay_us() delay_ms() Capture / Compare / PWM setup_ccpx() set_pwmx_duty() Processor Controls sleep() reset_cpu() restart_cause() disable_interrupts() enable_interrupts() ext_int_edge() read_bank() Standard C Memory memset() memcpy() RS232 I/O getc() putc() gets() puts() printf() kbhit() set_uart_speed() 1C2I/O i2c_start() i2c_stop() i2c_read() i2c_write() i2c_poll() Discrete I/O output_low() output_high() output_float() output_bit() input() output_x() input_x() port_b_pullups() set_tris_x() SPI 2 Wire I/O setup_spi() spi_read() spi_write() spi_data_is_in() Parallel Slave I/O setup_psp() psp_input_full() psp_output_full( ) psp_overflow() Timers setup_timer_x() set_timer_x() get_timer_x() setup_counters() setup_wdt() restart_wdt() Standard C Math abs() abs320 acos() asin() atan() ceil() cos() exp() floor() labs() log() logl0() pwr() sin() sqrt() tan() A/D Conversion setup_adc_ports() setup_adc() set_adc_channel() read_adc() Analog Compare setup_comparator() Voltage Ref setup_vref() Internal EEPROM read_eeprom() write_eeprom() read_program_eeprogram_eepr( ) write_program_eeprom() read_calibration() Bit Manipulation shift_right() shift_left() rotate_right() rotate_left() bit_clear() bit_set() bit_test() swap() 9/9