ME 333 Lab 2

From Mech
Revision as of 21:08, 12 January 2010 by NickMarchuk (talk | contribs)
Jump to navigationJump to search

PCB Artist

You should be familiar with making a circuit schematic and PCB design in PCB Artist.

Demonstrate that you can build a PCB by editing the following: ...

PIC32: Basic I/O and debugging

In this lab you will practice interfacing your NU32 PIC32 board to simple circuits.

The goal of this lab is to design and breadboard a circuit that reads an analog voltage from a potentiometer and constantly outputs the value of the voltage on the LCD as well as turns on an LED when the reading is over a certain value. When a push button, external to your NU32 PIC32 board, is pressed, the PIC sends the value of the voltage at that moment to a PC via an RS232 to USB cable and is displayed on your computer screen. You will put together this code in sections. This code covers the following topics:

  • Digital Output
  • Digital Input
  • Analog Input
  • Writing to Parallel LCD
  • Communicating with PC with RS232
    • To begin, create a new folder called ME333_Lab2 on your computer.
    • Copy and Paste HardwareProfile.h, HardwareProfile_NU32.h and the procdefs.ld file from HelloWorld into this new project folder.
    • In MPLAB, create a new project called ME333_Lab2 and add the above files to the project. (You have to move procdefs.ld to the ‘Other Files’ folder).
    • Add the 2 include paths (as done in HelloWorld) and the constant PIC32_NU32.

Digital Output

  • In MPLAB, create a new file and save it as ME333_Lab2.c.
  • Add this source file to your project.
  • Copy and Paste the code on PIC32MX: Digital Outputs into this new source file.
  • This code currently turns on / off PINS D1-D4 depending on the user switch on your board. Change this to turn on / off pins A2 and A3 with the following modifications
    • change the #define to only use A2 and A3
    • change the initialization (TRISx and LATx) to only have A2 and A3 as digital outputs (What are the hex numbers you need for this?)
    • change the if statement to turn on A2 and turn off A3 when the user switch is pressed and otherwise turn on A2 and turn on A3. (Note that swUser is 1 when not pressed).
  • Circuit: You will need 2 LEDs and 2x 1k resistors. Create an LED circuit that has one end connected to 3.3 V and the other end connected to the pin. Do this for both A2 and A3. (This is a sink).
  • Build the Hex file (Build All) and program this on your board. Verify that A3 is on when the switch is not pressed and A2 is on when the switch is pressed.

You have now completed the Digital Output section.

Digital Input

Now instead of using the user switch, we are going to add our own switch as a digital input.

  • Inspect the code for PIC32MX: Digital Inputs. You will need to copy two lines to use an external switch as a digital input
    • copy and paste the line of code that defines a constant with PORTx
    • copy and paste the line that initializes the pin as an input
  • Change these two lines of code to use pin A10 instead of pin A9
  • Change your If statement to use PIN_A10 instead of swUser
  • Circuit – Connect a 1k resistor to 3.3 V and the other end to one end of the switch. Use a multimeter to find out which pins are not connected when the switch is released. Connect the other end of the switch to ground. Connect the end of the switch by the resistor to Pin A10.
  • Build the Hex file (Build All) and program this on your board. Verify that A3 is on when the switch is not pressed and A2 is on when the switch is pressed.

You have now completed the Digital Input section.

Analog Input

Now we are going to read an analog input.

  • Inspect the code for PIC32MX: Analog Inputs. Copy the following lines of code into your code
    • initialization of channel4 variable (put this near your #define constants)
    • all the lines of code between “// configure and enable the ADC” and “while ( ! mAD1GetIntFlag() ) { }” … Note that these lines should be included
    • the line channel4 = ReadADC10(0) that is inside the while loop. Place this line of code above your If statement in the infinite while loop.
  • We are only going to use 1 analog input, so modify PARAM4 to only use analog input 6 (AN6).
  • Change PARAM5 to skip AN4 and AN5, but not skip AN6.
  • Remove the lines of code that turn off / on A3 from the If statement with digital input.
  • Create a new if statement that looks like this:
if (channel4 > 511)
{ 
	PIN_A3 = 0;
}
else
{
	PIN_A3 = 1;
}

This will turn on pin A3 when the analog input is greater than 511 (about half way), otherwise the LED is off.

  • Circuit: Get a 1k or 10k potentiometer. Connect one side to 3.3V and the other side to GND. Connect the middle pin to pin B6 (corresponds to analog 6).
  • Build the Hex file (Build All) and program this on your board. Verify that A2 is on when the user switch is pressed and A3 is on when the potentiometer is turned past 1.66V.

You have now completed the Analog Input section, but it would be much better to see the actual analog input number.

Work out this lab in steps:

  • Get a potentiometer and a push button and practice and .
  • Get an LCD and follow the instructions on PIC32MX: Parallel LCD to write a program to control it.
  • Follow the instructions at PIC32MX: RS232 and write a program that sends a message to a PC over the RS232 to USB cable in your group box.

When you have finished, demonstrate your circuit to the TA.