# PIC12F683 Pulse Counter

#### OsirisB

Joined Nov 20, 2013
4
Hi guys,
I was hoping someone could be of assistance.
>>I am building a project using the PIC12F683 to read in a 10V pulse *refer attachment for pulse
>> The program needs to read the number of pulses for a second, then multiply that by 60 to get rpm.
>>If then checks the condition of a variable (Temp).
>> If Temp=0 and rpm>300, then the pic will set a pin output to ground for a second.
>>Another condition (seperate from the previous) also monitors another pin to check if it has been set to ground, and then sets Temp=0.
>> The pic needs to reset everytime power is switched off and on.
>> Please note that pins GP1 and GP0 running condition is not HIGH or LOW, but no connection.
>> I have attached my code *please be gentle

/*
PIC12f683
Clock set to 4Mhz
*/

unsigned int temp, PulseCount,rpm;

void main() {
CMCON0 = 7; // Disable comparators
// 543210
TRISIO = 0b00100010; // GP1 --> Recieves GND Input from tact switch, GP5 --> 10V Pulse input for counting
// GP0 --> Pin set to GND when conditions are met
GPIO=0;
PIE1 = 0b00000001 ; // Enable TMR1IE

temp=0;
Pulsecount=0;
rpm=0;
do {
TMR1H = 0x00;
TMR1L = 0x00;
T1CON = 0b00000111; // Enable Timer 1
Delay_ms(1000);
T1CON = 0b00000110; // Disable Timer 1
PulseCount = TMR1H;
rpm= Pulsecount*60; //converts revolutions per second to revolutions per minute
IF((rpm > 300) && (temp = 0)) {
GPIO.GP0=0; //set the GP0 output from no connection to GND
Temp=1;} //tracks the condition of pin GP0
IF (GPIO.GP1=0) {//Checks if GP1 recieved GND input from no connection
Temp=0;}

} while(1);
}

#### Attachments

• 57.1 KB Views: 42
• 24.2 KB Views: 34

#### JohnInTX

Joined Jun 26, 2012
3,832
For starters:
Rich (BB code):
TMR1H = 0x00;
TMR1L = 0x00;
T1CON = 0b00000111; // Enable Timer 1
Delay_ms(1000);
T1CON = 0b00000110; // Disable Timer 1
PulseCount = TMR1H;
This only reads the upper byte of the count so that 256 counts in reads as 1 count. Is that what you want?

Also: you can't put 10V directly into a PIC input pin. Use a voltage divider to limit it to a little less than 5V. Many have used a series resistor (33K-68K) on the input to let the input's protection diode clip the voltage but this practice is not recommended by Microchip.

To switch GPIO1, you will need a pullup resistor on the pin OR you can use the internal pullups in the WPU control register.

If you use CODE tags around your code it helps to read it but thanks for posting the code and circuit on the first post!

Last edited:

#### OsirisB

Joined Nov 20, 2013
4
What I need is the the pic to read the each pulse, count how many it receives for a second, then completes the other conditions. Thanks for the reply, I always used google and these forums to understand code to do my projects, and this is the first time I'm stuck.

#### bertz

Joined Nov 11, 2013
327
What I need is the the pic to read the each pulse, count how many it receives for a second, then completes the other conditions. Thanks for the reply, I always used google and these forums to understand code to do my projects, and this is the first time I'm stuck.
Consider the use of a Hall sensor. This is what we use on our CDI ignitions for RC airplane engines.

#### Attachments

• 261.4 KB Views: 60

#### JohnInTX

Joined Jun 26, 2012
3,832
What I need is the the pic to read the each pulse, count how many it receives for a second, then completes the other conditions. Thanks for the reply, I always used google and these forums to understand code to do my projects, and this is the first time I'm stuck.
Did my post help or are you still stuck? If still stuck, describe your specific problem.

#### THE_RB

Joined Feb 11, 2008
5,438
I think your chosen method is not great.

You get a reading in Hz, then multiply by 60, then test for >300.

The problem is that measured 5 pulses per second will equal 300, then 6 pulses per second will equal 360. Resolution is terrible for a control system.

In actual use, the captured Hz will always dither by +/- 1 count, so near the 300 RPM speed the count each second will be 4, 5 or 6 pulses per second (depending where the captured pulse is in phase compared to the measurement gating point).

A much better system would be to clear TMR1 on the pulse / edge, and capture the TMR1 value on the next / edge. That will give a MUCH higher precision reading of the speed.

#### OsirisB

Joined Nov 20, 2013
4
@bertz : Thank you for the reply.
This is the signal I have to work with, but good suggestion.

@John : Thanks for the response John. Can you possibly share some code/and or schematic on how to do this (for me to understand better)

@THE RB : Hi there...thanks for your reply...also, can you share some code on how to achieve this? Im a bit confused on how to set a pin to ground (based on the condition). Is it just setting it LOW (0)? Without defining a pin as an input/output, what will be the outcome when using a logic check?

Last edited:

#### JohnInTX

Joined Jun 26, 2012
3,832
This is the signal I have to work with.
That does not matter. You still have to make it <5V or add a series resistor to keep from busting the PIC.

As RB says, using the capture register would be better but get the basics working first. Have you resolved the reading of the timer yet? That's first. Even if you go with the capture, you will still have to read the 2 8-bit registers and combine them into an integer.

Last edited: