RS485通讯
本次使用了MikroBus的接口,因此,Uart1的引脚使用了P2.2与P2.3两个引脚。RS485芯片的使能引脚使用了P2.4引脚。
Uart1外设有自己的外设时钟,因此,可以使用官方的外设库来初始化。不过,由于官方的示例全部使用的P0.4与P0.5引脚——坦白讲,挺让我不理解的。所以在实现的过程中,走了N多的弯路。这点体验超级差。
下面附上我的源代码供大家参考。
extern void
PORTS_2_enter_DefaultMode_from_RESET (void)
{
// $[P2 - Port 2 Pin Latch]
// [P2 - Port 2 Pin Latch]$
// $[P2MDOUT - Port 2 Output Mode]
/***********************************************************************
- P2.0 output is open-drain
- P2.1 output is open-drain
- P2.2 output is open-drain
- P2.3 output is push-pull
- P2.4 output is push-pull
- P2.5 output is open-drain
- P2.6 output is open-drain
***********************************************************************/
P2MDOUT = P2MDOUT_B0__OPEN_DRAIN | P2MDOUT_B1__OPEN_DRAIN
| P2MDOUT_B2__PUSH_PULL | P2MDOUT_B3__OPEN_DRAIN | P2MDOUT_B4__PUSH_PULL
| P2MDOUT_B5__OPEN_DRAIN | P2MDOUT_B6__OPEN_DRAIN;
// [P2MDOUT - Port 2 Output Mode]$
// $[P2MDIN - Port 2 Input Mode]
SFRPAGE = 0x20;
P2MDIN = P2MDIN_B0__DIGITAL | P2MDIN_B1__DIGITAL | P2MDIN_B2__DIGITAL
| P2MDIN_B3__DIGITAL | P2MDIN_B4__DIGITAL | P2MDIN_B5__DIGITAL
| P2MDIN_B6__DIGITAL | P2MDIN_B7__DIGITAL;
// [P2MDIN - Port 2 Input Mode]$
// $[P2SKIP - Port 2 Skip]
/***********************************************************************
- P2.0 pin is not skipped by the crossbar
- P2.1 pin is not skipped by the crossbar
- P2.2 pin is skipped by the crossbar
- P2.3 pin is skipped by the crossbar
***********************************************************************/
SFRPAGE = 0x20;
P2SKIP = P2SKIP_B0__SKIPPED | P2SKIP_B1__SKIPPED | P2SKIP_B2__NOT_SKIPPED
| P2SKIP_B3__NOT_SKIPPED | P2SKIP_B4__SKIPPED | P2SKIP_B5__SKIPPED
| P2SKIP_B6__SKIPPED | P2SKIP_B7__SKIPPED;
// [P2SKIP - Port 2 Skip]$
// $[P2MASK - Port 2 Mask]
// [P2MASK - Port 2 Mask]$
// $[P2MAT - Port 2 Match]
// [P2MAT - Port 2 Match]$
}
UART1_initRxFifo(UART1_RXTHRSH_ZERO, UART1_RXTIMEOUT_16, UART1_RXFIFOINT_ENABLE);
UART1_enableRxFifoInt(true);
UART1_enableRxInt(true);
UART1_init (24500000, 115200, UART1_DATALEN_8, UART1_STOPLEN_SHORT,
UART1_FEATURE_DISABLE, UART1_PARITY_SPACE, UART1_RX_ENABLE,
UART1_RX_CROSSBAR, UART1_MULTIPROC_DISABLE);