Frequency measurement principle: frequency is measured by counting method.
Implementation method: Using the feature of 51 MCU timer as external pulse counter, frequency counting can be realized. Specifically, two timers are used. In this example, timer 0 is used as external pulse counter to capture external pulse (falling edge trigger), and timer 3 generates 1s timing to extract counting frequency.
code show as below
//System clock frequency 24MHz
#include "STC8.H"
bit send_flag = 0;
unsigned int freq = 0; //frequency count
unsigned long cont_freq = 0; //Frequency count overflow
unsigned char res[6]; //Frequency conversion result
void TM0_Init();
void TM3_Init();
void UartSend(unsigned char dat);
void UartInit(void);
void UartSendStr(unsigned char *p);
void Num_Cut(unsigned long num);
void Uart_Send_Str(unsigned char str[],unsigned char len,bit mode);
void main ()
{
TM0_Heat();
TM3_Init();
UartInit();
while(1)
{
if(send_flag)
{
Num_Cut(cont_freq+(unsigned long)freq); //Frequency value equals overflow value plus current value
send_flag = 0;
UartSendStr("Freq: ");
Uart_Send_Str(res,6,1);
Uart_Send_Str(res,6,0);
UartSendStr(" Hzn");
}
}
}
/******************************************************************************
* Description: Timer 3 initialization function, timing time 1ms
* Parameters: None
* Return value: None
******************************************************************************/
void TM3_Init()
{
T3L = 0x35; //Set the initial timing value, 1ms
T3H = 0xA2; //Set the initial timing value
T4T3M |= 0x08; //Start the timer
T4T3M |= 0x02; //Timer clock 1T mode
IE2 |= ET3; //Enable timer interrupt
EA = 1; // Enable general interrupt
}
/******************************************************************************
* Description: Timer 0 initialization function, external counting mode
* Parameters: None
* Return value: None
******************************************************************************/
void TM0_Init()
{
TMOD |= 0x04; //Set external counting mode
TMOD |= 0x01; //Working mode 1, 16-bit does not automatically reload
TR0 = 1; //Open the counter
TH0 = 0x00; //Counter reset
TL0 = 0x00;
ET0 = 1; //Open counter interrupt
}
/******************************************************************************
* Description: Timer 0 interrupt service function
* Parameters: None
* Return value: None
******************************************************************************/
void TM0_Isr() interrupt 1
{
cont_freq += 65536; // Overflow count, each overflow, the count times is TH0<<8|TL0 = 0xffff, after debugging, add 1
TH0 = 0x00; //Reset counter
TL0 = 0x00; //Reset counter
}
/******************************************************************************
* Description: Timer 3 interrupt service function
* Parameters: None
* Return value: None
******************************************************************************/
void TM3_Isr() interrupt 19
{
static unsigned int ms_flag = 0; //1s count flag
if(ms_flag++ == 1000)
{
freq = (TH0<<8|TL0); //Get the current counter value
ms_flag = 0; //1s count flag reset
send_flag = 1; //Serial port send flag is set
TH0 = 0x00; //Reset counter
TL0 = 0x00; //Reset counter
}
AUXINTIF &= ~T3IF; // Clear interrupt flag
}
/******************************************************************************
* Description: Serial port 1 initialization function, baud rate 9600
* Parameters: None
* Return value: None
******************************************************************************/
void UartInit(void) //9600bps@24.000MHz
{
SCON |= 0x50; //8-bit data, variable baud rate
AUXR |= 0x01; //Serial port 1 selects timer 2 as the baud rate generator
AUXR |= 0x04; //Timer 2 clock is Fosc, i.e. 1T
T2L = 0x8F; //Set the initial timing value
T2H = 0xFD; //Set the initial timing value
AUXR |= 0x10; //Start timer 2
}
/******************************************************************************
* Description: Serial port character sending and function
* Input parameter: Send character
* Return value: None
******************************************************************************/
void UartSend(unsigned char dat)
{
SBUF =that;
while(!IF);
TI = 0;
}
/******************************************************************************
* Description: Serial port string sending function
* Input parameter: Send string
* Return value: None
******************************************************************************/
void UartSendStr(unsigned char *p)
{
while (*p)
{
UartSend(*p++);
}
}
/******************************************************************************
* Description: Number splitting function
* Input parameter: Split number
* Return value: None
******************************************************************************/
void Num_Cut(unsigned long num)
{ unsigned long temp = num;
unsigned char cont = 5;
while(temp != 0)
{
res[cont--] = (unsigned char)(temp % 10);
temp /= 10;
}
cont_freq = 0;
}
/******************************************************************************
* Description: Serial port digital array sending function
* Input parameters: str: array, len: array length or data length to be sent, mode: 1 to send, 0 to clear the array
* return value:
******************************************************************************/
void Uart_Send_Str(unsigned char str[],unsigned char len,bit mode)
{
unsigned char k;
if(mode == 1)
{
for(k = 0;k < len; k++)
{
UartSend(48 + str[k]);
}
}else
{
for(k = 0;k < len; k++)
{
str[k] = 0;
}
}
}
The test results are analyzed as follows:
Previous article:Blue Bridge Cup STC15 MCU Review - Use of IO Ports
Next article:STC15 single chip microcomputer frequency measurement
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- USB Type-C® and USB Power Delivery: Designed for extended power range and battery-powered systems
- RAQ #223: How to measure and determine soft-start timing without a soft-start equation?
- RAQ #223: How to measure and determine soft-start timing without a soft-start equation?
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- STMicroelectronics IO-Link Actuator Board Brings Turnkey Reference Design to Industrial Monitoring and Equipment Manufacturers
- Confirm that your personal information is correct
- [Raspberry Pi 3B+ Review] Burning System & Locking Static IP & SSH Login & SFTP Login
- STM32F407VET6 Another question about PWM wave
- Want to exchange, use TI LM3S8962EVALUATION KIT for an NXP Rapid IoT
- The difference between radio remote control and infrared remote control
- Fieldbus-based frequency conversion drive system for paper machines
- MSP430 infrared remote control design
- ADI [Hybrid Electric Vehicle (HEV)/Electric Vehicle (EV)] Lithium Battery Management Solutions
- Computer peripherals expert - Jinhetian mobile hard disk enclosure
- A New Inrush Current Limiter