Realizing Soft Timer Using Timer Interrupt of Single Chip Microcomputer

Publisher:hxcp18Latest update time:2015-11-16 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The following code has been tested and used in the project and can be used with confidence   
 
Author :   I am from Qi
 
 
#include "reg52.h"
#include "Intrins.h"
#include "TypeDef.h"
#include "CPUPin_Def.h"
#include "VarDef.h"
 
 
#define TIME_BASE_2MS   2
#define TIME_BASE_500MS 250
#define TIME_BASE_10MS 5
#define F_OSC 110592  //Define this to calculate the value of TH0  TL0     according to the 11.0592MHZ crystal oscillator by default.  Mode 1  16-bit timer
//Note that this writing method is only useful when the crystal oscillator is doubled.
//Because the 51 MCU hardware does not support  floating point operations,   12/11.0592 = 1  is rounded down to the same crystal value as 11.0592.
// If floating-point operations are supported, there will be special hardware floating-point operation instructions inside the chip. This writing method is not acceptable and  the type must be forced to convert
//For example, (double)12/(double)11.0592  needs to be converted to a floating point number.
// Currently, there is hardware support for floating point and software floating point  , which means that software can also implement floating point operations. For example, when the compiler sees special code, it will turn to
//A special function to handle. 51    STM32 microcontrollers currently do not seem to support hardware floating point numbers
 
 
 
void Timer0() interrupt   1   using 1
{
//Using static variables, this will only be executed once, no matter how many interrupts occur because it is a static variable
//But it cannot be written as
//If the actual crystal oscillator is 12MHZ, change it to   F_OSC / 120000  because TH0  TL0 is calculated according to 11.0592
static uchar TimeBase2ms = TIME_BASE_2MS * (F_OSC / 110592); 
  //If the crystal oscillator is 11.0592  , then the value is 1.   If it is 12.00MHZ  , then the value is  1.085xxxx.  If the crystal oscillator runs fast, the value should be larger.
//Just change the F_OSC macro directly. No need to change other
 
static uchar idata TimeBase500ms = TIME_BASE_500MS * (F_OSC / 110592);
static uchar TimeBase10ms = TIME_BASE_10MS *(F_OSC / 110592);
static unsigned char CountSignalDelay = 0;
//The above will only execute once static definition declaration initialization 
uchar i;
TH0=0xfc; //   Total timing is 1000 microseconds, it takes 12 cycles to enter the interrupt, so the actual timing is 1000 - 12 * 12 / F
TL0=0x72 ; 
//The tool calculates here that it should be FC 66      FC72-FC66 = 12 
//Let the loaded number  use 12 fewer cycles  to enter the interrupt because entering the interrupt requires 12 cycles plus exactly 1ms
_push_( SCONF ) ;
 
if( --TimeBase2ms   == 0 ){
TimeBase2ms = TIME_BASE_2MS * (F_OSC / 110592);
//0--3   is a timer with a time base of 2 milliseconds
for( i = 0; i < 4; i++ )
   {
   if( TimerNo[ i ] != 0 ){
   if( --( TimerNo[ i ] ) == 0 ){//Time is up
fTimer |=   0x01 << i;   //set to timeout
}
}
   }
 
if( --TimeBase10ms   == 0 )
   {
   TimeBase10ms = TIME_BASE_10MS * ( F_OSC / 110592 );
ScanCopierSignal() ;//Function that is called once every 10 milliseconds
   }
if( --TimeBase500ms   == 0 )
       {
       TimeBase500ms = TIME_BASE_500MS * ( F_OSC / 110592 );
//Processing timer with 500ms as time base
       for( i = 4; i < 8; i++ )
       {
       if( TimerNo[ i ] != 0 ){
       if( -- ( TimerNo[ i ] ) == 0 ) {
       fTimer |=   0x01 << i;
}
 
 
 
}
_pop_(SCONF);
}
 
Keywords:MCU Reference address:Realizing Soft Timer Using Timer Interrupt of Single Chip Microcomputer

Previous article:Process and program in single chip real-time operating system
Next article:Several good functions in C language

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号