Difference between revisions of "ME 333 Lab 2"

From Mech
Jump to navigationJump to search
 
(16 intermediate revisions by 2 users not shown)
Line 3: Line 3:
You should be familiar with making a circuit schematic and PCB design in [[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 making the schematic and PCB for:
Demonstrate that you can build a PCB by making the schematic and PCB for the circuit drawn in the mechatronics lab.

[[Image:Lab2_schematic.jpg]]


The circuit drawn in the mechatronics lab.


Use:
Use:
* 2 LM741 Op Amps
* 2 Single output 8 pin Op Amps
* 5 Resistors
* 5 Resistors
* 1 Capacitor
* 1 Capacitor
* 3 Input pads
*
* 1 Output pads
* +12V, -12V, and Ground input pads

Use silkscreen to label all components and their values.

[[Image:Lab2_pcb.jpg]]

Print your final schematic and PCB.


== PIC32: Basic I/O and debugging ==
== PIC32: Basic I/O and debugging ==
Line 24: Line 33:
* Communicating with PC with RS232
* Communicating with PC with RS232
** To begin, create a new folder called ME333_Lab2 on your computer.
** 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.
** Copy and Paste HardwareProfile.h, HardwareProfile_NU32.h and the procdefs.ld file from HelloWorld [[Media:HelloWorld_NU32.zip|(these files)]] 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).
** 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.
** Add the 2 include paths (as done in HelloWorld) and the constant PIC32_NU32.
Line 35: Line 44:
** change the #define to only use A2 and A3
** 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 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).
** change the if statement to turn on A2 and turn off A3 when the user switch is pressed and otherwise turn off 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).
* 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.
* Build the Hex file (Build All) and program this on your board. Verify that A3 is on (A2 off) when the switch is not pressed and A2 is on (A3 off) when the switch is pressed.
You have now completed the Digital Output section.
You have now completed the Digital Output section.


Line 48: Line 57:
** copy and paste the line that initializes the pin as an input
** 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 these two lines of code to use pin A10 instead of pin A9
* Change your If statement to use PIN_A10 instead of swUser
* Change your If statement to use INPUT_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.
* 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.
* 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.
Line 95: Line 104:
sprintf(LCDbuffer, "\fAnalog Input\n%d", channel4); // make the string
sprintf(LCDbuffer, "\fAnalog Input\n%d", channel4); // make the string
putsLCD(LCDbuffer); // write the contents of the variable
putsLCD(LCDbuffer); // write the contents of the variable
sprintf creates a string with our potentiometer reading.
</pre></code>
</pre></code>
sprintf creates a string with our potentiometer reading.
putsLCD will put the string on the LCD.
putsLCD will put the string on the LCD.
* After these lines of code, write:
* After these lines of code, write:
Line 105: Line 114:
* Wire up the LCD as detailed on the wiki
* Wire up the LCD as detailed on the wiki
* Build the Hex file (Build All) and program this on your board. Verify that the LCD displays Analog Input on the first line and a number between 0 – 1023. Turn the potentiometer and observe that the number changes. Verify that A2 is on when the user switch is pressed and A3 is on when the potentiometer is turned past 1.66V. Note that there may be a delay with the LEDs turning on and off because of the delay we added to the code.
* Build the Hex file (Build All) and program this on your board. Verify that the LCD displays Analog Input on the first line and a number between 0 – 1023. Turn the potentiometer and observe that the number changes. Verify that A2 is on when the user switch is pressed and A3 is on when the potentiometer is turned past 1.66V. Note that there may be a delay with the LEDs turning on and off because of the delay we added to the code.
You have now printed to the LCD, but what if we want to print to our computer?
You have now printed to the LCD, but what if we want to print to our computer?


=== RS232 Communication ===
=== RS232 Communication ===
Line 143: Line 152:
* Finally, copy and paste the interrupt routine (all of the code at the bottom of the wiki) to the bottom of your code (outside the main function).
* Finally, copy and paste the interrupt routine (all of the code at the bottom of the wiki) to the bottom of your code (outside the main function).
This creates an interrupt routine (to be discussed in class) that will stop your code when a key is pressed on your keyboard. It will check to see if this key is a ‘c’. If it is, LED2 will turn on and LED3 will turn off. If the key is not a ‘c’, LED2 will be off and LED3 will turn on. No matter what key is pressed, LED1 will toggle on and off.
This creates an interrupt routine (to be discussed in class) that will stop your code when a key is pressed on your keyboard. It will check to see if this key is a ‘c’. If it is, LED2 will turn on and LED3 will turn off. If the key is not a ‘c’, LED2 will be off and LED3 will turn on. No matter what key is pressed, LED1 will toggle on and off.
* Circuit: Connect Pins F4 to the Yellow Pin of the RS232 cable, F5 to the Orange Pin of the RS232 cable and GND to Black Pin of the RS232 cable.
* Circuit: Connect Pins F4 to the Orange Pin of the RS232 cable, F5 to the Yellow Pin of the RS232 cable and GND to Black Pin of the RS232 cable.
F4 is the PIC32 Receive for UART2 so it is connected to PC Transmit
F4 is the PIC32 Receive for UART2 so it is connected to PC Transmit
F5 is the PIC32 Transmit for UART2 so it is connected to PC Receive
F5 is the PIC32 Transmit for UART2 so it is connected to PC Receive
Line 163: Line 172:


You have now met the objectives for this lab. Demonstrate your circuit to the TA and clean up your station.
You have now met the objectives for this lab. Demonstrate your circuit to the TA and clean up your station.

== Lab 2 Code ==
[[Media:ME333_Lab2.c|ME333_Lab2.c]]

Latest revision as of 21:55, 19 January 2010

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 making the schematic and PCB for the circuit drawn in the mechatronics lab.

Lab2 schematic.jpg


Use:

  • 2 Single output 8 pin Op Amps
  • 5 Resistors
  • 1 Capacitor
  • 3 Input pads
  • 1 Output pads
  • +12V, -12V, and Ground input pads

Use silkscreen to label all components and their values.

Lab2 pcb.jpg

Print your final schematic and PCB.

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 (these files) 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 off 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 (A2 off) when the switch is not pressed and A2 is on (A3 off) 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 INPUT_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.

LCD

Displaying messages on an LCD is nice for debugging. We are going to display the analog input value with our LCD.

  • On the PIC32MX: Parallel LCD page, download the .h and .c files. Extract these files. Copy and paste the LCD.h and LCD.c into your project code. You don’t need the main.c file. You don’t have to know how this code works yet (we will discuss it in class on Thursday)
  • Read the section about LCD commands.
  • After #include "HardwareProfile.h", write #include “LCD.h”. This will include the header file to let you use the LCD commands.
  • Add the LCD.c and LCD.h to your project by right clicking on the source files folder and the header files folder.
  • Copy and paste the line about lcd_init() to your code after SYSTEMConfigPerformance().
  • After the line of code “while ( ! mAD1GetIntFlag())” define a buffer that will be sent to your LCD with the following line of code. We are using 33 characters because we can write 16x2 characters and you can include a new line character.
char LCDbuffer[33]; // this will store the string for the LCD
  • Add the following lines of code after you read the pot (channel4 = …)
sprintf(LCDbuffer, "\fAnalog Input\n%d", channel4); // make the string 
putsLCD(LCDbuffer);  // write the contents of the variable

sprintf creates a string with our potentiometer reading. putsLCD will put the string on the LCD.

  • After these lines of code, write:
Delayms(500);

MPLAB does not come with built in delay functions. This function is defined in LCD.h / LCD.c. We want to use a delay, so that the LCD isn’t update very fast.

  • Wire up the LCD as detailed on the wiki
  • Build the Hex file (Build All) and program this on your board. Verify that the LCD displays Analog Input on the first line and a number between 0 – 1023. Turn the potentiometer and observe that the number changes. Verify that A2 is on when the user switch is pressed and A3 is on when the potentiometer is turned past 1.66V. Note that there may be a delay with the LEDs turning on and off because of the delay we added to the code.

You have now printed to the LCD, but what if we want to print to our computer?

RS232 Communication

There are many different types of communication protocols. Some of these include USB, RS232, I2C, and SPI. We are going to communicate with your computer using RS232 with an RS232 to USB cable.

  • Inspect the PIC32MX: RS232 page. There are two examples of code. One is basic and the other uses interrupts. The basic code essentially waits until you push a character to do something, whereas the interrupt code allows your PIC to run through code and when you push a key, something will happen. There is no waiting with the interrupt code. We will discuss interrupts later.
  • Go to the interrupts example
  • Copy and paste the #define DESIRED_BAUDRATE line near your other defines.
  • Copy and paste the “volatile unsigned char data;” and place this somewhere before your main function. This is a variable will be explained later.
  • Right after the beginning of your main function, write:
int	pbClk;
  • Put “ pbClk =” in front of SYSTEMConfigPerformance to get the peripheral bus frequency.
  • After the Analog Input initialization but before you defined the LCDbuffer, copy and paste all of the lines from “// define setup Configuration 1 for OpenUARTx” to “INTEnableSystemMultiVectoredInt();”

In this bit of code, there is a function OpenUART2 which configures RS232 using the second set of UART pins. It also enables UART interrupts (discussed briefly below).

  • Put the following line of code before the buffer initialization
mInitAllLEDs();
  • Below the LCDbuffer initialization, write the following line:
char RS232_Out_Buffer[64];

This will be the buffer for RS232 communication. It can store up to 64 characters.

  • Right before your infinite while loop, put the following line:
putsUART2("*** Hello From the Mechatronics Lab ***\r\n");

This line of code is similar to the LCD puts function except that it will send this string to your computer. We only want this string to be displayed once, so we put it outside the main loop.

  • Inside the if statement for your digital input, put the following lines of code:
sprintf(RS232_Out_Buffer, "Analog Input %d\r\n", channel6); 
putsUART2(RS232_Out_Buffer);
  • Finally, copy and paste the interrupt routine (all of the code at the bottom of the wiki) to the bottom of your code (outside the main function).

This creates an interrupt routine (to be discussed in class) that will stop your code when a key is pressed on your keyboard. It will check to see if this key is a ‘c’. If it is, LED2 will turn on and LED3 will turn off. If the key is not a ‘c’, LED2 will be off and LED3 will turn on. No matter what key is pressed, LED1 will toggle on and off.

  • Circuit: Connect Pins F4 to the Orange Pin of the RS232 cable, F5 to the Yellow Pin of the RS232 cable and GND to Black Pin of the RS232 cable.

F4 is the PIC32 Receive for UART2 so it is connected to PC Transmit F5 is the PIC32 Transmit for UART2 so it is connected to PC Receive GND must be connected for both PC and PIC32

  • Build the Hex file (Build All) and program this on your board.
  • Open Hyperterminal located under Program Files -> Accessories -> Communication
  • Make up some name for the connection description
  • Click on the drop down for the Connect using and select COMXX for the virtual port. It will probably be a higher number like 10.
  • Use the following Port Settings – these were established in the initialization of RS232 (discussed on Thursday)
    • Bits per second – 9600 (this is the baud rate)
    • Data bits – 8
    • Parity – None
    • stop bits – 1
    • Flow Control – None
  • Click Ok. You will see a blank screen. Click the reset button on your board. You should see the welcome message.
  • Hold down the external button and you should see the analog number appear in Hyperterminal.
  • Turn the pot and hold down the external button again.
  • Verify that the LCD displays the same analog input that is on the PC. Verify that A2 is on when the user switch is pressed and A3 is on when the potentiometer is turned past 1.66V. Note that there may be a delay with the LEDs turning on and off because of the delay we added to the code.

You have now met the objectives for this lab. Demonstrate your circuit to the TA and clean up your station.

Lab 2 Code

ME333_Lab2.c