Lab 2 – GPIO and Interrupt C Programming for virtual Piano using Keil simulator
For this lab exercise you will Keil simulator (based on Nucleo-F103RB board). The objective is to understand how the GPIO device connects with the microprocessor and how to manipulate GPIO pins to serve our purpose.
Useful sources (QMplus)
GPIOonKeil project
Introduction
In this exercise, you will create a program that uses GPIO pins to control piano keys to play a music tune from the Sound of Music. This will be done on Keil simulator and it will not be possible to connect the GPIO pins to an actual virtual piano. Here is what you are asked to do:
Read a digit value between 1 and 8 which will be input in binary form using B3, B2, B1 pins where B3 is the most significant and B1 is the least significant bit.
Based on the number read, play one of the lines in Table 1.
Assign a GPIO pin for each of the highlighted eight piano keys in Figure 1.
Assign another GPIO pin to switch from one key to another. This is connected to a push button PB.
Figure 1- 8 Piano keys: C D E F G A B ^C (https://noobnotes.net/do-re-mi-sound-of-music/)
Table 1 - Lines from Sound of Music’s Do-Re-Mi song
Input digit Piano keys Lyrics
1 C D E C E C E Doe, a deer, a female deer
2 D E F F E D F Ray, a drop of golden sun
3 E F G E G E G Me, a name I call myself
4 F G A A G F A Far, a long, long way to run…
5 G C D E F G A Sew, a needle pulling thread
6 A D E F G A B La, a note to follow so
7 B E F G A B ^C Tea, a drink with jam and bread
8 ^C B A F B G ^C G E D That will bring us back to do oh oh oh…
Software Flow
You are required to write a C program to:
Initialise GPIO peripherals
Initialise global variables
Set interrupt trigger of GPIO pin PB that it connected to the push button as rising edge.
Set GPIO PB callback function to an PB_ISR (status) that you will write.
Repeat the following infinitely:
oRead the digit from pins B3 B2 B1 and store in variable LINE.
oCall the function PlayNote(LINE) that you will write. Upon the rising edge of PB, play the next key in the line LINE. E.g., if B3 B2 B1 = 011 then LINE =3. If it is the first rising edge of PB, you should activate the GPIO that is connected to key E. At the second rising edge of PB the GPIO connected to key F should be activated, etc.
Task 0
Initialise the GPIO peripherals:
-3 pins configured as input bits B3, B2, and B1
-1 pin configured as input connected to push button PB
-8 pins configured as outputs Define global variables:
-LINE
-Interrupt
Use gpio_get_range inbuilt function to read the value of LINE and call
Task 1
Write the interrupt service routine function PB_ISR(int status) that will set the global variable Interrupt to 0 every time it is called.
Task 2
Write the function PlayNote(LINE) that plays the keys corresponding to LINE based on Table 1. The function should wait for Interrupt to be activate before playing any key and should terminate once all keys in LINE are played.
Testing Strategy
In order to test the key-playing function, try to step your main program during debugging. You can inspect the values of some of your variables in the debugger and check that the output GPIO pins are correctly activated.
Set a breakpoint in your program after the push button has been released and examine the variables using the watch window.
Submission
Please be reminded that this lab is an individual work.
1.You are asked to test your code based on (1) the input digit that corresponds to the last digit of you BUPT ID number and (2) the last digit of your QMUL ID number (if your ID ends with 0 or 9 use 5 instead).
2.Prepare a 5-minute video to explain your code in details and show the execution of the function within the Keil simulation. This video should be submitted on QMPlus (60%). Please begin with a clear camera view of yourself, introducing your name and QMUL student ID.
3.Submit your final source code main.c (40%) to QMPlus when you have completed. Your source code will be checked for plagiarism.
- End of Lab 2 -