Verifying PIC24EP Clock Frequency by UART Module

In our previous article Configure PIC24 to run at 70 MIPS , I suggested a way to verify whether the PIC24E run at 70 MIPS by setting a timer output and measuring its period using an oscilloscope. If you are a normal person like me who does not have a scope sitting next to the computer, this approach might be infeasible (so why I proposed it in the first place? Doh!). Anyway, there is some other way that does not need a lab instrument. How about this? Let try setting up an asynchronous communication like UART. If the baud rate on the PIC24EP does not match that on the other side, say, our PC, then they couldn’t talk. To set up the baud rate, we have to put FCY = 70M into the equation. If the clock deviates from that value just a little bit, communcation would fail. Above all, we have a chance to learn how to set up UART communcation on the PIC24EP.

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

Let’s examine some of these steps.

Peripheral Pin Select

Modern 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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s