51单片机测量频率和占空比并用LCD显示代码
时间: 2023-11-26 21:05:14 浏览: 111
下面是用51单片机测量频率和占空比并用LCD显示的代码,注释已经添加在代码中:
```c
#include <reg51.h>
// 定义LCD显示所需的引脚和命令
#define LCD_RS P2_0
#define LCD_RW P2_1
#define LCD_EN P2_2
#define LCD_DAT P0
// 定义计时器计数值和捕获寄存器值
unsigned int count = 0;
unsigned int capture = 0;
// 函数声明
void LCD_Init();
void LCD_Com(unsigned char com);
void LCD_Data(unsigned char dat);
void LCD_Str(unsigned char *str);
void delay(unsigned int t);
void Timer_Init();
void Interrupt_Init();
void main()
{
// 初始化LCD和定时器
LCD_Init();
Timer_Init();
Interrupt_Init();
// 无限循环
while(1)
{
// 计算频率和占空比
float freq = 11059200.0 / (capture * 12.0);
float duty = (capture - count) * 100.0 / capture;
// 将频率和占空比转换为字符串
char freq_str[16];
char duty_str[16];
sprintf(freq_str, "Freq: %.2f Hz", freq);
sprintf(duty_str, "Duty: %.2f %%", duty);
// 在LCD上显示频率和占空比
LCD_Com(0x80); // 光标移到第一行第一列
LCD_Str(freq_str);
LCD_Com(0xC0); // 光标移到第二行第一列
LCD_Str(duty_str);
// 等待一段时间
delay(1000);
}
}
// LCD初始化函数
void LCD_Init()
{
LCD_Com(0x38); // 显示模式设置:16*2显示,5*7点阵,8位数据总线
LCD_Com(0x0C); // 显示控制:显示开,光标关,光标闪烁关
LCD_Com(0x06); // 输入模式设置:光标右移,字符不移动
LCD_Com(0x01); // 清屏
}
// 发送LCD命令函数
void LCD_Com(unsigned char com)
{
LCD_RS = 0; // 选择命令寄存器
LCD_RW = 0; // 选择写入模式
LCD_DAT = com; // 发送命令
LCD_EN = 1; // 使能LCD
delay(1); // 延时
LCD_EN = 0; // 关闭LCD
}
// 发送LCD数据函数
void LCD_Data(unsigned char dat)
{
LCD_RS = 1; // 选择数据寄存器
LCD_RW = 0; // 选择写入模式
LCD_DAT = dat; // 发送数据
LCD_EN = 1; // 使能LCD
delay(1); // 延时
LCD_EN = 0; // 关闭LCD
}
// 在LCD上显示字符串函数
void LCD_Str(unsigned char *str)
{
while(*str != '\0')
{
LCD_Data(*str++);
}
}
// 延时函数
void delay(unsigned int t)
{
unsigned int i, j;
for(i = 0; i < t; i++)
for(j = 0; j < 125; j++);
}
// 定时器初始化函数
void Timer_Init()
{
TMOD = 0x10; // 定时器1工作在模式1:16位定时器,仅用TR1打开
TH1 = 0x00; // 设置定时初值
TL1 = 0x00;
TR1 = 1; // 打开定时器
}
// 中断初始化函数
void Interrupt_Init()
{
EA = 1; // 总中断开关
ET1 = 1; // 打开定时器1中断
EX1 = 1; // 打开外部中断1(捕获中断)
IT1 = 1; // 设置外部中断1下降沿触发
}
// 定时器1中断服务函数
void Timer1_ISR() interrupt 3
{
count++; // 计数器加1
}
// 外部中断1(捕获中断)服务函数
void External1_ISR() interrupt 2
{
capture = TH1 * 256 + TL1; // 读取捕获寄存器值
count = 0; // 计数器清零
TH1 = 0x00; // 重置定时器初值
TL1 = 0x00;
}
```
注:该代码仅作为示例,具体实现方式可能会因为硬件等原因而有所不同。建议根据具体硬件情况进行调整。
阅读全文
相关推荐
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)