lab 6 setup
This commit is contained in:
@@ -1,8 +1,18 @@
|
||||
/*
|
||||
* main.c
|
||||
*
|
||||
* Created on: January 12, 2022
|
||||
* Author: Trevor Barnes
|
||||
/**
|
||||
* @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-19
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -17,72 +27,71 @@
|
||||
|
||||
#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, Q} };
|
||||
// Imperial March - Star Wars
|
||||
Note songIM[138] = {
|
||||
{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}
|
||||
};
|
||||
|
||||
// Imperial March
|
||||
Note songIM[138] = { {A3, Q}, {r, Q}, {A3, Q}, {r, 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} };
|
||||
// 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() {
|
||||
printf("*Commands*\n\r");
|
||||
printf("'rmw {hex address}' - Reads mem at a given address\n\r");
|
||||
printf("'wmw {hex address} {value}' - Writes the given value as a word to the given address\n\r");
|
||||
printf("'dm {hex address} {length}' - Dumps the memory at a given address. Defaults to 16 B if no "
|
||||
"length is given\n\r");
|
||||
printf("'ps {song choice}' - Plays a song with the given selection\n\r");
|
||||
printf("'songs' - Prints info about each song selection\n\r");
|
||||
printf("*Commands*\n\r");
|
||||
printf("'rmw {hex address}' - Reads mem at a given address\n\r");
|
||||
printf("'wmw {hex address} {value}' - Writes the given value as a word to the given address\n\r");
|
||||
printf("'dm {hex address} {length}' - Dumps the memory at a given address. Defaults to 16 B if no "
|
||||
"length is given\n\r");
|
||||
printf("'ps {song choice}' - Plays a song with the given selection\n\r");
|
||||
printf("'songs' - Prints info about each song selection\n\r");
|
||||
}
|
||||
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("Type 1 or 2 to play a song!\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) {
|
||||
init_usart2(57600, F_CPU);
|
||||
piezo_init();
|
||||
led_init();
|
||||
init_usart2(57600, F_CPU);
|
||||
piezo_init();
|
||||
led_init();
|
||||
char line[50];
|
||||
char command[10];
|
||||
int address;
|
||||
int data;
|
||||
int length;
|
||||
int songSelection;
|
||||
// play_note(261.63, 10000.00);
|
||||
for(;;) {
|
||||
// Get command from user
|
||||
fgets(line, 100, stdin);
|
||||
@@ -106,11 +115,15 @@ int main(void) {
|
||||
switch(songSelection) {
|
||||
case 1:
|
||||
printf("Playing Imperial March\n\r");
|
||||
play_song(&songIM[0]);
|
||||
break;
|
||||
play_song(songIM);
|
||||
break;
|
||||
case 2:
|
||||
printf("Playing Super Mario Bros Flagpole Fanfare\n\r");
|
||||
play_song(&songSMFF[0]);
|
||||
printf("Playing Metropolis Theme\n\r");
|
||||
play_song(songMT);
|
||||
break;
|
||||
case 3:
|
||||
printf("Playing Flower Garden\n\r");
|
||||
play_song(songFG);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @file memory.c
|
||||
* @author Trevor Barnes
|
||||
* @brief
|
||||
* @brief Contains functionality for memory manipulation through the console
|
||||
* @version 0.1
|
||||
* @date 2022-01-19
|
||||
*
|
||||
@@ -13,20 +13,13 @@
|
||||
#include <inttypes.h>
|
||||
#include "memory.h"
|
||||
|
||||
#define F_CPU 16000000UL
|
||||
|
||||
void initMemConsole() {
|
||||
init_usart2(57600, F_CPU);
|
||||
printf("Memory Console Initialized! Type 'help' for info.\n\r");
|
||||
}
|
||||
|
||||
void readMem(uint32_t addr) {
|
||||
// Assign and casts a new int pointer the value of addr
|
||||
uint32_t * memPtr = (uint32_t *)addr;
|
||||
// Formatted print with both hex and decimal values
|
||||
printf("Memory Value at %#08x\n\r"
|
||||
"Hex: %#08x\n\r"
|
||||
"Decimal: %d\n\r", addr, *memPtr, *memPtr);
|
||||
"Decimal: %u\n\r", (int)addr, (int)*memPtr, (int)*memPtr);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -36,7 +29,7 @@ void writeMem(uint32_t addr, uint32_t data) {
|
||||
// Write data
|
||||
*memPtr = data;
|
||||
// Confirmation printout showing the new value and address
|
||||
printf("Value written at %#08x: %u \n\r", addr, data);
|
||||
printf("Value written at %#08x: %u \n\r", (int)addr, (int)data);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -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,66 +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);
|
||||
|
||||
}
|
||||
|
||||
play_note(Note noteToPlay) {
|
||||
|
||||
//void play_note(double playFrequency, double playDuration) {
|
||||
|
||||
double freq = noteToPlay.noteFrequency;
|
||||
double dur = noteToPlay.noteDuration;
|
||||
|
||||
void play_note(Note noteToPlay) {
|
||||
*TIM3_PSC = 15;
|
||||
//Divisor controls pitch
|
||||
*TIM3_ARR = mil/freq;
|
||||
// Pitch divisor to scale with timer
|
||||
*TIM3_ARR = (pitchDivisor)/(noteToPlay.noteFrequency);
|
||||
|
||||
//Loudness (Smaller dividend = louder sound)
|
||||
freq = freq/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_1ms(dur);
|
||||
//Disables enable bit
|
||||
// Delay for duration of note
|
||||
delay_1ms(noteToPlay.noteDuration);
|
||||
// Disables timer
|
||||
*TIM3_CR1 &= ~1;
|
||||
}
|
||||
|
||||
|
||||
void play_song(Note *songToPlay){
|
||||
int i = 0;
|
||||
double freq = songToPlay[i].noteFrequency;
|
||||
while(freq != T) {
|
||||
// Iterate through song and play each note till "END" value
|
||||
while(songToPlay[i].noteFrequency != END) {
|
||||
play_note(songToPlay[i]);
|
||||
i++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user