counting using 8x7 seg display

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

so I have been playing around with my stk500 again recently. Just editing codes and attempting to write some fresh code of my own.

I have a 8x7 seg display board... http://www.futurlec.com.au/8x7_Segment.jsp
I have it connected to a Mega16 fine and working nicely. I attempted to make it count, multiplying the last number by 2. this worked fine but when it tries to go past 16,384 all the digits mess up and goes nuts :shock:

If someone could explain to me why my code does this I would appreciate it. Thanks

#include 
#include 
// 
//   send value to specified register in MAX7219 
// 

void Send(unsigned char reg, unsigned char value) 
{ 
   // combine register and value into sixteen bits 

   unsigned int data = reg; 
   data <<= 8; 
   data |=value; 

   // lower chip enable 

   PORTA = 0; 

   // send each bit and toggle strobe 
   unsigned int mask = 0x8000; 

   for (; mask; mask>>=1) 
   { 
      unsigned char bit = (data&mask) ? 1 : 0; 
      PORTA = bit; 
      _delay_us(1); 
      PORTA = bit|2; 
      _delay_us(1); 
   } 

   // raise chip enable 

   PORTA = 4; 
} 

int main (void) 
{ 
   // MAX7219 connected to port A lower 3 bits: 
   //      A:0 - data 
   //      A:1 - strobe 
   //      A:2 - chip enable (active low) 

   DDRA = 7; 
   PORTA = 4; 

   // initialize 7219 

   Send(0x0c, 0x00);   // shutdown during setup 
   Send(0x0f, 0x00);   // disable display test 
   Send(0x0b, 0x07);   // 4 digits 
   Send(0x09, 0xFF);   // numeric decode to use digits 7-0 
   Send(0x0a, 0x02);   // minimum intensity 
   Send(0x0c, 0x01);   // enable 

   

   int i = 1; 
   while (1) 
   { 
      if (i>99999999) 
         i = 1; 

      // convert to individual digits 

      unsigned char d0 = i%10; 
      unsigned char d1 = (i/10)%10; 
      unsigned char d2 = (i/100)%10;
      unsigned char d3 = (i/1000)%10;
      unsigned char d4 = (i/10000)%10;
      unsigned char d5 = (i/100000)%10;
      unsigned char d6 = (i/1000000)%10;
      unsigned char d7 = i/1000000; 

      // send each digit 

      
	  Send(0x08, d7);
	  Send(0x07, d6);
	  Send(0x06, d5);
	  Send(0x05, d4);
	  Send(0x04, d3); 
          Send(0x03, d2); 
          Send(0x02, d1); 
          Send(0x01, d0); 

      i=(i*2); 
      _delay_ms(10000); 
   } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I attempted to make it count, multiplying the last number by 2. this worked fine but when it tries to go past 16,384 all the digits mess up and goes nuts

   int i = 1;
   while (1)
   {
      if (i>99999999)
         i = 1;
...
      i=(i*2); 

How many bits has an int?

Stefan Ernst

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

16 bits having values from -32768-32767...and now i have my answer

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

So I substituted the 'int' with a 'long int' and bingo...Thanks sternst