单片机定时器和串口通信

单片机定时器和串口通信

一、驱动蜂鸣器鸣叫

1.代码实现

#include <REGX52.H>
sbit sound=P3^5;
void main()
{
	EA=1;
	ET1=1;
	TMOD=0x10;
	TH1=0xfx; //中断58日微秒
	TL1=0x33;
	TR1=1;
	while(1){ }
}
void Timer1_Routine() interrupt 3l//中断函数
{
	TH1=exfe;
	TL1=0x33;
	sound=~sound;
}

2.普中仿真结果
在这里插入图片描述

3.波形图展示

在这里插入图片描述

二、LED数码管秒表

1.proteus电路图

在这里插入图片描述

2.代码实现

#include<reg51.h>
typedef unsigned int uint;	 //定义无符号整形和字符型
typedef unsigned char uchar;

uchar led[] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar led1[] = {0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
uchar second; //秒数
uchar key; //按键次数
uint t; //用来计数,每500,代表0.1s

sbit keyif = P3^1;	//按键接口
void delay(){ //延时函数,用于消除抖动
	uchar i,j;
i = 36;
	j = 217;
	do
	{
		while (--j);
	} while (--i);
}

void init(void) //初始化
{
	TMOD = 0x02;  //0000 0001 使用方式1
	second = 0; //秒数初始化为0
	EA = 1;	 //总中断,定时器0中断允许
	ET0 = 1;  //允许定时器0中断
	key = 0; //按键次数初始化为0
	t = 0; //计数初始化为0
}

void main(){
	init();
	P2 = led1[second/10];
	P0 = led[second%10];
	while(1){
		if(keyif == 0){
			delay();//消除抖动	 
			if(keyif == 0){		  
				key++;
			switch(key){
				   case 1: //按一次,计时器开始
				   		TH0 =  0x38;
						TL0 = 0x38;
						TR0 = 1;			   		
				   		break;
				   case 2:	//按两次,暂停定时器
				   		t = 0; 
				   		TR0 = 0;
				   		break;
				   case 3: //按三次,停止计时,数据清零
				   		key = 0;
						second = 0;
						P2 = led1[0];
						P0 = led[0];
				   		break;
					}
				while(keyif == 0);  //若一直按下,使其停留			
			}
		}
	}
}

void timer() interrupt 1
{
	TR0 = 0; //停止计时
	t++;
	if(t ==500){
		second++;
		P2 = led1[second/10];
		P0 = led[second%10];
		t = 0;
	}
	if(second == 99){ //当计数到9.9秒,重新开始计时
		second = 0;
		key = 2; //相当于重新开始计时
	}
	TR0 = 1; //继续启动计时器		
}

3.proteus仿真结果

在这里插入图片描述

4.普中仿真结果

在这里插入图片描述

三、LCD1602显示时钟

1.proteus电路图

在这里插入图片描述

2.代码实现

1)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;
	}
}

2)普中代码

#include <REGX52.H>
//引脚配置
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_E=P2^7;
#define LCD_DataPort  P0

void Delay(unsigned int xms)		//@11.0592MHz
{
	unsigned char i,j;
	while(xms--)
	{
		i = 2;
		j = 199;
		do
		{
			while (--j);
		} while (--i);
	}
}
void LCD_WriteCommand(unsigned char Command)//LCD写指令
{	
	LCD_RS=0;
	LCD_RW=0;
	LCD_DataPort=Command;
	LCD_E=1;
	Delay(1);//高低电平转换过快单片机无法处理,加入延时
	LCD_E=0;
	Delay(1);
}
	
void LCD_WriteData(unsigned char Data)//LCD写数据
{
	LCD_RS=1;
	LCD_RW=0;
	LCD_DataPort=Data;
	LCD_E=1;
	Delay(1);
	LCD_E=0;
	Delay(1);
}

void LCD_Init()//LCD初始化
{
	LCD_WriteCommand(0x38);//八位数据接口,两行显示
	LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
	LCD_WriteCommand(0x06);//数据读写操作后,光标自动加1,画面不动
	LCD_WriteCommand(0x01);//清屏
}

void LCD_SetAC(unsigned char Line,unsigned char Column)//LCD设置光标位置
{
		//设置光标地位,即写入的地置
	if(Line==1)
	{
		LCD_WriteCommand(0x80|(Column-1));//0x80为DDRAM地址设置的指令
	}
	else
	{
		LCD_WriteCommand(0x80|(Column-1)+0x40);
	}
}

void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)//LCD显示一个字符:数据写入DDRAM地址,前16位映射显示屏
{
		LCD_SetAC(Line,Column);
		LCD_WriteData(Char);
}

void LCD_ShowString(unsigned char Line,unsigned char Column,unsigned char *String)
{
	unsigned char i;
		LCD_SetAC(Line,Column);//光标位置
	for(i=0;String[i]!='\0';i++)
	{
		LCD_WriteData(String[i]);
	}
}

int LCD_Power(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)//LCD显示数字
{
	unsigned char i;
	LCD_SetAC(Line,Column);
	for(i=Length;i>0;i--)
	{	//例如:789/100%10=7 789/10%10=8 789/1%10=9
		LCD_WriteData('0'+Number/LCD_Power(10,i-1)%10);//转化为ASCII码
	}
}

void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)//LCD显示有符号数字
{
	unsigned char i;
	unsigned int Number1;//有符号int:-32768~32767有一个数取反取不到,因此定义一个无符号int:Number1
	LCD_SetAC(Line,Column);
	if(Number>=0)
	{
		LCD_WriteData('+');
		 Number1=Number;
	}
	else 
	{
		LCD_WriteData('-');
		Number1=-Number;
	}
		for(i=Length;i>0;i--)
	{	//例如:789/100%10=7 789/10%10=8 789/1%10=9
		LCD_WriteData('0'+Number1/LCD_Power(10,i-1)%10);//转化为ASCII码
	}
}

void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)//LCD显示16进制数字
{
	unsigned char i;
	unsigned char SingleNumber;
	LCD_SetAC(Line,Column);
	for(i=Length;i>0;i--)
	{	
		SingleNumber=Number/LCD_Power(16,i-1)%16;
		if(SingleNumber<10)
		LCD_WriteData('0'+SingleNumber);//转化为ASCII码
		else
		LCD_WriteData('A'+SingleNumber-10);
	}
}

void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)//LCD显示2进制数字
{
	unsigned char i;
	unsigned char SingleNumber;
	LCD_SetAC(Line,Column);
	for(i=Length;i>0;i--)
	{	
		SingleNumber=Number/LCD_Power(2,i-1)%2;
		LCD_WriteData('0'+SingleNumber);//转化为ASCII码
	}
}


3.proteus仿真

在这里插入图片描述

4.普中仿真
在这里插入图片描述

四、双单片机信道

1、protues原理图

在这里插入图片描述

2、代码实现

//甲机发送
#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口显示
	}
}

3、proteus仿真
在这里插入图片描述

4、普中仿真
在这里插入图片描述

五、总结

串口的时候一定要弄得清楚谁接受谁发送

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值