📄 test_uart2_485.c
字号:
#include "def.h"
#include "2410addr.h"
#include "config.h"
#include "board.h"
#include "utils.h"
#define En485Tx() (rGPEDAT |= 1<<13)
#define En485Rx() (rGPEDAT &= ~(1<<13))
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();
}
static __inline void reset_io(void)
{
rGPHCON |= 0xf<<12; //RTS1, CTS1
}
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;
//while(1) {
//m = 'a';
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(!(rUTRSTAT2 & 0x4)); //Wait until THR is empty.
//WrUTXH2(0xaa);
//Delay(2);
}
reset_io();
}
static char title[] = "485串口通讯实验";
static char tip[] = "实验串口3(UART2)的485通讯功能,按ESC键返回";
//Test_Uart2_485_Item在prog_entry.c里被引用
TEST_PROGRAM_ITEM Test_Uart2_485_Item = {
(TEST_PROGRAM)Test_Uart2_485, //入口地址
title, //显示名称
tip, //帮助或提示信息,可为NULL
1}; //使用printf,puts,putch等函数时在LCD上也显示输出字符(串)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -