blakelivingston / avr_tinytune Goto Github PK
View Code? Open in Web Editor NEWAudio Synthesis and Song Playback Library for Atmel ATtiny85 microcontrollers
License: MIT License
Audio Synthesis and Song Playback Library for Atmel ATtiny85 microcontrollers
License: MIT License
Hi there,
I just stumbled upon this project and found it quite interesting.
I'd like to port it to the ATMega8 and higher, of which do not have the built-in PLL for fast PWM and a different configuration of timers.
I'm only new to AVRs, but have been thoroughly reading datasheets all day.
I've got an ATMega8 and to try and get it working with this code I've modified the timer related code in tinytune.c as follows:
Sample interrupt now comes from Timer1 (16 bit timer on ATMega) @ F_CPU/8/SAMPLE_CLOCK_DIVIDER = SAMPLE_RATE?
I changed to the appropriate interrupt vector (TIMER1_OVF_vect
) and can see some activity on PORTB[0:1] as should be in the interrupt code, so I /assume/ it's doing the right thing.
void initSampleInterrupt(void) {
/* OLD CODE
// Clock : Sys/8 (2mhz)
// Fast PWM mode (|WGM0-2)
TCCR0A = (1 << WGM00) | (1 << WGM01);
TCCR0B = (1 << CS01) | (1 << WGM02);
// Enable interrupt on overflow.
TIMSK |= (1 << TOIE0);
// In fast PWM mode, the timer overflows at OCR0A
// Our timer / OCR0A = SAMPLE RATE
// Do make sure this is under 255..
OCR0A = SAMPLE_CLOCK_DIVIDER;
sei();
// Enable interrupts
*/
TIMSK |= (1 << TOIE1);
// fast pwm ( WGM13, 12, 11,10) for TOP controlled by OCR1A
TCCR1B |= (1 << WGM12 ) | (1 << WGM13);
TCCR1A |= (1 << WGM10) | (1 << WGM11);
TCCR1B |= (1 << CS11); // CS11 for /8 divider
OCR1A = SAMPLE_CLOCK_DIVIDER;
sei();
}
The next change I made was to get 8-pwm going as fast as I can out of the other timer that supports it on the ATMega (Timer2) using the following code:
This should set up 8-bit PWM at a frequency of F_CPU / (prescale * 256).
void initPWMB(void) {
/*TCCR1 = (1 << CS10); // Run at PCK/1
GTCCR = (1 << PWM1B) | (1 << COM1B0); //Enable PWMB (pb4)
DDRB |= (1 << PB4) | (1 << PB0) | (1 << PB1); // Output on pb4
OCR1C = 0xff; */
// set up timer 2 - this will be the output PWM
TCCR2 |= ((1 << WGM21) | (1 << WGM20)); // Fast-PWM (p.115)
TCCR2 |= (1 << COM21); // non-inverting OC2 pin output (p115)
TCCR2 &= ~(1 << COM20);
TCCR2 |= (1 << CS10); // prescale 1x
OCR2 = 0;
}
I've also changed the first line of the interrupt routine to change the PWM duty cycle:
OCR2 = sample_buffer[sample_buf_clock++];
Everything should be in order, but alas, I get no sound.
I've tried a basic test sine table using the same timers and that seems to work fine, but here I get dead silence.
Would you happen to have some extra knowledge on how this code would be best ported to the ATMega hardware (looks like there's a hardware multiply instruction) or anything on minimum timings (perhaps an interrupt is being overflowed)? I've tried running from the internal 8 MHz RC as well as an external 16 MHz crystal as well lowering the sample rate.
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.