Menu

Timer counter Hardware or software problem ?

Help
Capaction
2010-09-10
2013-05-30
  • Capaction

    Capaction - 2010-09-10

    This piece of code does not work:

    **TstTimer0:    'pin 6 on Interdil  is low for this piece of code

    Dim IntCount as Word
    IntCount = 0

    InitTimer0 IntOsc,PS0_1/256 'Internal Oscillator is 4Mhz on my PIC 16F428
    ClearTimer 0
    StartTimer 0
    On Interrupt Timer0Overflow call Counter

    Do
    Again10:
    LEDoff                         ' LED on Pin 3
    If IntCount  < 595 then goto Again10  ' will count from 1 to 595x0,050432= 30s
    IntCount = 0                         'reset counter
    LEDon
    Wait 200 ms
    Loop
    **
    Sub Counter
    'Timer will overflow after 0,050432 s
    TMR0 = 132    'PreSet according to PIC TIMER CALCULATOR
                      'http://pictimer.picbingo.com/

    IntCount = IntCount + 1 'will count  0,050432 s with PS_1/256

    END sub

     
  • Hugh Considine

    Hugh Considine - 2010-09-10

    Try changing the InitTimer0 command. The oscillator should be "Osc" or "Ext", not IntOsc.

    If that doesn't fix the program, check to see if the timer is running properly. (Timer 0 should always run on a PIC, but it's good to check.) After the On Interrupt command, add this:

    Do
        If TMR0.7 = 1 Then
            Set LED On
        Else
            Set LED Off
        End If
    Loop

    That should make the LED flash. If that works, the next step is to check the interrupt. Remove that code, then try adding some code to the interrupt to turn on the LED at the start and turn it off at the end. If the LED flickers, the interrupt is working.

     
  • Nobody/Anonymous

    The LED stays  ON when I set TMR0.7 = 1
    May be I have a hardware problem on my design ?

    http://capaction.free.fr/Schem0908.JPG

     
  • pico

    pico - 2010-09-11

    try to change
    Wait 200 ms
    with
    wait 1sec

    Maybe you don´t see the 0.2 Sec flash of the led.
    On my calc (can be wrong) the led should flash after 18.888 second.

     
  • Nobody/Anonymous

    HARDWARE PROBLEM SOLVED
    I moved the LED pin 3  assignment from R4/TOCK1 to RA1. Pin 18 Then Pin 3 is not used  with a Dir PortA.3 OUT
    Timer0 is now working but I do not get the expected time between 2 LedON for 500ms.   See my code above.
    SOFTWARE PROBLEM
    I was expecting 30seconds and get 20s.                 

     
  • pico

    pico - 2010-09-13

    i tell you why, you have tree  mistakes
    you tmr0 timeout is not 0,050432 but 0.031744
    You reload the timer to value 132.    (256-132)*256=31744 µS
    for the value 50432 µS you must load the value 59
    Second, you introduce a error on reloading timer, this error amount to 12 mS
    this error cannot be corrected with that prescaler value. Live with it.
       movwf oscal

     
  • Capaction

    Capaction - 2010-09-13

    I don't understand your calculation to get 0.031744 with PS/256 ?
    PIC Timer Calculator  does not give this value
    Which formula did you use to get the Overflow value ?

     
  • pico

    pico - 2010-09-13

    The forumula, as written above is 132 = (256-132)*256=31744 clocks = 31744 µS on 4Mhz
    (256-TMR0)*PRESCALER=INSTRUCTION_CLK reload interval
    256-TMR0 value, because the timer count up and interrupt is generated when the timer overflows from 255 to 0.
    if you reload the timer to 255, then after after 256 (prescaler) timer clock you get a interrupt.
    If you load the timer with 0 , then after 256*256(Prescaler) timer clocks you get a interrupt, always if enabled.
    That´s why i count 256-Value.
    Annother option is to use "TMR0 -= val"  for reloading the timer.
    I make a example, with prescaler set to 4. If i want a interrupt every 1ms, i need a tmr0 value of 250
    1000 µS or instruction clocks / 4 = 250 ,
    So, or i reload the timer with the value 6, that is 256-250 = 6 or i reload the timer with "tmr0 -= 250" .
    Annother option is that i load timer with "tmr0 = -250" , code that seems not  work con GCB.
    Only this gives a interrupt every microsecond.

     

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.