7 segment display

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
automat2013
Posts: 2
Joined: Fri Jun 21, 2013 12:29 pm
Contact:

7 segment display

Post by automat2013 »

Code: Select all

Hello.
I need some help with this code : an up down counter on the 7 segment display.
The program count up until digit 9 after that "should" count down until digit 0 and so on.
Unfortunately my program just count up until 9 and start again from 0 to 9.
Could you give me some advices ?
Thank you. 


#include <system.h>

#pragma DATA _CONFIG1, _EXTRC_CLKOUT & _WDT_OFF & _LVP_OFF

#define DISPLAY_SIZE 1

int cnt = 0, led_counter = 0, counter = 0;

unsigned char enable[DISPLAY_SIZE] = {8};

unsigned char patterns[10] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x83, 0xf8, 0x80, 0x98};

void setup_hardware() 
{
    trisb = 0x00;
    trisa = 0xf0;
    ansel = 0x00;
    cmcon = 0x07;
    intcon = 0b10100000; 
    option_reg = 0b00000100;    
}

void interrupt() 
{
    if(intcon & 0x04) 
    {
      clear_bit(intcon, 2);
		counter++;
		if(counter >= 61)
		{
			counter = 0;
			porta = 0; 
			portb = patterns[cnt];
			porta = enable[led_counter];
			led_counter++; 
         cnt++;
			if(led_counter > 0)
			   led_counter = 0;
			if(cnt > 9)
			   cnt = 0;
		}
    }
}    

void main()
{
       setup_hardware();
       while (1)
       {
        
       }
}   

User avatar
Benj
Matrix Staff
Posts: 14835
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

Re: 7 segment display

Post by Benj »

Hello,

I would create a global variable named toggle.

Code: Select all

char toggle = 0;
Then in your interrupt routine instead of

Code: Select all

portb = patterns[cnt]; 
you would have:

Code: Select all

if (toggle)
  portb = patterns[9 - cnt];
else portb = patterns[cnt];
Then down where you have:

Code: Select all

if(cnt > 9)
  cnt = 0;
change this to

Code: Select all

if(cnt > 9)
{  
  cnt = 0;
  toggle = !toggle;
}

Post Reply