CCS C pulse counting

Thread Starter

raffter

Joined Feb 28, 2008
113
I will use 16F84 chip... this has no ADC or comparator built-in.... I want to know HOW it will read low freq "pulses"... I have read about using the RB0 as input(to read freq).. is there a simpler way?? I mean I wish NOT to use this ext_int but would like other port/s for input... I hope im clear..freq is a few Hz.. then value will be assinged as an integer...
 
Last edited:

mik3

Joined Feb 4, 2008
4,843
Its not necessary to use an interrupt. You can use any digital input and when your signal is high on this input then you will count a pulse and so on. See the code below for example


while (true)
{

if (input_state(pin_A5)
{
count++;
}

}

I use a general while state to be always true as the program wont end unless the power is off. Then i use the if state and if it is true (high input signal on the input) then it increments 'count' by one, thus it counts pulses.

The name of the pin i used in the if state is arbitrary, you can use whatever pin you like. Also, you have to define 'count' as an integer or long or long long according to how many pulses you need to count.

define 'count' like this for example:

long count=0;

you have to put it equal zero to be sure it will start count from zero
 

Thread Starter

raffter

Joined Feb 28, 2008
113
ok mike...

thanks :)

I will try that one out..

another question..what STATE(logic 1 or 0) will it count?? is "pin_a5" pulled to Vdd(w/resistor)?

EDIT:

ooopppsss it "triggers" on HIGH... :)
 

mik3

Joined Feb 4, 2008
4,843
ok mike...

thanks :)

I will try that one out..

another question..what STATE(logic 1 or 0) will it count?? is "pin_a5" pulled to Vdd(w/resistor)?

EDIT:

ooopppsss it "triggers" on HIGH... :)
It will count at logic 1. If you put if (!input_state(pin_a5) then it will count at logic 0
 

Thread Starter

raffter

Joined Feb 28, 2008
113
mik3,

here is my code ...
============================
void sense()
{
nokia_gotoxy(8,1);
printf(nokia_printchar," TEST ");

while(true)
{
long int a;

if(input(pin_a0))
{

nokia_gotoxy(1,3);
printf(nokia_printchar," dec: %ld",a);
delay_ms(25);
a++;

}
}
}
========================

I have added delay(debounce)...

how do I make this a "simple frequency counter" ? :)

EDIT:

I was thinking "counting" the pulses in 100mS(then multiply by 10) so I get updated every 100mS...

EDIT2:

TOCK/RA4??
 
Last edited:

mik3

Joined Feb 4, 2008
4,843
I think a more accurate solution is to start counting the time just before to start counting the pulses. Then count ten pulses and just after you finish counting the pulses stop the time counting. Finally, find this formula by this equation:

F=10/timecounted
 

Thread Starter

raffter

Joined Feb 28, 2008
113
ok mik3,

I will take note of that...

btw, I found this ASM code on the net.. a simple tacho

-----------------------------
DEVICE 16F84
DECLARE XTAL 4
DECLARE LCD_TYPE 0
DECLARE LCD_DTPIN PORTB.4
DECLARE LCD_ENPIN PORTB.3
DECLARE LCD_RSPIN PORTB.2
DECLARE LCD_INTERFACE 4
DECLARE LCD_LINES 1
PRINT $FE,128
W0 VAR WORD

START:
COUNT porta.0,1000,W0 ' COUNT FOR 1 SEC
W0=(W0*10)/2
CLS
PRINT DEC W0*6," RPM "
GOTO START
-----------------------

COUNT porta.0,1000,W0 <-- I cant translate this to C ..the rest of the statements, I can understand BUT the one I mentioned, Im having a hard time translating it ...
 
I need to make a dimmer for stabiliser
where the input is from 100Volt ac to 350 ac and out put 100 volt for auto transformer,
i preffer to use 16f676 because it adc built in.
Thank you
Abdul Majeed
 

Mark44

Joined Nov 26, 2007
628
COUNT porta.0,1000,W0 ' COUNT FOR 1 SEC


COUNT porta.0,1000,W0 <-- I cant translate this to C ..the rest of the statements, I can understand BUT the one I mentioned, Im having a hard time translating it ...
Caveat: I know a fair amount of x86 assembly, but not for the 16F84.
I looked at the data sheet for the 16F84 and learned that there are 37 instructions, none of which is a COUNT instruction. Could COUNT be some sort of macro that's defined elsewhere than in the code you showed?
 

AlexR

Joined Jan 16, 2008
732
Caveat: I know a fair amount of x86 assembly, but not for the 16F84.
I looked at the data sheet for the 16F84 and learned that there are 37 instructions, none of which is a COUNT instruction. Could COUNT be some sort of macro that's defined elsewhere than in the code you showed?
Don't know what language that snippet of code is written in but assembler it most certainly ain't!!

Could be one of the many flavours of BASIC.
 

Mark44

Joined Nov 26, 2007
628
Don't know what language that snippet of code is written in but assembler it most certainly ain't!!

Could be one of the many flavours of BASIC.
Now that you mention it, it does look like BASIC. The OP threw me off when he described it as ASM (assembly) code, which caused my brain to start thinking in those terms.
 

zereshki

Joined Feb 1, 2009
1
ok.how we can count pulses in defined pin in a defined period of time.
for example there is a command COUNT in PICBASIC that can do it.how it is possible in C?
 
Top