-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwm_player.ino
109 lines (93 loc) · 8.55 KB
/
pwm_player.ino
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <SPI.h>
/************************************************************
* PWM on (PCINT22/OC0A/AIN0)PD6, Arduino Uno digital pin 6, timer0.
* The digital pulse width (based on when pin is toggled off) is modulated by values in array
* So pulsewidth/osscilation is changed in freq based on timer1 in (CTC mode),
* which has ISR attached that sets new OCR for timer0.
*
* In between it keeps pulsing, high from 0 to OCR, low for OCR to TOP.
* Then flips over to 0 and is turned high again. (Bebahvior based on settings in TCCR0A)
*
* To smoothen out output wave, simple RC filter can be used.
**********************************************************/
//#define LOAD_TO_SRAM
#include "sram.h"
#define TIMER1_PRESCALER 1 // 1, 8, 64, 256 or 1024
#define TIMER1_COMPARE_MATCH 2000 //~8000hz
const int audio_len = 1199;
int audio_idx = -1;
long sram_offset = 0;
#ifdef LOAD_TO_SRAM
const unsigned char audio_data[] = {
0, 0, 244, 19, 0, 0, 100, 97, 116, 97, 244, 19, 0, 0, 125, 126, 128, 127, 128, 129, 128, 128, 130, 127, 127, 127, 123, 124, 126, 123, 128, 130, 128, 131, 129, 126, 125, 125, 123, 128, 129, 127, 128, 125, 123, 125, 126, 125, 127, 126, 123, 125, 124, 126, 131, 130, 129, 130, 125, 126, 128, 128, 131, 133, 132, 132, 133, 132, 134, 135, 131, 129, 128, 125, 128, 132, 133, 135, 132, 128, 126, 125, 124, 126, 127, 128, 129, 130, 127, 126, 126, 122, 124, 124, 123, 129, 129, 125, 126, 123, 121, 125, 127, 128, 129, 128, 123, 123, 122, 118, 125, 126, 123, 127, 121, 117, 124, 123, 130, 142, 141, 138, 135, 123, 119, 124, 126, 134, 141, 138, 137, 136, 130, 132, 133, 128, 128, 127, 122, 128, 131, 132, 135, 131, 124, 122, 120, 123, 130, 132, 129, 126, 120, 116, 121, 124, 127, 131, 129, 127, 128, 127, 128, 128, 123, 119, 116, 118, 121, 127, 132, 130, 125, 119, 113, 116, 121, 131, 143, 146, 143, 136, 126, 120, 122, 125, 134, 140, 139, 137, 133, 130, 131, 133, 132, 130, 127, 124, 124, 129, 131, 133, 134, 128, 124, 122, 120, 125, 128, 127, 130, 126, 122, 123, 122, 123, 126, 125, 122, 126, 126, 125, 129, 123, 117, 120, 113, 115, 126, 123, 127, 132, 122, 123, 126, 124, 133, 138, 138, 139, 136, 127, 124, 123, 122, 128, 134, 132, 136, 134, 129, 133, 131, 130, 133, 129, 127, 132, 131, 134, 136, 134, 130, 130, 126, 123, 125, 122, 121, 123, 121, 123, 125, 126, 129, 128, 123, 118, 115, 115, 121, 127, 131, 131, 125, 119, 116, 116, 120, 123, 123, 123, 121, 122, 126, 132, 140, 143, 141, 136, 126, 121, 121, 123, 131, 136, 136, 135, 130, 126, 127, 129, 132, 134, 131, 128, 124, 124, 128, 134, 140, 142, 140, 134, 127, 123, 121, 123, 127, 128, 129, 130, 126, 125, 122, 120, 121, 121, 122, 124, 123, 125, 124, 122, 122, 120, 119, 120, 118, 118, 117, 114, 116, 111, 111, 120, 130, 142, 153, 151, 144, 130, 115, 109, 109, 120, 133, 142, 149, 147, 138, 132, 124, 121, 121, 121, 123, 125, 127, 130, 134, 136, 136, 138, 139, 140, 143, 139, 133, 126, 119, 119, 126, 134, 143, 145, 139, 129, 118, 111, 109, 112, 117, 123, 128, 129, 127, 126, 123, 121, 118, 113, 109, 108, 109, 110, 116, 117, 125, 138, 144, 149, 151, 139, 129, 120, 107, 111, 120, 126, 140, 146, 145, 146, 137, 129, 123, 117, 114, 117, 121, 126, 134, 138, 140, 143, 139, 131, 133, 130, 130, 135, 131, 129, 130, 126, 128, 132, 132, 134, 130, 121, 116, 113, 110, 116, 124, 126, 128, 125, 117, 113, 113, 113, 118, 118, 114, 109, 103, 112, 127, 144, 166, 171, 159, 143, 112, 96, 97, 105, 129, 147, 153, 157, 145, 134, 131, 123, 126, 125, 118, 117, 116, 119, 131, 140, 147, 147, 141, 135, 129, 132, 135, 134, 135, 127, 122, 125, 126, 138, 145, 142, 136, 122, 107, 102, 102, 109, 119, 125, 126, 123, 119, 115, 112, 110, 108, 102, 99, 106, 116, 144, 176, 181, 179, 155, 109, 87, 75, 80, 119, 146, 167, 180, 163, 147, 134, 116, 113, 111, 106, 107, 107, 116, 134, 156, 169, 169, 152, 124, 102, 93, 103, 128, 151, 164, 163, 149, 133, 119, 114, 117, 119, 122, 122, 120, 121, 122, 124, 123, 117, 110, 102, 95, 98, 102, 108, 117, 123, 136, 165, 178, 177, 167, 128, 92, 76, 70, 97, 136, 164, 188, 183, 162, 139, 115, 102, 100, 101, 109, 116, 124, 135, 148, 160, 165, 158, 139, 114, 95, 91, 101, 124, 148, 164, 169, 163, 151, 138, 125, 115, 106, 102, 105, 113, 127, 139, 144, 137, 122, 105, 91, 85, 85, 90, 105, 112, 119, 136, 167, 187, 190, 181, 138, 90, 65, 50, 74, 127, 166, 199, 203, 175, 145, 114, 92, 90, 95, 104, 113, 123, 135, 149, 167, 174, 168, 150, 115, 83, 70, 72, 107, 163, 199, 221, 208, 156, 109, 72, 55, 79, 112, 143, 166, 160, 140, 121, 101, 90, 89, 85, 86, 85, 84, 105, 130, 178, 229, 220, 200, 146, 59, 31, 26, 54, 135, 190, 221, 225, 184, 139, 104, 87, 86, 96, 108, 107, 114, 127, 141, 173, 194, 189, 168, 123, 75, 52, 56, 94, 153, 204, 227, 217, 177, 123, 83, 64, 69, 96, 124, 143, 154, 149, 135, 121, 104, 86, 70, 65, 67, 80, 119, 180, 221, 238, 227, 159, 88, 42, 11, 41, 104, 157, 212, 226, 200, 168, 128, 98, 88, 87, 90, 95, 106, 119, 143, 177, 196, 197, 178, 129, 83, 59, 55, 95, 153, 194, 222, 209, 165, 123, 86, 71, 84, 103, 121, 134, 134, 127, 119, 112, 101, 92, 83, 78, 90, 130, 178, 207, 223, 194, 132, 85, 42, 42, 85, 131, 181, 204, 193, 167, 135, 113, 103, 106, 109, 106, 106, 108, 119, 148, 174, 188, 186, 153, 111, 83, 71, 94, 137, 171, 194, 187, 157, 127, 104, 95, 104, 115, 122, 123, 117, 107, 104, 102, 98, 94, 86, 95, 121, 165, 203, 214, 202, 145, 84, 48, 31, 69, 126, 176, 213, 207, 178, 140, 108, 95, 95, 104, 109, 110, 114, 122, 143, 172, 187, 186, 163, 122, 89, 76, 85, 122, 160, 185, 190, 170, 140, 113, 97, 93, 99, 108, 113, 115, 113, 110, 109, 96, 86, 88, 109, 169, 199, 216, 220, 141, 91, 53, 17, 73, 124, 172, 219, 205, 176, 139, 109, 98, 101, 115, 111, 107, 109, 106, 132, 172, 189, 200, 184, 131, 95, 72, 66, 105, 147, 173, 194, 182, 149, 127, 103, 88, 96, 100, 102, 113, 110, 103, 102, 88, 84, 108, 152, 193, 217, 217, 164, 101, 58, 23, 52, 110, 159, 208, 214, 184, 152, 118, 99, 102, 111, 112, 109, 106, 105, 124, 160, 186, 200, 190, 151, 109, 80, 72, 94, 133, 167, 184, 183, 158, 130, 110, 91, 90, 100, 102, 111, 117, 111, 106, 80, 79, 112, 141, 208, 232, 212, 183, 91, 41, 28, 39, 115, 171, 211, 218, 183, 148, 111, 102, 107, 111, 118, 105, 93, 103, 121, 161, 204, 210, 201, 160, 98, 70, 62, 84, 135, 169, 188, 186, 156, 123, 100, 84, 84, 95, 101, 103, 104, 92, 81, 94, 122, 180, 224, 229, 214, 138, 66, 32, 13, 68, 136, 187, 228, 212, 174, 138, 109, 100, 109, 112, 104, 96, 89, 99, 137, 183, 214, 222, 194, 136, 87, 56, 59, 99, 144, 180, 195, 181, 153, 124, 98, 84, 84, 86, 91, 98, 99, 99, 98, 111, 141, 184, 218, 212, 188, 128, 61, 38, 35, 79, 147, 191, 217, 207, 170, 135, 112, 102, 104, 110, 104, 97, 99, 111, 141, 184, 204, 206, 189, 131, 91, 72, 64, 104, 142, 166, 190, 176, 149, 127, 97, 83, 85, 84, 92, 99, 96, 92, 99, 109, 153, 216, 213, 225, 186, 81, 64, 20, 27, 115, 150, 201, 226, 191, 161, 138
};
#endif
void print_settings()
{
//interrupt frequency (Hz) = (Arduino clock speed 16,000,000Hz) / (prescaler * (compare match register + 1))`
unsigned long hz = 16000000 / (TIMER1_PRESCALER * (TIMER1_COMPARE_MATCH + 1));
char buffer[100];
sprintf (buffer, "timer1 freq: %luhz", hz);
Serial.println(buffer);
}
void setup_timers()
{
cli();
/*** timer0 ***/
DDRD |= 1 << PD6; //Set correct data direction output pin, //Arduino Uno digital pin 6
TCCR0A = (TCCR0A | 1 << COM0A1) & ~(1 << COM0A0); //Clear output pin on compare match
TCCR0B = (TCCR0B & ~(1 << CS02 | 1 << CS01)) | 1 << CS00; //No prescaler
// Fast PWM, TOP = 255 (default/non-varying)
TCCR0A |= (1 << WGM01 | 1 << WGM00);
TCCR0B &= ~(1 << WGM02);
OCR0A = -1;
/*** timer1 ***/
//CTC mode
TCCR1B = (TCCR1B & ~(1 << WGM13)) | 1 << WGM12;
TCCR1A &= ~(1 << WGM11 | 1 << WGM10);
//TODO: preprocessor?
switch(TIMER1_PRESCALER){
case 1:
TCCR1B = (TCCR1B & ~(1 << CS12 | 1 << CS11)) | 1 << CS10;
break;
case 8:
TCCR1B = (TCCR1B & ~(1 << CS12 | 1 << CS10)) | 1 << CS11;
break;
case 64:
TCCR1B = (TCCR1B & ~(1 << CS12)) | 1 << CS11 | 1 << CS10;
break;
case 256:
TCCR1B = (TCCR1B & ~(1 << CS11 | 1 << CS10)) | 1 << CS12;
break;
case 1024:
TCCR1B = (TCCR1B & ~(1 << CS11)) | 1 << CS12 | 1 << CS10;
break;
default:
Serial.println("Invalid prescaler timer1");
}
OCR1A = TIMER1_COMPARE_MATCH;
TIMSK1 |= 1 << OCIE1A; //ISR on compare match
sei();
}
// Executed when timer1 reaches OCR1B value (0xFF) to load the next sample.
ISR(TIMER1_COMPA_vect)
{
audio_idx ++;
if (audio_idx >= audio_len){
audio_idx = 0;
}
ram_read((char*)&OCR0A, sram_offset + audio_idx, 1);
//OCR0A = audio_data[audio_idx];
}
void setup()
{
Serial.begin(9600);
ram_init();
#ifdef LOAD_TO_SRAM
ram_write((char*)audio_data, sram_offset, audio_len);
#endif
print_settings();
setup_timers();
}
void loop()
{}