You are on page 1of 13

Lab III: Introduction to the PIC18F4520

By Kyle Thomson Revised 1/26/2010 blw You will need the PIC datasheet (DO NOT PRINT), available here: http://ww1.microchip.com/downloads/en/DeviceDoc/39631a.pdf Any use of figures or text from this document for lab reports will result in immediate failure of the lab.

A. Getting started
A-1 On the protoboard, connect the left side Vdd to the Right side Vdd with red wire. Connect the left side ground to the right side ground with black wire. A-2 Place the microcontroller in the proto-board. Pin 1 of the microcontroller should be at hole 11 of the protoboard. Connect pins 11 and 32 to Vdd, and connect pins 12 and 31 to ground. A-3 Connect a 10K resistor between pin 1 of the microcontroller and Vdd. (Hint for final projects: This is a pull-up resistor, grounding this pin acts as a reset button for the microcontroller.) A-4 Put the clock in the protoboard, the silver dot should be in the lower right hand corner. The top should be located at hole 33 of the protoboard. Connect the power to the clock as shown in the figure to the right. A-5 Connect the pin shown to pin 13 of the microcontroller. A-6 Put the RJ45 header, from your parts kit, in the top of the protoboard, starting pin 1 of the header in hole 1. Connect the pins shown in the figure on the left. Connect pin 2 of the header to Vdd. Connect pin 3 of the header to ground.

A-7 Start MPLAB. It is located in Start > All Programs > Microchip > MPLAB IDE v8.43 > MPLAB IDE A-8 Under Projects, start the Project Wizard. Select Next. A-9 In this lab, we are using the PIC18F4520. Make sure this device is selected, and hit Next. A-10 Make sure the Active Toolsuite is set to Microchip C18 Toolsuite. Select Next. A-11 Select Create New Project File and browse to the location where you want to save your project. Ex. M:/ece480/Lab3. Select Save and select Next A-12 The window will prompt you to add an existing file. You need to select the file located in C:\ >Program Files > Microchip > MCC18 > Bin > LKR > 18f4520_g.lkr. Select Add. Your screen should look like the figure to the right. Select Next and select Finish A-13 Go to File > New. Copy and paste this block of code into the new window
#include <p18cxxx.h> #pragma config WDT=OFF long int count; void main() { TRISD = 0x00; //Set all pins of D to outputs // TRISD = 0xFF sets all pins of D to inputs PORTD = 0; while(1) { PORTD = 0x01; //Set pin 0 of PORTD to 1 for(count = 1; count < 200000; count++); PORTD = 0x02; //Set Pin 1 of PORTD to 1 for(count = 1; count < 200000; count++); PORTD = 0x03; //Set Pins 0 and 1 of PORTD to 1 for(count = 1; count < 200000; count++); } }

A-14 Go to File > Save. Type the file name as M:\ece480\LAB3.c. Select the check box at the bottom labeled Add File To Project. Select Save. It should look like the figure to the right A14-1. Verify Library search path. Go to Project > Build Options > Select Project under the directories tab change to view the Library Search Path the path should be c:\Program Files\ Microchip\MCC18\lib if it is not browse to the directory and update the path otherwise select Apply and OK

A-15 We now need to hook up LEDs to our Port D. Pins 0 and 1 of Port D are located on pins 19 and 20 of the microcontroller. Hook up the LEDs as shown in the figure to the left. The Resistors can be anywhere from 200-500 Ohms.

A-16 Turn on the Hewlett Packard DC Power Supply. Press the +6V button under METER. Set this to 5V using the +6V dial. Turn the power supply back off. Connect a black banana-to-banana connector from COM to the Green Ground. Plug a black banana-to-hook from the COM to your protoboards ground. Plug a red banana-to-hook from the +6V to your protoboards Vdd. It is now time to turn on the power supply.

WARNING! If a yellow light comes on under OVERLOAD, or your amps go past 0.10, turn off the power supply IMMEDIATELY and ask your TA for help. You can burn out your microcontroller very easily, and it will not be replaced! WARNING!

A-17 Now plug the Microchip MPLAB ICD 2 into the adaptor on the protoboard. In MPLAB, select under Debugger > Select Tool > MPLAB ICD 2. If you get a warning about downloading a new OS select OK otherwise your output should look like the figure to the right. If that does not happen, there must be a problem in your wiring. Turn off the power supply and locate the problem. A-18 Go to Project > Build All. You should not get any red errors. If you do, then there is a coding error that needs to be corrected. Go back to 13 and check your code. A-19 Go to Debugger > Program. This will upload the program to the microcontroller. This can also be achieved by pressing button 1 highlighted below. A-20 Run the program by going to Debugger > Run. This can also be achieved by pressing button 2 highlighted below. The Red and Green LEDs should now be flashing in pattern. Have your TA sign off on your lab. A-21 Stop the program by pressing button 3 highlighted below. NOTE: You must always follow this pattern when trying new code: STOP > SAVE > BUILD ALL > PROGRAM > RUN

A-22 Modify the code so that all for statements look like this.
for(count = 1; count < 500000; count++);

STOP > SAVE > BUILD ALL > PROGRAM > RUN A-23 Explain in your own words what is happening.

B. Accepting Inputs
B-1 Add the circuit shown to the right. Remember to always turn the power supply off before making changes to your circuit. Follow the WARNING! Box. Note that the push button only fits in the board in one direction. If you improperly install the pins, the push button will not work.

B-2 You will now modify your code to accept inputs.


#include <p18cxxx.h> #pragma config WDT=OFF long int count; void main() { TRISD = 0b00000100; //Set Pin 2 of Port D to be an input //the rest are set to be outputs //TRISD = 0x04; this would also work while(1) { if (PORTDbits.RD2) //if Pin2 is high (switch open) { PORTDbits.RD0 = 1; } else //if Pin2 is low (switch closed) { PORTDbits.RD1 = 1; } for(count = 1; count < 40000; count++); PORTDbits.RD1 = 0; //turn off LED1 PORTDbits.RD0 = 0; //turn off LED2 for(count = 1; count < 40000; count++); } }

B-3 STOP > SAVE > BUILD ALL > PROGRAM > RUN. Verify that the circuit is functioning properly. Have your TA sign off that your circuit is working. B-4 Explain, in your own words, what is happening.

C. Analog to Digital Conversion


The PIC microcontroller can perform ADC with a resolution of 10 bits, with a varying reference voltage. C-1 Remember to always turn the power supply off before making changes to your circuit. Follow the WARNING! Box. Add the circuit shown on the left. Use the table below to determine RA, RB, and RC. RA corresponds to the first digit your student ID, RB the third digit, and RC the last digit. Fill those numbers and their corresponding resistances in using the table below.
ID # 0 1 2 3 4 5 6 7 8 9 Resistance 15K 18K 22K 27K 33K 39K 47K 56K 68K 82K

C-2 Hook the Digital Multimeter to Pin 5 and GND. Record the voltage in your lab report, using as many significant figures as the DM gives. C-3 Hook the Digital Multimeter to Pin 2 and GND. Record the voltage in your lab report, using as many significant figures as the DM gives. Disconnect the DM and turn the power off.

C-4 Enter the code below


#include <p18cxxx.h> #include <ADC.h> #pragma config WDT=OFF long int count; int adc_result, loop; void main() { TRISD = 0x04; PORTDbits.RD0 = 0; PORTDbits.RD1 = 0; OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_12_TAD, ADC_CH0 & ADC_INT_OFF, 0); //open adc port for reading ADCON1 =0x00; //set VREF+ to VDD and VREF- to GND (VSS) SetChanADC(ADC_CH3); //Set ADC to Pin 5 while(1) { while (PORTDbits.RD2) { for(count = 1; count < 40000; count++); PORTDbits.RD0 = !PORTDbits.RD0; } ConvertADC(); //perform ADC conversion while(BusyADC()); //wait for result adc_result = ReadADC(); //get ADC result for(loop = 1; loop < 21; loop++) { for(count = 1; count < 20000; count++); PORTDbits.RD1 = !PORTDbits.RD1; } while (PORTDbits.RD2) { for(count = 1; count < 40000; count++); PORTDbits.RD0 = !PORTDbits.RD0; } SetChanADC(ADC_CH0); //Set ADC to Pin 2 ConvertADC(); //perform ADC conversion while(BusyADC()); //wait for result adc_result = ReadADC(); //get ADC result } }

C-5 STOP > SAVE > BUILD ALL > PROGRAM > RUN. Make sure there are no compile errors. The circuit should be flashing the Red LED. Press the Button ONCE. The green LED should flash shortly, and then return to Red. Hit the STOP button on the control panel in MPLAB. C-6 Go to View > 9 Watch. Under symbol name, enter adc_result. It will display a value in the red box, shown on the right. Record this value in your lab report. C-7 This is a ten bit voltage value in Hexadecimal. First, we need to convert to a decimal value. Record this value in the lab report. We set VREF+ to 5V, and VREF- to GND, or 0V. This means we have 1024 bits on a range of 0-5V. Thus, we can turn our adc_result into a voltage value. Multiply the first value of C-6 by 5 , our voltage reference over our bit range. Is this close to our value in C-2? 1024 C-8 Press Run again. The Red LED should be flashing again. Press the button again. Once again, The green LED should flash shortly, and then return to Red. Hit the STOP button on the control panel in MPLAB. C-9 Go to View > 9 Watch. The value of adc_result should have changed. Record this value in your lab report. C-10 Once again, convert to Decimal, record the value, and determine the voltage. Is this close to the value in C-3?

C-11 Make the code changes reflected below.


#include <p18cxxx.h> #include <ADC.h> #pragma config WDT=OFF long int count; int adc_result, loop; void main() { TRISD = 0x04; PORTDbits.RD0 = 0; PORTDbits.RD1 = 0; OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_12_TAD, ADC_CH0 & ADC_INT_OFF, 0); //open adc port for reading ADCON1 =0x10; //set VREF+ to CH3 and VREF- to GND (VSS) while(1) { while (PORTDbits.RD2) { for(count = 1; count < 40000; count++); PORTDbits.RD0 = !PORTDbits.RD0; } SetChanADC(ADC_CH0); for(loop = 1; loop < 21; loop++) { for(count = 1; count < 20000; count++); PORTDbits.RD1 = !PORTDbits.RD1; } ConvertADC(); //perform ADC conversion while(BusyADC()); //wait for result adc_result = ReadADC(); //get ADC result } }

C-12 STOP > SAVE > BUILD ALL > PROGRAM > RUN. Press the Button ONCE. Stop the program. C-13 The program now uses ADC Channel 3 as VREF+ voltage. Thus, we are measuring Channel 0 and comparing it to Channel 3. Go to View > 9 Watch. Record the hexadecimal value of adc_result. See page 226 of the datasheet to understand the ADCON1 register. C-14 We will now calculate the voltage. Convert the hexadecimal value to a decimal. C 2 Multiply this value by your answer from C-2 divided by 1024, i.e. C 14 . 1024 Is this close to your value in C-3? Closer than C-10? Why?

D. Mini-Project
D-1 Remove your wiring from C-1, and assemble the circuit to the right. Add the YELLOW LED on PIN 22 of the microcontroller. Be sure to include a current limiting resistor. Turn the +20 Voltage knob on the power supply all the way to the left. Using a banana-to-grabber, hook the 20 Volt output on the power supply to pin 2 of the microcontroller. This will simulate our battery. Note: Take care when using the +20 Voltage Supply to simulate a battery, if you adjust the voltage above 6 volts you may irreparably damage your PIC.

D-2 Modify the code to perform the specifications of the mini-project listed below. This project is due at the end of next weeks lab period.

In this project, you will use the PIC to measure the voltage output of a 1.5 volt, AA cell battery. Flash the red LED if the battery is dead, yellow LED if the battery is weak and a green LED if battery is good. The flashing of the light should be at a rate of 5 Hz for 3 seconds. A voltage level above 1.2 is considered good, and a voltage level of 1.2 to 0.8 is considered weak. Use the push button to start the test. Use the reference voltage created by the voltage divider to increase the accuracy of the battery tester.

D-3 Have your TA sign off that your circuit is working.

D-4 Staple your lab report, schematic, and commented code to the back of the last 3 pages, and hand them in.

Lab Report
Lab III - Introduction to PIC18F4520 Name: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Date: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Lab Section Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Code of Ethics Declaration All of the attached work was performed by me as listed above. I did not obtain any information or data from any other student in this lab or any other lab.

Signature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

A-20 I, _________________ (Lab instructors initials) verify that part A is working correctly. A-23 ________________________________________________________________

________________________________________________________________

________________________________________________________________

B-3 I, _________________ (Lab instructors initials) verify that part B is working correctly. B-4 ________________________________________________________________

________________________________________________________________

________________________________________________________________

C-1 A: Digit_____ Resistance _______

B: Digit_____ Resistance _______

C: Digit_____ Resistance _______

C-2 Voltage at Pin 5: __________

C-3 Voltage at Pin 2: ______________

C-6 adc_result: _______

C-7 Decimal Value: ________ Corresponding Voltage: ____________ Close? ___________ C-9 adc_result: _______

C-10 Decimal Value: ________ Corresponding Voltage: ____________ Close? ___________

C-13 adc_result: ______________

C-14 Decimal Value: __________ Corresponding Voltage: _____________

Close? _________ Why?

Closer? __________

________________________________________________________________

________________________________________________________________

________________________________________________________________

D-3 I, _________________ (Lab instructors initials) verify that part D is working correctly.

You might also like