51 MCU counter T1 counts external pulses

Publisher:丹青妙手Latest update time:2020-07-06 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/**********************************************

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.

Reference address:51 MCU counter T1 counts external pulses

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

Design of Serial Communication between PC and Multiple MCS-51 Single-Chip Microcomputers
1 Introduction In measurement and control systems and engineering applications, it is common to encounter situations where multiple tasks need to be executed simultaneously, so the master-slave multi-machine distributed system has become a widely used model in modern industry. Most of them are composed of IBM-PC micr
[Microcontroller]
Design of Serial Communication between PC and Multiple MCS-51 Single-Chip Microcomputers
How to use 89S51 watchdog
The 89S51 and 89C51 in ATMEL's 89S51 series have the same functions. The instructions are compatible. HEX programs can be used directly without any conversion. The 89S51 only adds a watchdog function compared to the 89C51. For other functions of the 89S51, please refer to the 89C51 data. The specific usage of the wa
[Microcontroller]
51 MCU peripheral module——A/D analog-to-digital conversion
1. Main technical indicators of A/D converter 1. Resolution The resolution of an ADC refers to the change in the input analog voltage required to change the output digital value by one adjacent digital number. It is usually expressed in binary bits. For example, the resolution of a 12-bit ADC is 12 bits, or the resolu
[Microcontroller]
51 MCU peripheral module——A/D analog-to-digital conversion
C8051F320 - Parallel Port and Cross* Switch
C8051F320 has 25 I/O pins, namely P0, P1, P2, and P3.0. In order to use the internal digital system resources as needed, a digital cross-switch must be used to map the resources to specific pins. The analysis is as follows: If no resources are used, all 25 pins can be used as normal parallel pins. If digital
[Microcontroller]
C8051F320 - Parallel Port and Cross* Switch
High speed SOC microcontroller C8051F
Based on the rapid development of semiconductor integration technology, various types of single-chip microcomputers are flooding into the market with each passing day, providing great convenience for the users of single-chip microcomputer technology.  Based on the MCS-48 series, INTEL has launched the high-performanc
[Microcontroller]
High speed SOC microcontroller C8051F
Programming method of 51 single chip microcomputer matrix keyboard
According to the circuit schematic, the keyboard scanning method is: row lines P1O~P13 are output lines, and column lines P14~P17 are input lines. At the beginning, the microcontroller outputs all row lines (P1O-P13) at a low level. At this time, the column line data is read in. If the column lines are all high, it me
[Microcontroller]
Programming method of 51 single chip microcomputer matrix keyboard
Failure analysis of 51 microcontroller programming chip failure to work
Fault phenomenon: I built a microcontroller temperature monitoring system. Everything is normal on the emulator, but the programming chip cannot work. Related circuit block diagram: Use 51 as the processor, the peripheral circuit is as shown in the figure, and a double integral conversion chip ICL7135 is used as A
[Microcontroller]
Failure analysis of 51 microcontroller programming chip failure to work
8051 MCU internal ROM structure, address distribution, resource utilization
  Management of program memory of 51 series microcontrollers:    Each ROM unit (byte) corresponds to a unique 16-bit address code (Address).    When the CPU wants to fetch instructions from a certain ROM unit, it does so by writing the address into a 16-bit special function register, the program counter PC (Progra
[Microcontroller]
8051 MCU internal ROM structure, address distribution, resource utilization
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号