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

Reposting of question about timer speed with

This routine runs but it runs fast the crystal is 12Mhz and the configuration


/*
*FILE NAME:    msecWait.c
*
*NOTE:Since the counter counts up it is necessarey to *     subtract the nuber of counts/millsecnd from the *     maximum count for Counter 0 which is 0xFC65) *     seperated into bytes(unsigned char) hiByte =
*     0xFC and lowByte = 0x65.
*
*DESCRIPTION:  Delays for the input argument number *              of miliseconds
*
*INPUTS: Requested number of miliSeconds (0.001 *        Seconds) to delay
*
*OUTPUTS:        processor is on hold for the proscribed *          number of milliseconds
*
#include "F100GlobalDataTypeDefs.h"
#include "F100GlobalDef.h"
#include "F100GlobalVars.h"
#include "F100Functions.h"
#include "F100HW.h"
#include <stdio.h>
void msecWait(unsigned int milliSeconds)
{
//1000 counts/(milisecond)
unsigned char highByte = 0xFC;
lowByte = 0x65;  // up count, thus count is
               //  from 64286 (FB1E)bit
bitET0, bitTR0; //      Storage locations

bitTR0 = TR0;         // Save TR0   //
TR0 = 0;                   // Stop T0    //
bitET0 = ET0;             // Save ET0   //
ET0 = 0;                 // Disable T0 //

while (milliSeconds > NOMILLISECONDS)
 {
  TH0 = highByte;
  TL0 = lowByte;
  TR0 = 1;           //start timer 0 //
  while (!TF0) {;}  // wait for T0 to overflow
  TR0 = 0 ;        // stop timer 0 //
  TF0 = 0 ;      // Reset the overflow
  milliSeconds--;// Decrement the count
 } // end  while (milliSeconds > NOMILLISECONDS)

ET0 = bitET0; // Restore ET0 //
TR0 = bitTR0; // Restore TR0 //
} // end msecWait(unsigned int milliSeconds)

  • 1) what processor? not all clock the timer the same way.

    2) the most confusing comment I have seen in a while ET0 = 0; // Disable T0 //

    3) why (not that it makes anything fail) do you load TL from a constant and TH from a var, a waste of precious DATA space.

    4) while (milliSeconds > NOMILLISECONDS)
    NOMILLISECONDS, is that a zero?, I see no definition

    Erik

  • You could have just re-posted it in your existing thread.
    http://www.keil.com/forum/docs/thread9801.asp

    It's still a bit of a mess, though, isn't it?

    Anyhow, how have you determined that it runs "fast"?

    How much too fast?

  • The processor is a Philips 89C51RD2 Clock is 12 MHz NOMILLISECONDS = 0

    As for "ET0 = 0; // Disable T0 //"
    If you set ET0 to Zero it disables Timer Zero (T0) interrupt

    Both highByte and lowByte are Unsigned Char the translation to the internet the uchar on lowByte was lost - I got memory to burn

  • Timing was checked by lighting an LED prior to entering the routine then turning off the LED upon exiting the routine. The On to Off time was measured with With a Digital Oscilliscope.

    When milliSeconds was set to 5000 which should give a delay of 5 seconds the LED on off time was 4 seconds. Of course I can adjust for this by increasing the count by 20%. But what is wrong with my math, circuit or the chip?

    12 MHz / 12 clock pulses/program cycle = 1Mhz clock count rate.

    Thus 1msc would be 1,000 counts.

    The number of counts of the 16 bit timer is 65,536.

    The counter counts up

    The count would be from 64535 to overflow

    64535 Decimal is hex is FC17.

    0xFC is preload is into the counter 0 high byte

    0x17 is pre loaded into counter 0 high byte

    No I don't think it is a mess

  • If you set ET0 to Zero it disables Timer Zero (T0) interrupt
    I know that but it does not disable the timer as you misleadingly comment.

    delay of 5 seconds the LED on off time was 4 seconds
    your count 10000h - fc65h = 398h = 923d are you sure itm is not 4.5 seconds?

    Erik

  • The number of counts of the 16 bit timer is 65,536.

    The counter counts up

    The count would be from 64535 to overflow

    64535 Decimal is hex is FC17.

    could that be it, you load fc65 per your code

    the translation to the internet the uchar on lowByte was lost

    NEVER "translate to the internet " ALWAYS cut-and-paste. When you retype, errors are discussed you do not have that appear in the retype, but not in your code.

    Erik

    PS, you have tabs in the displayed code, always convert tabs to spaces before posting

  • "always convert tabs to spaces before posting"

    Better to never use TABs in the first place!

    The interpretation of TABs is entirely undefined: you can't even rely on getting the same results in the same application - as someone may have fiddled with the TAB settings!

  • Yes I am sure it was 4 seconds, a technician verified it with me.

  • The FC65 was a leftover from the testing after we found the problem.