ClearCore Library
Loading...
Searching...
No Matches
PlayTone.cpp

Return to SDK Examples for Microchip Studio

1/*
2 * Title: PlayTone
3 *
4 * Objective:
5 * This example demonstrates how to play a sequence of tones on the
6 * ClearCore's H-Bridge connectors using the musical frequency values defined
7 * in the pitches.h header file.
8 *
9 * Description:
10 * This example sets up an H-Bridge connector into output mode, sets the
11 * volume, and plays a melodic sequence of tones of equal duration.
12 *
13 * Requirements:
14 * ** A speaker or other audio output device connected to IO-4
15 *
16 * Links:
17 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
18 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
19 *
20 *
21 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
22 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
23 */
24
25#include "ClearCore.h"
26#include "pitches.h"
27
28// Notes to be played in sequence as part of a melody.
29// The maximum tone frequency is 1/4 of the tone interrupt rate, i.e. 5512 Hz.
30// Any commanded frequency above 5512 Hz will get clipped to 5512 Hz.
31// See the "pitches.h" file for the frequency definitions of these notes.
32const uint16_t melody[] = {NOTE_C4, NOTE_E4, NOTE_G4, NOTE_C5,
33 NOTE_CS4, NOTE_FS4, NOTE_AS4, NOTE_CS5,
34 NOTE_E4, NOTE_G4, NOTE_C5, NOTE_E5,
35 NOTE_FS4, NOTE_AS4, NOTE_CS5, NOTE_FS5
36 };
37const uint32_t noteCount = sizeof(melody) / sizeof(melody[0]);
38
39const uint32_t toneDuration = 200; // in milliseconds
40const int16_t toneAmplitude = INT16_MAX / 100; // max volume is INT16_MAX
41
42// Tone output is supported on connectors IO-4 and IO-5 only.
43#define tonePin ConnectorIO4
44
45int main() {
46 // Set the tone connector into output mode.
47 tonePin.Mode(Connector::OUTPUT_TONE);
48
49 // Set the volume of the tone connector to the value specified
50 // by toneAmplitude.
51 tonePin.ToneAmplitude(toneAmplitude);
52
53 while (true) {
54 // Play the melody in order with equal note durations.
55 for (uint8_t note = 0; note < noteCount; note++) {
56 tonePin.ToneContinuous(melody[note]);
57 Delay_ms(toneDuration);
58 }
59
60 // Stop the tone generation.
61 tonePin.ToneStop();
62
63 // Wait a second, then repeat...
64 Delay_ms(1000);
65 }
66}
void Delay_ms(uint32_t ms)
Blocks operations for ms milliseconds.
Definition SysTiming.h:287