All right thank you very much!
I also had to remove the macro usage, in order to finally compile my sketch, but... I can't listen anything
My idea is to output some audio by using a 8-bit-timer-driven PWM signal, changing the duty cycle by using the OCRxA and OCRxB registers.
I have the idea that 2560's timer0 is an 8-bit one; and it maps to the Arduino pins 4 and 13. Am I correct?
Furthermore, I tried to use a function that changes the prescaler of timer0, taken from the Arduino Playground (see code below); yet don't know if it's actually valid for the 2560's timer0.
Ladies and gentlemen, here's the code:
#include "close0.h"
#include "close1.h"
#include "close2.h"
#include "far0.h"
#include "far1.h"
#include "far2.h"
#include "far3.h"
#include "far4.h"
#include <TimerOne.h>
// Files and library required on this sketch
volatile unsigned long count = 0;
void setup() {
//pinMode(2, INPUT_PULLUP);
pinMode(4, OUTPUT); // Left
pinMode(13, OUTPUT); // Right
// setting up the pin.
setPwmFrequency(5, 1); // I use 5 because this function is made for ATmega328P, but it should still set timer0's prescaler
// Adjust the PWM frequency to the highest possible. Thus, the second argument must ALWAYS be 1.
// Do not use the Timer0 (pins 5, & 6), otherwise "delay" and "millis" functions may not work properly.
analogWrite(4, 1); // Initialize PWM mode on that pin (timer)
analogWrite(13, 1);
Timer1.initialize();
cli();
Timer1.attachInterrupt(writeSamples, 45); // 45 microseconds should yield an interrupt rate of 22 KHz,
// which is the intended sampling rate.
sei();
pgm_read_byte(&close0[0]);
pgm_read_byte(&close1[0]);
pgm_read_byte(&close2[0]);
pgm_read_byte(&far0[0]);
pgm_read_byte(&far0[0]);
pgm_read_byte(&far1[0]);
pgm_read_byte(&far2[0]);
pgm_read_byte(&far3[0]);
// Forces compiler to write these arrays, even if we apparently aren't using them.
}
void loop() {}
void writeSamples() { // Thankfully TimerOne library allows me to declare a timer-based ISR more friendly
if (count == 253952UL) // 253952 bytes is the 2560 total program memory, right?
count = 0;
OCR0B = pgm_read_byte_far(count); // I think the "far" function is for 24 or 32-bit pointers.
count++;
OCR0A = pgm_read_byte_far(count);
count++;
// Scan throughout the program memory to figure out where the heck are the samples (arrays) written.
// Not by showing up, but by listening. I will later figure it out by dumping on a SD card.
}
void setPwmFrequency(int pin, int divisor) {
// A function that modifies the PWM frequency of the specified pin.
// Taken from http://playground.arduino.cc/Code/PwmFrequency
byte mode;
if (pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch (divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if (pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if (pin == 3 || pin == 11) {
switch (divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x7; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}
If you are going to attempt to compile it, then ask me for the necessary files; or just delete all includes except TimerOne, and the pgm_read_byte lines on the setup.
Thank you one more time!
PD: hope that the timer0 has been set to "fast PWM" mode by default, and not to "phase-correct" mode.