Frequency is defined as cycles per second. It can also be defined as the reciprocal of the total time "T". In this project we will count the number of pulses entering port 3.5 of 8051 microcontroller and display it on 16*2 LCD display. So basically we measured the signal frequency at 8051 port 3.5. Here we use the AT89S528051 chip and use the 555 IC in unstable mode to generate sampling pulses for demonstration.
Required components:
8051 microcontroller (AT89S52)
16*2 LCD display
Frequency source (555 timer)
Potentiometer
Connecting line
Circuit diagram:
Use 8051 timer to measure frequency:
The 8051 microcontroller is an 8-bit microcontroller with 128 bytes of on-chip RAM, 4K bytes of on-chip ROM, two timers, one serial port and four 8-bit ports. 8052 microcontroller is an extension of microcontroller. To configure port 3.5 as a counter, set the TMOD register value to 0x51. The figure below shows the TMOD register.
Door | C/T | M1 | M0 | Door | C/T | M1 | M2 |
Timer 1 | timer 0 |
GATE - When GATE is set, the timer or counter is only enabled when the INTx pin is high and the TRx control pin is set to the TRx control pin. When GATE is cleared, the timer is enabled as long as the TRx control bit is set to U.
C/T – When C/T=0, it acts as a timer. When C/T = 1, it acts as a counter.
M1 and M0 indicate working modes.
对于 TMOD = 0x51,定时器1充当计数器,并在模式1(16位)下运行。
16 * 2 LCD用于以赫兹(Hz)显示信号的频率。如果您不熟悉 16x2 LCD,请在此处查看有关 16x2 LCD 引脚及其命令的更多信息。另请查看如何将液晶屏与8051连接。
555定时器作为频率源:
频率源应产生方波,最大幅度限制为5V,因为8051微控制器的端口无法处理大于5V的电压。它可以测量的最大频率为655.35 KHz,因为TH1和TL1寄存器的内存限制(每个8位)。在 100 毫秒内,TH1 和 TL1 最多可容纳 65535 个计数。因此,可以测量的最大频率为 65535 * 10 = 655.35 KHz。
在这个 8051 频率计项目中,我在非稳定模式下使用 555 定时器来产生可变频率方波。555 IC产生的信号频率可以通过调整电位计来改变,如本项目结束时给出的视频所示。
在本项目中,Timer1 (T1) 对进入 8051 微控制器端口 3.5 的脉冲数进行 100 毫秒的计数。计数值将分别存储在 TH1 和 TL1 寄存器中。为了组合TH1和TL1寄存器的值,使用以下公式。
Pulses = TH1 * (0x100) + TL1
现在,“脉冲”将在 100 毫秒内具有周期数。但信号的频率定义为每秒的周期数。要将其转换为频率,请使用以下公式。
Pulses = Pulses * 10
工作和代码解释:
本项目结束时给出了该频率计的完整 C 程序。代码被分成有意义的小块,并在下面解释。
对于与8051微控制器的16 * 2 LCD接口,我们必须定义16 * 2 LCD连接到8051微控制器的引脚。16*2 LCD 的 RS 引脚连接到 P2.7,16*2 LCD 的 RW 引脚连接到 P2.6,16*2 LCD 的 E 引脚连接到 P2.5。数据引脚连接到 8051 微控制器的端口 0。
sbit rs=P2^7;
sbit rw=P2^6;
sbit en=P2^5;
接下来,我们必须定义一些在程序中使用的函数。延迟功能用于创建指定的时间延迟。Cmdwrt功能用于向16 * 2 LCD显示器发送命令。Datawrt功能用于将数据发送到16 * 2 LCD显示器。
void delay(unsigned int) ;
void cmdwrt(unsigned char);
void datawrt(unsigned char);
在代码的这一部分中,我们将命令发送到 16*2 lcd。清除显示、递增光标、强制光标以 1 开头等命令圣在指定的时间延迟后,线被一一发送到16 * 2液晶显示器。
for(i=0;i<5;i++)
{
cmdwrt (cmd[i]);
delay (1);
}
在代码的这一部分中,定时器1配置为计数器,操作模式设置为模式1。
定时器0配置为定时器,操作模式设置为模式1。定时器1用于计算脉冲数,定时器0用于产生延时。TH1 和 TL1 值设置为 0,以确保计数从 0 开始。
TMOD=0x51;
TL1=0;
TH1=0;
在代码的这一部分中,计时器运行 100 毫秒。使用延迟功能生成 100 毫秒的延迟。TR1=1 用于启动计时器,TR1=0 用于在 100 毫秒后停止计时器。
TR1=1;
delay(100);
TR1=0;
在代码的这一部分中,将 TH1 和 TL1 寄存器中存在的计数值组合在一起,然后乘以 10 以获得 1 秒内的总周期数。
Pulses = TH1*(0x100) + TL1;
Pulses = pulses*10;
在代码的这一部分中,频率值被转换为单个字节,以便于在16 * 2 LCD显示器上显示。
d1 = pulses % 10;
s1 = pulses % 100;
s2 = pulses % 1000;
s3 = pulses % 10000;
s4 = pulses % 100000;
d2 = (s1-d1) / 10;
d3 = (s2-s1) / 100;
d4 = (s3-s2) / 1000;
d5 = (s4-s3) / 10000;
d6 = (pulses-s4) / 100000;
在代码的这一部分中,频率值的各个数字被转换为ASCII格式,并显示在16 * 2 LCD显示屏上。
If (pulses>=100000)
datawrt ( 0x30 + d6);
if(pulses>=10000)
datawrt( 0x30 + d5);
if(pulses>=1000)
datawrt( 0x30 + d4);
if(pulses>=100)
datawrt( 0x30 + d3);
if(pulses>=10)
datawrt( 0x30 + d2);
datawrt( 0x30 + d1);
在代码的这一部分中,我们将命令发送到 16*2 LCD 显示器。该命令将复制到 8051 微控制器的端口 0。对于命令写入,RS 设置为低电平。对于写入操作,RW 设置为低电平。在使能 (E) 引脚上施加高到低脉冲以启动命令写入操作。
void cmdwrt (unsigned char x)
{
P0=x;
rs=0;
rw=0;
en=1;
delay(1);
en=0;
}
In this part of the code, we send data to the 16 * 2 LCD display. The data will be copied to port 0 of the 8051 microcontroller. RS is set high for command writes. For write operations, RW is set low. Apply a high-to-low pulse on the enable (E) pin to initiate a data write operation.
void datawrt (unsigned char y)
{
P0=y;
rs=1;
rw=0;
en=1;
delay(1);
en=0;
}
This is how we measure the frequency of any signal using 8051 microcontroller.
#include
sbit rs=P2^7;
sbit rw=P2^6;
sbit en=P2^5;
void delay(unsigned int);
void cmdwrt(unsigned char);
void datawrt(unsigned char);
void main (void)
{
unsigned long int pulses;
unsigned char i;
unsigned int s1,s2,s3,s4;
unsigned char d1,d2,d3,d4,d5,d6;
unsigned char cmd[]={0x38,0x01,0x06,0x0c,0x82} ;
unsigned char msg[]={"Freq: "};
unsigned char msg2[]={" Hz"};
for(i=0;i<5;i++)
{
cmdwrt(cmd[i]);
delay(1 );
}
while(1)
{
TMOD=0x51;
TL1=0;
TH1=0;
TR1=1;
delay(100);
TR1=0;
pulses= TH1*256 + TL1;
pulses=pulses*10;
d1=pulses %10;
s1=pulses%100;
s2=pulses%1000;
s3=pulses%10000; s4=pulses%100000
;
d2=(s1-d1)/10;
d3=(s2-s1)/100;
d4=( s3-s2)/1000;
d5=(s4-s3)/10000;
d6=(pulses-s4)/100000;
cmdwrt(0x01);
delay(1);
for(i=0;msg[i]!=' ';i++)
datawrt(msg[i]);
if(pulses>=100000)
datawrt(0x30+d6);
if(pulses>=10000)
datawrt(0x30+d5);
if(pulses>=1000)
datawrt(0x30 +d4);
if(pulses>=100)
datawrt(0x30+d3);
if(pulses>=10)
datawrt(0x30+d2);
datawrt(0x30+d1);
for(i=0;msg2[i]! ='';i++)
datawrt(msg2[i]);
delay(1000);
}
}
void cmdwrt (unsigned char x)
{
P0=x;
rs=0;
rw=0;
en=1;
delay(1);
en=0;
}
void datawrt (unsigned char y)
{
P0=y;
rs=1;
rw=0;
en=1;
delay(1);
en=0;
}
void delay(unsigned int z)
{
unsigned int p ;
for(p=0;p
Previous article:How many interrupt sources are there in 8051 microcontroller? There are several interrupt sources in 8051 microcontroller.
Next article:Audio peak collection terminal design based on C8051F020 microcontroller and RTL8019AS
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- 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
- Writing cmd files in CCS
- Switching Power Supply Interest Group 17th Task
- The live broadcast has ended [Microchip Embedded Security Solutions | RISC-V Safe Place]
- I am offering a reward to find a DSP expert with sufficient skills to be my mentor. It will be paid.
- Old topic: Why do we need to place a 0.1uF capacitor near the chip IC?
- fdc2214 oled capacitor test msp430 program source code
- Can NPN stn0214 work with 500V added to VCE?
- The application principle and connection method of AND gate circuit
- Fundamentals of Digital Logic with Verilog Design 3rd Edition
- [2022 Digi-Key Innovation Design Competition] Material Unboxing STM32H745I-DISCO