+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french part)
    Posts
    722

    Default PIC 18F - read TMR0 registers in 16 bits mode

    Hello All,

    I'm not used to play with 18F PICs so I'm having difficulties finding out how to read (if this actually IS the problem) TMR0H and TMR0L in a 16-bit mode configuration.

    This is what the holy DS says:
    12.4 16-Bit Mode Timer Reads and Writes
    TMR0H is not the high byte of the timer/counter in 16-bit mode, but is actually a buffered version of the high byte of Timer0 (refer to Figure 12-2). The high byte of the Timer0 counter/timer is not directly readable nor writable. TMR0H is updated with the contents of the high byte of Timer0 during a read of TMR0L. This provides the ability to read all 16 bits of Timer0 without having to verify that the read of the high and low byte were valid due to a rollover between successive reads of the high and low byte. A write to the high byte of Timer0 must also take place through the TMR0H Buffer register. Timer0 high byte is updated with the
    contents of TMR0H when a write occurs to TMR0L. This allows all 16 bits of Timer0 to be updated at once.
    So after looking around, I found this clear statement made by a cleverer man than me who understood the DS (at least, I hope so ):
    In 16-bit mode, two SFR are used TMR0L and TMR0H;
    In write operations, TMR0H must be written before TMR0L;
    In read operations, TMR0L must be read before TMR0H.

    In my case, I'd like to READ the registers and made this simple program, started with a button launching the serial output to my terminal.
    Code:
    ' Fuses PIC18F1330
    @ __CONFIG _CONFIG1H, _OSC_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
    @ __CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOR_OFF_2L & _BORV_0_2L
    @ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
    @ __CONFIG _CONFIG3L, _HPOL_HIGH_3L & _LPOL_HIGH_3L & _PWMPIN_OFF_3L
    @ __CONFIG _CONFIG3H, _FLTAMX_RA7_3H & _T1OSCMX_LOW_3H & _MCLRE_OFF_3H
    @ __CONFIG _CONFIG4L, _STVREN_ON_4L & _BBSIZ_BB256_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    @ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
    @ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
    @ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
    @ __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
    @ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
    @ __CONFIG _CONFIG7H, _EBTRB_OFF_7H    
    
    OSCCON  = %00000000 'OSCILLATOR CONTROL REGISTER
    OSCTUNE = %00000000 'OSCILLATOR TUNING REGISTER
    ADCON0  = %00000000 'A/D CONTROL REGISTER 0
    ADCON1  = %00000000 'A/D CONTROL REGISTER 1
    ADCON2  = %00000000 'A/D CONTROL REGISTER 2
    INTCON  = %10000000 'INTERRUPT CONTROL REGISTER, Enable global interrupts
    INTCON2 = %00000000 'INTERRUPT CONTROL REGISTER 2
    INTCON3 = %00000000 'INTERRUPT CONTROL REGISTER 3
    PORTA   = %00000000 'State High (1) or Low (0)
    TRISA   = %00000000 'Data Direction Control Register (Input/Output)
    PORTB   = %00000000 'State High (1) or Low (0) 
    TRISB   = %00010000 'Data Direction Control Register (Input/Output)
    
    T0CON   = %11000111 '16-bit mode enabled
    
    Define OSC 4 ' external oscillator
    
    'In 16-bit mode, two SFR are used TMR0L and TMR0H;
    'In write operations, TMR0H must be written before TMR0L;
    'In read operations, TMR0L must be read before TMR0H;
    
    TMR0_LOW  var byte
    TMR0_HIGH var byte
    
    ' READ TMR0 values
    TEST:
        IF PORTB.4 = 0 THEN 'have a "start" button here
            T0CON.7 = 0 'stop the timer
            TMR0_LOW = TMR0L   'first I read the lower TMR0 - is this not a valid READ?
            TMR0_HIGH = TMR0H   'second, I read the higher TMR0
            serout2 PORTB.1, 84,[DEC TMR0L,13]
            serout2 PORTB.1, 84,[DEC TMR0H,13]
            T0CON.7 = 1 ' restart the timer
            pause 100 ' debounce
        ENDIF
    
        GOTO Test
    Anyhow I try, I always read "0" in the TMR0H (HIGH)?!

    What am I missing please?
    Roger

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    1,342

    Default Re: PIC 18F - read TMR0 registers in 16 bits mode

    Code:
    TRISB   = %00010000 'Data Direction Control Register (Input/Output)
    
    T0CON   = %11000111 '16-bit mode enabled  ?????  how do you figure that
    
    Define OSC 4 ' external oscillator
    from data sheet


    REGISTER 12-1: T0CON: TIMER0 CONTROL REGISTER

    bit 6

    T016BIT: Timer0 16-Bit Control bit
    1 = Timer0 is configured as an 8-bit timer/counter
    0 = Timer0 is configured as a 16-bit timer/counter
    This is more entertaining than Free to Air TV

  3. #3
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french part)
    Posts
    722

    Default Re: PIC 18F - read TMR0 registers in 16 bits mode

    Well Richard,

    It was, as a matter of fact, far too obvious to me to have bit 6 set to enable the 16-bit mode

    AAhhhhrrgggg!!!! Why doesn't these guys make customisable datasheets???

    Thanks a lot

    PS: next visit, my optician...
    Roger

Similar Threads

  1. shiftout with 32 bits long (ADF4351 registers)
    By F1CHF in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 26th August 2016, 16:15
  2. How to use symbolic names for bits within registers?
    By DwayneR in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th December 2009, 04:19
  3. XT oscillator mode & configuration bits
    By Ramonetnet in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 24th April 2008, 11:51
  4. Internal EEPROM Read/write Addressing Errors with 18F PIC's
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 18
    Last Post: - 12th July 2005, 19:42
  5. How to work with LCD in a mode of 8 bits?
    By MCU Destroyer in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 1st July 2005, 21:10

Members who have read this thread : 16

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