📄 test_uart_rs485.c
字号:
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
#include "2440slib.h"
/********************************************************************************************************************
【函数名称】void Uart0_Uart1_Test( void )
【功能描述】在串口0和1之间切换
********************************************************************************************************************/
void Uart0_Uart1_Test( void )
{
U8 m ;
U8 n = 1 ;
rGPHCON = ( rGPHCON & (~( 0xf << 4 )) ) | ( 0x0a << 4 ); //TXD0,RXD0
rGPHCON = ( rGPHCON & (~( 0xf << 8 )) ) | ( 0x0a << 8 ); //TXD1,RXD1
Uart_Printf( "\nUART1 Test\n" ) ;
Uart_Printf( "\nPlease pull out COM cable and plug into the UART1 connector\n" ) ;
Uart_Select( 1 ) ;
while( (m=Uart_GetKey()) != ESC_KEY )
{
if( m != 0 )
{
Uart_Printf( "'ESC' key return to UART0, The input of you is " ) ;
Uart_SendByte( m ) ; Uart_Printf( "\n" ) ;
n = 0 ;
}
else
{
if( n == 1 )
{
Uart_Printf( "Now is UART1 output, do you see? Any key to display, 'ESC' key return to UART0 \n" ) ;
n = 1 ;
Delay( 500 ) ;
}
}
}
Uart_Printf( "\nPlease pull out COM cable and plug into the UART0 connector\n" ) ;
Uart_Select( 0 ) ;
while( (m=Uart_GetKey()) != ESC_KEY )
{
Uart_Printf( "Please Enter 'ESC' key to Main menu \n" ) ;
Delay( 500 ) ;
}
}
/********************************************************************************************************************
【函数名称】void Test_Uart1_RS485(void)
【功能描述】UART1用作RS485测试
********************************************************************************************************************/
void Test_Uart1_RS485(void)
{
U16 m ;
U32 baud;
rGPHCON = ( rGPHCON & (~( 0xf << 4 )) ) | ( 0x0a << 4 ); //TXD0,RXD0
rGPHCON = ( rGPHCON & (~( 0xf << 12 )) ) | ( 0x0a << 12 ); //TXD2,RXD2
Uart_Printf("Test Uart2 Use As RS485,Please select baud rate\n1: 9600\n2: 19200\n3: 57600\n4: 115200\n");
while(1)
{
m = Uart_Getch();
if( m >= '1' && m <= '4' )
break;
if( m == ESC_KEY )
return;
}
switch(m)
{
case '1':
baud = 9600;
break;
case '2':
baud = 19200;
break;
case '3':
baud = 57600;
break;
default:
baud = 115200;
}
Uart_Printf("set UART2 baud rate to %d\n", baud);
rUFCON2 = 0x0;
rULCON2 = 0x3;
rUCON2 = 0x245;
rUBRDIV2 = ( ( int ) ( FCLK / 16. / baud + 0.5 ) - 1 );
while(1)
{
m = Uart_GetKey();
if( m == ESC_KEY )
break;
if(m) {
RS485_2_Out_In( TRUE );
while(!(rUTRSTAT2 & 0x4)); //Wait until THR is empty.
WrUTXH2(m);
while(!(rUTRSTAT2 & 0x4)); //Wait until THR is empty.
RS485_2_Out_In( FALSE );
}
if((rUTRSTAT2 & 0x1)) //Receive data ready
Uart_SendByte( RdURXH2() );
}
}
/********************************************************************************************************************
【函数名称】void Test_Uart2_RS485(void)
【功能描述】UART2用作RS485测试
********************************************************************************************************************/
void Test_Uart2_RS485(void)
{
U8 m ;
U8 n = 2 ;
rGPHCON = ( rGPHCON & (~( 0xf << 4 )) ) | ( 0x0a << 4 ); //TXD0,RXD0
rGPHCON = ( rGPHCON & (~( 0xf << 12 )) ) | ( 0x0a << 12 ); //TXD2,RXD2
Uart_Printf( "\nUART2 As RS485 Test\n" ) ;
Uart_Printf( "\nPlease pull out COM cable and plug into the RS485 connector\n" ) ;
Uart_Select( n ) ;
while( (m = Uart_GetKey()) != ESC_KEY )
{
if( m != 0 )
{
Uart_Printf( "'ESC' key return to UART0, The input of you is " ) ;
Uart_SendByte( m ) ; Uart_Printf( "\n" ) ;
n = 0 ;
}
else
{
if( n == 2 )
{
Uart_Printf( "Now is UART2 output, do you see? Any key to display, 'ESC' key return to UART0 \n" ) ;
n = 2 ;
Delay( 500 ) ;
}
}
}
Uart_Printf( "\nPlease pull out COM cable and plug into the UART0 connector\n" ) ;
Uart_Select( 0 ) ;
while( (m=Uart_GetKey()) != ESC_KEY )
{
Uart_Printf( "Please Enter 'ESC' key to Main menu \n" ) ;
Delay( 500 ) ;
}
}
/********************************************************************************************************************
【函数名称】void Test_Uart2_RS485(void)
【功能描述】UART2用作RS485测试
********************************************************************************************************************/
/*
void Test_Uart2_RS485(void)
{
U8 m ;
U8 n = 2 ;
rGPHCON = ( rGPHCON & (~( 0xf << 4 )) ) | ( 0x0a << 4 ); //TXD0,RXD0
rGPHCON = ( rGPHCON & (~( 0xf << 8 )) ) | ( 0x0a << 8 ); //TXD1,RXD1
rGPHCON = ( rGPHCON & (~( 0xf << 12 )) ) | ( 0x0a << 12 ); //TXD2,RXD2
rGPBCON = ( rGPBCON & ~( 0x3 << 14 ) ) | ( 0x01 << 14 ); //OUTPUT
rGPBCON = ( rGPBCON & ~( 0x3 << 16 ) ) | ( 0x01 << 16 ); //OUTPUT
//rGPBDAT = ( rGPBDAT & ~( 0x1 << 7 ) ) | ( 0x01 << 7 ); //RS485 TX
rGPBDAT = ( rGPBDAT & ~( 0x1 << 7 ) ) | ( 0x00 << 7 ); //RS485 RX
rGPBDAT = ( rGPBDAT & ~( 0x1 << 8 ) ) | ( 0x01 << 8 ); //RS485 TX
//rGPBDAT = ( rGPBDAT & ~( 0x1 << 8 ) ) | ( 0x00 << 8 ); //RS485 RX
Uart_Printf( "\nUART2 As RS485 Test\n" ) ;
Uart_Printf( "\nPlease pull out COM cable and plug into the RS485 connector\n" ) ;
while( 1 )
{
//rUTXH1 = 0x55 ;
//rUTXH1 = 0xaa ;
rUTXH2 = 0x55 ;
rUTXH2 = 0xaa ;
}
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -