OK Here is the full code listing such as it is.
`#include <timer.h>
#define LED_RATE 2800 // 2800 is best rate
/*
a prescale 7200
1=2.5 khz
100=49.5 hz
200=24.9 hz
300=16.6 hz
400=12.5 hz
500=10.0 hz
a prescale of 2880
128=99.6 hz
64=197.7 hz
32=389.4 hz
16=756.0 hz
8=1.428 khz
4=2.570 khz
2=4.284 khz
1=6.426 khz
a prescale of 720
1=25 khz
100=495 hz
200=249 hz
300=166 hz
400=125 hz
500=100 hz
a prescale 288
500=250 hz
1000=125 hz
2000=62 hz
3000=41 hz
4000=31 hz
5000=25 hz
*/
// We'll use timer 2
HardwareTimer timer(2);
volatile byte Lar[10];
volatile int tar[]={128,64,32,16,8,4,2,2};
volatile int bam;
void setup() {
// Set up the LED to blink
pinMode(BOARD_LED_PIN, OUTPUT);
pinMode(30, OUTPUT);
pinMode(31, OUTPUT);
pinMode(32, OUTPUT);
pinMode(33, OUTPUT);
pinMode(34, OUTPUT);
pinMode(35, OUTPUT);
pinMode(36, OUTPUT);
pinMode(37, OUTPUT);
//SerialUSB.begin(9600);
// Pause the timer while we're configuring it
timer.pause();
// Set up period
timer.setPrescaleFactor(LED_RATE); // in microseconds
// Set up an interrupt on channel 1
timer.setChannel1Mode(TIMER_OUTPUT_COMPARE);
timer.setCompare(TIMER_CH1, 1); // Interrupt 1 count after each update
timer.setOverflow(1);
timer.attachCompare1Interrupt(handler_led);
// Refresh the timer's count, prescale, and overflow
timer.refresh();
// Start the timer counting
timer.resume();
}
void loop() {
// delay(1000);
SerialUSB.println("START UP");
for(int a=0;a<8;a++)
{
Lar[a]=255; // sets the LED on
delay(50);
SerialUSB.println(" **");
Lar[a]=0; // sets the LED off
delay(50);
SerialUSB.println(" ***");
}
SerialUSB.println("Done Testing LEDs");
for(int a=0;a<255;a+=10)
{
for(int b=0;b<8;b++)
{
Lar[b]=a;
SerialUSB.print(Lar[b]);
SerialUSB.print(" ");
delay(50);
}
SerialUSB.println(" *");
}
for(int a=0;a<8;a++)
{
Lar[a]=0; // sets the LED off
}
delay(100);
}
void handler_led(void) {
digitalWrite(13,LOW);
for(int s=0;s<8;s++) // work along LED columns
{
if(bitRead(Lar[s],bam)==1)
{
digitalWrite(30+s,LOW); // on
//digitalWrite(13,LOW);
}
else
{
digitalWrite(30+s,HIGH); // off
// digitalWrite(13,HIGH);
}
}
if(bam++==7)
{
bam=0;
}
timer.setOverflow(tar[bam]);
timer.refresh();
digitalWrite(13,HIGH);
//toggleLED();
}
I think the frequency is off by a factor of 2 because of toggle() only changes every 2nd pass through the interrupt. I've changed it to digitalWrites to get a more accurate idea. I've hooked my ChronoVu up to pin 13 and the frequency doesn't change from when the ISR exits until it enters it again at about 40us. I did notice that none of the timer keywords are syntax colored in my code, does this indicate a problem? As far as I know bitread is a macro according to the documentation.
Cheers
Mike