HW3 - ndm736/ME433_2023 GitHub Wiki
We will use several types of digital communication protocols in this course. The first is UART - universal asynchronous receive and transmit, also called serial communication or RS-232.
A microcontroller can communicate with a computer directly over USB if it has the right peripheral (the PIC32MX100 series does not, but the virtually identical PIC32MX200 series does), but the code is quite complicated. A common way to get around this is to use a UART to USB converter chip, available from companies like FTDI and Silicon Labs. The port generated on the computer is the same, so you can test with the external chip and later switch to internal USB and not have to change your computer code. We'll be using the Adafruit CP2102N Friend USB to UART converter. You may need to install drivers, see the Adafruit page for more details.
For this assignment, program the PIC to send a message to your computer every time the pushbutton is pressed.
The UART is a peripheral that sends 8 bit characters out of the TX pin of one device and into the RX pin of another. The devices must:
- have common ground
- agree on the period of a bit (called a baud rate) Between the CP2102N and the PIC, connect the grounds, the CP2102N TXD pin to the PIC RX pin, and the CP2102N RXD pin to the PIC TX pin.
Pins are already picked
You get to choose the PIC TX and RX pins, but they don't appear on the pinout:
This is because the UART pins can be relocated in code, using RPx pin names. In the IO chapter of the family reference manual (section 12), you'll find the following tables for setting a pin as an output:
or an input:
For example, if you pick pin B3 for the PIC U1TX and pin A2 for the PIC U1RX, the code looks like:
U1RXRbits.U1RXR = 0b0000; // Set A2 to U1RX
RPB3Rbits.RPB3R = 0b0001; // Set B3 to U1TX
Take a look at the UART initialization function from the NU32 library, here. Edit this code to initialize your UART at 230400 baud, with 8N1, and without the RTS and CTS pins. (Section 21) is the family reference manual on the UART.
Use the NU32DIP UART functions
8 bit unsigned chars are sent over the UART pins using ASCII representation:
Use the stdio library to generate character arrays using sprintf() or decode arrays using sscanf().
Every time you push the USER button, send a single cycle of a sine wave in 100 data points, with a 0.01 second delay between each data point. Verify that it works with a terminal emulator.
Use plotter.py to view the data as a graph.
Save your code in a folder called HW3 on your repo, and make a short demonstration video, and submit both on Canvas.