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-08-06 02:52:03 +02:00
|
|
|
#include "synth.h"
|
2012-08-07 20:03:41 +02:00
|
|
|
#include "usart.h"
|
2012-07-26 01:34:49 +02:00
|
|
|
|
|
|
|
static void init_sampletimer(void)
|
|
|
|
{
|
2012-08-06 13:25:33 +02:00
|
|
|
/*
|
|
|
|
* Timer 0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* set timer0 to CTC & prescaler 64 → 125kHz increment */
|
2012-07-26 01:50:48 +02:00
|
|
|
TCCR0A = (1 << WGM01);
|
|
|
|
TCCR0B = (1 << CS00) | (1 << CS01);
|
2012-08-06 13:25:33 +02:00
|
|
|
|
2012-08-08 03:24:21 +02:00
|
|
|
OCR0A = 6; /* TOP */
|
2012-08-06 01:45:37 +02:00
|
|
|
TCNT0 = 0;
|
2012-08-06 13:25:33 +02:00
|
|
|
/*enable interrupt*/
|
2012-08-06 00:16:17 +02:00
|
|
|
TIMSK0 |= (1<<OCIE0A);
|
2012-07-26 01:34:49 +02:00
|
|
|
}
|
|
|
|
|
2012-07-18 15:55:20 +02:00
|
|
|
static inline void init_pwm(void)
|
|
|
|
{
|
2012-08-06 13:25:33 +02:00
|
|
|
/* set PB2 as output (buzzer pwm port):*/
|
|
|
|
DDRB |= (1 << PORTB2);
|
|
|
|
|
|
|
|
/* analog value preselection :*/
|
|
|
|
OCR1B = 0x007F;
|
2012-07-18 16:12:48 +02:00
|
|
|
|
2012-08-06 13:25:33 +02:00
|
|
|
/* Top value. Timer overflows here. Thus we have a resulting 8bit pwm */
|
|
|
|
ICR1 = 0x00FF;
|
|
|
|
|
|
|
|
/* only b-chan , fastpwm (mode 14), no prescale */
|
2012-08-08 03:24:21 +02:00
|
|
|
TCCR1A = (1 << COM1B1) | (1 << WGM11);
|
2012-08-06 13:25:33 +02:00
|
|
|
TCCR1B = (1 << WGM13 ) | (1 << WGM12) | (1 << CS10);
|
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
|
|
|
{
|
2012-08-06 13:25:33 +02:00
|
|
|
/* enable LED channels as output */
|
|
|
|
DDRC |= (1 << PORTC0) | (1 << PORTC2) |
|
|
|
|
(1 << PORTC3) | (1 << PORTC1) ;
|
|
|
|
/* initially one led is on */
|
2012-08-11 21:08:50 +02:00
|
|
|
PORTC = 0b00000101;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Timer 2
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* set timer0 to CTC & prescaler 64 → 125kHz increment */
|
|
|
|
TCCR2A = (1 << WGM21);
|
|
|
|
TCCR2B = (1 << CS20) | (1 << CS21);
|
|
|
|
|
|
|
|
OCR2A = 6; /* TOP */
|
|
|
|
TCNT2 = 0;
|
|
|
|
/*enable interrupt*/
|
|
|
|
TIMSK2 |= (1<<OCIE2A);
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-18 15:55:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-07-18 02:34:15 +02:00
|
|
|
|
2012-07-18 15:55:20 +02:00
|
|
|
inline void setleds(uint8_t state)
|
|
|
|
{
|
2012-08-06 13:25:33 +02:00
|
|
|
/* 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)
|
|
|
|
{
|
2012-08-06 13:25:33 +02:00
|
|
|
/* vibration motor on B1, initially off: */
|
|
|
|
DDRB |= (1 << PORTB1);
|
|
|
|
PORTB &= ~( 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-17 01:05:27 +02:00
|
|
|
|
2012-08-06 00:16:17 +02:00
|
|
|
|
2012-07-18 15:55:20 +02:00
|
|
|
int main(void)
|
|
|
|
{
|
2012-08-06 13:25:33 +02:00
|
|
|
/* hardware initialisation: */
|
2012-07-18 15:55:20 +02:00
|
|
|
init_leds();
|
2012-08-07 20:03:41 +02:00
|
|
|
USART0_Init();
|
2012-08-06 01:45:37 +02:00
|
|
|
// init_motor();
|
2012-07-18 15:55:20 +02:00
|
|
|
init_pwm();
|
2012-07-26 02:20:55 +02:00
|
|
|
init_sampletimer();
|
2012-08-06 13:25:33 +02:00
|
|
|
|
|
|
|
/* syntesizer initialisation */
|
2012-08-06 01:45:37 +02:00
|
|
|
synth_init();
|
2012-08-06 13:25:33 +02:00
|
|
|
|
|
|
|
/* here the show begins:*/
|
2012-08-06 01:45:37 +02:00
|
|
|
sei();
|
2012-08-06 00:16:17 +02:00
|
|
|
|
2012-08-07 20:08:57 +02:00
|
|
|
for(;;) /* ever */ {
|
|
|
|
|
|
|
|
//do something
|
|
|
|
|
|
|
|
USART0_put_uint16(0x2342);
|
|
|
|
USART0_crlf();
|
|
|
|
};
|
2012-07-18 15:55:20 +02:00
|
|
|
|
2012-08-06 13:25:33 +02:00
|
|
|
/* never */ return 0;
|
2012-07-18 15:55:20 +02:00
|
|
|
}
|
2012-08-05 22:13:43 +02:00
|
|
|
|
|
|
|
|
2012-08-06 00:16:17 +02:00
|
|
|
ISR(TIMER0_COMPA_vect)
|
2012-08-05 22:13:43 +02:00
|
|
|
{
|
2012-08-06 13:25:33 +02:00
|
|
|
/* calculate next analog sample value in synth mixer:*/
|
2012-08-06 01:45:37 +02:00
|
|
|
OCR1B = synth_mix();
|
2012-08-05 22:13:43 +02:00
|
|
|
}
|
2012-08-11 21:08:50 +02:00
|
|
|
ISR(TIMER2_COMPA_vect)
|
|
|
|
{
|
|
|
|
//PORTC = 0b0000000;
|
|
|
|
|
|
|
|
}
|