Pic controlers and counting ?


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2010
    Posts
    8

    Exclamation Pic controlers and counting ?

    Hello

    New to PIC's but not to programming.

    Currently I am counting input pluses with a PIC 16f886 at 20khz.

    I am doing some thing like

    IF pin high THEN
    X=X+1

    (not exact code but I am sure you get the gerneral idea)

    The problem I am haveing is that the code is long and has quite a few RS232 outs
    and by the time the logic loops back around to the IF statement I have missed counts.

    Is there some special counting command I should be thinking of useing or can a make a pin a interupt that has precendence over what is going on ?

    My only concern if there is a interupt fuction that I can not cut a RS232 string out in half.

    Any thoughs ?

    Mark

  2. #2
    Join Date
    Mar 2006
    Location
    China
    Posts
    266


    Did you find this post helpful? Yes | No

    Default Counter

    Hi,

    If you look at the datasheet you can find that the hardware TIMERS can be used in 2 different ways (timer or counter).

    In the counter setup you have an external signal (TMR0 CLOCK INPUT for ie timer0) that will increment the counter on a selectable edge (rising or falling edge).

    This way the hardware is counting everything in the background and after you have setup the counter in the SW you dont have to care at all about it until you want to read the counted number.

    Just make sure you choose a timer that fits your biggest counted number. Some counters have 8 bit and some are 16 bit. As long as you make sure you do not overflow the counter it will not miss a single edge regardless what you do in your code.

  3. #3
    Join Date
    Nov 2010
    Posts
    8


    Did you find this post helpful? Yes | No

    Lightbulb A bit more help

    Hello

    Thanks

    I am hopeing to use RC0 and RC1 on my chip.
    In the data sheet they are marked as Timers
    There lables in the data sheet are T10S0/T1CK1 and T1OS1.
    Assumeing these can be used. Do you happen to know how to tell the chip these are now counters ?
    Also once this is done. Where/How to I read there value and get it into a varriable in my logic.

    thanks for any thoughs

    Mark

  4. #4
    Join Date
    Mar 2006
    Location
    China
    Posts
    266


    Did you find this post helpful? Yes | No

    Default Maybe not really like that

    Hi,

    If we look the datasheet for the PIC18F886 you will notice that we have 3 timers:

    Timer0: 8-bit Timer/Counter with 8-bit
    Programmable Prescaler
    • Enhanced Timer1:
    - 16-bit timer/counter with prescaler
    - External Gate Input mode
    - Dedicated low-power 32 kHz oscillator
    • Timer2: 8-bit Timer/Counter with 8-bit Period
    Register, Prescaler and Postscaler

    Timer0 can be clocked with the T0CKI (RA4)
    Timer1 can be clocked with the T1CKI (RC0)
    Timer2 can NOT be clocked with an external pin.

    There is an other possibility and that is that you use interrupts for counting but that might upset the serial data unless you also do that part interrupt based.

    But since you say that you are new to pics I would suggest that you use the timers as counters. Associated with the timers are the timer registers and that is where the current number of counts are stored.

    8-bit timer/counter register (TMR0) for Timer0
    16-bit timer/counter register pair (TMR1H:TMR1L) for Timer1

    I suggest you start with Timer0 since that is the easiest one to set up.
    5.0 TIMER0 MODULE in the datasheet is something you must read and understand before you will be successful with this.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts