From 7aa3aa793ca0ce08917d093d3add14a25e5fa8be Mon Sep 17 00:00:00 2001 From: Richard Osterloh Date: Sat, 12 Apr 2014 12:55:18 +0100 Subject: [PATCH] Today's work --- DAC/DAC.c | 13 ++++++++++--- DAC/Lab13.c | 2 +- DAC/Piano.c | 29 +++++++++++++++++++++++++---- DAC/Sound.c | 16 ++++++++++++++-- DAC/Sound.h | 15 +++++++++++++++ 5 files changed, 65 insertions(+), 10 deletions(-) diff --git a/DAC/DAC.c b/DAC/DAC.c index c601104..126fb06 100644 --- a/DAC/DAC.c +++ b/DAC/DAC.c @@ -13,8 +13,15 @@ // Initialize 4-bit DAC // Input: none // Output: none -void DAC_Init(void){ - +void DAC_Init(void){ volatile unsigned long delay; + SYSCTL_RCGC2_R |= 0x02; // 1) activate Port B + delay = SYSCTL_RCGC2_R; // allow time for clock to stabilize + // 2) no need to unlock + GPIO_PORTB_AMSEL_R &= ~0x0F; // 3) disable analog functionality on PB3-0 + GPIO_PORTB_PCTL_R &= ~0x0000FFFF;// 4) configure PB3-0 as GPIO + GPIO_PORTB_DIR_R |= 0x0F; // 5) make PB3-0 out + GPIO_PORTB_AFSEL_R &= ~0x0F; // 6) disable alt funct on PB3-0 + GPIO_PORTB_DEN_R |= 0x0F; // 7) enable digital I/O on PB3-0 } @@ -23,5 +30,5 @@ void DAC_Init(void){ // Input: 4-bit data, 0 to 15 // Output: none void DAC_Out(unsigned long data){ - + GPIO_PORTB_DATA_R = data; } diff --git a/DAC/Lab13.c b/DAC/Lab13.c index d29026a..3ab78e5 100644 --- a/DAC/Lab13.c +++ b/DAC/Lab13.c @@ -26,7 +26,7 @@ int main(void){ // Real Lab13 EnableInterrupts(); // enable after all initialization are done while(1){ // input from keys to select tone - + } } diff --git a/DAC/Piano.c b/DAC/Piano.c index 37083d4..5401281 100644 --- a/DAC/Piano.c +++ b/DAC/Piano.c @@ -15,8 +15,16 @@ // Initialize piano key inputs // Input: none // Output: none -void Piano_Init(void){ - +void Piano_Init(void){ volatile unsigned long delay; + SYSCTL_RCGC2_R |= 0x10; // activate Port E + delay = SYSCTL_RCGC2_R; // allow time for clock to stabilize + // no need to unlock + GPIO_PORTE_AMSEL_R &= ~0x0F; // disable analog functionality on PE3-0 + GPIO_PORTE_PCTL_R &= ~0x0000FFFF;// configure PE3-0 as GPIO + GPIO_PORTE_DIR_R &= ~0x0F; // make PE3-0 in + GPIO_PORTE_AFSEL_R &= ~0x0F; // disable alt funct on PE3-0 + GPIO_PORTF_PUR_R |= 0x0F; // enable pull-up on PE3-0 + GPIO_PORTE_DEN_R |= 0x0F; // enable digital I/O on PE3-0 } // **************Piano_In********************* // Input from piano key inputs @@ -25,6 +33,19 @@ void Piano_Init(void){ // 0x01 is key 0 pressed, 0x02 is key 1 pressed, // 0x04 is key 2 pressed, 0x08 is key 3 pressed unsigned long Piano_In(void){ - - return 0; // remove this, replace with input + int key = GPIO_PORTE_DATA_R; + // For now just return the lowest value + if(key&0x01) { + key = 0x01; + } else if(key&0x02) { + key = 0x02; + } else if(key&0x04) { + key = 0x04; + } else if(key&0x08) { + key = 0x08; + } else { + key = 0x00; + } + + return key; } diff --git a/DAC/Sound.c b/DAC/Sound.c index 6168adb..902e7e8 100644 --- a/DAC/Sound.c +++ b/DAC/Sound.c @@ -10,13 +10,22 @@ #include "DAC.h" #include "..//tm4c123gh6pm.h" +unsigned char Index; +const unsigned char SineWave[16] = {4,5,6,7,7,7,6,5,4,3,2,1,1,1,2,3}; + // **************Sound_Init********************* // Initialize Systick periodic interrupts // Also calls DAC_Init() to initialize DAC // Input: none // Output: none void Sound_Init(void){ - + DAC_Init(); // Port B is DAC + Index = 0; + NVIC_ST_CTRL_R = 0; // disable SysTick during setup + NVIC_ST_RELOAD_R = 90909-1; // reload value for 880Hz => (1/880Hz) / (1/80Mhz) + NVIC_ST_CURRENT_R = 0; // any write to current clears it + NVIC_SYS_PRI3_R = (NVIC_SYS_PRI3_R&0x00FFFFFF)|0x20000000; // priority 1 + NVIC_ST_CTRL_R = 0x00000007; // enable with core clock and interrupts } // **************Sound_Tone********************* @@ -28,6 +37,7 @@ void Sound_Init(void){ // Output: none void Sound_Tone(unsigned long period){ // this routine sets the RELOAD and starts SysTick + NVIC_ST_RELOAD_R = period-1; // reload value } @@ -36,11 +46,13 @@ void Sound_Tone(unsigned long period){ // Output: none void Sound_Off(void){ // this routine stops the sound output + DAC_Out(0); } // Interrupt service routine // Executed every 12.5ns*(period) void SysTick_Handler(void){ - + Index = (Index+1)&0x0F; + DAC_Out(SineWave[Index]); } diff --git a/DAC/Sound.h b/DAC/Sound.h index 7ae0b55..857ed93 100644 --- a/DAC/Sound.h +++ b/DAC/Sound.h @@ -5,6 +5,21 @@ // Daniel Valvano, Jonathan Valvano // March 13, 2014 +enum Notes { + A, + B, + C, + D, + E, + F, + G, +}; + +#define C 9556 // 80Mhz/(523.251*16) = 9555.6435 +#define D 8513 // 80Mhz/(587.330*16) = 8513.1017 +#define E 7584 // 80Mhz/(659.255*16) = 7584.3187 +#define G 6378 // 80Mhz/(783.991*16) = 6577.6242 + // **************Sound_Init********************* // Initialize Systick periodic interrupts // Also initializes DAC