pentabug/firmware/main.c

113 lines
1.8 KiB
C
Raw Normal View History

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-07-17 01:05:27 +02:00
2012-07-26 01:34:49 +02:00
static void init_sampletimer(void)
{
// Timer 0
2012-08-06 01:45:37 +02:00
// set timer0 to CTC & prescaler 64 == 125k
TCCR0A = (1 << WGM01);
TCCR0B = (1 << CS00) | (1 << CS01);
2012-08-06 01:45:37 +02:00
// count up to 5 :
2012-08-06 00:16:17 +02:00
OCR0A = 3;
2012-08-06 01:45:37 +02:00
TCNT0 = 0;
// 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 01:45:37 +02:00
// PB2 set to output:
DDRB |= (1 << PORTB2);
2012-08-06 01:45:37 +02:00
OCR1B = 0x001F; // preselect some default
2012-08-06 00:19:47 +02:00
ICR1 = 0x00FF; // TOP-wert
2012-07-18 16:12:48 +02:00
TCCR1A = (1 << COM1B1) | (1 << WGM11); // only b-chan , fastpwm (mode 14)
2012-08-06 01:45:37 +02:00
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS10); // Fastpwm, no prescale
2012-07-18 16:12:48 +02:00
2012-08-06 01:45:37 +02:00
// TIMSK1 |= (1 << OCIE1B); //enable timer 1 Output compare
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 01:45:37 +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)
{
2012-08-06 01:45:37 +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 01:45:37 +02:00
// vibration motor on B1:
2012-07-18 15:55:20 +02:00
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-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 01:45:37 +02:00
// hardware initialisation:
2012-07-18 15:55:20 +02:00
init_leds();
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 01:45:37 +02:00
synth_init();
// OCR1B = 0x00F0;
sei();
2012-08-06 00:16:17 +02:00
2012-08-06 01:45:37 +02:00
while(1) {}
2012-07-18 15:55:20 +02:00
2012-08-06 01:45:37 +02:00
// never get here
2012-07-26 01:26:45 +02:00
return 0;
2012-07-18 15:55:20 +02:00
}
2012-08-05 22:13:43 +02:00
2012-08-06 01:45:37 +02:00
// 25kHz
2012-08-06 00:16:17 +02:00
ISR(TIMER0_COMPA_vect)
2012-08-05 22:13:43 +02:00
{
2012-08-06 01:45:37 +02:00
// test stuff
/*
static uint16_t counter = 0;
static uint16_t pulsewidth = 0;
static uint16_t maxcounter = 0x133;
static uint16_t pulsecounter = 0;
2012-08-06 00:16:17 +02:00
counter++;
if (counter > maxcounter){
counter = 0;
};
pulsecounter++;
2012-08-06 01:45:37 +02:00
if (pulsecounter > 0x300){
2012-08-06 00:16:17 +02:00
pulsecounter = 0;
pulsewidth++;
if (pulsewidth > maxcounter){
pulsewidth = 0;
}
}
2012-08-06 01:45:37 +02:00
OCR1B = ((counter > pulsewidth) ? 0xff : 0x00);
*/
2012-08-06 00:16:17 +02:00
2012-08-06 01:45:37 +02:00
OCR1B = synth_mix();
2012-08-06 00:16:17 +02:00
2012-08-05 22:13:43 +02:00
}