×
单片机 > 单片机程序设计 > 详情

P89C669 串口C51源程序

发布时间:2020-06-03 发布时间:
|
#include

#include
#include
#include "INC\UART.H"


unsigned char xdata BaudRate = 0;
unsigned char xdata Uart0_Counter = 0;
unsigned char xdata Uart1_Counter = 0;
unsigned char xdata Uart0_Buff[U0BUF_SIZE];
unsigned char xdata Uart1_Buff[U1BUF_SIZE];

/**********************************************************************/
/*名称: Uart_Init()
/*说明: 串行口初始化程序
/*输入: BaudRate 串行口波特率   
/*输出: 无
/**********************************************************************/
void Uart_Init(unsigned char Baud)
{
 unsigned int BaudR;
 unsigned long RCAP,BRGR;

 switch(Baud)
 {
  case 0x00 : BaudR = 300; break;
  case 0x01 : BaudR = 1200; break;
  case 0x02 : BaudR = 2400; break;
  case 0x03 : BaudR = 4800; break;
  case 0x04 : BaudR = 9600; break;
  case 0x05 : BaudR = 19200; break;
  default: BaudR = 19200; break; 
 }
 RCAP = 0xFFFF - ((F_OSC/BaudR)/16);
 RCAP2H = RCAP/0x100;
 RCAP2L = RCAP%0x100;  
 T2MOD = 0x00;
 T2CON = 0x34;
 S0CON = 0x50;
 ES0  = 1;

 BRGR = F_OSC/BaudR - 16;
 BRGR1 = BRGR/0x100;
 BRGR0 = BRGR%0x100; 
 BRGCON = 0x01;
 S1CON = 0x50;   
 ES1  = 1;

 PCON  = 0x80;
 EA = 1;
}   
/**********************************************************************/
/*名称: Uart0_ByteRcv()
/*说明: 串行口0数据读取中断程序
/*输入: 无 
/*输出: 无
/**********************************************************************/
static void Uart0_ByteRcv(void) interrupt 4 using 3
{
 RI_0 = 0;
 Uart0_Buff[Uart0_Counter] = S0BUF;
 Uart0_Counter++;
 if(Uart0_Counter >= U0BUF_SIZE)
 {
  Uart0_Counter = 0;
  memset(Uart0_Buff,0x00,U0BUF_SIZE);
 }
}

/**********************************************************************/
/*名称: Uart0_StrNSend()
/*说明: 串行口0指定长度字符串输出程序
/*输入: *str 字符串指针
/*  length 字符串长度 
/*输出: 无
/**********************************************************************/
void Uart0_StrNSend(unsigned char *str ,unsigned char length)
{
 unsigned char i;
 ES0 = 0; 
 for(i=0;i  {
  S0BUF = *(str+i);
  while(!TI_0);
  TI_0 = 0;
 }
 ES0 = 1;

}

/**********************************************************************/
/*名称: Uart1_ByteRcv()
/*说明: 串行口1数据读取中断程序
/*输入: 无 
/*输出: 无
/**********************************************************************/
static void Uart1_ByteRcv (void) interrupt 10 using 3
{
 RI_1 = 0;
 Uart1_Buff[Uart1_Counter] = S1BUF;
 Uart1_Counter++;
 if(Uart1_Counter >= U1BUF_SIZE)
 {
  Uart1_Counter = 0;  
  memset(Uart1_Buff,0x00,U1BUF_SIZE);  
 }
}

/**********************************************************************/
/*名称: Uart1_ByteSend()
/*说明: 串行口1单字符输出程序
/*输入: byte 要输出的字符
/*输出: 无
/**********************************************************************/
void Uart1_ByteSend(char byte)
{                                                                                                                                                                                                       
 ES1 = 0;
 S1BUF = byte;
 while(!TI_1);
 TI_1 = 0;
 ES1 = 1;
}

/**********************************************************************/
/*名称: Uart1_StrNSend()
/*说明: 串行口1指定长度字符串输出程序
/*输入: *str 字符串指针
/*  length 字符串长度 
/*输出: 无
/**********************************************************************/
void Uart1_StrNSend(unsigned char *str ,unsigned char length)
{
 unsigned char i;
 ES1 = 0;
 for(i=0;i  {
  S1BUF = *(str+i);
  while(!TI_1);
  TI_1 = 0;
 }
 ES1 = 1;
}
/**********************************************************************/
/*名称: Uart1_StrSend()
/*说明: 串行口1字符串输出程序
/*输入: *str 字符串指针 
/*输出: 无
/**********************************************************************/
void Uart1_StrSend(unsigned char *str)

 unsigned char i;
 ES1 = 0;
 for(i=0;i  {
  S1BUF = *(str+i);
  while(!TI_1);
  TI_1 = 0;
 }
 ES1 = 1;
}



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
AVR熔丝位操作时的要点和需要注意的相关事项