/*********************************************************************************************
* File: uart_communication.c
* Author: embest
* Desc: uart test file
* History:
* R.X.Huang, Programming modify, March 12, 2005
*********************************************************************************************/
/*------------------------------------------------------------------------------------------*/
/* include files */
/*------------------------------------------------------------------------------------------*/
#include "2410lib.h"
/*********************************************************************************************
* name: uart0_test
* func: uart test function
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void uart0_test() //RS232通信函数
{
char cInput[256];
UINT8T ucInNo=0;
char c;
uart_init(0,115200,0);
uart_printf("\n UART0 Communication Test Example\n");
uart_printf(" Please input words, then press Enter:\n");
#ifdef BOARDTEST
sprintf(&cInput, "Type via UART0 to test.");
print_lcd(195,170,0x1c,&cInput);
#endif
uart_printf(" ");
g_nKeyPress = 1;
while(g_nKeyPress==1) // only for board test to exit
{
c=uart_getch();
//uart_sendbyte(c);
uart_printf("%c",c);
if(c!='\r')
cInput[ucInNo++]=c;
else
{
cInput[ucInNo]='\0';
break;
}
}
delay(1000);
uart_printf(" \nThe words that you input are: %s\n",cInput);
uart_printf(" end.\n");
}
void uart1_test(void) { //RS485通信函数
char rx,ch;
int i=0;
char buffer[16];
Uart_Port_Set();
Uart_Select(0);
Uart_TxEmpty(1);
//wait until tx buffer is empty.
Uart_TxEmpty(2); //wait until tx buffer is empty.
rULCON1=(0<<6)|(0<<3)|(0<<2)|(3); // Normal,No parity,One stop bit, 8bit
rULCON2=(0<<6)|(0<<3)|(0<<2)|(3); // Normal,No parity,One stop bit, 8bit
rUCON1 &= 0x400; // For the PCLK <-> UCLK fuction
rUCON2 &= 0x400;
// For the PCLK <-> UCLK fuction
rUCON1|=TX_INTTYPE<<9)|(RX_INTTYPE<<8)|(0<<7)|(1<<6)|(0<<5)|(0<<4)|(1<<2)|(1);
rUCON2|=(TX_INTTYPE<<9)|(RX_INTTYPE<<8)|(0<<7)|(1<<6)|(0<<5)|(0<<4)|(1<<2)|(1);
rINTSUBMSK|=(BIT_SUB_RXD2|BIT_SUB_TXD2|BIT_SUB_ERR2); // Just for the safety
rINTSUBMSK|=(BIT_SUB_RXD1|BIT_SUB_TXD1|BIT_SUB_ERR1); // Just for the safety
rINTMSK|=(BIT_UART1); // Unmask sub int
rINTMSK|=(BIT_UART2);
// Unmask sub int
rGPGCON =rGPGCON&(~(0x3<<10))|(0x1<<10); // [11:10]=[01]:GPG5:output 4851_de_re
rGPGCON =rGPGCON&(~(0x3<<12))|(0x1<<12); //[13:12]=[01]:GPG6:output 4852_de_re
rGPGDAT = rGPGDAT|(0x1<<6); // enable 485_2 tx
rGPGDAT = rGPGDAT&(~(0x1<<5)); // enable 485_1 rx
rGPGDAT = rGPGDAT&(~(0x1<<6)); //GPG6=0 enable 485_2 rx
rGPGDAT = rGPGDAT|(0x1<<5); //GPG5=1 enable 485_1 tx
//循环两个RS485相互互发10次
for(i=0;i<10;i++) {
Uart_Printf("data=%d : tx2 ---> rx1 \n",i);
rGPGDAT = rGPGDAT|(0x1<<6); // enable 485_2 tx
rGPGDAT = rGPGDAT&(~(0x1<<5)); // enable 485_1 rx
rUTXH2 = i; //RS485-2发
while(!(rUTRSTAT2 & 0x2)); //Wait until THR is empty.
while(1)
{
if((rUTRSTAT1&0x1)==0x1) //RS485-1收
{ rx=rURXH1;
break;
}
}
Uart_Printf(" tx1 -- > rx2 test OK\n");
rGPGDAT = rGPGDAT&(~(0x1<<6)); //GPG6=0 enable 485_2 rx //设置485控制
rGPGDAT = rGPGDAT|(0x1<<5); //GPG5=1 enable 485_1 tx
rUTXH1=50-i; //RS485-1发
Uart_Printf("data=%d : tx1 ---> rx2 \n",50-i);
while(1) //RS485-2收
{ if((rUTRSTAT2&0x1)==0x1)
{ rx=rURXH2;
break;
}
}
Uart_Printf(" tx2 -- > rx1 test OK\n\n");
}
}