Go Down

Topic: Bit Angle Modulation problem (Read 1 time) previous topic - next topic

Vinter

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) {
           ANODE = pgm_read_byte(&(PIN[col]));
           if (row < col && row <5) {
             PORTD_CATHODES = PORTD_CATHODES | pgm_read_byte(&(PIN[row]));
           }
           else if (row < col && row >= 5) {
             PORTB_CATHODES = PORTB_CATHODES | pgm_read_byte(&(PIN[row]));
           }
           else if (row >= col && row <4) {
             PORTD_CATHODES = PORTD_CATHODES | pgm_read_byte(&(PIN[row+1]));
           }
           else if (row >= col && row >= 5) {
             PORTB_CATHODES = PORTB_CATHODES | pgm_read_byte(&(PIN[row+1]));
           }
         }
       }
      
       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) {
           ANODE = pgm_read_byte(&(PIN[col]));
           if (row < col && row <5) {
             PORTD_CATHODES = PORTD_CATHODES | pgm_read_byte(&(PIN[row]));
           }
           else if (row < col && row >= 5) {
             PORTB_CATHODES = PORTB_CATHODES | pgm_read_byte(&(PIN[row]));
           }
           else if (row >= col && row <4) {
             PORTD_CATHODES = PORTD_CATHODES | pgm_read_byte(&(PIN[row+1]));
           }
           else if (row >= col && row >= 5) {
             PORTB_CATHODES = PORTB_CATHODES | pgm_read_byte(&(PIN[row+1]));
           }
         }
       }
   
       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) {
           ANODE = pgm_read_byte(&(PIN[col]));
           if (row < col && row <5) {
             PORTD_CATHODES = PORTD_CATHODES | pgm_read_byte(&(PIN[row]));
           }
           else if (row < col && row >= 5) {
             PORTB_CATHODES = PORTB_CATHODES | pgm_read_byte(&(PIN[row]));
           }
           else if (row >= col && row <4) {
             PORTD_CATHODES = PORTD_CATHODES | pgm_read_byte(&(PIN[row+1]));
           }
           else if (row >= col && row >= 5) {
             PORTB_CATHODES = PORTB_CATHODES | pgm_read_byte(&(PIN[row+1]));
           }
         }
       }
   
       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) {
           ANODE = pgm_read_byte(&(PIN[col]));
           if (row < col && row <5) {
             PORTD_CATHODES = PORTD_CATHODES | pgm_read_byte(&(PIN[row]));
           }
           else if (row < col && row >= 5) {
             PORTB_CATHODES = PORTB_CATHODES | pgm_read_byte(&(PIN[row]));
           }
           else if (row >= col && row <4) {
             PORTD_CATHODES = PORTD_CATHODES | pgm_read_byte(&(PIN[row+1]));
           }
           else if (row >= col && row >= 5) {
             PORTB_CATHODES = PORTB_CATHODES | pgm_read_byte(&(PIN[row+1]));
           }
         }
       }
   
       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) {
           ANODE = pgm_read_byte(&(PIN[col]));
           if (row < col && row <5) {
             PORTD_CATHODES = PORTD_CATHODES | pgm_read_byte(&(PIN[row]));
           }
           else if (row < col && row >= 5) {
             PORTB_CATHODES = PORTB_CATHODES | pgm_read_byte(&(PIN[row]));
           }
           else if (row >= col && row <4) {
             PORTD_CATHODES = PORTD_CATHODES | pgm_read_byte(&(PIN[row+1]));
           }
           else if (row >= col && row >= 5) {
             PORTB_CATHODES = PORTB_CATHODES | pgm_read_byte(&(PIN[row+1]));
           }
         }
       }
     
       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?
Thanks in advance.

tobyb121

When you say doesn't work, do you mean it doesn't compile or doesn't do what you expect?

Vinter

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

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!

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

Vinter

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

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


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!

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

Vinter


Go Up