?? test_uart2_485.c.bak
字號:
/****************************************************************************
【文 件 名 稱】Test_Uart2_485.c
【功 能 描 述】FS2410XP教學平臺實驗程序
【程 序 版 本】3.0
【創建及創建日期】優龍公司/2005-XX-XX
【修改及修改日期】2005-5-23
****************************************************************************/
//頭文件定義
#include "def.h"
#include "2410addr.h"
#include "config.h"
#include "board.h"
#define En485Tx() (rGPEDAT |= 1<<13)
#define En485Rx() (rGPEDAT &= ~(1<<13))
/********************************************************************
Function name: set_io
Parameter : void
Description : 將相應的GPIO設置為TXD2,RXD2
Return : void
Argument :
Autor & date :
*********************************************************************/
static __inline void set_io(void)
{
rGPHCON &= ~(0xf<<12);
rGPHCON |= 0xa<<12; //TXD2,RXD2
rGPHUP |= 3<<6; //Uart port pull-up disable
rGPGCON &= ~((3<<4)|(3<<12));
rGPGCON |= (1<<4)|(1<<12);
rGPGDAT &= ~(1<<2);
rGPGDAT |= 1<<6;
rGPECON &= ~(3<<26);
rGPECON |= 1<<26;
En485Rx();
}
/********************************************************************
Function name: reset_io
Parameter : void
Description : void
Return : void
Argument :
Autor & date :
*********************************************************************/
static __inline void reset_io(void)
{
rGPHCON |= 0xf<<12; //RTS1, CTS1
}
/********************************************************************
Function name: Test_Uart2_485
Parameter : void
Description : RS485通訊實驗
Return : void
Argument :
Autor & date :
*********************************************************************/
void Test_Uart2_485(void)
{
U16 m ;
U32 baud;
printf("485 通訊實驗, please select baud rate\n1: 9600\n2: 19200\n3: 57600\n4: 115200\n");
while(1) {
m = getch();
if(m>='1'&&m<='4')
break;
if(m==ESC_KEY)
return;
}
set_io();
switch(m) {
case '1':
baud = 9600;
break;
case '2':
baud = 19200;
break;
case '3':
baud = 57600;
break;
default:
baud = 115200;
}
printf("set UART2 baud rate to %d\n", baud);
rUFCON2 = 0x0;
rULCON2 = 0x3;
rUCON2 = 0x245;
rUBRDIV2 = ((int)(GetMasterClock()/16./baud + 0.5) -1);
while(1)
{
/*m = getkey();
if(m==ESC_KEY)
break;
if(m) {
En485Tx();
while(!(rUTRSTAT2 & 0x4)); //Wait until THR is empty.
WrUTXH2(m);
while(!(rUTRSTAT2 & 0x4)); //Wait until THR is empty.
En485Rx();
}
if((rUTRSTAT2 & 0x1)) //Receive data ready
putch(RdURXH2());
*/
while((rUTRSTAT0 & 0x1)==0x0); /* 等待數據 */
data = rURXH0; /* 讀取數據 */
if(data=='\r') /*如果是換行字符,補充一個回車 */
{
while(!(rUTRSTAT0 & 0x2)); /* 等待發送緩沖空.*/
rUTXH0 = data; /* 將數據寫到數據端口 */
while(!(rUTRSTAT0 & 0x2)); /* 等待發送緩沖空.*/
rUTXH0 = '\n'; /* 將數據寫到數據端口 */
}
else
{
while(!(rUTRSTAT0 & 0x2)); /* 等待發送緩沖空.*/
rUTXH0 = data; /* 將數據寫到數據端口 */
}
}
reset_io();
}
/*****************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -