Go Down

### Topic: Bit Angle Modulation problem (Read 2704 times)previous topic - next topic

#### Vinter

##### Jul 22, 2013, 07:03 am
Hello everyone,
?t's late and I got a serious headache trying to solve this.
basically I tried to optimize my code a bit by getting rid of a switch statement.
When like this, the whole program works perfectly:
(the led array contains bytes)

Quote

ISR(TIMER1_COMPA_vect) {

if (BAM_Counter == 1){
BAM_Bit++;}
else if (BAM_Counter == 2){
BAM_Bit++;}
else if (BAM_Counter ==4){
BAM_Bit++;}
else if (BAM_Counter ==8){
BAM_Bit++;}
else  if (BAM_Counter ==16){
BAM_Bit = 0;
BAM_Counter = 0;}

BAM_Counter++;

switch (BAM_Bit) {

case 0 :
for (int col =0; col <9; col++) {

PORTD_CATHODES = 0;
PORTB_CATHODES = 0;
ANODE =0;
for (int row =0; row <9; row++) {

if (led[col][row] >> 0 & 1) {
if (row < col && row <5) {
}
else if (row < col && row >= 5) {
}
else if (row >= col && row <4) {
}
else if (row >= col && row >= 5) {
}
}
}

if (col < 5) {
DDRD = (DDRD & 3) | (PORTD_CATHODES & B11111100) | ANODE;
PORTD = ANODE;}
else {
DDRB =  PORTB_CATHODES | ANODE;
PORTB = ANODE;}
}
break;

case 1 :
for (int col =0; col <9; col++) {

PORTD_CATHODES = 0;
PORTB_CATHODES = 0;
ANODE =0;
for (int row =0; row <9; row++) {

if (led[col][row] >> 1 & 1) {
if (row < col && row <5) {
}
else if (row < col && row >= 5) {
}
else if (row >= col && row <4) {
}
else if (row >= col && row >= 5) {
}
}
}

if (col < 5) {
DDRD = (DDRD & 3) | (PORTD_CATHODES & B11111100) | ANODE;
PORTD = ANODE;}
else {
DDRB =  PORTB_CATHODES | ANODE;
PORTB = ANODE;}
}
break;

case 2 :
for (int col =0; col <9; col++) {

PORTD_CATHODES = 0;
PORTB_CATHODES = 0;
ANODE =0;
for (int row =0; row <9; row++) {

if (led[col][row] >> 2 & 1) {
if (row < col && row <5) {
}
else if (row < col && row >= 5) {
}
else if (row >= col && row <4) {
}
else if (row >= col && row >= 5) {
}
}
}

if (col < 5) {
DDRD = (DDRD & 3) | (PORTD_CATHODES & B11111100) | ANODE;
PORTD = ANODE;}
else {
DDRB =  PORTB_CATHODES | ANODE;
PORTB = ANODE;}
}
break;

case 3 :
for (int col =0; col <9; col++) {

PORTD_CATHODES = 0;
PORTB_CATHODES = 0;
ANODE =0;
for (int row =0; row <9; row++) {

if (led[col][row] >> 3 & 1) {
if (row < col && row <5) {
}
else if (row < col && row >= 5) {
}
else if (row >= col && row <4) {
}
else if (row >= col && row >= 5) {
}
}
}

if (col < 5) {
DDRD = (DDRD & 3) | (PORTD_CATHODES & B11111100) | ANODE;
PORTD = ANODE;}
else {
DDRB =  PORTB_CATHODES | ANODE;
PORTB = ANODE;}
}
break;

case 4 :

etc...

but when I change it to
Quote

ISR(TIMER1_COMPA_vect) {

if (BAM_Counter == 1){
BAM_Bit++;}
else if (BAM_Counter == 2){
BAM_Bit++;}
else if (BAM_Counter ==4){
BAM_Bit++;}
else if (BAM_Counter ==8){
BAM_Bit++;}
else  if (BAM_Counter ==16){
BAM_Bit = 0;
BAM_Counter = 0;}

BAM_Counter++;

for (int col =0; col <9; col++) {

PORTD_CATHODES = 0;
PORTB_CATHODES = 0;
ANODE =0;
for (int row =0; row <9; row++) {

if (led[col][row] >> BAM_Bit & 1) {
if (row < col && row <5) {
}
else if (row < col && row >= 5) {
}
else if (row >= col && row <4) {
}
else if (row >= col && row >= 5) {
}
}
}

if (col < 5) {
DDRD = (DDRD & 3) | (PORTD_CATHODES & B11111100) | ANODE;
PORTD = ANODE;}
else {
DDRB =  PORTB_CATHODES | ANODE;
PORTB = ANODE;}
}

}

it doesn't work. Any thoughts?

#### tobyb121

#1
##### Jul 22, 2013, 08:05 am
When you say doesn't work, do you mean it doesn't compile or doesn't do what you expect?

#### Vinter

#2
##### Jul 22, 2013, 09:04 am
It does compile but it doesn't do what I want. What confuses me is that it shouldn't work differently than it's switch version. What may be happening is that the interrupt takes more time to run with the no switch version since it has to pass the BAM_Bit variable first and then swift the bits, leaving no time for anything else to occur. I am calling the interrupt as fast as it goes in the switch version so it's not unlikely if it takes many more cycles to pass the variable data.

#### Nick Gammon

#3
##### Jul 22, 2013, 09:11 am
Call me obsessive, but I find that code hard to read. Please use code tags, and not "copy for forum" in the IDE. Also please do not put the closing "}" on the same line as other code.

Can you post the whole sketch? Maybe something not shown is the reason, for example a missing volatile keyword.
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### Vinter

#4
##### Jul 22, 2013, 09:25 am
I actually intended to post the whole sketch and I commented everything but the forums don't let me post it because of the character limit. It's why I didn't include any commends and I admit the code is hard to read since I'm using it for a charlieplexed 3d led cube while taking advantage of two ports to control the anodes. If you can tell me how to post the whole code I will gladly do so. By the way, reducing the interrupt frequency seems to fix the problem. Also sorry for the brackets closing in the same line, I didn't notice that.

#### AWOL

#5
##### Jul 22, 2013, 09:30 am
Quote
the forums don't let me post it because of the character limit

I'm not aware of any character limit for attachments.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### Nick Gammon

#6
##### Jul 22, 2013, 09:32 am

I actually intended to post the whole sketch and I commented everything but the forums don't let me post it because of the character limit.

How to use this forum

Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### Vinter

#7
##### Jul 22, 2013, 10:04 am
Attached the code.

Go Up

Please enter a valid email to subscribe