📄 io_uart232.c
字号:
/*
*******************************************************************************
*51 IO口模拟串口通讯C源程序
* Copyright (Ren) 17-Oct-2006
* All rights reserved
* 文件名称: C51io_uart_t0.C
* 当前版本: 2.0V
* 作 者: JACK.REN(任永)
* 完成日期: Nov./02/2006
*------------------------------------------------------------------------------
* 取代版本: 1.0V
* 作 者: JACK.REN(任永)
* 完成日期: Oct./30/2006
*------------------------------------------------------------------------------
*******************************************************************************
* 硬件描述
* VCC: DC-5V
* VSS: DC-0V
* CPU: MCS51
* Fosc: CPU —— 22.1184 MHz
* RS232 IC: MAX232EPA
*******************************************************************************
* MCS51 IO模拟UART(232)通讯占用MCS51硬件资源
* 1、占用引脚端口:P3.0,P3.1;
*
* 通讯方式:
* 1、Baudrate 9600,8位数据位,1位起始位,1位停止位,无效验位;
*
*******************************************************************************
* 功能描述
* 按上层函数提供的数据播发语音。如在播发语音时,又有新的语音到来,放弃
* 当前正在播发的语音,立即播放新的语音。
*******************************************************************************
*/
#include "marco.H"
#include "IO_UART_H.H"
#include <string.H>
#include <stdio.H>
/*
**-----------------------------------------------------
** 函数原型: void IO_UART_Send_1Byte( uint_8 In_ch )
** 功能描述: 该函数用于模拟232端口发送一个字节
** 参数说明: In_ch 将要从模拟232端口发送的——
** 字节缓存寄存器。
**
** 返回值: 无
**
** 全局变量: 无
**
** 调用模块: 无
**
** 作 者: 任永
** 日 期: 2006年10月30日
**-----------------------------------------------------
*/
void IO_UART_Send_1Byte( uint_8 In_ch )
{
uint_8 xdata ii, iVal;
uint_8 xdata iBaud;
Dis_CPU_ISR();
iVal = In_ch;
InitAUART232_IO( );
A_TXD = LOW; //start bit
iBaud = UART_baudrate + 1;
BaudrateDelay( iBaud );
_nop_();_nop_();
// 发送数据位,低位在前
for (ii = 0; ii < 8; ii++ )
{
iVal >>= 1;
A_TXD = CY; // data bit
iBaud = UART_baudrate + 1;
BaudrateDelay( iBaud );
}
_nop_();_nop_();_nop_();
A_TXD = HIGH; // stop bit
iBaud = UART_baudrate + 3;
BaudrateDelay( iBaud );
En_CPU_ISR();
}// end of void IO_UART_Send_1Byte( uint_8 In_ch )
/*
**-----------------------------------------------------
** 函数原型: uint_8 IO_UART_Get_1Byte( void )
** 功能描述: 该函数用于模拟232端口接收一个字节
** 参数说明: 无
**
** 返回值: uint_8 模拟232端口接收到的——
** 字节临时寄存器。
**
** 全局变量: 无
**
** 调用模块: 无
**
** 作 者: 任永
** 日 期: 2006年10月30日
**-----------------------------------------------------
*/
uint_8 IO_UART_Get_1Byte( void )
{
uint_8 xdata out_ch, ibuf1, ibuf2, ibuf3, ibuf4, ii;
uint_8 xdata iBaud;
Dis_CPU_ISR();
A_RXD = HIGH;
while ( A_RXD == HIGH ) // 等待起始位 2Mach
{
//if (10mS超时判断)
{
//IO_UART_Get_FlagReg = IO_UART_Get_Err;
//return ( 0 );
;
//...................................
}
}
iBaud = UART_baudrate + 1;
BaudrateDelay( iBaud );
_nop_();_nop_();_nop_();_nop_();
// read data bit
for (out_ch = ii = 0; ii < 8; ii++ )
{
ibuf1 = ibuf2 = ibuf3 = 0;
// 集中到中心频率度数
iBaud = 8;
BaudrateDelay( iBaud );
ibuf1 = A_RXD; //47
iBaud = 2;
BaudrateDelay( iBaud );
ibuf2 = A_RXD;
iBaud = 2;
BaudrateDelay( iBaud );
ibuf3 = A_RXD;
// 数据干扰识别
if ( ibuf1 == ibuf2 )
{
ibuf4 = ibuf1;
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
else if ( ibuf2 == ibuf3 )
{
ibuf4 = ibuf2;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
else if (ibuf1 == ibuf3)
{
ibuf4 = ibuf3;
}
else
{
// 通讯失败
IO_UART_Get_FlagReg = IO_UART_Get_Err;
break;
//.............................
}
out_ch >>= 1;
if ( ibuf4 )
{
out_ch |= 0x80;
}
else
{
_nop_();_nop_();_nop_();_nop_();_nop_();
}
iBaud = 1;
BaudrateDelay( iBaud );
}
// check stop bit
iBaud = UART_baudrate;
BaudrateDelay( iBaud );
if ( A_RXD )
{
IO_UART_Get_FlagReg = IO_UART_Get_OK;
}
else
{
IO_UART_Get_FlagReg = IO_UART_Get_Err;
}
A_RXD = HIGH;
En_CPU_ISR();
return ( out_ch );
}// end of uint_8 IO_UART_Get_1Byte( void )
/*
**-----------------------------------------------------
** 函数原型: void main( void )
** 功能描述: 该函数用于调度个功能函数
** 参数说明: 无
**
** 返回值: 无
**
** 全局变量: 无
**
** 调用模块: 无
**
** 作 者: 任永
** 日 期: 2006年10月30日
**-----------------------------------------------------
*/
void main( void )
{
uint_8 xdata GetChar;
uint_8 xdata Txt_buf1[ ] = {"调试软件\n"};
uint_8 xdata Txt_buf2[ ] = {"KeilC51汉字内码测试软件\n测试码:三二一\n "};
uint_8 xdata Txt_buf[ ] = {"调试成功,2006年11月06日\n"};
uint_8 xdata iVal, ilen;
ilen = strlen( Txt_buf2 );
for (iVal = 0; iVal < ilen; iVal++ )
{
IO_UART_Send_1Byte( Txt_buf2[ iVal ] );
}
ilen = strlen( Txt_buf1 );
for (iVal = 0; iVal < ilen; iVal++ )
{
IO_UART_Send_1Byte( Txt_buf1[ iVal ] );
}
//******************** 功能测试代码 Start ******************
ilen = strlen( Txt_buf );
GetChar = 0;
while ( 1 )
{
GetChar = IO_UART_Get_1Byte();//接收1字节。
}// end of while ( 1 )
//******************** 功能测试代码 End *******************
}// end of void main( void )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -