HW1 - ndm736/ME433_2020 GitHub Wiki
There are three parts to this assignment. First, build and test the 3.3V regulator. Second, build the minimum necessary connections for the PIC32MX170F256B. Third, write a short program to test the PIC circuit. Each section has deliverables in bold, these are the things that you need to turn in, and sometimes demo videos to record. Make a folder in your ME433 repo called HW1 and place all of the deliverables in it. On Canvas, submit a link to your HW1 folder, and any demo videos (don't put the videos in your repo, just code and images).
Build and test the 3.3V regulator
Take a look at the L4931 datasheet. Note the pinout. Using the Pololu micro USB breakout, check that you have 5V between the VBUS and GND pins. Use VBUS as the input to the L4931, and see if you get 3.3V out, without using any of the suggested bypass capacitors. Use nScope to get a screenshot of the output voltage at a very high sample rate, look for any high frequency noise. Add a red LED in series with a 330 ohm resistor to the output voltage, and get the same screenshot from nScope, again looking for high frequency noise. Add a 0.1uF capacitor from VBUS to GND and both a 1uF and 10uF capacitor from 3.3V to GND and get the same screenshot from nScope. If there was any noise, hopefully it goes away when you added the capacitors.
Note that the nScope needs to have the same ground as the voltage regulator (common ground). It is also a good idea to connect GND and 3.3V to both power rails on your breadboard, we'll use them a lot. 5V will not be used very often, it does not need a power rail.
Build the PIC32 circuit
As you should always do, first draw a circuit diagram of the voltage regulator and PIC32MX170F256B. This is a big circuit. This image is a useful cheat sheet.
- Connect all VDD pins to 3.3V, all VSS pins to GND. Each VDD pin gets a 0.1uF or 1uF capacitor to GND (either value works). The capacitor is supposed to be as close to the pins as possible, to suppress noise that might make the PIC turn off if the power line fluctuates.
- Connect the VCAP pin to a 10uF capacitor to GND, note that the 10uF capacitor is polarized.
- Use the long header pins to connect the MPLAB Snap programmer to the breadboard. Pin one of the Snap is labeled with a black triangle. Connect the Snap VDD to 3.3V and GND to GND. Connect PGD to PGED1 and PGC to PGEC1 of the PIC. Connect the Snap MCLR to MCLR of the PIC.
- Connect MCLR to a 10k pull-up resistor and a pushbutton to GND, this is the Reset button.
- Connect the PIC OSC1 and OSC2 pins to the outside pins of the 8MHz resonator, and the center pin of the resonator to GND. The wires that connect the resonator to the PIC should be the same length and the resonator should be as close to the PIC as you can make it. These are the minimum connections necessary to make the PIC work, but with no inputs or outputs it is hard to tell, so we'll add an LED and a User button.
- Connect pin A4 to a green LED to a 330 ohm resistor to GND.
- Connect pin B4 to a 10k pull-up resistor and a pushbutton to GND.
Build the circuit as neatly as you can, we will use it all quarter. Power your board and make sure the 3.3V is still the correct value (no shorts). Take a photo of your circuit diagram and a photo of your circuit.
Program the PIC32
Start a new project in the MPLABX IDE. By default the project is saved to a folder called MPLABXProjects, your should redirect it to the HW1 folder in your repo. Copy the code template over the main.c file. I left the pragma values in the template as Xs. Microchip doesn't put these pragma values in the datasheet, instead their definitions are part of the XC32 compiler. The documentation is in an html file, something like
(some installation path)/Microchip/xc32/v5.35/docs/config_docs/32mx170f256b.html
Find the appropriate pragma values and put them in your code. At this point you can compile and program the PIC by pressing the downwards green arrow. There are many ways this can generate an error, from syntax errors in the code, not connecting the Snap to 3.3V and GND, not connecting the Snap to the programming pins, not powering the PIC completely.
When the PIC programs successfully, add code that does the following:
- initializes B4 as input and A4 as output that is initially off.
- in the infinite loop, if the value of B4 is pushed, turn on A4 for 0.5s, off for 0.5s, on for 0.5s, and off for 0.5s (so two one-second square wave blinks).
Upload your code to your repo, and take a video of your blinking LED, also showing an nScope trace that proves your LED is on for exactly 0.5s in the video.
Remember that the TRISx SFR controls whether a pin is an input (1) or an output (0). The PORTx SFR is read to determine if an input pin is on (1) or off (0). The LATx SFR sets whether an output pin is high (1) or low (0). The function _CP0_SET_COUNT(value) sets the value of the core timer, and _CP0_GET_COUNT() returns the value of the timer, which ticks at half the system clock (we are using 48MHz for sys clock). Typically a delay function sets the core timer to 0, then in a while loop gets the value of the core timer and compares it to the number of ticks necessary to wait the correct amount of time. Note that the MPLAB X IDE has autocompletion, so as you type something like TRISBbits. the available bits appear! You can also right-click on a SFR or function and go to the definition, and the IDE will open up the file where it is defined. Very useful!