//Timer 0 sets the timing time to 1 second Timer 1 sets the number of pulses counted within 1 second
void time0_init()
{
TMOD|=0x01; //Timer 0 timing mode count range 65536
TH0=0x3c; //Timing initial value 50ms
TL0=0xb0;
TR0=0; //Pause timer 0
}
Reference address:51 MCU simple frequency meter source code
#include
#define uchar unsigned char
#define uint unsigned int
#define uint unsigned int
uint ff; //Accept frequency value
uchar start=1; //Timer, counting mode start variable
uchar start=1; //Timer, counting mode start variable
code uchar seg[]={0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60};
code uchar tab[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe };
code uchar tab[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe };
void delay(uint k) //delay function
{
while(k--);
}
{
while(k--);
}
void show(uint k) //Numerical display function
{
static uchar i,j;
uint ss[5];
i=0;
do
{
ss[i]=k%10;
i++;
}while(k/=10);
for(j=0;j {
P0=seg[ss[j]];
P2=tab[j];
delay(100);
P0=0xff;
P2=0xff;
}
}
{
static uchar i,j;
uint ss[5];
i=0;
do
{
ss[i]=k%10;
i++;
}while(k/=10);
for(j=0;j {
P0=seg[ss[j]];
P2=tab[j];
delay(100);
P0=0xff;
P2=0xff;
}
}
void time0_init()
{
TMOD|=0x01; //Timer 0 timing mode count range 65536
TH0=0x3c; //Timing initial value 50ms
TL0=0xb0;
TR0=0; //Pause timer 0
}
void time1_init()
{
TMOD|=0x50; //Timer 1 counting mode, counting range 65536
TH1=0; //Counting initial value 0
TL1=0;
TR1=0; //Pause counter
}
{
TMOD|=0x50; //Timer 1 counting mode, counting range 65536
TH1=0; //Counting initial value 0
TL1=0;
TR1=0; //Pause counter
}
void str_init() //Timer interrupt
{
EA=1;
ET0=1;
}
{
EA=1;
ET0=1;
}
void time0_event() interrupt 1
{
static uchar i;
i++;
TH0=0x3c; //timing initial value 50ms
TL0=0xb0;
if(i==20)
{
i=0;
TR1=0; //stop counting
TR0=0; //stop timing
ff=(TH1*256+TL1); //find the frequency value, which is the number of pulses in 1 second
TH1=0; //clear the count value
TL1=0;
TH0=0x3c; //timing initial value 50ms
TL0=0xb0;
start=1; //start the timer and turn on the variable
}
}
{
static uchar i;
i++;
TH0=0x3c; //timing initial value 50ms
TL0=0xb0;
if(i==20)
{
i=0;
TR1=0; //stop counting
TR0=0; //stop timing
ff=(TH1*256+TL1); //find the frequency value, which is the number of pulses in 1 second
TH1=0; //clear the count value
TL1=0;
TH0=0x3c; //timing initial value 50ms
TL0=0xb0;
start=1; //start the timer and turn on the variable
}
}
void main()
{
time0_init();
time1_init();
str_init();
{
time0_init();
time1_init();
str_init();
while(1)
{
if(start==1)
{
TR0=1; //Start timer
TR1=1; //Start counter
start=0; //Close the start variable bit to ensure 1 second
{
if(start==1)
{
TR0=1; //Start timer
TR1=1; //Start counter
start=0; //Close the start variable bit to ensure 1 second
}
show(ff);
}
}
show(ff);
}
}
Previous article:PCF8591 (AD-DA) chip-51 single chip microcomputer program
Next article:51 MCU 18B20 temperature and serial port experiment
Recommended Content
Latest Microcontroller Articles
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
- 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
MoreDaily News
- New breakthrough! Ultra-fast memory accelerates Intel Xeon 6-core processors
- New breakthrough! Ultra-fast memory accelerates Intel Xeon 6-core processors
- Consolidating vRAN sites onto a single server helps operators reduce total cost of ownership
- Consolidating vRAN sites onto a single server helps operators reduce total cost of ownership
- 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!
Guess you like
- Watch the video to win a JD card | Taixiang's actual test of Shui Ge's secrets [Second issue]
- How to choose MCU in applications running at high temperature
- EEWORLD University Hall ---- 2019UDE on-site report: Appotronics leads laser display to a new peak
- SK Hynix only pays 17 months' salary as year-end bonus, sparking employee dissatisfaction
- Microcontroller Program Outsourcing: Urgently need help to write an MCU to control the RF chip to send and receive
- Apply for ADI smoke detection module for free, design smoke detectors more reasonably, and save lives more effectively.
- ROHM R Classroom: Power supply design technical information is continuously updated. Click to download
- Is the era of Wi-Fi 6 really coming? Are the original wireless routers going to be eliminated?
- CB140 Hardware Introduction
- ADI's LT8645SIV#PBF chip uses the EN pin to control power on and off, causing it to burn out. Help~