-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
106 lines (88 loc) · 2.69 KB
/
main.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
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
/*!
* Polyphonic synthesizer for microcontrollers: Atmel ATTiny85 port.
* (C) 2016 Stuart Longland
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include "poly.h"
#include "fifo.h"
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define SAMPLE_LEN 16
static volatile uint8_t sample_buffer[SAMPLE_LEN];
static struct fifo_t sample_fifo;
int main(void) {
struct poly_evt_t poly_evt;
/* Turn on all except ADC */
PRR = (1 << PRADC);
/* Start up PLL */
PLLCSR = (1 << PLLE);
while (!(PLLCSR & (1<<PLOCK)));
PLLCSR |= (1<<PCKE);
fifo_init(&sample_fifo, sample_buffer, SAMPLE_LEN);
/* Reset the synthesizer */
poly_reset();
/* Enable output on PB4 (PWM out) */
DDRB |= (1 << 4) | (1 << 3);
PORTB |= (1 << 4) | (1 << 3);
/* Timer 1 configuration for PWM */
OCR1B = 128; /* Initial PWM value */
OCR1C = 255; /* Maximum PWM value */
TCCR1 = (1 << CS10); /* No prescaling, max speed */
GTCCR = (1 << PWM1B) /* Enable PWM */
| (2 << COM1B0); /* Clear output bit on match */
/* Timer 0 configuration for sample rate interrupt */
TCCR0A = (2 << WGM00); /* CTC mode */
TCCR0B = (1 << CS00); /* No prescaling */
OCR0A = F_CPU / _POLY_FREQ; /* Sample rate */
TIMSK |= (1 << OCIE0A); /* Enable interrupts */
/* Configure the synthesizer */
poly_evt.flags = POLY_EVT_TYPE_ENABLE;
poly_evt.value = 1;
poly_load(&poly_evt);
poly_evt.flags = POLY_EVT_TYPE_IFREQ;
poly_evt.value = 1000;
poly_load(&poly_evt);
poly_evt.flags = POLY_EVT_TYPE_IAMP;
poly_evt.value = 255;
poly_load(&poly_evt);
poly_evt.flags = POLY_EVT_TYPE_ASCALE;
poly_evt.value = 8;
poly_load(&poly_evt);
sei();
while(1) {
poly_evt.flags = POLY_EVT_TYPE_TIME;
poly_evt.value = 64000;
poly_load(&poly_evt);
while (poly_remain) {
while (sample_fifo.stored_sz < SAMPLE_LEN) {
int16_t s = poly_next();
fifo_write_one(&sample_fifo,
128 + (s >> 9));
}
PORTB ^= (1 << 3);
}
}
return 0;
}
ISR(TIM0_COMPA_vect) {
uint8_t sample = fifo_read_one(&sample_fifo);
if (sample >= 0)
OCR1B = sample;
else
OCR1B = 128;
}