# urgently needing help about 'counter'

Status
Not open for further replies.

#### blibala

##### New Member
i would like ot use the counter function in the PIC but dont know how.can anyone show me how,preferrable in BASIC?i need it really really urgent.

the counter will receceive square pulses for 5 seconds and then reset again, then the PIC will do calculation based on formula and send to LCD.

PLZ HELP ME.OR ELSE I WILL NOT ABLE TO FINISH MY PROJECT!!!

#### ivancho

##### New Member
I am not sure I understand what you want to do. Do you want to count how many pulses come in 5 seconds? You could use an interrupt to increase a counter every time. A counter is just a variable that keeps track of how many interrupts have occurred.

Track VAR BYTE

Track = Track + 1

Or you can use the capture port to find out the frequency, given the pulses come at certain frequency and not just randomly.
Good Luck

Ivancho

#### blibala

##### New Member
no.the frequency may not fixed.actually i want to build a wind speed meter based on the slot disc cutting the IR beam. the PIC will receive pulses for 5 seconds and determine how many pulses have received.then it performs calculation to determine the actually speed in km/h. after receiving pulses for 5 seconds, it will reset.and how to reset it, i dont know.can anyone guide me?
i found in PICBsic Pro compiler programming manual that there is a command called 'COUNT' which counts the pulses occured on a pin for certain period of time.is it suitable for my project?
i couldnt find more detail about this type of counter on the internet and my knowledge in assembly language is poor.can anyone guide me in PIC BASIC language?

#### stef

##### New Member
Send me the circuit diagram of your setup and I can write you a piece of assembler code if you like.

You can use the interrupt on portB together with TMR0 to count the pulses. You could also use BTFSS command (assembler).

#### blibala

##### New Member
the setup would probably look like this,since i dont know which pin i should send in the pulses, so i assume it is on RB4.may b u can help me to correct it and decide which pin is more suitable.there is a switch connected to RB1.(this may also change if u feel that it has connected wrongly)

when the switch is pressed, the PIC will start to count the square pulses coming in for duration of 5 seconds and then reset. the fromula is:

speed = how many counts in 5 seconds x 0.006 / (1.39x10 power -3)

each time the switch is pressed, there will b only one reading on the LCD.i dont want make it continuosly showing different readings and i only want it to show reading each time the switch is pressed.therefore no looping in the program. that means each time the switch is pressed, the user needs to wait for 5 seconds to know the speed. the LCD module is hitachi 44780 based.

#### Attachments

• Image7.gif
8.1 KB · Views: 624

#### ivancho

##### New Member
I belive your problem will not be on how to count the pulses in 5 seconds but more how you are going to be able to use your formula.

PIC are not good number crunchers.... PIC stands for Periperal Interface Controller..... and although it has math capability, it does not like floating point :shock: You are going to have to come with a way to use floating numbers, the easiest way is to deal with instead of 3.45 work with 345.

How exact has those 5 seconds have to be?
A simple loop that checks RB0 will do the trick. Something like:
Code:
Loop:

If PortB.0 = 1 then
Count = Count + 1
end if

If TimeisUp = X then ExitLoop

goto Loop.
It will do it fast enough that it would be difficult to miss a pulse. You can use the interrupt on the RB0 to make it more reliable.

To keep track of how long you do this, you set a timer and count how many times it overflows. You can calculate how many overflows have to occur to have 5 seconds.

Check out timers and interrupts tutorials.

Also you should think on having your equation look more like:

Speed = (#pulses in 5 sec) * 431

that is 100 ( 0.006/1.39E-3) = 431

And when it is time to display just break up the number.

Good Luck
Ivancho

#### stef

##### New Member
I have written this program to time a fan motor for a money shower. It has two pushbuttons on PORTB; one to select the seconds (60, 120 or 180), and one to start the motor. I monitored the flag bit for portb interrupt to check for a change on portb. The interrupt routine will determine which button was pressed. This program could easily be adapted to suit your app. At the very least , the 1 sec timer & BCD coversion should be useful.

#### Attachments

• timer1fa.zip
2 KB · Views: 148

#### blibala

##### New Member
sadly, i cannot understand assembly language ....

any good website has tutorial about the PIC programming?

#### stef

##### New Member
Just look up the commands on the data sheets, it'll make perfect sense

Status
Not open for further replies.

Replies
0
Views
1K
Replies
1
Views
2K
Replies
2
Views
975
Replies
11
Views
2K
Replies
7
Views
1K