-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound.c
59 lines (52 loc) · 1.67 KB
/
Sound.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Sound.c
// Runs on LM4F120 or TM4C123,
// edX lab 13
// Use the SysTick timer to request interrupts at a particular period.
// Daniel Valvano, Jonathan Valvano
// December 29, 2014
// This routine calls the 4-bit DAC
#include "Sound.h"
#include "DAC.h"
#include "..//tm4c123gh6pm.h"
static unsigned short enabled, index;
const unsigned char SineWave32[32] = {8,9,11,12,13,14,14,15,15,15,14,14,13,12,11,9,8,7,5,4,3,2,2,1,1,1,2,2,3,4,5,7};
// **************Sound_Init*********************
// Initialize Systick periodic interrupts
// Also calls DAC_Init() to initialize DAC
// Input: none
// Output: none
void Sound_Init(void){
NVIC_ST_CTRL_R = 0; // disable SysTick during setup
NVIC_SYS_PRI3_R = NVIC_SYS_PRI3_R&0x00FFFFFF; // priority 0
NVIC_ST_CTRL_R = 0x00000007; // enable with core clock and interrupts
DAC_Init();
index=0;
}
// **************Sound_Tone*********************
// Change Systick periodic interrupts to start sound output
// Input: interrupt period
// Units of period are 12.5ns
// Maximum is 2^24-1
// Minimum is determined by length of ISR
// Output: none
void Sound_Tone(unsigned long period){
// this routine sets the RELOAD and starts SysTick
NVIC_ST_RELOAD_R = period-1; // reload value for 500us (assuming 80MHz)
enabled=1;
}
// **************Sound_Off*********************
// stop outputing to DAC
// Output: none
void Sound_Off(void){
// this routine stops the sound output
DAC_Out(0x00); //Turns off the output
enabled=0;
}
// Interrupt service routine
// Executed every 12.5ns*(period)
void SysTick_Handler(void){
index = (index+1)&0x1F;
if (enabled != 0) {
DAC_Out(SineWave32[index]);
}
}