Musical Keyboard : STM32 UART Receiver

Musical Keyboard

Let's configure the STM32F401 Nucleo-64 Development Board to work as a UART Receiver and emulate a Musical Keyboard by using our own libraries and drivers. We will use the STM32F401's 
  • GPIOA Port
  • USART2 Peripheral
  • SysTick Timer



The basic functionality is to Receive a character from the PC Keyboard and play the sound note mapped to the keystroke in the Keyboard.h file. I have written separate Modules for each peripherals, that is,
  • GPIO.h 
    To initialise and Toggle the GPIO Pin Output
  • UART.h
    To initialise the USART2 Peripheral, Receive the Characters from PC and to Print info on the Serial console 
  • Delay.h
    To produce Micro-second and Milli-second specific delays
  • Keyboard.h
    Mapping the Music Note periods and the function definition for playing a specific Musical Note

GPIO Module


The GPIO Module has two functions. The GPIO_Init() function enables the clock access to AHB1 Bus, Configures the GPIO Port A Pin 5 to Output Mode. GPIO_Toggle() function toggles the Output Data Register bit of PA5.

UART Module



The UART Module has four functions. UART_Init() functions enables clock access to UART Peripheral and GPIOA Port and then configures PA2 and PA3 as USART1_Tx and USART2_Rx by setting the Alternate Function Registers. UART_Tx() function transmits a character to the VCOM Port and UART_Tx_String() function transmits a string of characters. UART_Rx() function  receives a character from the computer.

Delay Module



The Delay module has two functions, named delay_us() and delay_ms(). As the name suggests, it produces a delay of user required delay in Microsecond and Millisecond precision. The SysTick timer is used to produce these delays.

Keyboard Module


In the Keyboard.h file, I have defined the period duration for different Musical Notes that I may have to use. We will not be using the full list of Notes in this project.


The Keyboard module has two functions. The playNote() function plays the Music Note corresponding to the Keystroke. It accepts two arguments, Period of the Music Note and Note Duration Length. The rest() function does nothing except created a delay for the specified time when an unspecified key is pressed.

The main.c file

Note Duration is defined as 0xF000, that is, 61440 in Decimal. The character variable 'rx_char' is used to store the received character from computer. Two arrays, keys[] and notes[] are initialized with the Computer Keys and the Music Notes respectively. Then the UART and GPIO Modules are initialized.


Inside the while(1) loop, initially we receive the character from the computer and is checked with the keys[] array. If a match is found, the loop is broken and the corresponding Music Note is played.



If a key other than those specified in the array keys[] is pressed, it is dealt accordingly. That is, the keys '[' and ']' are pressed to alter the tempo of the notes that the instrument is playing.







Popular posts from this blog

Capturing images using the Digital Camera Interface | STM32L4 | DCMI | CMSIS

Cifradopro: A baremetal Hardware Security Module using the STM32L4S5 Cortex-M4 MCU

SignGlove: Bridging the Communication Gap for Paralyzed Patients