From 76f65fd1e98b86863c15592c375e8bdac1113208 Mon Sep 17 00:00:00 2001 From: tbarnes98 Date: Sun, 20 Feb 2022 19:39:36 -0600 Subject: [PATCH] project changes --- .project | 11 ++++++ labW6barnestr/Inc/piezoSpeaker.h | 62 +++++++++++++++++--------------- labW6barnestr/Src/piezoSpeaker.c | 32 ++++++++++++----- labW9barnestr/.gitignore | 1 - labW9barnestr/Src/tasker.c | 2 +- 5 files changed, 69 insertions(+), 39 deletions(-) create mode 100644 .project delete mode 100644 labW9barnestr/.gitignore diff --git a/.project b/.project new file mode 100644 index 0000000..0347a22 --- /dev/null +++ b/.project @@ -0,0 +1,11 @@ + + + CE2812-Workspace + + + + + + + + diff --git a/labW6barnestr/Inc/piezoSpeaker.h b/labW6barnestr/Inc/piezoSpeaker.h index 2005d7d..428669d 100644 --- a/labW6barnestr/Inc/piezoSpeaker.h +++ b/labW6barnestr/Inc/piezoSpeaker.h @@ -41,7 +41,8 @@ typedef struct { // GPIOB #define GPIOBEN 1 -#define AFRL_TIM3_CH1_EN 17 +#define PB4_AF_V 0b10 +#define PB4_AF_P 9 typedef struct { uint32_t MODER; @@ -56,41 +57,44 @@ typedef struct { uint32_t AFRH; } GPIO; -// Timer 3 + +typedef struct { + uint32_t CR1; // Control Register 1 + uint32_t CR2; // Control Register 2 + uint32_t SMCR; // Slave Mode Control Register + uint32_t DIER; // DMA/Interrupt Enable Register + uint32_t SR; // Status Register + uint32_t EGR; // Event Generation Register + uint32_t CCMR1; // Capture/Compare Mode Register 1 + uint32_t CCMR2; // Capture/Compare Mode Register 2 + uint32_t CCER; // Capture/Compare Enable Register + uint32_t CNT; // Counter + uint32_t PSC; // Prescaler + uint32_t ARR; // Auto-Reload Register + uint32_t _RESERVED_1_; + uint32_t CCR1; // Capture/Compare Register 1 + uint32_t CCR2; // Capture/Compare Register 2 + uint32_t CCR3; // Capture/Compare Register 3 + uint32_t CCR4; // Capture/Compare Register 4 + uint32_t _RESERVED_2_; + uint32_t DCR; // DMA Control Register + uint32_t DMAR; // DMA Address for Full Transfer + uint32_t TIM2_OR; // TIM2 Option Register + uint32_t TIM5_OR; // TIM5 Option Register +} TIM; + +// Timers 3 & 4 #define TIM3_EN 1 +#define TIM4_EN 2 +#define AFRL_TIM3_CH1_EN 17 #define OC1PE 3 #define OC1M_PWM2 0b1110000 #define CCER_CC1E 1 #define EGR_UG 1 #define CR_ARPE_EN 7 #define CR_CEN 1 -typedef struct { - uint32_t CR1; - uint32_t CR2; - uint32_t SMCR; - uint32_t DIER; - uint32_t SR; - uint32_t EGR; - uint32_t CCMR1; - uint32_t CCMR2; - uint32_t CCER; - uint32_t CNT; - uint32_t PSC; - uint32_t ARR; - uint32_t _RESERVED_1_; - uint32_t CCR1; - uint32_t CCR2; - uint32_t CCR3; - uint32_t CCR4; - uint32_t _RESERVED_2_; - uint32_t DCR; - uint32_t DMAR; - uint32_t TIM2_OR; - uint32_t TIM5_OR; -} TIM; +#define DIER_TIE 6 -#define PB4_AF_V 0b10 -#define PB4_AF_P 9 #define pitchDivisor 1000000 // SYSCFG @@ -125,7 +129,7 @@ extern Note songIM[]; extern Note songMT[]; extern Note songFG[]; -// Initializes the piezo speaker to be used with timer 3 +// Initializes the piezo speaker to be used with timer 3. Also initializes timer 4 for note duration interrupt void piezo_init(); // Plays a given note at a certain frequency for a certain duration diff --git a/labW6barnestr/Src/piezoSpeaker.c b/labW6barnestr/Src/piezoSpeaker.c index a3c4f1d..a43d1e7 100644 --- a/labW6barnestr/Src/piezoSpeaker.c +++ b/labW6barnestr/Src/piezoSpeaker.c @@ -16,7 +16,10 @@ volatile RCC* const rcc = (RCC*) 0x40023800; volatile GPIO* const gpiob = (GPIO*) 0x40020400; +// PWM Timer volatile TIM* const tim3 = (TIM*) 0x40000400; +// Interrupt Timer for Duration +volatile TIM* const tim4 = (TIM*) 0x40000800; volatile SYSCFG* const syscfg = (SYSCFG*) 0x40013800; volatile EXTI* const exti4 = (EXTI*) 0x40013C00; @@ -64,11 +67,13 @@ Note songFG[77] = { {END} }; +int volumeDivisor = 10; void piezo_init(){ - // GPIOB/Timer3 enable in RCC + // GPIOB/Timer3/Timer4 enable in RCC (*rcc).AHB1ENR |= (1<SR = 0; if(PLAY) { (*tim3).PSC = 15; // Pitch divisor to scale with timer //current note frequency global variable - //(*tim3).ARR = (pitchDivisor)/(noteToPlay.noteFrequency); + (*tim3).ARR = (pitchDivisor)/(currentSong[currentNoteIndex].noteFrequency); // Volume (Smaller dividend = louder sound) //current note frequency global variable - //unsigned int freq = (noteToPlay.noteFrequency/10); + unsigned int freq = (currentSong[currentNoteIndex].noteFrequency/volumeDivisor); // Clear CCR (*tim3).CCR1 = ((*tim3).CCR1&~(0xFFFF)); - //(*tim3).CCR1 = freq; + (*tim3).CCR1 = freq; // Set EGR (*tim3).EGR |= EGR_UG; - + // Set timer 4 count to duration of note + (*tim4).CNT = (currentSong[currentNoteIndex].noteDuration); + (*tim4).EGR |= EGR_UG; + // Start timer 4 + (*tim4).CR1 |= 1; // Playing note // Enables timer (*tim3).CR1 |= 1; // Delay for duration of note - // This is gonna have to change + //delay_1ms(noteToPlay.noteDuration); + + // Load timer with value of note duration //currentNote = currentSong[currentNoteIndex]; } else { @@ -150,6 +165,7 @@ void TIM3_IRQHandler(void) { } //currentNote = currentSong } + currentNoteIndex++; } void play_song_br(Note *songToPlay) { diff --git a/labW9barnestr/.gitignore b/labW9barnestr/.gitignore deleted file mode 100644 index 3df573f..0000000 --- a/labW9barnestr/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/Debug/ diff --git a/labW9barnestr/Src/tasker.c b/labW9barnestr/Src/tasker.c index ae759b0..ea68653 100644 --- a/labW9barnestr/Src/tasker.c +++ b/labW9barnestr/Src/tasker.c @@ -73,4 +73,4 @@ void PendSV_Handler(void) { stack_pointer = tasks[current_task].stack_pointer; asm volatile("pop {r4-r11,lr}\n\t" "bx lr"); -} \ No newline at end of file +}