Final Exam - December l&l997 University of Toronto at Scarborough CSC CSSF - Microprocessor Systems 3 hours - No notes or similar aids allowed. Marks Examiner: Gyula Lorincz P3 oc-3-20 1. Explain in detail what the following C program does. Bits Pi.0 to P1.3 are connected to four switches while bits P1.4 t0 P1.7 are connected to LEDs. For full credit you must comment each line of the program, and explain the overall operation of the program. #pragma nointvector #pragma code symbols #include <reg83c51.h> #include <stdio.h> unsigned char varl=o; -,,J&, L.&y t&&< unsigned char var2=100; $J,&i 5 " V' unsigned char var3=250; s&a /. sbit Pl-0 = 0x90; sbit Pl-1 = 0x91; sbit Pl-2 = 0x92; sbit Pl-3 = 0x93; sbit Pl-4 = 0x94; sbit Pl-5 = 0x95; sbit Pl-6 = 0x96; 0, sbit Pl-7 = 0x97; -,Jj,/&A, / i 2 void int-handler (void) interrupt 1 us&g l{,; -. _..A TRO = 0; THO = -20000/256; TLO = -200008256; TRO = 1; ++varl; if (varl==var2) Pl=OxOF;.,,,~,~p else if (varl==var3){ varl=o; Pl-4=Pl-3; --, Pl 7=Pl-0; ) - 1 t void main (void){ TMOD=OxOl; Pl=OxOF; c - 3,,ywfi TFO=l; E=Ox82; while (l)( 1
20 3. A photogate is used to count boxes movin,u along a conveyor belt. The output of the photogate, which is connected to the Tl input of an,8052, is logic 0 when a box blocks the beam and logic 1 otherwise. Write a program in 8052 assembly language that uses an interrupt service routine to trigger an alarm connected to output P1.7 after five hundred boxes pass the photogate. Assume that the alarm is on if P1.7 equals 1. You must include the code needed in the main routine to set up the interrupts. Comments are required. 20 3. The diagram below shows an external (serial in/parallel out) shift register connected to a the TXD and RXD lines of the serial port of a 8051. This allows an extra 8 bits to be output from the 8051. Write a program that initializes the 8051 serial port for shift register mode (mode 0) and then uses a timer interrupt to output the contents of a 10 element array in external data memory to the extra 8 outputs, with a delay between values of 1 second. You must include the code needed in the main routine to set up the interrupt. Comments are required. 8 Exm outpurs FGURE 54 Serial port shift register mode TxD(F3.1) RXD (P3.0) ) CLOCK * Daa Shifi register 15 4. (a) nterrupt service routine NTl makes use of the 8051 registers RO to R7 for local variables. Assume that the main routine also uses the same registers in bank 0. Explain using an example, how this can cause the main program to malfunction. Modify NTl to use register bank 3 instead. NTl:......... 1 (b) Suppose NT performs some CJNE instructions. Expain how this can also cause the main program to malfunction. Modify NTl to prevent these problems.
25 p3 3of YJ- 5. The following program is to be executed on the 8051 controlled train system used in the assignments. A single train starts off in sector 1. Explain in detail what the program does. For full credit you must comment each line of the program, and explain the overall operation of the program and each subroutine. $ XREF ERRORPRNT CONTROL EQU OFBOOH PORTA EQU OF800H PORTB EQU OF900H RESETH EQU OE200H RESETL EQU OEOOOH C EQU ODH CSEG AT 5000H USNG 0 START: SUB4 SETB P1.2 SETB C BT,C SUB2 SETB P1.0 L2: A,#00110000B RO,#16 DPTR,#PORTA L3: A,#11101111B C,Pl.O JC L4 NEWA,A NEWA,#OOOO1111B JMP L5 L4: NC A ORL A,#00110000B DJNZ RO,L3 A,#00110000B RO,#8 L7: DPTR,#PORTA A,#11011111B X @DPTR,A C,Pl.O JC L8 NEWA,A NEWA,#00001111B ORL NEwA.~oao1oooas
JMP L8: NC ORL DJNZ JMP L5: CPL DV ADD ADD JMP L5 A A,#0011000OB RO,L7 SUB3 L2 SUB3 BT C,BT SUB2 A,NEWA B,#lO AB A,#30H SUB1 A,B A,#30H SUB1 A,#C SUB1 L2 SUBl: JNB T,$ CLR T SBUF,A SUBZ: DPTR,#PORTB Pl.l,C R6,#8 A,#00111000B LOOPl: X @DPTR,A A,#11000111B ORL A,#00111000B NC A DJNZ R6,LOOPl SUB3: RO,#lOO L6: Rl,#lOO DJNZ Rl,$ DJNZ R0,L6
sl.24: X DJNZ X X DPTR,#RESETH @DPTR,A R6,#25 R6,$ DPTR,#RESETL @DPTR,A DPTR,#CONTROL A,#80H @DPTR,A BSEG AT 0 BT: DBT 1 DSEG AT 30H NEWA: DS 1 END
MNEMONC DESCRPTON MNEMONC DESCRPTON Arithmetic Operations ADD Asource ADD A,#data ADDC A.source ADDC A,#data SUBB A,source SUBB A,#data NC A NC source DEC A DEC source NC DPTR MUL A6 DV AB DA A Logical Operations ANL Asource ANL A.#data ANL dire&a ANL direct,#data ORL Asource ORL A,#data ORL direct,a ORL direct,#data XRL Asource add source to A add with carry subtract from A with borrow increment decrement increment DPTR multiply A & B divide A by B decimal adjust A logical AND logical OR logical XOR XRL A,#data XRL direct,a XRL direct,#data CLR A clear A CPL A complement A RL A rotate A left RLC A (through C) RR A rotate A right RRC A (through C) SWAP A swap nibbles LEGEND Rn direct @Ri source dest #data #data1 6 bit rel addrli addrl6 register addressing using RO-R7 8-bit internal address (OOH-FFH) indirect addressing using RO or Rl any of [Rn,direct.@ri] any of (Rn,direct,@ri] 8-bit constant included in instr. 16-bit constant 8-bit direct address of bit signed 8-bit offset 11 -bit address in current 2k page 16-bit address FlGURE A - Quick reference chart MNEMONC DESCRPTON MNEMONC DESCRlPTlON Data Transfer Operations Asource A,#data dest.a destsource dest,#data DPTR,#datalG C A,@A + DPTR C A,@A + PC X A,@Ri X A,@DPTR X @Ri,A X @DPTR,A PUSH direct POP direct, XCH Asource XCHD A,@Ri move source to destination move from code memory move from data mewry push onto stack pop from stack exchange bytes exchange low order digits Boolean Variable Manipulation CLR C clear bit CLR bit SETB C set bit SETB bit CPL c - complement bit CPL bit ANL C,bit AND bit with C ANL C./bit AND NOT bit with C ORL ORL JC JNC JB JNB JBC C.bit C./bit C,bit bit,c rel rel bit,rel bit.rel bit,rel Program Branching A addrl 1 L addr16 AJMP addrll LJMP addr16 SJMP rel JMP @A + DPTR JZ rel JNZ rel CJNE A,direct,rel CJNE A,#data.rel CJNE Rn,#data,rel CJNE @Ri.#data,rel DJNZ Rn,rel DJNZ NOP dire&ret OR bit with C OR NOT bit with C move bit to bit jump if C set if C not set jump if bit set if bit not set if set then clear call subroutine return from sub. from interrupt jump jumpifa=o ifanot = 0 compare and jump if not equal decrement and jump if not zero no operation
- -- PT2 TMOD n GATE C/ Ml MO - msb lsb ctfl TCON TR TFO TRO El n-1 EO T T0 T2CON 2 a-2 CP/R SCON c SMO PSW C AC FO RSl RSO OV - P EA -- ET?, Es ET1 Exl Fxo Exo P PS PT pm Pro PXO