Debugging in CVAvr

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey, so this is my code, I'm trying to make blocks appear row by row into an 8 x 8 LED matrix where the shift register controls the grounding of cathodes and the PORTB.2 controls a clock pulse that iterates over the column using a decade counter. So everything about the matrix, including the code I had was working just fine. But I tried to clean up the code a little (make it tidier). I think I might have changed something but have no idea how to debug it. 

Any Ideas? If I can view the flow control or variable outputs, that too would be very helpful, as I am quite used to coding and debugging on normal programming languages.

 

#include <mega8.h>
#include <delay.h>
#include <stdint.h>
#include <iobits.h>  
#include <stdlib.h>
#define sclock PORTB.1    
#define slatch PORTB.2
#define sdata PORTB.0

uint8_t f,vc=0,a,b,lm=0,rm=0,o=1, j,d[]={0,0,0,0,0,0,0,0},s[8]={0},t[8]={240,148,254,202,27,14,4,0},v[4]={0}; uint16_t i=0;

void newb(uint8_t o)
{
    switch(o)
    {
        case 1:v[1]= (t[2]&0b11000000)>>4;          //o
                v[2]=(t[3]&0b11000000)>>4;
        break;
        case 2:v[1]= (t[2]&0b00011110)>>1;             //i
        break;
        case 3:v[0]=(t[2]&0b00000010)>>1;
                v[1]=(t[3]&0b00000010)>>1;
                v[2]=(t[4]&0b00000010)>>1 ;
                v[3]=(t[5]&0b00000010)>>1  ;
        break;
        case 4: v[0]=(t[1]&0b00000110)>>1;                //s
                v[1]=(t[2]&0b00000110)>>1;
                v[2]=(t[3]&0b00000110)>>1 ;
        break; 
        case 5: v[1]=(t[4]&0b00000111);
                v[2]=(t[5]&0b00000111); 

        break;
        case 6: v[0]=(t[4]&0b00000110)>>1;              //z
                v[1]=(t[5]&0b00000110)>>1;
                v[2]=(t[6]&0b00000110)>>1;

        break; 
        case 7: v[1]=(t[4]&0b00011100)>>2;
                v[2]=(t[5]&0b00011100)>>2;
        break;
        case 8: v[0]=(t[3]&0b00000110)>>1;              //j
                v[1]=(t[4]&0b00000110)>>1;
                v[2]=(t[5]&0b00000110)>>1 ;

        break;
        case 9: v[1]=(t[1]&0b11100000)>>5;
                v[2]=(t[2]&0b11100000)>>5;

        break; 
        case 10: v[0]=(t[2]&0b00001100)>>2;              
                 v[1]=(t[3]&0b00001100)>>2;
                 v[2]=(t[4]&0b00001100)>>2; 
        break;
        case 11: v[1]=(t[0]&0b01110000)>>5;
                 v[2]=(t[1]&0b01110000)>>5; 
        break;  
        case 12: v[0]=(t[2]&0b00000110)>>1;              //l
                v[1]=(t[3]&0b00000110)>>1;
                v[2]=(t[4]&0b00000110)>>1 ;

        break;
        case 13: v[1]=(t[1]&0b01110000)>>5;
                v[2]=(t[2]&0b01110000)>>5;

        break; 
        case 14: v[0]=(t[3]&0b00001100)>>2;              
                 v[1]=(t[4]&0b00001100)>>2;
                 v[2]=(t[5]&0b00001100)>>2; 
        break;
        case 15: v[1]=(t[0]&0b11100000)>>5;
                 v[2]=(t[1]&0b11100000)>>5; 
        break; 
        case 16: v[0]=(t[2]&0b00000110)>>1;              //t
                v[1]=(t[3]&0b00000110)>>1;
                v[2]=(t[4]&0b00000110)>>1 ;

        break;
        case 17: v[1]=(t[1]&0b01110000)>>5;
                v[2]=(t[2]&0b01110000)>>5;

        break; 
        case 18: v[0]=(t[3]&0b00001100)>>2;              
                 v[1]=(t[4]&0b00001100)>>2;
                 v[2]=(t[5]&0b00001100)>>2; 
        break;
        case 19: v[1]=(t[0]&0b11100000)>>5;
                 v[2]=(t[1]&0b11100000)>>5; 
        break; 
        
    
    
    }
}
void vtransfer()
{
      for(f=0;f<8;f++)
            d[f]=d[f]<<1;                         //shifts down
      for(f=0;f<4;f++)
      {     
                    
            if(v[f])
            if(v[f]&8) d[f+2]=d[f+2]|1;             //shifts v[f] to d[f]
            v[f]<<1;
            v[f]=v[f]&15;
      }
     
}
void lmov()
{  
    if(!d[0])
        {
            for(f=0;f<7;f++)
                d[f]=d[f+1];
            d[7]=0;
        } 
        
}
void rmov()
{
    if(!d[7])
        {
            for(f=7;f>0;f--)
                d[f]=d[f-1];
            d[0]=0;  
        }
}
void dmov()
{
    uint8_t c=0;
    for(f=0,c=1;f<8;f++)                              //condition for empty array
        if(d[f])
            c=0;
    for(f=0;f<8;f++)
        if(d[f]&128)                                // condition for bottom
            c++;  
    for(f=0;f<8;f++)
        if((d[f]<<1)&s[f])                        //condition for collision
            c++;
    if(!c)
        for(f=0;f<8;f++)
            {
                vtransfer();
            }    
    
    else
    {
        for(f=0;f<8;f++)
        {
            s[f]=d[f]|s[f];
            d[f]=0;
        }
        newb(1);
        vtransfer();  
        
    }     
   
}

void dshift(uint8_t data)
{  uint8_t i=0;
    data=~data;
   for(i=0;i<8;i++)
   {
      
      if(data & 0b10000000)
      {
         sdata=1;
      }
      else
      {
         
         sdata=0;
      }

      sclock=1;sclock=0;
      data=data<<1;  //Now bring next bit at MSB position

   }
   
   
   slatch=1;slatch=0;
   delay_us(5);
}
 
void main(void)
{
    

// Input/Output Ports initialization
// Port B initialization
// Function: Bit7=Out Bit6=Out Bit5=Out Bit4=Out Bit3=Out Bit2=Out Bit1=Out Bit0=Out 
DDRB=(1<<DDB7) | (1<<DDB6) | (1<<DDB5) | (0<<DDB4) | (1<<DDB3) | (1<<DDB2) | (1<<DDB1) | (1<<DDB0);
// State: Bit7=0 Bit6=0 Bit5=0 Bit4=0 Bit3=0 Bit2=0 Bit1=0 Bit0=0 
PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);

// Port C initialization
// Function: Bit6=Out Bit5=Out Bit4=Out Bit3=Out Bit2=Out Bit1=Out Bit0=Out 
DDRC=(1<<DDC6) | (1<<DDC5) | (1<<DDC4) | (1<<DDC3) | (1<<DDC2) | (1<<DDC1) | (1<<DDC0);
// State: Bit6=0 Bit5=0 Bit4=0 Bit3=0 Bit2=0 Bit1=0 Bit0=0 
PORTC=(0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);

// Port D initialization
// Function: Bit7=Out Bit6=Out Bit5=Out Bit4=Out Bit3=Out Bit2=Out Bit1=Out Bit0=Out 
DDRD=(1<<DDD7) | (1<<DDD6) | (1<<DDD5) | (1<<DDD4) | (1<<DDD3) | (1<<DDD2) | (1<<DDD1) | (1<<DDD0);
// State: Bit7=0 Bit6=0 Bit5=0 Bit4=0 Bit3=0 Bit2=0 Bit1=0 Bit0=0 
PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);


while (1)
      {  
         dshift(d[0]);
         for(i=0;i<25600;i++)
         {  
                  
            dshift(0);
            PORTB.3=1; 
            PORTB.3=0;
            if((i/8)%2)  
                dshift(d[(i+1)%8]);
            else
                dshift(s[(i+1)%8]);
             if(PINB.4==1 && lm==0)
            {
                lm++;
                lmov(); 
            
            }   
             if(PINB.5==1 && rm==0)
             {
                rm++;
                rmov(); 
            
            } 
            if(PINB.5==0)rm=0;
            if(PINB.4==0)lm=0;   
            delay_us(1);
         }  
        delay_ms(40);
        if(d[4]==1)
            PORTD.7=1;
        else
            PORTD.7=0; 
        dmov(); 
        
        }
      
}

 

Last Edited: Fri. Feb 22, 2019 - 08:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My advice would be to rewrite everything using

long descriptive names for all of your variables.

We've no idea what any of: f,vc,a,b,lm,rm,o,j,d,

s,t,v, or i are supposed to represent.

 

Then do the same for passed-in arguments and

functions.  You will be better able to understand

the code you wrote too and perhaps you will find

the problem yourself!  If not, then we'll at least

be in a better position to help.

--Mike

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

but have no idea how to debug it. 

You need a debugger ie Atmel ICE and Atmel Studio installed, all going well CV will invoke Studio to do the debugging with the ICE.

 

I haven't used the combination for a few years now but worked very well before.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Follow avr-mike's advice in #2.    i.e. use sensible names for your variables.   Add explanatory comments.

 

The ATmega8 does not have any hardware debug.   Your only choice is the AS7 Simulator.

 

When you have tidied up your code,   please post again.

Debugging CV code is very pleasant.     Far better than GCC.

 

There are several CV users on this forum.    When your code is "readable",   we can help you with CV and with AS7.

 

David.