Pages: [1]   Go Down
Author Topic: Bit Angle Modulation problem  (Read 1088 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
Listen to the wind singing in the grass my love, as you lay yourself down to rest. Listen to the cry of the river in autumn cold, my dear.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
Listen to the wind singing in the grass my love, as you lay yourself down to rest. Listen to the cry of the river in autumn cold, my dear.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
Listen to the wind singing in the grass my love, as you lay yourself down to rest. Listen to the cry of the river in autumn cold, my dear.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the forums don't let me post it because of the character limit

I'm not aware of any character limit for attachments.
Logged

"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.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
Listen to the wind singing in the grass my love, as you lay yourself down to rest. Listen to the cry of the river in autumn cold, my dear.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Attached the code.

* Bit_Angle_Modulation_Led_Cube.ino (9.76 KB - downloaded 25 times.)
* Bit_Angle_Modulation_Led_Cube_Version_2.ino (5.41 KB - downloaded 20 times.)
Logged

Pages: [1]   Go Up
Jump to: