Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

7 segment display - counts from 0 to 9999

Status
Not open for further replies.

mircea2012

Newbie level 4
Newbie level 4
Joined
Jan 2, 2013
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,321
Hello.
I have this program on the internet.
Count from 0 to 9999, using a 7 segment display.
My question is why is need a for loop ?

Code:
#include <system.h>
#pragma DATA _CONFIG1, _HS_OSC & _WDT_OFF & _LVP_OFF

unsigned i, Digit_0, Digit_1, Digit_2, Digit_3; 
unsigned int Count, counter; 

unsigned mask(unsigned num) 
{ 
      switch(num) 
      { 
	    case 0 : return 0xC0; 
	    case 1 : return 0xF9; 
	    case 2 : return 0xA4; 
	    case 3 : return 0xB0; 
	    case 4 : return 0x99; 
	    case 5 : return 0x92; 
	    case 6 : return 0x82; 
	    case 7 : return 0xF8; 
            case 8 : return 0x80; 
	    case 9 : return 0x90; 
      } 
}

void interrupt() 
{
    if(intcon & 4) 
    {
        clear_bit(intcon, 2);
	counter++;
	if(counter > 30)
	{
	   counter = 0;
        }
    }
} 

void main() 
{ 
     trisb = 0x00;
     trisa = 0xf0;
     intcon = 0b10100000; 
     option_reg = 0b00000100;
     Count = 0; 
     do 
     { 
	  Digit_0 = Count%10; 
	  Digit_0 = mask(Digit_0); 
	  Digit_1 = (Count/10)%10; 
	  Digit_1 = mask(Digit_1); 
	  Digit_2 = (Count/100)%10; 
	  Digit_2 = mask(Digit_2); 
	  Digit_3 = (Count/1000); 
	  Digit_3 = mask(Digit_3); 
	  for(i = 0; i<=10000; i++) 
	  { 
	 	portb = Digit_0; 
	 	porta = 0x08;
		interrupt();  
		portb = Digit_1; 
		porta = 0x04;  
		interrupt();
		portb = Digit_2; 
		porta = 0x02;
		interrupt(); 
		portb = Digit_3; 
		porta = 0x01;
		interrupt(); 
	   } 
	   Count++; 
	   if(Count > 9999) 
	   Count = 0; 
      } 
      while(1); 
}
 

I only lights one display digit at a time. In the loop it moves from one to the next so they switch fast enough that your eyes think they are continuously on. With the loop broken it will only scan once, you would see the first digit light then go out, the second light then go out, the third light and go out and the last digit would be left lit with the digit stuck on it.

The way the loops are organized and polling the interrupt like that isn't an example of good programming though!

Brian.
 

Hi,

Your question is why is need a for loop ? is not clear.

The code is meant to do two basic task increment count from 0 - 9999 in the part of code towards the bottom "Count++"
and go back to 0 when count reaches 9999 in the part of code towards the bottom
if(Count > 9999)
Count = 0;

If you wish to have an action occur like the press of a button or a sensor simply change that line to an If condition
and if you do not counter to loop simply comment or remove the two code lines above.

Let me know if your question is answered.
Cheers
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top