The following code has been tested and used in the project and can be used with confidence
//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.
{
if( TimerNo[ i ] != 0 ){
if( --( TimerNo[ i ] ) == 0 ){//Time is up
}
{
TimeBase10ms = TIME_BASE_10MS * ( F_OSC / 110592 );
}
{
TimeBase500ms = TIME_BASE_500MS * ( F_OSC / 110592 );
for( i = 4; i < 8; i++ )
{
if( TimerNo[ i ] != 0 ){
if( -- ( TimerNo[ i ] ) == 0 ) {
fTimer |= 0x01 << i;
Keywords:MCU
Reference address:Realizing Soft Timer Using Timer Interrupt of Single Chip Microcomputer
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);
//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++ )
fTimer |= 0x01 << i; //set to timeout
}
}
if( --TimeBase10ms == 0 )
ScanCopierSignal() ;//Function that is called once every 10 milliseconds
if( --TimeBase500ms == 0 )
//Processing timer with 500ms as time base
}
}
_pop_(SCONF);
}
Previous article:Process and program in single chip real-time operating system
Next article:Several good functions in C language
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
Guess you like
- [Anxinke UWB indoor positioning module NodeMCU-BU01] No.001-unboxing test
- EEWORLD University Hall----Live Replay: Detailed Explanation of NXP Embedded Human Machine Interface Solutions
- How is the circuit of the LED aging rack connected? Series or parallel?
- VICOR "Bidirectional 48V/12V Converter Module" evaluation board is now available for free!
- Sorting out the msp430f149 port functions and settings
- Grounding of electronic equipment
- What is the difference between the full-port and half-port of Zhongying SH367309 5-16 series lithium battery protection chip?
- 【DIY Creative LED】The soldered board has normal charging function
- ESP32 SD/MMC hardware connection method
- Domestic chips are amazing. I just applied for a board. CH579 is so small but it integrates network, USB, and Bluetooth.