Menu

TIMER 0 problems

Help
2025-10-28
2025-10-29
  • Fratta Matteo

    Fratta Matteo - 2025-10-28

    Hi everyone, I'm trying to use timer 0 to generate a frequency of 500 khz on porta.0.
    unfortunately at most I managed to make it work at a maximum of 130 khz.
    the micro is a pi 18F25k22.
    the GCSTUDIO version is the latest. I put some comments in the program I attach to clarify what I do.
    Thanks for your help

    chip 18F25k22, 64

    config osc = INTIO7 'on pin A.6 I see 16 MHz with an oscilloscope

    set OSCCON = b'01110000' 'CONFIGURO OSCILLATORE E PLL
    'st OSCCON2 = b'00000100'
    PLLCFG = on
    PLLEN = on
    set PRICLKEN=off
    set PRISD= on

    wait 1 s

    ; ----- Define Hardware settings
    ' Define I2C settings - CHANGE PORTS
    #define I2C_MODE Master
    #define I2C_DATA PORTC.4
    #define I2C_CLOCK PORTC.3
    #define I2C_DISABLE_INTERRUPTS ON
    'Optionally, you can reduce the I2C timings.
    #define I2C_BIT_DELAY 0 us
    #define I2C_CLOCK_DELAY 1 us
    #define I2C_END_DELAY 0 us

    '''Set up LCD
    #define LCD_IO 10
    #define LCD_I2C_Address_1 0x4E
    ' #define LCD_I2C_Address_1 0x4C ; default to 0x4E
    #define LCD_WIDTH 20 ;specified lcd width for clarity only. 20 is the default width

    ;----- Variables
    Dim LCD_I2C_ADDRESS_CURRENT as Byte
    Dim tmr0 as byte

    DIR porta.0 OUT

    InitTimer0 OSC, PRE0_1 + TMR0_FOSC4

    On Interrupt Timer0Overflow Call Flash_LED
    'T08BIT= 1

    PSA= 1 'It's handmade because it doesn't appear in a bookshop, from INTCON

    set tmr0= 224
    starttimer 0

    'debug registers on display ---------------------------
    locate 0,0

    print T0CON 'I read 200
    locate 0,6

    print INTCON 'I read 226

    do

    loop

    SUB Flash_LED
    ' Clearing timer flag
    set TMR1IF = 0
    porta.0 = ! porta.0
    set tmr0= 224
    END SUB

     
  • Anobium

    Anobium - 2025-10-28

    Hi,

    The demo folder has an example: See C:\GCstudio\gcbasic\demos\Vendor_Boards\StartPIC18_Board\18f25k22\110_Rotate_the_LEDs_using_16bit_Timer0_.gcb

     
  • Fratta Matteo

    Fratta Matteo - 2025-10-28

    👍

     
  • Fratta Matteo

    Fratta Matteo - 2025-10-29

    hi,
    I tried to improve the code, but unfortunately on port.0 I only get 192 kHz; I would expect a few MHz, even looking at the block diagram of the TMR0.

    MY CODE

    chip 18F25k22, 64

    config osc = INTIO7 'on pin A.6 I see 16 MHz with an oscilloscope

    OPTION Explicit

    wait 1 s

    DIR porta.0 OUT

    InitTimer0 OSC, PRE0_1

    On Interrupt Timer0Overflow Call clock

    set T0CS = 0
    set psa = 1
    set tmr0l= 255
    starttimer 0

    do

    loop

    SUB clock
    ' Clearing timer flag
    set TMR0IF = 0
    porta.0 = ! porta.0
    set tmr0l= 255
    END SUB

     
  • Anobium

    Anobium - 2025-10-29

    You need to use the 16bit timer. See the demo example.

    This is a bit of the demo.

    '   Initialise the timer.
    '   Firstly tell the compiler to use a 16bit timer for this demonstration
    #DEFINE TMR0_16BIT
    
    '       Prescaler
    '           1:64
    InitTimer0 Osc, PRE0_64
    
    '     Start the Timer
    StartTimer 0
    
    'Every 1 sec.  0x0bdc is calculated value using a tool like Mr E. Timer Helper using a 16bit value.
    SetTimer ( 0 , 0x0bdc  )
    

    Mr E. Timer Helper is also in the demo folders.

     
  • Fratta Matteo

    Fratta Matteo - 2025-10-29

    Hi

    From a post on the Microchip website, I realized that with such a fast interrupt, the instructions must be limited, precisely because of the speed.
    So they recommend using the HPWM output. I needed a 500 kHz clock to drive a display, so I'll try that...
    Regards, Matteo

     
  • Anobium

    Anobium - 2025-10-29

    The post is incorrect. Do the maths and then manage the signal in the ISR.

     
    • Anobium

      Anobium - 2025-10-29

      Prescaler 1:1; TMR1 Preload = 65504; Actual Interrupt Time 2us.

      Now... you can do anything else... when this is running.


      So, what display are driving? I think that is real question. What requires a frequency like that?

       
  • Fratta Matteo

    Fratta Matteo - 2025-10-29

    Ciao Anobium, volevo usare un timer a 8 bit per essere più veloce.

     
  • Fratta Matteo

    Fratta Matteo - 2025-10-29

    ok I'll try the Timer1 route, the question is more than legitimate, I recovered some old displays and wanted to try to drive them, the controller chip is HD44100R, or LC7940.... I think it will be difficult, but I'll try

     
  • Anobium

    Anobium - 2025-10-29

    I think you need to check the requirement for the LCD Drive (Multiplex) Signal Operating Frequency. You will cause permanent damage at the frequency you are trying to achieve. I think the frequency is a lot, lot lower.

     
  • Fratta Matteo

    Fratta Matteo - 2025-10-29

    It's true, I have to do some checks, but I took this data with an oscilloscope, and eventually it could be driven at lower frequencies.

     
    • Anobium

      Anobium - 2025-10-29

      Check the display datasheet.

       
  • Fratta Matteo

    Fratta Matteo - 2025-10-29

    👍

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.