i want to increase the speed of increment and decrement whrrn button is pressed. Plz help me. this code is for atmega32.

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

/*
 * 7seg count.c
 *
 * Created: 08-02-2019 10:19:22
 * Author : Rakeshkumar
 */ 

#include <avr/io.h>  //header to enable data flow control over pins
#define F_CPU 16000000   //telling controller crystal frequency attached
#include <util/delay.h> //header to enable delay function in program

int main(void)
{

    DDRB = 0x00; // Taking portB as input port

    DDRA = 0xff; //taking porta as output port

    DDRD = 0b11111111; //taking portd as output

    int DISPLAY1 [10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67};

    //character display values from 0-9

    int c = 0;
    int one = 0; //memory for storing ones place value
    int ten =0; //memory for storing tens place value
    int hun = 0;
    int temp = 0;
    int PRESSED = 0;
    int PRESSED_CONFIDENTIAL_LEVEL = 0;
    //int RELEASED_CONFIDENTIAL_LEVEL =0;

    while(1)
    {
        if(bit_is_clear(PINB,0)) //when button one  is pressed
        {
            PRESSED_CONFIDENTIAL_LEVEL++;
            if(PRESSED_CONFIDENTIAL_LEVEL > 80)
            {
                if(PRESSED == 0 && c >= 0 && c < 999)
                {
                    c++;

                }
                PRESSED_CONFIDENTIAL_LEVEL = 0;
            }
        }

     if(bit_is_clear(PINB,1)) //when button one  is pressed
        {
            PRESSED_CONFIDENTIAL_LEVEL++;
            if(PRESSED_CONFIDENTIAL_LEVEL > 80)
            {
                if(PRESSED == 0 && c <= 999 && c > 0)
                {
                    c--;
                }
                PRESSED_CONFIDENTIAL_LEVEL = 0;
            }
        }

        hun = c/100;
        temp = c%100;
        ten = temp/10;
        one = temp%10;

        PORTD &=~(1<<PIND6);
        PORTA = DISPLAY1[one];
        _delay_ms(1);
        PORTD |=(1<<PIND6); 

        PORTD &=~(1<<PIND5);
        PORTA = DISPLAY1[ten];
        _delay_ms(1);
        PORTD |=(1<<PIND5);

        PORTD &= ~(1<<PIND4);
        PORTA = DISPLAY1[hun];
        _delay_ms(1);
        PORTD |= (1<<PIND4);       

    }
}
Last Edited: Wed. Feb 13, 2019 - 11:12 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the <> button in the post editor is still really useful for code.

 

also not visible here: any explanation of what help you need. does your code currently work? does it not work? does it do something you don't expect?

 

 

There's a lot of things in this code that seem weird, but the most obvious thing is that you appear to be checking whether two different bits in PINB are "clear", rather than checking a single bit for whether or not it's set.

 

Also, you're gonna have problems if c gets over 999, which it could do in the code as written -- if c is 998 when you enter the first loop, but PRESSED_CONFIDENTIAL_LEVEL is 400, it'll go to 1000, and then you'll be reading past the end of an array, which you should not do.

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

Sir, I am new in coding.  This code is working Properly. actually i want to increase the speed of increment if button is pressed and hold but i am not getting any clue how to increase the increment speed if button is pressed for long time.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
        if(bit_is_clear(PINB,0)) //when button one  is pressed
        {
            PRESSED_CONFIDENTIAL_LEVEL++;
            if(PRESSED_CONFIDENTIAL_LEVEL > 80)
            {
                if(PRESSED == 0 && c >=0 && c < 999)
                {
                    c++;
                    if(PRESSED_CONFIDENTIAL_LEVEL > 400)
                    {
                        ++c;

                    }

                }
                PRESSED_CONFIDENTIAL_LEVEL = 0;
            }
        }

     if(bit_is_clear(PINB,1)) //when button one  is pressed
        {
            PRESSED_CONFIDENTIAL_LEVEL++;
            if(PRESSED_CONFIDENTIAL_LEVEL > 80)
            {
                if(PRESSED == 0 && c <= 999 && c > 0)
                {
                    c++;
                    if(PRESSED_CONFIDENTIAL_LEVEL > 400)
                    {
                        ++c;

                    }

                }
                PRESSED_CONFIDENTIAL_LEVEL = 0;
            }
        }

This code is clearly just "cut and paste". You have duplicated the same block and only changed PINB,0 to PINB,1 so a few things:

 

1) at least change the comment. They can't BOTH be handling "button one" !!

 

2) more seriously you just re-use c. Surely c is an ongoing counter as the while(1) loop runs that allows you to sample the button state multiple times until you are confident it is pushed. Well you can't use the same c for both the PINB,0 and PINB,1 cases !

 

3) same true for PRESSED_CONFIDENTIAL_LEVEL (and isn't it "CONFIDENCE" not "CONFIDENTIAL"??)

 

4) also do NOT use all upper case for C variable names. Reserve the use of all upper case for pre-processor defined macros. Just call the variable pressed_confidence_level_button_0 or something.

Last Edited: Wed. Feb 13, 2019 - 09:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
Sir, here is my code i used two buttons. One for increment and one foe decrement. i also got output properly.
but i want to increase the speed of increment or decrement when i press and hold button. i m not getting any clue can you please tell me how to do.





#include <avr/io.h>  //header to enable data flow control over pins
#define F_CPU 16000000   //telling controller crystal frequency attached
#include <util/delay.h> //header to enable delay function in program

int main(void)
{

    DDRB = 0x00; // Taking portB as input port

    DDRA = 0xff; //taking porta as output port

    DDRD = 0b11111111; //taking portd as output

    int DISPLAY1 [10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67};

    //character display values from 0-9

    int c = 0;
    int one = 0; //memory for storing ones place value
    int ten =0; //memory for storing tens place value
    int hun = 0;
    int temp = 0;
    int PRESSED = 0;
    int PRESSED_CONFIDENTIAL_LEVEL = 0;
    //int RELEASED_CONFIDENTIAL_LEVEL =0;

    while(1)
    {
        if(bit_is_clear(PINB,0)) //when button one  is pressed
        {
            PRESSED_CONFIDENTIAL_LEVEL++;
            if(PRESSED_CONFIDENTIAL_LEVEL > 80)
            {
                if(PRESSED == 0 && c >= 0 && c < 999)
                {
                    c++;

                }
                PRESSED_CONFIDENTIAL_LEVEL = 0;
            }
        }


     if(bit_is_clear(PINB,1)) //when button one  is pressed
        {
            PRESSED_CONFIDENTIAL_LEVEL++;
            if(PRESSED_CONFIDENTIAL_LEVEL > 80)
            {
                if(PRESSED == 0 && c <= 999 && c > 0)
                {
                    c--;
                }
                PRESSED_CONFIDENTIAL_LEVEL = 0;
            }
        }

        hun = c/100;
        temp = c%100;
        ten = temp/10;
        one = temp%10;

        PORTD &=~(1<<PIND6); 
        PORTA = DISPLAY1[one]; 
        _delay_ms(1); 
        PORTD |=(1<<PIND6); 


        PORTD &=~(1<<PIND5);
        PORTA = DISPLAY1[ten];
        _delay_ms(1);
        PORTD |=(1<<PIND5);


        PORTD &= ~(1<<PIND4);
        PORTA = DISPLAY1[hun];
        _delay_ms(1);
        PORTD |= (1<<PIND4);       

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

Oh I see "c" (bad name for a variable) is actually some kind of "display counter" and you want the buttons to either increment it or decrement it?

 

But you are still using PRESSED_CONFIDENTIAL_LEVEL for both buttons. Surely you need a counter for each.

 

Also the rate at which c increments or decrements seems to be dependent on when that confidence counter exceeds 80. Surely if you want faster increment/decrement you reduce the "> 80" limit ?

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

Sir 80  is used for key debounce if i will reduce it than key debounce will occur.  when i press  and hold the button value increments but speed of increment or decrements is very less. i want to increase the speed.

 

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

Well you can't have your cake and eat it!

 

Either you need a very long debounce period so you can't get that many presses or you want a fast update rate (in which case you need buttons that bounce less)

 

In the code that follows the button checking (apart from the other things it does) you have three calls to _delay_ms(1) so one loop round the while(1) is a little over 3ms. If you are counting to 80 to register a button press that suggests a settling period of about 240ms. So the best you can hope for is 4 or 5 counter updates per second.

 

240ms(+) does seem a VERY long time for button debounce. Most bouncing happens well within 50ms or less. So I'd say your debounce loops could be 4 or 5 times shorter so replace 80 with 20 or even 16 and it should still debounce.

 

Another approach is to take the button handling out of the software delayed while(1) loop all together. Instead run a ticker interrupt at something like 5ms or 10ms and consider buttons "Stable" after 5..10 interrupts where they are seen to have the same state.

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

sir 80 is got by trial and error. when i put 70 i got some debounce. sir can u guide me how to increment or decrement a value  after every 1ms when button is press and hold. 

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

As I say bounce normally settles in 10's of ms. Very unusual to be 240ms as your 3 * 80 would suggest.

 

One possibility is to use external components (R & C) to debounce the button so software doesn't have to if you want a "!"quick" resolution.

 

Otherwise look at timer based horizontal or vertical counters for an interrupt based debounce solution.

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

when you change c you should set a flag, (that get's cleared when not pressed).

 

if that flag is set (you hold the key down longer than  debounce time) , you should compare against a number less than 80 (something like 20).

 

Or if set add 10 to c each time

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

Also I was just looking at your code and don't see anything about enabling the pull-ups on PB0/PB1. So what is being done in the circuit with those signals when the buttons are inactive and open-circuit? Please don't say they just float! (that sure could look a lot like unexpected bouncing!)