2012-07-17 01:05:27 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <util/delay.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
2012-07-26 01:50:48 +02:00
|
|
|
volatile uint8_t sample_pending;
|
2012-07-26 01:34:49 +02:00
|
|
|
|
2012-07-26 01:26:45 +02:00
|
|
|
|
|
|
|
// sample rate is 8M / (5 * 64) = 25000
|
|
|
|
|
|
|
|
enum {
|
|
|
|
synth_channel_count = 2
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint16_t phase;
|
|
|
|
uint16_t speed;
|
|
|
|
|
|
|
|
} synth_channel_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
synth_channel_t channels[synth_channel_count];
|
|
|
|
uint16_t output;
|
|
|
|
} synth_t;
|
2012-07-26 01:34:49 +02:00
|
|
|
|
2012-07-26 01:26:45 +02:00
|
|
|
static synth_t synth;
|
|
|
|
|
2012-07-26 02:19:59 +02:00
|
|
|
//25kHz
|
|
|
|
ISR(TIMER0_OVF_vect)
|
|
|
|
{
|
|
|
|
PORTC ^= 0b1;
|
|
|
|
ICR1 = synth.output;
|
|
|
|
sample_pending = 1;
|
|
|
|
}
|
|
|
|
|
2012-07-26 01:26:45 +02:00
|
|
|
static void synth_init(void)
|
|
|
|
{
|
|
|
|
// some test values
|
|
|
|
synth.channels[0].phase = 0;
|
|
|
|
synth.channels[0].speed = 1153;
|
|
|
|
|
|
|
|
synth.channels[1].phase = 0;
|
2012-07-26 02:27:12 +02:00
|
|
|
synth.channels[1].speed = 1728;
|
2012-07-26 01:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void synth_mix(void)
|
|
|
|
{
|
|
|
|
synth.output = 0;
|
|
|
|
|
2012-07-26 01:50:48 +02:00
|
|
|
for (int i = 0; i < synth_channel_count; i++) {
|
|
|
|
synth_channel_t *chan = &synth.channels[i];
|
2012-07-26 01:26:45 +02:00
|
|
|
chan->phase += chan->speed;
|
|
|
|
|
2012-07-26 02:19:59 +02:00
|
|
|
synth.output += (chan->phase >> 8) & 0xff;
|
2012-07-26 01:26:45 +02:00
|
|
|
}
|
2012-07-26 01:34:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void init_sampletimer(void)
|
|
|
|
{
|
|
|
|
// Timer 0
|
|
|
|
//
|
|
|
|
//set timer0 to CTC & prescaler 64 == 125k
|
2012-07-26 01:50:48 +02:00
|
|
|
TCCR0A = (1 << WGM01);
|
|
|
|
TCCR0B = (1 << CS00) | (1 << CS01);
|
2012-07-26 01:34:49 +02:00
|
|
|
//count up to 5 :
|
|
|
|
OCR0A = 5;
|
|
|
|
|
|
|
|
//enable interrupt
|
2012-07-26 01:50:48 +02:00
|
|
|
TIMSK0 |= (1 << TOIE0);
|
2012-07-26 01:34:49 +02:00
|
|
|
}
|
|
|
|
|
2012-07-18 15:55:20 +02:00
|
|
|
static inline void init_pwm(void)
|
|
|
|
{
|
2012-07-18 02:34:15 +02:00
|
|
|
//PB1 set to output:
|
2012-07-26 01:50:48 +02:00
|
|
|
DDRB |= (1 << PORTB2);
|
|
|
|
OCR1B = 0xefff; //preselect some default
|
|
|
|
ICR1 = 0xffff; // TOP-wert
|
2012-07-18 16:12:48 +02:00
|
|
|
|
2012-07-26 01:50:48 +02:00
|
|
|
TCCR1A = (1 << COM1B1) | (1 << WGM11); // only b-chan , fastpwm (mode 14)
|
|
|
|
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS10); //Fastpwm, no prescale
|
2012-07-18 16:12:48 +02:00
|
|
|
|
2012-07-26 01:50:48 +02:00
|
|
|
return;
|
2012-07-18 02:34:15 +02:00
|
|
|
}
|
|
|
|
|
2012-07-18 15:55:20 +02:00
|
|
|
static void init_leds(void)
|
2012-07-17 01:05:27 +02:00
|
|
|
{
|
|
|
|
//enable LED channels as output
|
2012-07-18 15:55:20 +02:00
|
|
|
DDRC |= (1 << PORTC0) | (1 << PORTC2) | (1 << PORTC3) | (1 << PORTC1);
|
|
|
|
PORTC = 1; //one led is on...
|
|
|
|
return;
|
|
|
|
}
|
2012-07-18 02:34:15 +02:00
|
|
|
|
2012-07-18 15:55:20 +02:00
|
|
|
inline void setleds(uint8_t state)
|
|
|
|
{
|
|
|
|
//set leds according to
|
2012-07-26 02:19:59 +02:00
|
|
|
PORTC |= (state | 0b00001111);
|
|
|
|
PORTC &= ~(state | 0b11110000);
|
2012-07-18 15:55:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-07-17 01:05:27 +02:00
|
|
|
|
2012-07-18 15:55:20 +02:00
|
|
|
static void init_motor(void)
|
|
|
|
{
|
|
|
|
//vibration motor on B1:
|
|
|
|
DDRB |= (1 << PORTB1);
|
2012-07-17 01:05:27 +02:00
|
|
|
|
2012-07-18 15:55:20 +02:00
|
|
|
}
|
2012-07-18 02:34:15 +02:00
|
|
|
|
2012-07-18 15:55:20 +02:00
|
|
|
static void stupid_pwmtest(void)
|
|
|
|
{
|
2012-07-26 01:26:45 +02:00
|
|
|
uint8_t i, t, r;
|
|
|
|
ICR1 = 0xAA00;
|
|
|
|
t = r = 1;
|
2012-07-18 15:55:20 +02:00
|
|
|
for (;;) {
|
2012-07-24 23:07:08 +02:00
|
|
|
|
2012-07-26 01:50:48 +02:00
|
|
|
t = (r) ? (t + 1) : (t - 1);
|
2012-07-24 23:07:08 +02:00
|
|
|
|
|
|
|
ICR1 = (t << 7);
|
2012-07-26 01:50:48 +02:00
|
|
|
if (t == 0)
|
|
|
|
r ^= 1;
|
|
|
|
for (i = 1; i < 100; i++)
|
|
|
|
__asm("nop");
|
2012-07-24 23:07:08 +02:00
|
|
|
|
2012-07-17 01:05:27 +02:00
|
|
|
}
|
2012-07-26 01:50:48 +02:00
|
|
|
return; //never
|
2012-07-17 01:05:27 +02:00
|
|
|
}
|
|
|
|
|
2012-07-18 15:55:20 +02:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
|
2012-07-26 01:26:45 +02:00
|
|
|
//hardware initialisation:
|
2012-07-18 15:55:20 +02:00
|
|
|
init_leds();
|
|
|
|
init_motor();
|
|
|
|
init_pwm();
|
2012-07-26 02:20:55 +02:00
|
|
|
init_sampletimer();
|
2012-07-26 02:19:59 +02:00
|
|
|
sample_pending = 0;
|
2012-07-26 01:26:45 +02:00
|
|
|
synth_init();
|
2012-07-18 15:55:20 +02:00
|
|
|
|
2012-07-26 01:50:48 +02:00
|
|
|
sei();
|
|
|
|
|
2012-07-26 02:19:59 +02:00
|
|
|
while(1) {
|
2012-07-26 01:50:48 +02:00
|
|
|
while (0 == sample_pending) ;
|
|
|
|
sample_pending = 0;
|
|
|
|
synth_mix();
|
|
|
|
}
|
2012-07-18 15:55:20 +02:00
|
|
|
|
|
|
|
//never get here
|
2012-07-26 01:26:45 +02:00
|
|
|
return 0;
|
2012-07-18 15:55:20 +02:00
|
|
|
}
|