• AVR Freaks

Example for simple Timer2 timebase setup on PIC18F46K42

Author
meson
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2019/04/24 09:04:56
  • Location: 0
  • Status: offline
2019/06/08 14:23:52 (permalink)
0

Example for simple Timer2 timebase setup on PIC18F46K42

Hi guys,
for days now, I'm trying to figure out how to setup Timer2 as a simple timebase for the I2C module (TMR2 postscaled output).
I'm using a PIC18F46K42 with HFINTOSC at 4MHz (MPLAB X v5.15 , XC8 v2.05 and MCC v3.75).
Reading the datasheet there are so many options for external clock and reset sources but nothing how to manage a simple timebase.
So I tried using MCC and first selected 'Roll over puls' as control mode, as the other modes sound to be single shot only.
The 'Start/Reset option' shows 'Software control' as the only option, that doesn't require and external reset pin.
Does 'Software control' mean, I have to add a special reset code, and then how or when or where should it be executed?
Clock source is FOSC/4 and timer period is set to 100us.
Looking at the registers, T2RST is set to 0x0. The datasheet says that reset source is now 'Pin selected by T2INPPS' and the PinManager show a T2IN pin to be selected.
But the 'Start/Reset option' is set as 'Software control' with no external pin ....very confusing!!

Actually, as I have expected, that configuration doesn't work.
So it seems I'm missing something basic. Mostly some not so much confusing documentation.

I'd really be glad, if anybody could give a simple setup (MCC or c-code) or a non confusing explanation how Timer2 could be used as 'TMR2 postscaled output'.
Great Thanks!
#1

4 Replies Related Threads

    mbrowning
    Just a Member
    • Total Posts : 1355
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: online
    Re: Example for simple Timer2 timebase setup on PIC18F46K42 2019/06/10 11:01:05 (permalink)
    0
    I use T0 as a 100us time tick. T0 on K42 in 8bit mode is very similar to T2.
    I use T6 for I2C clock, but the configuration is identical to use as a time tick. (T6 is identical to T2)
    T0 configuration:
    void init_tmr0 (void) {                // 100uS interrupt timer
        T0CON1        = 0x56;                // FOSC/4, 1:64 post, not sync
        TMR0H        = 24;                // div by 25*64 = 10KHz = 100uS
        TMR0IF        = 0;
        TMR0IE        = 1;
        TMR0IP        = 0;
        T0CON0        = 0x80;                // 1:1 pre, 8b tmr, enable
    }

    T6 configuration:
        T6CLK        = 0x01;        // CLK = Fosc/4 (16MHz)
        T6PR        = 7;        // output = 16MHz / (7+1) = 2MHz (400KHz SCL)
        T6HLT        = 0x00;        // pre not sync, rising edge, clk not sync, SW ctrl
        T6CON        = 0x80;        // T6 on, pre 1:1, post 1:1

     
    Software Control just means that the timer is stopped/started by software setting/clearing the enable bit in T2CON.
    post edited by mbrowning - 2019/06/10 11:02:50

    Oh well - there's always next year
    #2
    davea
    Junior Member
    • Total Posts : 114
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: 0
    • Status: offline
    Re: Example for simple Timer2 timebase setup on PIC18F46K42 2019/06/10 16:10:44 (permalink)
    0
    I'm using a PIC18F46K42 with HFINTOSC at 4MHz (MPLAB X v5.15 , XC8 v2.05 and MCC v3.75).
    you may have to adjust your timer values if everything is to slow
    #3
    meson
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2019/04/24 09:04:56
    • Location: 0
    • Status: offline
    Re: Example for simple Timer2 timebase setup on PIC18F46K42 2019/06/11 11:12:41 (permalink)
    0
    Many thanks, now Timer2 works just fine.
    The initial problem of Timer2 not working together with I2C, is not the timer itself.
    It seems to be a driver problem of MCC for the K42:
    https://www.microchip.com/forums/m1092747.aspx
    #4
    Howard Long
    Super Member
    • Total Posts : 672
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: Example for simple Timer2 timebase setup on PIC18F46K42 2019/06/11 14:22:03 (permalink)
    0
    meson

    It seems to be a driver problem of MCC for the K42:
    https://www.microchip.com/forums/m1092747.aspx


    We should ask ourselves at times like this whether it takes longer to debug half baked vendor tools compared to rolling our own reusable code. I think I know the answer!
    #5
    Jump to:
    © 2019 APG vNext Commercial Version 4.5