lab 5 final

This commit is contained in:
2022-01-31 01:11:41 -06:00
parent f409bfa319
commit 8817dae579
3 changed files with 204 additions and 296 deletions

View File

@@ -1,9 +1,11 @@
/**
* @file piezoSpeaker.h
* @author Trevor Barnes
* @brief
* @brief Header file for the piezo speaker's C file that provides all of the necessary timer, RCC,
* and GPIOB values for working with the respective components. Note frequency values and durations
* are also defined.
* @version 0.1
* @date 2022-01-19
* @date 2022-01-12
*
* @copyright Copyright (c) 2022
*
@@ -48,226 +50,135 @@ typedef struct{
unsigned int noteDuration;
} Note;
// Initializes the piezo speaker to be used with timer 3
void piezo_init();
// Plays a given note at a certain frequency for a certain duration
void play_note(Note noteToPlay);
// void play_note(double playFrequency, double playDuration);
// Iterates through an array of note structs and ends at the termination value "END"
void play_song(Note *songToPlay);
// Note Frequency Symbols
// Rest technically = 0
//#define r 0
//#define C0 16.35
//#define Db0 17.32
//#define D0 18.35
//#define Eb0 19.45
//#define E0 20.60
//#define F0 21.83
//#define Gb0 23.12
//#define G0 24.50
//#define Ab0 25.96
//#define A0 27.50
//#define Bb0 29.14
//#define B0 30.87
//#define C1 32.70
//#define Db1 34.65
//#define D1 36.71
//#define Eb1 38.89
//#define E1 41.20
//#define F1 43.65
//#define Gb1 46.25
//#define G1 49.00
//#define Ab1 51.91
//#define A1 55.00
//#define Bb1 58.27
//#define B1 61.74
//#define C2 65.41
//#define Db2 69.30
//#define D2 73.42
//#define Eb2 77.78
//#define E2 82.41
//#define F2 87.31
//#define Gb2 92.50
//#define G2 98.00
//#define Ab2 103.83
//#define A2 110.00
//#define Bb2 116.54
//#define B2 123.47
//#define C3 130.81
//#define Db3 138.59
//#define D3 146.83
//#define Eb3 155.56
//#define E3 164.81
//#define F3 174.61
//#define Gb3 185.00
//#define G3 196.00
//#define Ab3 207.65
//#define A3 220.00
//#define Bb3 233.08
//#define B3 246.94
//#define C4 261.63
//#define Db4 277.18
//#define D4 293.66
//#define Eb4 311.13
//#define E4 329.63
//#define F4 349.23
//#define Gb4 369.99
//#define G4 392.00
//#define Ab4 415.30
//#define A4 440.00
//#define Bb4 466.16
//#define B4 493.88
//#define C5 523.25
//#define Db5 554.37
//#define D5 587.33
//#define Eb5 622.25
//#define E5 659.26
//#define F5 698.46
//#define Gb5 739.99
//#define G5 783.99
//#define Ab5 830.61
//#define A5 880.00
//#define Bb5 932.33
//#define B5 987.77
//#define C6 1046.50
//#define Db6 1108.73
//#define D6 1174.66
//#define Eb6 1244.51
//#define E6 1318.51
//#define F6 1396.91
//#define Gb6 1479.98
//#define G6 1567.98
//#define Ab6 1661.22
//#define A6 1760.00
//#define Bb6 1864.66
//#define B6 1975.53
//#define C7 2093.00
//#define Db7 2217.46
//#define D7 2349.32
//#define Eb7 2489.02
//#define E7 2637.02
//#define F7 2793.83
//#define Gb7 2959.96
//#define G7 3135.96
//#define Ab7 3322.44
//#define A7 3520.01
//#define Bb7 3729.31
//#define B7 3951.07
//#define C8 4186.01
//#define Db8 4434.92
//#define D8 4698.64
//#define Eb8 4978.03
// Frequency Value Signifying a rest
#define r 1
// Note Frequency Symbols (Rounded to nearest integer value)
#define C0 16
#define Db0 17
#define D0 18
#define Eb0 19
#define E0 21
#define F0 22
#define Gb0 23
#define G0 25
#define Ab0 26
#define A0 28
#define Bb0 29
#define B0 31
#define C1 33
#define Db1 35
#define D1 37
#define Eb1 39
#define E1 41
#define F1 44
#define Gb1 46
#define G1 49
#define Ab1 52
#define A1 55
#define Bb1 58
#define B1 62
#define C2 65
#define Db2 69
#define D2 73
#define Eb2 78
#define E2 82
#define F2 87
#define Gb2 93
#define G2 98
#define Ab2 104
#define A2 110
#define Bb2 117
#define B2 123
#define C3 131
#define Db3 139
#define D3 147
#define Eb3 156
#define E3 165
#define F3 175
#define Gb3 185
#define G3 196
#define Ab3 208
#define A3 220
#define Bb3 233
#define B3 247
#define C4 262
#define Db4 277
#define D4 294
#define Eb4 311
#define E4 330
#define F4 349
#define Gb4 370
#define G4 392
#define Ab4 415
#define A4 440
#define Bb4 466
#define B4 494
#define C5 523
#define Db5 554
#define D5 587
#define Eb5 622
#define E5 659
#define F5 698
#define Gb5 740
#define G5 784
#define Ab5 831
#define A5 880
#define Bb5 932
#define B5 988
#define C6 1047
#define Db6 1109
#define D6 1175
#define Eb6 1245
#define E6 1319
#define F6 1397
#define Gb6 1480
#define G6 1568
#define Ab6 1661
#define A6 1760
#define Bb6 1865
#define B6 1976
#define C7 2093
#define Db7 2217
#define D7 2349
#define Eb7 2489
#define E7 2637
#define F7 2794
#define Gb7 2960
#define G7 3136
#define Ab7 3322
#define A7 3520
#define Bb7 3730
#define B7 3951
#define C8 4186
#define Db8 4435
#define D8 4699
#define Eb8 4978
// Value that indicates the end of a song
#define END 0
#define r 0
#define C0 1635
#define Db0 1732
#define D0 1835
#define Eb0 1945
#define E0 2060
#define F0 2183
#define Gb0 2312
#define G0 2450
#define Ab0 2596
#define A0 2750
#define Bb0 2914
#define B0 3087
#define C1 3270
#define Db1 3465
#define D1 3671
#define Eb1 3889
#define E1 4120
#define F1 4365
#define Gb1 4625
#define G1 4900
#define Ab1 5191
#define A1 5500
#define Bb1 5827
#define B1 6174
#define C2 6541
#define Db2 6930
#define D2 7342
#define Eb2 7778
#define E2 8241
#define F2 8731
#define Gb2 9250
#define G2 9800
#define Ab2 10383
#define A2 11000
#define Bb2 11654
#define B2 12347
#define C3 13081
#define Db3 13859
#define D3 14683
#define Eb3 15556
#define E3 16481
#define F3 17461
#define Gb3 18500
#define G3 19600
#define Ab3 20765
#define A3 22000
#define Bb3 23308
#define B3 24694
#define C4 26163
#define Db4 27718
#define D4 29366
#define Eb4 31113
#define E4 32963
#define F4 34923
#define Gb4 36999
#define G4 39200
#define Ab4 41530
#define A4 44000
#define Bb4 46616
#define B4 49388
#define C5 52325
#define Db5 55437
#define D5 58733
#define Eb5 62225
#define E5 65926
#define F5 69846
#define Gb5 73999
#define G5 78399
#define Ab5 83061
#define A5 88000
#define Bb5 93233
#define B5 98777
#define C6 104650
#define Db6 110873
#define D6 117466
#define Eb6 124451
#define E6 131851
#define F6 139691
#define Gb6 147998
#define G6 156798
#define Ab6 166122
#define A6 176000
#define Bb6 186466
#define B6 197553
#define C7 209300
#define Db7 221746
#define D7 234932
#define Eb7 248902
#define E7 263702
#define F7 279383
#define Gb7 295996
#define G7 313596
#define Ab7 332244
#define A7 352001
#define Bb7 372931
#define B7 395107
#define C8 418601
#define Db8 443492
#define D8 469864
#define Eb8 497803
// Ending "termination" value indicating end of a song
#define T 5000
// Tempo value set at 120 beats per min
#define BPM 120
#define H 2*Q
// Quarter Note
#define Q 60000/BPM
#define E Q/2
#define S Q/4
// Half Note
#define H 2*Q
// Whole Note
#define W 4*Q
// Eighth Note
#define E Q/2
// Sixteenth Note
#define S Q/4
#endif

View File

@@ -1,8 +1,18 @@
/*
* main.c
/**
* @file main.c
* @author Trevor Barnes
* @brief Main Driver for the Week 5 "Play a Tune" Lab. This program uses a similar menu style to the previous
* lab. The user is given a new option to play 3 different hard coded songs in arrays of "Note" structs,
* provided by the piezoSpeaker.h file.
* Experience: As a former music student I enjoyed working on this lab very much, to the point that I became
* sort of a perfectionist with its implementation. Getting the timer configured and working with the piezo speaker
* was certainly the most difficult and time consuming part. Once I had that figured out though figuring
* out the best note implementation and songs to play was very enjoyable.
* @version 0.1
* @date 2022-01-12
*
* @copyright Copyright (c) 2022
*
* Created on: January 12, 2022
* Author: Trevor Barnes
*/
#include <stdio.h>
@@ -17,51 +27,43 @@
#define F_CPU 16000000UL
// Super Mario "Flagpole Fanfare"
Note songSMFF[66]= { {G3, S/3}, {Ab3,S/3}, {A3, S/3}, {Bb3,S/3}, {B3,S/3},
{C4, S/3}, {Db4,S/3}, {D4, S/3}, {Eb4,S/3}, {E4,S/3},
{F4, S/3}, {Gb4,S/3}, {G4, S/3}, {Ab4,S/3}, {A4,S/3},
{Bb4,S/3}, {B4, S/3}, {C5, S/3}, {Db5,S/3}, {D5,S/3},
{Eb5,S/3}, {E5, S/3}, {F5, S/3}, {Gb5,S/3}, {G5,S/3},
{Ab5,S/3}, {A5, S/3}, {Bb5,S/3}, {B5, S/3}, {C6,S/3},
{Db6,S/3}, {D6, S/3}, {Eb6,S/3}, {E6, S/3}, {F6,S/3},
{Eb6,S/3}, {G6, S/3}, {r, 7*S}, {G3, Q}, {C4, Q},
{E4, Q}, {G4, Q}, {C5, Q}, {E5, Q}, {G5, H},
{E5, H}, {Ab3, Q}, {C4, Q}, {Eb4, Q}, {Ab4, Q},
{C5, Q}, {Eb5, Q}, {Ab6, H}, {Eb5, H}, {Bb3, Q},
{D4, Q}, {F4, Q}, {Bb4, Q}, {D5, Q}, {F5, Q},
{Bb5, W}, {B5, Q}, {B5, Q}, {B5, Q}, {C6, W}, {T}
};
// Imperial March
Note songIMold[138] = { {A3, Q}, {A3, Q}, {A3, Q}, {r, Q},
{F3,E+S}, {r, E+S}, {C4, S}, {r, S}, {A3, Q}, {r, Q},
{F3,E+S}, {r, E+S}, {C4, S}, {r, S}, {A3, H}, {r, H},
{E4, Q}, {r, Q}, {E4, Q}, {r, Q}, {E4, Q}, {r, Q},
{F4,E+S}, {r, E+S}, {C4, S}, {r, S}, {Ab3, Q}, {r, Q},
{F3,E+S}, {r, E+S}, {C4, S}, {r, S}, {A3, H}, {r, H},
{A4, Q}, {r, Q}, {A3, E+S}, {r, E+S}, {A3, S}, {r, S},
{A4, Q}, {r, Q}, {Ab4,E+S}, {r, E+S}, {G4, S}, {r, S},
{Gb4, Q}, {r, S}, {E4, S}, {r, S}, {F4, E}, {r, E},
{r, E}, {Bb3, E}, {r, E}, {Eb4, Q}, {r, Q}, {D4,E+S},
{r, E+S}, {Db4, S}, {r, H}, {C4, S}, {r, S}, {B3, S},
{r, S}, {C4, E}, {r, E}, {r, E}, {F3, E}, {r, E},
{Ab3, Q}, {r, Q}, {F3, E+S}, {r, E+S}, {A3, S}, {r, S},
{C4, Q}, {r, Q}, {A3, E+S}, {r, E+S}, {C4, S}, {r, S},
{E4, H}, {r, H}, {A4, Q}, {r, Q}, {A3, E+S}, {r, E+S},
{A3, S}, {r, S}, {A4, Q}, {r, S}, {Ab4,E+S}, {r, E+S},
{G4, S}, {r, S}, {Gb4, S}, {r, S}, {E4, S}, {r, S},
{F4, E}, {r, E}, {r, E}, {Bb3, E}, {r, E}, {Eb4, Q},
{r, Q}, {D4,E+S}, {r, E+S}, {Db4, S}, {r, S}, {C4, S},
{r, S}, {B3, S}, {r, S}, {C4, E}, {r, E}, {r, E},
{F3, E}, {r, E}, {Ab3, Q}, {r, Q}, {F3, E+S}, {r, E+S},
{C4, S}, {r, S}, {A3, Q}, {r, Q}, {F3, E+S}, {r, E+S},
{C4, S}, {r, S}, {A3, H}, {r, H}, {T, Q}
};
// Imperial March - Star Wars
Note songIM[138] = {
{A3, Q}, {A3, Q}, {A3, Q}, {F3, S*3}, {C4, S}, {A3, Q}, {F3, S*3}, {C4, S}, {A3, H},
{E4, Q}, {E4, Q}, {E4, Q}, {F4, S*3}, {C4, S}, {Ab3, Q}, {F3, S*3}, {C4, S}, {A3, H}, {T}
{A4, Q}, {A4, Q}, {A4, Q}, {F4, S*3}, {C5, S}, {A4, Q}, {F4, S*3}, {C5, S}, {A4, H},
{E5, Q}, {E5, Q}, {E5, Q}, {F5, S*3}, {C5, S}, {Ab4, Q}, {F4, S*3}, {C5, S}, {A4, H},
{A5, Q}, {A4, S*3}, {A4, S}, {A5, Q}, {Ab5, S*3}, {G5, S},
{Gb5, S}, {F5, S}, {Gb5, E}, {r, E}, {Bb4, E}, {Eb5, Q}, {D5, S*3}, {Db5, S},
{C5, S}, {B4, S}, {C5, E}, {r, E}, {F4, E}, {Ab4, Q}, {F4, S*3}, {A4, S},
{C5, Q}, {A4, S*3}, {C5, S}, {E5, H},
{A5, Q}, {A4, S*3}, {A4, S}, {A5, Q}, {Ab5, S*3}, {G5, S},
{Gb5, S}, {F5, S}, {Gb5, E}, {r, E}, {Bb4, E}, {Eb5, Q}, {D5, S*3}, {Db5, S},
{C5, S}, {B4, S}, {C5, E}, {r, E}, {F4, E}, {Ab4, Q}, {F4, S*3}, {C5, S},
{A4, Q}, {F4, S*3}, {C5, S}, {A4, H},
{END}
};
// Metropolis Theme - Ratchet & Clank
Note songMT[33] = {
{B5, E}, {G5, E}, {E5, E}, {G5, E}, {B5, E}, {G5, E}, {E5, E}, {B5, E},
{Bb5, E}, {F5, E}, {D5, E}, {F5, E}, {Bb5, E}, {F5, E}, {D5, E}, {Bb5, E},
{B5, E}, {G5, E}, {E5, E}, {G5, E}, {B5, E}, {G5, E}, {E5, E}, {B5, E},
{Bb5, E}, {F5, E}, {D5, E}, {F5, E}, {Bb5, E}, {F5, E}, {D5, E}, {Bb5, E},
{END}
};
// Flower Garden - Yoshi's Island
Note songFG[77] = {
{E4, E}, {r, E}, {G4, E}, {r, S}, {G4, S}, {E4, E}, {C4, E}, {r, Q},
{A3, E}, {r, E}, {C4, E}, {r, S}, {A3, S}, {D4, E}, {E4, E}, {r, Q},
{E4, E}, {r, E}, {G4, E}, {r, S}, {G4, S}, {E4, E}, {C4, E}, {r, Q},
{A3, E}, {r, E}, {C4, E}, {r, S}, {A3, S}, {E4, E}, {D4, E}, {r, Q},
{G5, S}, {Gb5, S}, {G5, E+(Q*3)},
{r , E}, {F5, E}, {E5, E}, {F5, E}, {E5, E}, {C5, E}, {A4, E}, {G4, E+(Q*5)}, {r, E},
{C5, E}, {B4, E}, {D5, E}, {A5, E}, {G5, E+H+Q}, {r, E},
{A5, E}, {B5, E}, {A5, E}, {G5, E}, {F5, E}, {E5, E}, {D5, E}, {E5, Q}, {C5, E}, {G4, E+(Q*3)}, {r, E},
{C5, E}, {B4, E}, {C5, E}, {D5, E}, {E5, E+Q}, {G5, Q}, {C5, Q}, {E5, Q},
{F5, E}, {E5, E}, {F5, E}, {D5, E*2}, {C5, E}, {B4, E}, {C5, E+W},
{END}
};
void printHelp() {
@@ -75,8 +77,9 @@ void printHelp() {
}
void songInfo() {
printf("Type 1 or 2 to play a song!\n\r");
printf("Song 1: Imperial March\n\r");
printf("Song 2: Super Mario Bros Flagpole Fanfare\n\r");
printf("Song 1: Imperial March from Star Wars\n\r");
printf("Song 2: Metropolis Theme from Ratchet & Clank\n\r");
printf("Song 3: Flower Garden from Yoshi's Island");
}
int main(void) {
@@ -115,8 +118,12 @@ int main(void) {
play_song(songIM);
break;
case 2:
printf("Playing Super Mario Bros Flagpole Fanfare\n\r");
play_song(songSMFF);
printf("Playing Metropolis Theme\n\r");
play_song(songMT);
break;
case 3:
printf("Playing Flower Garden\n\r");
play_song(songFG);
break;
default:
break;

View File

@@ -1,9 +1,9 @@
/**
* @file piezoSpeaker.c
* @author Trevor Barnes
* @brief
* @brief Provides funtionality for initializing, playing notes, and playing songs on the piezo speaker.
* @version 0.1
* @date 2022-01-19
* @date 2022-01-12
*
* @copyright Copyright (c) 2022
*
@@ -16,62 +16,52 @@
void piezo_init(){
//enable GPIOB and Timer 3 RCC
// GPIOB/Timer3 enable in RCC
*RCC_AHB1ENR |= (1<<GPIOBEN);
*RCC_APB1ENR |= (1<<TIM3_EN);
//set GPIO B to alternate function (0b10<<9)
//clears the two bits and then set it
// Set to "alternate function" mode
*GPIOB_MODER = (*GPIOB_MODER&~(0b11<<8)) | (PB4_AF_V<<8);
//set alternate function low register to TIM3
// Set AF to low
*GPIOB_AFRL |= (1<<AFRL_TIM3_CH1_EN);
//Configure capture/compare mode register configuration
//to enable preload and set to pwm
// Set to output capture
*TIM3_CCMR1 |= OC1M_PWM2;
*TIM3_CCMR1 |= (1<<OC1PE);
//Configure CCER to enable timer 3 as output capture
*TIM3_CCER |= CCER_CC1E;
//Configure control register to enable preload
// Enable Preload
*TIM3_CR1 |= (1<<CR_ARPE_EN);
}
void play_note(Note noteToPlay) {
//void play_note(double playFrequency, double playDuration) {
*TIM3_PSC = 15;
//Divisor controls pitch
*TIM3_ARR = (pitchDivisor)/(noteToPlay.noteFrequency/100);
// Pitch divisor to scale with timer
*TIM3_ARR = (pitchDivisor)/(noteToPlay.noteFrequency);
//Loudness (Smaller dividend = louder sound)
double freq = (noteToPlay.noteFrequency/100)/10;
// Volume (Smaller dividend = louder sound)
unsigned int freq = (noteToPlay.noteFrequency/10);
//clear ccr1
// Clear CCR
*TIM3_CCR1 = (*TIM3_CCR1&~(0xFFFF));
*TIM3_CCR1 = freq;
//set EGR (accept only a byte of info so steps)
// Set EGR
*TIM3_EGR |= EGR_UG;
//~~~Plays the notes
//Enables enable bit control register
// Playing note
// Enables timer
*TIM3_CR1 |= 1;
//delay that leaves the speaker on for desired amount of time
// Delay for duration of note
delay_1ms(noteToPlay.noteDuration);
//Disables enable bit
// Disables timer
*TIM3_CR1 &= ~1;
}
void play_song(Note *songToPlay){
int i = 0;
// double freq = songToPlay[i].noteFrequency;
// Note newNote = {songToPlay[i].noteFrequency, songToPlay[i].noteDuration };
while(songToPlay[i].noteFrequency != T) {
// Iterate through song and play each note till "END" value
while(songToPlay[i].noteFrequency != END) {
play_note(songToPlay[i]);
i++;
}