comm.c

来自「芯片at89s52」· C语言 代码 · 共 118 行

C
118
字号
/******************************************************************************
 * File   Name: comm.c														  *
 * Create Time: 2007/ / 													  *
 * Description:																  *
 * Copyright  :																  *
 * History    :																  *
 * |---Data---|---Author---|--Version--|--------------Description-------------|
 *****************************************************************************/
 
 #ifdef _LCD_ONOFF_
/******************************
 *      Head File			  *
 *****************************/
 #include"comm.h"
/******************************
 *     Global Variable		  *
 *****************************/
 uchar rdata=0x00;
/******************************
 *	   Const Segment		  *
 *****************************/
 code const uchar pos[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//片选
 uchar posbit=0x00;
/******************************
 *      Function  Implement	  *
 *****************************/
 
/*******************************************************************************
Function Name: 端口初始化程序												   *
Target :  进行端口初始化													   *
Input  Parameter : 															   *
Output Parameter : 															   *
Function Description : 														   *
*******************************************************************************/
void Comm_init(void)
{   
	EA = 0;
	RCLK = 1;  //接收器的波特率由RCLK选择,1时由T/C2决定,0时由T/C1决定
	TCLK = 1;  //发送器的波特率由TCLK选择,1时由T/C2决定,0时由T/C1决定
	EXEN2 = 1; //T/C2外部允许标志 1 
	CP_RL2 = 1; //设置T/C2,波特率发生器工作方式,做为串口的波特率发生器
	RCAP2H = 0xFF; //T/C2的初值,使波特率为9600
	RCAP2L = 0xB8; //波特率=Fosc/(2*16*[65536-(RCAP2H、RCAP2L)]	fosc=22118400
	SM0 = 0;  //使串口工作在方式1
	SM1 = 1;  
	REN = 1; //串行口初始化 ,允许接收
	TR2 = 0x01; //启动T/C2 
/**/ES = 0;	  //串行接口中断允许位  1允许中断   0 禁止中断
	EA = 1;	  //cpu中断允许位   1 cpu允许中断   0 cpu 屏蔽一切中断	
	
}							
/*******************************************************************************
Function Name: 	单字符串口发送函数											   *
Target : 通过串口发送一个字符												   *
Input  Parameter : 需要发送的字符内容,unsigned char 类型					   *
Output Parameter : 无														   *
Function Description : 														   *
*******************************************************************************/
void ComSendChar(uchar out)
{
   SBUF = out; //发送数据   
   while (TI == 0) //等待数据发送	 TI 发送中断标志位,发送完一帧信息,由硬件置1
   {;}
   TI = 0; //TI清零 
}
/*******************************************************************************
Function Name: 字符串串口发送函数											   *
Target :  通过串口发送一个字符串											   *
Input  Parameter : 	需要发送的字符串										   *
Output Parameter : 															   *
Function Description : 														   *
*******************************************************************************/
void ComSendStr(char str[])
{
	uchar tmp,tmp2;
	tmp2 = strlen(str);//
	for(tmp=0;tmp<tmp2;tmp++)
	ComSendChar(*(str+tmp));//*(str++)为什么不行
}
/*******************************************************************************
Function Name: 延时函数														   *
Target :  实现软件延时														   *
Input  Parameter : 	延时时间 ,单位毫秒										   *
Output Parameter : 															   *
Function Description : 														   *
*******************************************************************************/
void Delay(int i) //延时程序
{
	int k;
	while ((i--)!= 0)
	{
		for (k = 0; k<= 0xFFF; k++)
		{;}			 
	}
}
/*******************************************************************************
Function Name: 单字符串口接受函数											   *
Target :  通过串口接受发送的一个字符										   *
Input  Parameter : 无														   *
Output Parameter : 一个char													   *
Function Description : 														   *
*******************************************************************************/
uchar ComReceiveChar(void)	
{ 
	uchar in;
	if (RI == 1) //判断是否有数据到达
	{
		in = SBUF;
		RI = 0;
	}
	else in = 0xFF; //超时返回错误信息0xFF 	
	return in;
} 

#endif
/*******************************************************************************
 *                        The end of the entire file			               *
 ******************************************************************************/

⌨️ 快捷键说明

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