6.1 Setup, Main Loop and Idle Subroutine

When the program is started, the setup of the memory and internal peripherals is initiated. This involves the setup of the following peripherals:

  • ADCC for sampling of the microphone
  • DAC for output to the speaker
  • SPI for interfacing external memory
  • PORT settings for button interfacing
  • TMR2 and CLC for button debouncing
  • TMR0 for correct sampling and playback frequency
  • DMA for data transfer between peripherals and memory
  • PWM and Comparator for power amplifier
  • TMR6 as time base for LED blinking
  • TMR1 as time base for button press delays
  • UART for debugging

In addition to the peripherals, external memory is also initialized. If the external memory can not be identified, then the program defaults to using the internal program memory as storage.

This is followed by loading the appropriate audio metadata from the EEPROM. The audio metadata contains the memory location where existing audio is stored and the size of the audio. If no audio is stored, the audio size is zero bytes.

The setup and main loop functionality is shown in Figure 6-1.

In the PIC18F57Q43 Curiosity Nano, there is only one button available (SW0 button). In this case, a single button press enters Playback mode, a double press enters Record mode, and a long-press erases the memory. There is a special button (SW1 button) available on the custom-made PROTO Click Board (see Figure 7-6), along with the power amplifier. A long press of this button enters into Passthrough mode. This functionality is depicted in Figure 6-2.
Figure 6-1. Setup and Main Loop
Figure 6-2. Idle Subroutine