PIC12F683 Pulse Counter

Discussion in 'The Projects Forum' started by OsirisB, Nov 20, 2013.

  1. OsirisB

    Thread Starter New Member

    Nov 20, 2013
    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 [​IMG]

    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
    ANSEL = 0x00; // No analog inputs
    PIE1 = 0b00000001 ; // Enable TMR1IE

    do {
    TMR1H = 0x00;
    TMR1L = 0x00;
    T1CON = 0b00000111; // Enable Timer 1
    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

    } while(1);
  2. JohnInTX


    Jun 26, 2012
    For starters:
    Code ( (Unknown Language)):
    1. [FONT=Courier New]TMR1H = 0x00;[/FONT]
    2. [FONT=Courier New]TMR1L = 0x00;[/FONT]
    3. [FONT=Courier New]T1CON = 0b00000111; // Enable Timer 1[/FONT]
    4. [FONT=Courier New]Delay_ms(1000);[/FONT]
    5. [FONT=Courier New]T1CON = 0b00000110; // Disable Timer 1[/FONT]
    6. [FONT=Courier New]PulseCount = TMR1H;[/FONT]
    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: Nov 20, 2013
  3. OsirisB

    Thread Starter New Member

    Nov 20, 2013
    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.
  4. bertz


    Nov 11, 2013
    Consider the use of a Hall sensor. This is what we use on our CDI ignitions for RC airplane engines.
  5. JohnInTX


    Jun 26, 2012
    Did my post help or are you still stuck? If still stuck, describe your specific problem.
  6. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    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. :)
  7. OsirisB

    Thread Starter New Member

    Nov 20, 2013
    @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: Nov 21, 2013
  8. JohnInTX


    Jun 26, 2012
    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: Nov 21, 2013
  9. OsirisB

    Thread Starter New Member

    Nov 20, 2013
    I value the quick response guys...okay let me try and wrap my head around these suggestions...and I will reply when I have something concrete...

    Thanks again :)