⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 communication1.c.bak

📁 用KEILC写的单片机典型C51编程
💻 BAK
字号:
/*********************************************************************************/
	//程序名:commucation1.c
	//功能描述:利用单片机的串口实现双机双工通信
	//调用函数:Timer_10ms(uint_1ms),Led_Off(),Led_On()
	//输入:
	//返回:
	//设计者:wei												日期:2006.10.25
	//修改者:												日期:
	//版本:
	/********************************************************************************/
	
	
	#include<Reg51.h>
	#define	unit		unsigned int
	#define uchar	unsigned char
	#define ulong unsigned long
 
	#define OSC_FREQ						12000000
	#define	C10ms								(65536-OSC_FREQ/(12000000/9970))
	
	uchar ucSBUF232;
	
	/******************************************************************************************
	 *********************************初始化CPU****************************************
	 *****************************************************************************************/
	 void	serial_Init(void)
	 {
	 	IE =0x00;
	 	TMOD = 0x20;
	 	TH0 = C10ms/256;
	 	TL0 = C10ms%256;
	 	TH1 = 0xfa;
	 	TL1 = 0xfa;
	 	PCON = 0x00;
	 	SCON = 0xd8;
	 	
	 	RI = 0 ;
	 	TI = 0;
	 	TR0 = 0;
	 	TR1 = 1;
	 	ES = 1;
	 	EA = 1;
	 }
	 /******************************************************************************************
	 *********************************串行口中断处理****************************************
	 *****************************************************************************************/
	 void	serialInterrupt() interrupt 4
	 {
	 	if(RI)
	 		{
	 			RI = 0;
	 			ucSBUF232 = SBUF;
	 			if(ucSBUF232 == 0x01)
	 				{
	 				  P1 = 0x01;
	 				}
	 			else if(ucSBUF232 == 0x02)
	 				{
	 					P1 = 0x02;		
	 				}
	 			else if(ucSBUF232 == 0x04)
	 				{
	 					P1 = 0x04;		
	 				}	
	 		}		
	 	else if(TI)
	 		{ 
	 			TI = 0;
	 			REN =1;
	 			ucSBUF232 = 0xff;
	 		}
	 }
	  
 /******************************************************************************************/
	//函数名:void Delay(void)
	//功能:延时
	//调用函数:
	//输入参数:
	//输出参数:
	//说明:
	/*******************************************************************************************/

 void Delay(void)
 {
 	uchar i,j,h;
 	for(i = 0;i<2;i++)
 	{
 		for(j=0;j<200;j++)
 		{
 			for(h=0;h<250;h++);
 		}
 	}
 }
 /******************************************************************************************
	 *********************************主函数:初始化CPU****************************************
	 *****************************************************************************************/
	 
	 void main(void)

	 {
	 	uchar i=0;
	 	serial_Init();
	 	REN = 0;
	 	RI = 0;
	 	while(1)
	 	{
	 		if(ucSBUF232 == 0x01)
	 			{
	 			    P1=0x01;
	 				Delay();
	 				Delay();
	 				SBUF = 0x02;
	 			}
	 		else if(ucSBUF232 == 0x02)
	 			{
	 				P1=0x02;
	 				Delay();
	 				Delay();
	 				SBUF = 0x04;
	 			}	
	 		else if(ucSBUF232 == 0x04)
	 			{
	 				P1=0x04;
	 				Delay();
	 				Delay();
	 				SBUF = 0x01;
	 			}		
	 			else
	 				{
	 					SBUF=0x01;
	 					Delay();
	 					Delay();
	 					Delay();
	 					P1 =0x00;
	 					if(REN && ucSBUF232 ==0xff)
	 						REN=0;
	 				}
	 	}
	 }
	 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -