Problem with Arduino, or any software layer between your code and the hardware

The Problem:
So I’ve been working on getting the LED’s to change on the interrupt for timer1b, since I have the audio on timer1a. I first created a non interrupt version, which worked fine.  I then ported that into my code, and changed it into an interrupt version.  And it works fine too, for the first 8.4 seconds, then it freezes.  Changing the rate at which the LED’s flash doesn’t change anything, it is always at 8.4 seconds.

I then bring the interrupt code to a fresh project, hey, maybe something in my project is messing it up (having smaller code means quicker iterations).  Same problem exists there.  I change it to timer 1a, and poof, it works.  Now I must hunt for what is resetting timer 1b, or verify that I set it up right. :/

Code:

#define LATCHPIN 9
#define CLOCKPIN 8
#define DATAPIN 10

#define RACE 1
#define FLASHALL 2
#define COUNT 3
#define KNIGHTRIDER 4

#define LEDUPDATE 12
#define ONEMS   8
#define DELAYTIME  2  //4 ms
#define PWMRATE 4

void changeled(void);
uint16_t dataLED;
uint8_t ledmode;
uint8_t dataFIRST;
uint8_t dataSECOND;
uint8_t pwmrate;
uint8_t updatespeed;
uint8_t dataLEDdir = 0;
int i = 0;

void timer_1a_init(void)
{
//TCCR1A Not needed since default is no output,
cli();
TCCR1A = 0;
TCCR1B = 0;

//setup so divide by 1024
TCCR1B |= (1 << WGM12) | (1 << CS12) |  (1 << CS10);
sei();

}

void timer_1b_init(void)
{
// a 0.126 ms clock, *8 for 1 ms
TIMSK1 = (1 << OCIE1A);
//ledmode = RACE;
dataLED = 1;
//pinMode(LATCHPIN, OUTPUT);
//pinMode(CLOCKPIN, OUTPUT);
//pinMode(DATAPIN, OUTPUT);
ledmode = COUNT;
OCR1A = TCNT1 + 100; //for 8MHz proc
}

void changeled(void)
{
//if in this function, change will occur
if(ledmode == RACE)
{
//use dataLED
//in this mode, 2 LEDS are on, spaced about 3 or 4 apart
dataLED= dataLED << 1;
if(dataLED == 0)
dataLED = 1;
if (dataLED == 0x08)
dataLED += 1;  //add second light 😛
}
if(ledmode == FLASHALL)
{
if(dataLED == 0)
dataLED = 0xFFFF;
else
dataLED = 0;
}
if(ledmode == COUNT)
{
dataLED += 1;
}

//set values
dataFIRST = (dataLED >> 8);  //write first, shows second
dataSECOND = dataLED;  //write second, shows first

}

ISR(TIMER1_COMPA_vect)
{
//simplest interrupt possible
/* int intagain;
if(updatespeed– == 0)
{
//changeled();

updatespeed = LEDUPDATE;
} */
/*
digitalWrite(LATCHPIN, 0);
shiftOut(DATAPIN, CLOCKPIN,MSBFIRST, dataFIRST);
shiftOut(DATAPIN, CLOCKPIN,MSBFIRST, dataSECOND);
digitalWrite(LATCHPIN, 1);
OCR1B = TCNT1 + ONEMS*DELAYTIME; //1 ms
*/
dataLED++;
digitalWrite(LATCHPIN, 0);
shiftOut(DATAPIN, CLOCKPIN,MSBFIRST, (dataLED >> 8));
shiftOut(DATAPIN, CLOCKPIN,MSBFIRST, dataLED);
digitalWrite(LATCHPIN, 1);
pwmrate = 1;
OCR1A = TCNT1 + 8; //1 ms

}

void setup()
{
timer_1a_init();
timer_1b_init();

pinMode(LATCHPIN, OUTPUT);
pinMode(CLOCKPIN, OUTPUT);
pinMode(DATAPIN, OUTPUT);
ledmode = COUNT;
}

void loop()
{
/*
delay(5000);
if(i == 0)
{
digitalWrite(LATCHPIN, 0);
shiftOut(DATAPIN, CLOCKPIN,MSBFIRST, 0xEE);
shiftOut(DATAPIN, CLOCKPIN,MSBFIRST, 0xEE);
digitalWrite(LATCHPIN, 1);
i++;
}*/
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s