A simple project goal is set. We want to control a timer by some commands sent via UART. After reset, timer 1 is set to blink an LED at 200 milliseconds period (so it changes state 5 times/sec). We should be able to turn the timer on/off by issuing command TON or TOFF. The PIC24EP must send some text response back. If this works, it verifies that our FCY is precisely at 70 MHz. On our small prototype, an FT232R from Future Technology Int’l Ltd. is installed on board as USB to UART converter. So a simple terminal program can be used to test the connection. There is nothing complicated on the hardware part. We assign RX and TX of UART1 to pin 21 (RP42) and pin 22 (RP43), which are wired to pins TXD and RXD of FT232R, respectively. So most tasks worth discussion are on the software side. For someone unfamiliar with UART programming on a PIC family, a good place to start is PIC24EP datasheet and Family Reference Manual (DS705082B section 17. UART), where code examples are provided. A common to-do list is as follows (order is not quite relevant here).
- Set up the peripheral pin select so that RP42 and RP43 serve as U1TX and U1RX, respectively.
- Set up the same protocol as the other side: ex. 8-bit data, 1 stop bit, no parity, no hardware flow control
- Select the same baud rate as that of the other side: ex. 9600, 19200, 57600, 115200, etc
- Set up some other relevant parameters such as high/low speed, auto-baud enable bit
- Compute and set baud rate parameter
- Select communication scheme (polling or interrupt)
- Write transmit/receive C functions
Peripheral Pin SelectModern microcontrollers are designed for more flexibility. During old time when peripheral pin assignments are fixed, it is quite common for a programmer to realize that the derired functions are on the same pin. This is quite a constraint especially for a chip with low pin counts. Peripheral Pin Select is a feature to circumvent such problem. In PIC24EP, several peripherals can be assigned to physical pins in software. UART1 is one of them. On our experiment board, the U1TX and U1RX are assigned to pin RP42 and RP43, respectively. This can be done using the following code (see examples and explanation in DS705098B: PIC24EP FRM, Section 10. I/O Ports).
RPINR18bits.U1RXR = 43; // U1RX to RP43 RPOR4bits.RP42R = 1; // U1TX to RP42