FACULTY OF ENGINEERING LAB SHEET MICROCONTROLLER AND MICROPROCESSOR SYSTEMS ECE2216 TRIMESTER 1 (2017/2018) MP2: Construction and programming of a basic electronic piano *Note: On-the-spot evaluation may be carried out during or at the end of the experiment. Students are advised to read through this lab sheet before doing experiment. Your performance, teamwork effort, and learning attitude will count towards the marks.
MP2: Construction and programming of a basic electronic piano Objectives: To demonstrate the GPIO operations of an 8051 based microcontroller. To analyse the timing operations of an 8051 based microcontroller. To develop an MCS-51 assembly program to generate different musical notes. Equipment: Atmel USB Programmer Desktop computer with USB interface USB cable 5V DC Power Supply Unit 2x breadboards Software: Notepad++ with MCS-51 assembler ProgISP (for Atmel USB Programmer) Electronic Components: No Component Quantity 1 Crystal 12MHz 1 2 Micro-tact switch 5 3 1K 9-way resistor network 1 4 100 ohm resistor 1 5 8.2K resistor 1 6 22pF capacitor 2 7 10uF capacitor 1 8 AT89S51 or AT89S52 1 9 2N2222 transistor 1 10 8 0.5W speaker 1 Introduction The 8051 based microcontroller can be programmed to play a musical note by generating a tone of specific frequency and duration. Table 1 shows the frequencies required to generate the tone to play three basic musical notes. These tones can be generated by using the built-in timers of the microcontroller. For example, to generate the musical note C6, Timer 0 can be set to overflow after every 478 s. Each time Timer 0 overflows, it will toggle a GPIO pin (e.g. P0.0). This would generate a tone with a frequency of 1046 Hz, as shown in Figure A1. Table 1. Basic musical notes and their frequency Musical Note Frequency (f) Half-period ( 1 /2f) C6 1046 Hz 478 s D6 1175 Hz 426 s E6 1319 Hz 379 s Multimedia University Page 1 of 12
f = 1046 Hz T = 1 / f = 956 s T / 2 = 478 s Figure 1. Half-period calculation for musical note C6. Hardware Setup 1. Type the program below in Notepad++ and save it as test.asm and assemble it by pressing F6 key and click OK. ORG 0000H CLR P0.0 ; switch off buzzer MOV P1, #0FFH ; configure P1 as input LOOP: MOV A, P1 ; move buttons status to accumulator CPL A ; invert accumulator JZ LOOP ; jump if accumulator is zero CALL TONE ; play a tone SJMP LOOP ; repeat TONE: MOV R0, #250 RPT1: MOV R1, #5 RPT2: MOV R2, #100 DJNZ R2, $ DJNZ R1, RPT2 CPL P0.0 DJNZ R0, RPT1 END 2. Download test.hex into the microcontroller (refer to the Appendix A on how to download the file to the microcontroller). 3. Construct the microcontroller circuit as shown in Figure 2. Multimedia University Page 2 of 12
+5V 100 10 F Micro-tact switches S1 S2 S3 P1.0 (1) P1.1 (2) P1.2 (3) P1.3 (4) P1.4 (5) P1.5 (6) P1.6 (7) P1.7 (8) RST (9) AT89S52 Vcc (40) P0.0 (39) P0.1 (38) P0.2 (37) P0.3 (36) P0.4 (35) P0.5 (34) P0.6 (33) P0.7 (32) 8x1k +5V +5V Pin 1 ( ) + B Gang resistor C E 2N2222 Flat surface E BC Speaker 8.2k +5V 22pF XTAL2 (18) EA (31) 22pF XTAL1(19) Vss (20) Figure 2. Microcontroller circuit connection; the pin numbers are shown in brackets. 4. Switch on the DC power supply unit (PSU) and verify that the microcontroller circuit is working correctly (the buzzer will beep each time any of the buttons connected to P1 is pressed). 5. If the circuit is not functioning as expected, check all connections according to Figure 2. Exercise A Objective: To play basic musical notes by generating tones of different frequencies using the built-in timer. Procedure: 1. Execute Notepad++ from Windows Desktop 2. Click on Language on the pull-down menu and select Assembly_8051. 3. Type the assembly program (case insensitive) in Table A1 below. Table A1. Assembly program for Exercise A ; This program plays 3 musical notes (C6 D6 E6) continuously. DO6 EQU 478 ; half-period for C6 RE6 EQU 426 ; half-period for D6 MI6 EQU 379 ; half-period for E6 LB EQU 30H ; address of RAM to store low byte HB EQU 31H ; address of RAM to store high byte ORG 0000H MOV TMOD, #01H ; configure Timer 0 CLR P0.0 ; switch off buzzer LOOP: MOV LB, #LOW(-DO6) ; load low byte of DO6 Multimedia University Page 3 of 12
MOV HB, #HIGH(-DO6) ; load high byte of DO6 CALL PLAY_NOTE ; call subroutine to play music note MOV A, #1 CALL DELAYS ; call subroutine to generate pause MOV LB, #LOW(-RE6) ; load low byte of RE6 MOV HB, #HIGH(-RE6) ; load high byte of RE6 CALL PLAY_NOTE ; call subroutine to play music note MOV A, #1 CALL DELAYS ; call subroutine to generate pause MOV LB, #LOW(-MI6) ; load low byte of MI6 MOV HB, #HIGH(-MI6) ; load high byte of -MI6 CALL PLAY_NOTE ; call subroutine to play music note MOV A, #1 CALL DELAYS ; call subroutine to generate pause SJMP LOOP ; repeat PLAY_NOTE: MOV R2, #50 ; load the no. of time L1 loop is repeated L1: MOV R1, #25 ; load the no. of time L2 loop is repeated L2: MOV TH0, HB ; load TH0 register from high byte address MOV TL0, LB ; load TL0 register from low byte address SETB TR0 ; start Timer 0 JNB TF0, $ ; wait until Timer 0 overflows CLR TR0 ; stop Timer 0 CLR TF0 ; clear Timer 0 overflow flag CPL P0.0 ; complement P0.0 for tone generation DJNZ R1, L2 ; jump to L2 until R1 is decreased to 0 DJNZ R2, L1 ; jump to L1 until R2 is decreased to 0 CLR P0.0 ; switch off buzzer ; exit subroutine DELAYS: MOV R3, A ; load the no. of time RPT loop is repeated RPT: MOV R2, #250 ; load the no. of time AGN loop is repeated AGN: MOV R1, #250 ; load the no.of time HERE loop is repeated HERE: NOP NOP DJNZ R1, HERE ; jump to HERE until R1 is decreased to 0 DJNZ R2, AGN ; jump is AGN until R2 is decreased to 0 DJNZ R3, RPT ; jump to RPT until R3 is decreased to 0 ; exit subroutine END 4. Save the program as EXA1.asm. 5. Assemble and link the program by pressing F6 key and click OK, or simply press CTRL+F6. 6. If no error is reported, exit Notepad++ (optional). 7. Switch off the DC PSU and carefully unplug the microcontroller from the breadboard and insert it into the programmer. 8. Download the file EXA1.hex from the PC to the microcontroller (refer to Appendix A on how to download the file). 9. Plug the microcontroller back onto the breadboard and switch on the PSU. 10. Observe and verify that the microcontroller circuit is working as expected (playing C6 D6 E6 repeatedly). 11. Complete Table A2 in the Answer Sheet by calculating the half-period for the remaining musical notes. 12. Modify the assembly program in Table A1 above so that it will play 8 musical notes (C6 D6 E6 F6 G6 A6 B6 C7) continuously. Multimedia University Page 4 of 12
Exercise B Objective: To develop an assembly program to playback preset musical notes using lookup table. Procedure: 1. Type the program listed in Table B1 below into Notepad++, save it as EXB1.asm and assemble it by pressing CTRL-F6. Table B1. Assembly program for Exercise B ; Playback preset musical notes using lookup table DO6 EQU 478 ; half-period of C6 RE6 EQU 426 ; half-period of D6 MI6 EQU 379 ; half-period of E6 LB EQU 30H ; address of RAM to store low byte UB EQU 31H ; address of RAM to store high byte ORG 0000H MOV TMOD, #01H ; configure Timer 0 CLR P0.0 ; switch off buzzer MOV DPTR, #MUSIC_TABLE ; load address of lookup table into DPTR LOOP: MOV R0, #0 ; initialize R0 to 1st element in lookup table NEXT: MOV A, R0 ; load R0 to accumulator MOVC A, @A+DPTR ; load current byte pointed by (A+DPTR) to Acc MOV UB, A ; store accumulator to high byte address INC R0 ; increase R0 to point to next byte MOV A, R0 ; load R0 to accumulator MOVC A, @A+DPTR ; load current byte pointed by (A+DPTR) to Acc MOV LB, A ; store accumulator to low byte address CALL PLAY_NOTE ; call subroutine to play music note MOV A, #1 CALL DELAYS ; call subroutine to generate a short pause INC R0 ; increase R0 to point to the next byte CJNE R0, #12, NEXT ; jump to NEXT if R0 is still within range MOV A, #4 CALL DELAYS ; call subroutine to generate a longer pause SJMP LOOP ; repeat PLAY_NOTE: MOV R2, #25 ; load the no. of time L1 loop is repeated L1: MOV R1, #50 ; load the no. of time L2 loop is repeated L2: MOV TH0, UB ; load TH0 register from high byte address MOV TL0, LB ; load TL0 register from low byte address SETB TR0 ; start Timer 0 JNB TF0, $ ; wait until Timer 0 overflows CLR TR0 ; stop Timer 0 CLR TF0 ; clear Timer 0 overflow flag CPL P0.0 ; complement P0.0 for tone generation DJNZ R1, L2 ; jump to L2 until R1 is decreased to 0 DJNZ R2, L1 ; jump to L1 until R2 is decreased to 0 CLR P0.0 ; switch off buzzer ; exit subroutine DELAYS: MOV R3, A ; load the no. of time RPT loop is repeated RPT: MOV R2, #250 ; load the no. of time AGN loop is repeated AGN: MOV R1, #250 ; load the no. of time HERE loop is repeated HERE: NOP Multimedia University Page 5 of 12
NOP DJNZ R1, HERE ; jump to HERE until R1 is decreased to 0 DJNZ R2, AGN ; jump to AGN until R2 is decreased to 0 DJNZ R3, RPT ; jump to RPT until R3 is decreased to 0 ; exit subroutine MUSIC_TABLE: DW -DO6, -RE6, -MI6, -MI6, -RE6, -DO6 ; lookup table with preset music END 2. Switch off the PSU and carefully unplug the microcontroller from the breadboard. 3. Download EXB1.hex from the computer into the microcontroller following the steps performed in previous part. 4. Plug the microcontroller back onto the breadboard and switch on the PSU. 5. Observe and verify that the microcontroller circuit is working as expected (playing C6 D6 E6 and E6 D6 C6 repeatedly). 6. Modify the assembly program in Table B1 so that it will play the musical notes for Happy Birthday as shown in Table B2 below Table B2. Musical notes for Happy Birthday C6, C6, D6, C6, F6, E6, C6, C6, D6, C6, G6, F6, C6, C6, C7, A6, F6, E6, D6, B6, B6, A6, F6, G6, F6 7. Write the new and modified assembly instructions in the space provided in the Answer Sheet. Exercise C Objective: To develop an assembly program to generate different musical notes depending on the key pressed. Procedure 1. Enter the program listed in Table C1 below into Notepad++, save it as EXC1.asm and assemble it by pressing CTRL-F6. Table C1. Assembly program for Exercise C DO6 EQU 478 ; half-period for C6 RE6 EQU 426 ; half-period for D6 MI6 EQU 379 ; half-period for E6 S1: S2: S3: ORG 0000H MOV TMOD, #01H ; configure Timer 0 CLR P0.0 ; switch off buzzer MOV P1, #0FFH ; configure P1 as input JB P1.0, S2 ; jump to check S2 if S1 is not pressed JNB P1.0, $ ; wait until S1 is released CALL PLAY_DO ; call subroutine to play DO6 (C6) JB P1.1, S3 ; jump to check S3 if S1 is not pressed JNB P1.1, $ ; wait until S2 is released CALL PLAY_RE ; call subroutine to play RE6 (D6) JB P1.2, S1 ; jump to check S1 if S3 is not pressed JNB P1.2, $ ; wait until S3 is released CALL PLAY_MI ; call subroutine to play MI6 (E6) SJMP S1 ; jump back to check S1 Multimedia University Page 6 of 12
PLAY_DO: ; subroutine to play DO6 (C6) MOV R1, #50 D1_L1: MOV R0, #21 D1_L2: MOV TH0, #HIGH(-DO6) MOV TL0, #LOW(-DO6) SETB TR0 JNB TF0, $ CLR TR0 CLR TF0 CPL P0.0 DJNZ R0, D1_L2 DJNZ R1, D1_L1 CLR P0.0 PLAY_RE: ; subroutine to play RE6 (D6) MOV R1, #50 RE_L1: MOV R0, #21 RE_L2: MOV TH0, #HIGH(-RE6) MOV TL0, #LOW(-RE6) SETB TR0 JNB TF0, $ CLR TR0 CLR TF0 CPL P0.0 DJNZ R0, RE_L2 DJNZ R1, RE_L1 CLR P0.0 PLAY_MI: ; subroutine to play MI6 (E6) MOV R1, #50 MI_L1: MOV R0, #21 MI_L2: MOV TH0, #HIGH(-MI6) MOV TL0, #LOW(-MI6) SETB TR0 JNB TF0, $ CLR TR0 CLR TF0 CPL P0.0 DJNZ R0, MI_L2 DJNZ R1, MI_L1 CLR P0.0 END 2. Switch off the PSU and carefully unplug the microcontroller from the breadboard. 3. Download EXC1.hex from the computer into the microcontroller following the steps performed in previous part. 4. Plug the microcontroller back onto the breadboard and switch on the PSU. 5. Observe and verify that the microcontroller circuit is working as expected (a musical note is played when one of three buttons connected to P1.0 - P1.2 is pressed). 6. Switch off the DC PSU and connect one more micro-tact switch (S4) to P1.3 (refer to Figure 2 on page 3). 7. Modify the program in Table C1 so that the new switch (connected to P1.3) will play the musical notes F6 when it is pressed. Multimedia University Page 7 of 12
8. Write the new and modified assembly instructions in the space provided in the Answer Sheet. Important Note: Lab Assessment will be carried out during the lab session. It is the student's responsibility to complete the given tasks and report to the lab supervisor for assessment. Appendix A Using Atmel USB Programmer 1. Place the microcontroller into the socket. (End of experiment) 2. Launch the programmer software ProgISP. 3. Click the Select Chip drop-down list to select the correct chip. Multimedia University Page 8 of 12
4. Click RD to read the chip ID. 5. Make sure that only 4 items are checked (circled in red). 6. Click File and select Load Flash. Multimedia University Page 9 of 12
7. Select the Hex file to download. 8. Click Auto to erase, black check, program and verify the Hex file. 9. A text should be printed to confirm that the Hex file has been successfully downloaded. Multimedia University Page 10 of 12
Appendix B Breadboard connection for the microcontroller, resistor network and micro-tact switches. Appendix C The 8051 has two on-chip 16-bit up timers/ counters: Timer 0 & Timer 1. Initial value can be programmed F F F 6 F F F 7 F F F 8 1 F F F F F F F E Timer overlfow flag 10000H - FFF6H = AH = 10 10 If the timer is incremented every 1 s, it will overflow in every 10 s TH0 F F F D TL0 D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 TH1 TL1 D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 Multimedia University Page 11 of 12
TMOD 7 6 5 4 3 2 1 0 GATE C/T M1 M0 GATE C/T M1 M0 GATE C/T Timer 1 Timer 0 Gating control; when set, timer/counter is enabled only while the INTx pin is high and the TRx control bit is set; when cleared, the timer is enabled whenever the TRx control bit is set Timer or counter selection; cleared for timer operation; set for counter operation M1 Mode select bit 1 M0 Mode select bit 0 M1 M0 Mode Operating Mode 0 0 0 13-bit timer mode 8-bit timer/counter; THx with TLx as 5-bit prescaler 0 1 1 16-bit timer mode 16-bit timer/counter; THx and TLx are cascaded; no prescaler 1 0 2 8-bit auto reload 8-bit auto reload timer/counter; THx holds a value to be reloaded into TLx each time it overflows 1 1 3 Split timer mode TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 TCON 7 6 5 4 3 2 1 0 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 bit addressable Timer 1 overflow flag; set by hardware when the timer/counter 1 overflow; cleared by software or hardware as processor vectors to interrupt service subroutine Timer 1 run control bit; set/cleared by software to turn timer/counter 1 ON/OFF Timer 0 overflow flag Timer 0 run control bit External interrupt 1 edge flag; set by hardware when external interrupt edge is detected; cleared by hardware when interrupt is processed Interrupt 1 type control bit; set/cleared by software to specify falling-edge/lowlevel triggered external interrupt External interrupt 0 edge flag Interrupt 0 type control bit (End) Multimedia University Page 12 of 12