Counting from external pin....

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi,

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

Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
#define SW RB7
 char count;
main(){
SW=0;

count=0;
while(1){

if(SW==1){

++count;
PORTB=count;
}
PORTB=count;
}
}
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
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??
 

tshuck

Joined Oct 18, 2012
3,534
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??
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).
 

tshuck

Joined Oct 18, 2012
3,534
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).
 

t06afre

Joined May 11, 2009
5,934
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
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
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
Hi, your logic is not cleared to me....
 

tshuck

Joined Oct 18, 2012
3,534
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?
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
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?
OK, you were talking about using variables, right then its ok......
 

tshuck

Joined Oct 18, 2012
3,534
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.
 
Top