This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Timer in autoload mode

I use Keil sometimes and while using a timer in autoload mode, the TH0 is loaded with the calculated value but TL0 starts incrementing from 00 instead of the calculated value F0.Am I missing something while using or configuring the keil simulator?

  • 'will continue, a running timer (0 or 1) will (eventually) set TF to 1.'

    that's the point of the entire discussion, erik: his code, as well as yours, SHOULD work. but he reported it didn't work.

    both of you used exactly the same return mechanism "while (TFn==0);". so if his code, for whatever reason, didn't return from that, your code wouldn't return from that either. using mode 1 vs. 2 does NOT matter at all from that perspective.

    jesus! is it really that unreasonable to expect people to have some minimum reading comprehension?

  • Are we sure that we have the full source code?

    It was quite some time since I used an 8051 but isn't TF1 auto-cleared if interrupts are used with the timer, while manual-clear without interrupts? So a program that does enable an interrupt handler for this timer but just have an empty ISR would get an interesting competition between the interrupt logic of the chip and the TF1 polling code.

  • your code will have the exact same "problem" - if the code didn't return from his "while (TF1==0);", it wouldn't return from your "while (TF1==0);". thus why i called your line of thoughts "secondary".

    TR1 = 1;
      while(TF1==0);/*Polling mode is used instead of interupt mode*/
    

    will continue, a running timer (0 or 1) will (eventually) set TF to 1.

    Erik

  • Erik,

    I am beginning to suspect that Ashley is Jack Sprat's mother in law...

  • "why not just straight code"

    it doesn't fundamentally solve his problem - that the code doesn't return (for some extremely strange reason, in my view) to main().

    your code will have the exact same "problem" - if the code didn't return from his "while (TF1==0);", it wouldn't return from your "while (TF1==0);". thus why i called your line of thoughts "secondary".

    at this point, I am inclined to think this is a user (observational) problem.

  • void delay()
    {
      TMOD = 0X20; /*Timer 1 operation in mode 2*/
      TH1 = -184;
      TR1 = 1;
      while(TF1==0);/*Polling mode is used instead of interupt mode*/
      TR1 = 0;
      TF1 = 0;
    }
    

    It works in autoload mode and the TL1 starts intially from 00 and later gets

    loaded by the value in TH1 register.The problem is that it doesnt leave the delay routine

    to go back to main program again

    how can it 'work' when "The problem is "

    for the first delay you depend on whatever is in TL1 for your delay no autoload takes place before the timer roll over. for the next delay you will get the autoload value plus whetever counts happened before TR1=0. You are confusing the whole issue by using autoload. With a lot of feniggeling I have no doubt this could be done with autoload but why use auotoload when only one run in the routine. Using illogical modes does nothing but make the issue confusing, complex. less maintainable, less understandable, and error prone.

    why not just straight code

    void delay()
    {
      TMOD = 0X10; /*Timer 1 operation in mode 1*/
      TH1 =  0xff;
      TL1 = -184;
      TR1 = 1;
      while(TF1==0);/*Polling mode is used instead of interupt mode*/
      TR1 = 0;
      TF1 = 0;
    }
    

    Erik

  • <QUOTE>tammy: for the same reason that I have given up on you.</QUOTE>

    i remember tapeer as a stewdent. to teech him i used code like this

    
      while ( still_breathing )
      {
        send_message( to_tapeer, "read abi, page x, section y" );
    
        receive_message( from_tapeer, msg );
    
        if ( is_response_an_insult ( msg ) )
        {
           // msg prob from tapeer
    
           if (! is_response_like ( msg, "you are wrong, r0 is not preserved") )
           {
             // tapeer might at finally be listening
    
             if ( is_response_like ( msg, "im very good, you dont know anything") )
             {
               // tapeer found information but too stubborn to admit help was good
    
               break;
             }
           }
         }
       }
    
       do_some_real_work ( );
      }
    
    

    always yo're freind.

    Zeusti

  • "Why don't you just help the poor chap?! Come on, the guy suffered enough (I would if I could!)"

    tammy: for the same reason that I have given up on you.

    at some point, a person is deemed beyond help.

  • Ashley,

    Why don't you just help the poor chap?! Come on, the guy suffered enough (I would if I could!)

  • 'The problem is that it doesnt leave the delay routine'

    you just cannot help killing yourself, can you?

  • /*Program for generating sqwave on pin 7 of port 2*/

    #include <REG51.h>

    sbit mybit = P2^7;
    void delay();

    main()
    { while(1)
    { mybit = mybit^1; /*Invert mybit to toggle between 0 and 1*/
    delay ();
    } }

    void delay()
    { TMOD = 0X20; /*Timer 1 operation in mode 2*/
    TH1 = -184;
    TR1 = 1;
    while(TF1==0);/*Polling mode is used instead of interupt mode*/
    TR1 = 0;
    TF1 = 0;
    }

    It works in autoload mode and the TL1 starts intially from 00 and later gets

    loaded by the value in TH1 register.The problem is that it doesnt leave the delay routine

    to go back to main program again where "mybit" is inverted to generate square wave of

    50%duty cycle. The timer starts again after overflow and step mode execution is stuck at

    while(TF1==0). But when it runs in mode 1, everything functions smoothly and the delay

    routine returns to the main program.

  • "so be kind and stop being an @#$@"

    Oh brother! That from the like of you, who is without question the biggest, most overt "@#$*" we've had on this forum, the Microchip forum, and the HI-TECH forum! Sheesh!

  • "show some actual, complete, indented, commented code"

    his code is actual and complete, sufficient to get you to his "problem", as is.

    but here is his code presented in a nicer way, per your request.

    #include <reg51.h>
    
    void delay();
    sbit mybit = P1^5;
    
    main()
    {
            while(1)
            {
                    mybit = mybit^1;
                    delay();
            }
    }
    
    void delay()
    {
            TMOD = 0x02;
            TH0 = 0xF0;
            TL0 = 0xF0;
            TR0 = 1;
            while (TF0==0);
            TR0 = 0;
            TF0 = 0;
    }
    

  • "Nonsense."

    no reason to be so harsh.

    yes, his posts were not the clearest; and his mastery of english words wasn't as smooth as yours.

    but he got the gist of his points across after a few tries.

    maybe you can ask him for more clarification, but there is no point in beating down a newbie to establish your superiority, because everyone of us was a newbie in the not-so-distant past and will be a newbie in the not-so-distant future on some matters.

    so be kind and stop being an @#$@

  • The code clearly states

    Nonsense. What "code" you showed us here doesn't state anything clearly.

    that its polling and interupt mode.

    And that's an obvious self-contradiction.

    all I am getting are replies which does not solve my problem.

    That's because you failed quite spectacularly at presenting what your problem even is.

    the above code works perfectly well when run on keil simulator in mode 1. It does work when using mode 2.

    So it works in both modes, you say. What was your problem, again?