Counting from external pin....

Discussion in 'Programmer's Corner' started by RRITESH KAKKAR, Apr 9, 2013.

  1. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi,

    I was testing this code for counting but the variable count is not putting its value to PORTB, why??

    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    3. #define _XTAL_FREQ 20000000
    4. #define SW RB7
    5.  char count;
    6. main(){
    7. SW=0;
    8.  
    9. count=0;
    10. while(1){
    11.  
    12. if(SW==1){
    13.  
    14. ++count;
    15. PORTB=count;
    16. }
    17. PORTB=count;
    18. }
    19. }
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    What about the TRISB register setting
     
  3. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    OK, that mean it will work!!
     
  4. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Its working then what the use of using Timer as counter if we can do like this???
    and how to convert this binary to decimal??
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    This method blocks the processor from doing other things and will miss any transitions occurring when not polling the port (during processing/incrementing the count).
     
  6. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Then how to use counter for more than 3 like this??
     
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You mean to count 3 different pins and assuming you are still using the 16F877A? You don't. At least not directly. You could use the interrupt on change and compare against a register representing the previous states and increment individual counters for each input. Or, if you get a device that has multiple timer/counters, you could use them the same way, each with its own associated timer (counter).
     
  8. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Example.........??
     
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Just read a group of bits from PORTB. Then sort out the counting afterhand. If we assume that only one button can be pressed at the same time and 3 counters, and the idle setting of PORTB xxxxx111 then no button pressed. If PORTB reads 110 increase counter number one. If the PORTB read xxxxx101 counter two and so on
     
  10. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi, your logic is not cleared to me....
     
  11. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I think you need to learn the basics before moving to another controller. :)
     
  12. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Save the state of the port you are using with the buttons, then, whenever there is a difference, increment a counter associated with that button.
     
  13. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Not getting you again.....
     
  14. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    If you have your inputs pulled up, the default state is 1 on the inputs. Whenever these inputs go low, you know that a button was pressed, right? Then increment the corresponding counter. What part of that is confusing, that way we can address the more serious issue?
     
  15. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    OK, you were talking about using variables, right then its ok......
     
  16. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    If the inputs will default to a known state without a button press, you need not save the port state to detect a button press, as any change of the state of the port is a button press. So the only variables would be associated with the count of the individual pins.
     
Loading...