/**********************************************
Method 1: Delay method
Hardware: 11.0592MHz crystal, STC89C52, RXD P1.0 TXD P1.1 T1 P3.5
Baud rate: 9600
Description: T0 is used for timing, mode 1, timing time 50ms, interrupt mode; T1 is used for counting. After the 10s timing time is up, the counted pulse is sent out through the analog serial port. The external pulse is a square wave with a frequency of 1Hz generated by STM8L101. 1 falling edge every 1s
**********************************************/
#include "reg52.h"
#define uchar unsigned char
sbit P1_0 = 0x90;
sbit P1_1 = 0x91;
sbit P1_2 = 0x92;
#define RXD P1_0
#define TXD P1_1
#define WRDYN 44 //Write delay
#define RDDYN 43 //Read delay
void Delay2cp(unsigned char i);
unsigned char num=0;
//Write a byte to the serial port
void WByte(uchar input)
{
fly i=8;
TXD=(bit)0; //Send start bit
Delay2cp(39);
//Send 8 bits of data
while(i--)
{
TXD=(bit)(input&0x01); //Transmit low bit first
Delay2cp(36);
input=input>>1;
}
//Send check digit (none)
TXD=(bit)1; //Send end bit
Delay2cp(46);
}
//Read a byte from the serial port
fly RByte(void)
{
uchar Output=0;
fly i=8;
flying temp=RDDYN;
//Send 8 bits of data
Delay2cp(RDDYN*1.5); //Note here, wait until the start bit
while(i--)
{
Output >>=1;
if(RXD) Output |=0x80; //Receive low bit first
Delay2cp(35); //(96-26)/2, the loop takes up 26 instruction cycles in total
}
while(--temp) //Search for the end position within the specified time.
{
Delay2cp(1);
if(RXD)break; //exit after receiving the end bit
}
return Output;
}
//Delay program*
void Delay2cp(unsigned char i)
{
while(--i); //Exactly two instruction cycles.
}
void TimerInit()
{
TMOD=0x41; //T1, counting mode, T0, timing mode, mode 1
TH0=0x4C; //T0,50ms timing initial value
TL0=0x00; //T0,50ms timing initial value
TH1=0x00; //Count initial value cleared to 0
TL1=0x00; //Count initial value cleared to 0
TR0=1; //Start timer T0
TR1=1; //Open counter T1
ET0=1; //Open timer T0 interrupt
EA=1; //Open the general interrupt
}
void main()
{
flying ccc;
TimerInit();
while(1){}
}
void timer0(void) interrupt 1
{
TH0=0x4C; //Reset the timing initial value
TL0=0x00; //Reset the timing initial value
num++;
if(num==200) // count the pulses within 10 seconds
{
WByte(TH1);
WByte(TL1);
num=0; //clear to 0
TH1=0;
TL1=0;
}
TF0=0; //Software clears interrupt flag
}
Count 10 pulses in 10 seconds and 1 falling edge in 1 second.
Previous article:Write a complete function using 51 assembly
Next article:Use of 51 MCU timer T0 1-interrupt method
Recommended ReadingLatest update time:2024-11-16 16:49
- Popular Resources
- Popular amplifiers
- Siemens PLC Programming Technology and Application Cases (Edited by Liu Zhenquan, Wang Hanzhi, Yang Kun, etc.)
- Siemens PLC from Beginner to Mastery with Color Illustrations (Yang Rui)
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [Review of Arteli Development Board AT32F421] - Data Collection
- [2022 Digi-Key Innovation Design Competition] [Intelligent Garden Integrated Control System] TouchGFX displays Chinese menu
- Steering wheel direction sensing sensor selection
- Qorvo's new product makes it easier for 5G and Wi-Fi to work together
- msp430g2553-minimum system
- [Qinheng RISC-V core CH582] Transplantation of RF code 01
- Control principle of ADC0832
- The pins of the components in the PADS self-made package library are missing
- Application of Keithley Source Meter in Electronic Thin Film Materials
- How to switch the unit of Proteus simulation software from inches to mm? Pressing m does not work, and Baidu can't find it