1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Tmr0 16F505

Discussion in 'Microcontrollers' started by be80be, Aug 27, 2017.

  1. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    I been play with the timer on a 16f505
    Trying to figure how to load a counter and test each time it fills up.
    Code (text):

    #include <xc.h>
    unsigned long counter; //counter variable to count
    //the number of TMR0 overflows
    Init(void)
    {
    TMR0 = 0;//Clear the TMR0 register
    /*Configure Timer0 as follows:
    - Use the internal instruction clock
    as the source to the module
    - Assign the Prescaler to the Watchdog
    Timer so that TMR0 increments at a 1:1
    ratio with the internal instruction clock*/
    OPTION = 0B11010111;
    }
    /*----------------------------------------------------------
    Subroutine: main
    Parameters: none
    Returns: nothing
    Synopsys: Main program function
    -----------------------------------------------------------*/
    main(void)
    {
    Init(); //Initialize the relevant registers
    TMR0 = 0;
    while(1)
    {
    //Poll the T0IF flag to see if TMR0 has overflowed
    if (TMR0)
    {
    ++counter;//if T0IF = 1 increment the counter
    //variable by 1
    //TMR0 = 0;//Clear the T0IF flag so that
    //the next overflow can be detected
    }
    }
    }
     
     
  2. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,894
    Likes:
    501
    Location:
    Cleveland, OH, USA
    ONLINE
    You'll wait a long tome for the IF.;) The 12(16)F50x do not have interrupts.

    upload_2017-8-27_13-8-37.png
     
  3. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    Not much to work with
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,894
    Likes:
    501
    Location:
    Cleveland, OH, USA
    ONLINE

    I think the only way to detect overflow of TMR0 per se is to poll TMR0. For example, a "movf TMR0,w" or its equivalent in C should set the zero flag in STATUS (i.e., STATUS,2) when the timer rolls over. I have not actually tried that, though. Running it at 1:1 may be more difficult.

    If you want counts less than 256, then you can do presets or various subtractions or additions to determine the actual count. At each rollover, you need to repeat any presets.

    John
     
  6. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,015
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    If you test bit 7 of TMR0 it will go to zero on an overflow.

    Mike.
     
  7. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    Poll the bit I though about that. Trying to bit bang serial in C but i'm learning.
    Moved up to a bigger chip. Pic 16F15376 got serial timer2 and pwm working on it
    Lets you dim a led using pwm. And read the adc reading over serial.
     

Share This Page