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)

Parents
  • 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

Reply
  • 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

Children
No data