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.

[SOLVED] help for a simple counter

Status
Not open for further replies.

Cyrus the Great

Member level 4
Joined
Oct 30, 2011
Messages
79
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Location
IRAN
Activity points
1,765
hi.i want program my micro for counting by pushing button.i write half of my programming which can count and show the number on 7-segment.i want make it down counter by another button. please help me.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <mega16.h>
 
char digits[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,
0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
unsigned char;
unsigned char p_state;
unsigned char key;
unsigned char i;
 
void main(void)
{
 
    DDRD = 0xFF;
    PORTD = digits[0];
    DDRC = 0x00;
    PORTC = 0xFF;
    
    while(1)
    {
    
        if(!PINC.0)
        {
                if(key!=p_state)
                {
                        if(i==15)
                        {
                                i=0;
                                PORTC=digits[0];
                        }
                        else
                        i++;
                
                        PORTD = digits[i];
                        p_state=key;
                };
        }
        else
            p_state=0xFF;
}

 
Last edited by a moderator:

hi.i want program my micro for counting by pushing button.i write half of my programming which can count and show the number on 7-segment.i want make it down counter by another button. please help me.


Code C - [expand]
1
2
3
#include <mega16.h>
 
char digits[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};


If its a seven segment display, as you have mentioned above, why you have declared and initialized an array containing 16 elements!! You want to display numbers from 0 to 9

For common cathod display use
unsigned char digits[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
 

hi dear papunblg .I want to show 10,11,12,13,14,15, by A,B,C,D,E,F,.I want to know how can I count from 15 to 0 .I mean, I want count 1-2-3-4-5-6-7-8-9-a-b-c-d-e-f by one push button and count f-e-d-c-b-a-9-8-7-6-5-4-3-2-1 by another push button.
regard
pesarirouni
 

Can you describe your interface?
PINC.0 has a button but I don't see any other button in your code so how do you choose to count up and down?

---------- Post added at 18:15 ---------- Previous post was at 18:01 ----------

I would expect a code like


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while(1)
    {
        if(!PINC.0) // count up
        {
            if(i==15) i=0;
            else i++;
            // add some delay here
        }
        else
            if(!PINC.1) // count down
            {
                if(i==0) i=15;
                else i--;
                // add some delay here
            }
    }



you can add the display code
 

dear alexan_e .as i said,i just know how count up.so as you say when i want count down i should use another pin as button.
regard
pesarirouni
 

In my example I have used PINC.1 for a second button (or change it if you want to another pin) so what more do you need to make it work?
 

I do what you say,but unfortunately it doesn't work yet.I Chang my program to thise
Code:
#include <mega16.h>
#include <delay.h>
char digits[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,
0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
unsigned char;
unsigned char p_state;
unsigned char key;
unsigned char i;

void main(void)
{

    DDRD = 0xFF;
    PORTD = digits[0];
    DDRC = 0x00;
    PORTC = 0xFF;
    
    while(1)
    {
    
        if(!PINC.0)
        {
                if(key!=p_state)
                {
                        if(i==0)
                        {
                                i=15;
                                PORTC=digits[0];
                        }
                        else
                        i++;
                
                        PORTD = digits[i];
                        p_state=key;
                        delay_ms(200);
                };
        }
        else
            p_state=0xFF;
        
        if(!PINC.1)
        {
                if(key!=p_state)
                {
                        if(i==0)
                        {
                                i=15;
                                PORTC=digits[0];
                        }
                        else
                        i--;
                
                        PORTD = digits[i];
                        p_state=key;
                };
        }
        else
            p_state=0x00;    
                
           delay_ms(200);
    }
    
}
and now the down counter is work.but up counter doesn't work yet.
 

What is the point of using the key and p_state variables?
And why do you use PORTC=digits[0]?

This should work fine

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <mega16.h>
#include <delay.h>
char digits[16]= {0x3F,0x06,0x5B,0x4F,0x66,0x6D,
                  0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71
                 };
unsigned char;
unsigned char p_state;
unsigned char key;
unsigned char i;
 
void main(void)
{
    DDRD = 0xFF;
    PORTD = digits[0];
    DDRC = 0x00;
    PORTC = 0xFF;
 
    while(1)
    {
        if(!PINC.0)
        {
            if(i==0)
            {
                i=15;
            }
            else
                i++;
 
            PORTD = digits[i];
            delay_ms(200);
        }
        else
            if(!PINC.1)
            {
                if(i==0)
                {
                    i=15;
                }
                else
                    i--;
 
                PORTD = digits[i];
                delay_ms(200);
            }
    }
}

 

my dear friend.i use key & p_state variables for the time that the user push the button and don't release that.by this variable when the user push the button & don't release, the counter doesn't count.and the user should push and release the button to count.
 
Last edited:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <mega16.h>
#include <delay.h>
char digits[16]= {0x3F,0x06,0x5B,0x4F,0x66,0x6D,
                  0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71
                 };
unsigned char button_down;
unsigned char p_state;
unsigned char key;
unsigned char i;
 
void main(void)
{
    DDRD = 0xFF;
    PORTD = digits[0];
    DDRC = 0x00;
    PORTC = 0xFF;
 
    while(1)
    {
        if(!PINC.0)
        {   button_down=1;
 
            if(!button_down) {
                if(i==0)
                {
                    i=15;
                }
                else
                    i++;
            }
 
            PORTD = digits[i];
            delay_ms(200);
        }
        else
            if(!PINC.1)
            {   button_down=1;
 
                if(!button_down) {
                    if(i==0)
                    {
                        i=15;
                    }
                    else
                        i--;
                }
 
                PORTD = digits[i];
                delay_ms(200);
            }
            else
                button_down=0;
    }
}

 

hi.i finally solve my problem.the answer is
Code:
#include <mega16.h>
#include <delay.h>
unsigned char p_state;
unsigned char key;
unsigned char i;

void main(void)
{

    DDRD = 0xFF;
    PORTD = i;
    DDRC = 0x00;                  
    PORTC = 0xFF;
    
    while(1)
    {

        if(!PINC.0)
        {
                if(key!=p_state)
                {
                        if(i==10)
                        {
                                i=0;
                                PORTD=i;
                        }
                        else
                      delay_ms(1000);
                       i++;
                       delay_ms(1000);
                        PORTD=i;
                        p_state=key;
                }
        }
        else
            p_state=0xFF;    
        
        
    if(!PINC.1)
        {
                if(key!=p_state)
                {
                        if(i==0)
                        {
                                i=10;
                                PORTD=i;
                        }
                        else
                      delay_ms(1000);  
                          i--; 
                      delay_ms(1000);     
                        PORTD = i;
                        p_state=key;
                };
        }
        else
            p_state=0xFF;    
        
    }
      }

I use 7448 for my 7 segment and in this case it doesn't need to write
Code:
char digits[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,
0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71}
regard
pesarirouni
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top