一、利用中断发出1Khz的方波信号,驱动蜂鸣器鸣叫
在Proteus中绘制仿真图:
实现代码:
#include<reg51.h>
sbit sound = P1 ^ 7;
#define f1(a) (65536-a)/256
#define f2(a) (65536-a)%256
unsigned int i = 500;
unsigned int j = 0;
void main(void)
{
EA = 1;
ET1 = 1;
TMOD = 0x10;
TL1 = f2(i);
TR1 = 1;
while (1)
{
i = 460;
while (j < 2000);
j = 0;
i = 360;
while (j < 2000);
j = 0;
}
}
void T1_(void) interrupt 3 using 0
{
TR1 = 0;
sound = ~sound;
TH1 = f1(i);
TL1 = f2(i);
j++;
TR1 = 1;
}
结果演示:
普中开发板演示:
视频:
普中开发板蜂鸣器
二、LED数码管秒表的制作。
Proteus仿真图绘制:
实现代码:
#include<reg51.h> //头文件
unsigned char code discode1[] = { 0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef };
//数码管显示0~9的段码表, 带小数点
unsigned char code discode2[] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f };
//数码管显示0~9的段码表,不带小数点
unsigned char timer = 0; //timer记录中断次数
unsigned char second; //second储存秒
unsigned char key = 0; //key记录按键次数
main() //主函数
{
TMOD = 0x01; //定时器T0方式1定时
ET0 = 1; //允许定时器T0中断
EA = 1; //总中断允许
second = 0; //设初始值
P0 = discode1[second / 10]; //显示秒位0
P2 = discode2[second % 10]; //显示0.1s位0
while (1) //循环
{
if ((P3 & 0x80) == 0x00) //当按键被按下时
{
key++; //按键次数加1
switch (key) //根据按键次数分三种情况
{
case 1: //第一次按下为启动秒表计时
TH0 = 0xee; //向TH0写入初值的高8位
TL0 = 0x00; //向TL0写入初值的低8位,定时5ms
TR0 = 1; //启动定时器T0
break;
case 2: //按下两次暂定秒表
TR0 = 0; //关闭定时器T0
break;
case 3: //按下3次秒表清0
key = 0; //按键次数清
second = 0; //秒表清0
P0 = discode1[second / 10]; //显示秒位0 P2=discode2[second%10]; //显示0.1s位0
break;
}
while ((P3 & 0x80) == 0x00); //如果按键时间过长在此循环
}
}
}
void int_T0() interrupt 1 using 0 //定时器T0中断函数
{
TR0 = 0; //停止计时,执行以下操作(会带来计时误差)
TH0 = 0xee; //向TH0写入初值的高8位
TL0 = 0x00; //向TL0写入初值的低8位,定时5ms
timer++; //记录中断次数
if (timer == 20) //中断20次,共计时20*5ms=100ms=0.1s
{
timer = 0; //中断次数清0
second++; //加0.1s
P0 = discode1[second / 10]; //根据计时,即时显示秒位
P2 = discode2[second % 10]; //根据计时,即时显示0.1s位
}
if (second == 99) //当计时到9.9s时
{
TR0 = 0; //停止计时
second = 0; //秒数清0
key = 2; //按键数置2,当再次按下按键时, //key++,即key=3,秒表清0复原
}
else //计时不到9.9s时
{
TR0 = 1; //启动定时器继续计时
}
}
Proteus演示:
普中开发板演示:
视频:
普中开发板数码管秒表
三、使用定时器实现一个LCD显示时钟。
Proteus仿真图绘制:
代码实现:
#include <REGX52.H>
//LCD引脚配置
sbit LCD_RS=P3^5;
sbit LCD_RW=P3^6;
sbit LCD_EN=P3^7;
#define LCD_DataPort P2
unsigned char Hour=23,Min=59,Sec=55;
//LCD延时函数
void Delay()
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
//LCD写命令
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_EN=1;
Delay();
LCD_EN=0;
Delay();
}
//LCD写数据
void LCD_WriteData(unsigned char Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_EN=1;
Delay();
LCD_EN=0;
Delay();
}
//LCD设置光标位置
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
else if(Line==2)
{
LCD_WriteCommand(0x80|(Column-1+0x40));
}
}
//LCD显示字符串
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
//LCD显示数字
int LCD_Pow(int X,int Y)
{
unsigned char i;
int Result=1;
for(i=0;i<Y;i++)
{
Result*=X;
}
return Result;
}
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
}
}
//LCD初始化
void LCD_Init()
{
LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
LCD_WriteCommand(0x01);//光标复位,清屏
}
//定时器初始化
void Timer0_Init()
{
TMOD=0x01;
TH0=0xFc;
TL0=0x66;
TF0=0;
TR0=1;
ET0=1;
EA=1;
}
void main()
{
LCD_Init();
Timer0_Init();
LCD_ShowString(1,1," : : ");
while(1)
{
LCD_ShowNum(1,1,Hour,2);
LCD_ShowNum(1,4,Min,2);
LCD_ShowNum(1,7,Sec,2);
}
}
void Timer0_Routine() interrupt 1
{
static unsigned int T0Count;
TH0=0xFc;
TL0=0x66;
T0Count++;
if(T0Count==1000)
{
Sec++;
T0Count=0;
}
if(Sec>=60)
{
Sec=0;
Min++;
}
if(Min>=60)
{
Min=0;
Hour++;
}
if(Hour>=24)
{
Hour=0;
}
}
Proteus演示:
普中开发板演示:
普中开发板LCD秒表
四、甲乙两个单片机串口通信
Proteus仿真图绘制:
代码实现:
//甲机发送
#include <REGX52.H>
sbit T_P=PSW^0;
unsigned char code Tab[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//流水灯程序
void Send(unsigned char dat)
{
TB8=T_P;
SBUF=dat;
while(TI==0);
TI=0;
}
void Delay(unsigned char xms) //@11.0592MHz
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
}
void main()
{
unsigned char i;
TMOD=0x20;
SCON=0xc0;
PCON&=0x7f;
TH1=0xfd;
TL1=0xfd;
TR1=1;
while(1)
{
for(i=0;i<8;i++)
{
Send(Tab[i]);
Delay(200);//约200ms发送数据
}
}
}
//乙机接收
#include <REGX52.H>
sbit R_P=PSW^0;
unsigned char Receive()//接收一字节数据
{
unsigned char dat;
while(RI==0);//检测RI,RI=0,未接收完
RI=0; //接收数据完成RI手动清0
ACC=SBUF; //将接收缓冲器的数据存于ACC
if(RB8=R_P) //只有偶检验成功才能往下执行,接收数据
{
dat=ACC; //将ACC数据存于dat
return dat; //将接收的数据返回
}
}
void main()
{
TMOD=0x20; //设置定时器为方式2,8位自动重载
SCON=0xd0; //串口为方式3,允许接收REN=1
PCON&=0x7f; //波特率不加倍
TH1=0xfd; //波特率9600
TL1=0xfd;
TR1=1;
//REN=1;
while(1)
{
P2=Receive(); //将接收的数据送至P2口显示
}
}
Proteus演示:
五、总结
通过本次实验,我对Proteus和普中51单片机的应用开发有了更为深入的了解。在实践中,我逐步掌握了中断、定时器和串口通信等关键技术,不仅加深了对理论知识的理解,还提高了实际操作能力。在编程过程中,我锻炼了逻辑思维和解决问题的能力,感受到了单片机编程的魅力与挑战。这次实验让我收获颇丰,对未来在单片机领域的学习和工作都具有重要的指导意义。