Lab Work 4: PIC Programming in C
Lab Work 4: PIC Programming in C
At the end of this lab session, students will: 1. Use MPLAB and to write program in c language for PIC Microcontroller. 2. Use Proteus to simulate c language for PIC Microcontroller. 3. Using hardware Equipment list: 1. Personal computer (PC) 2. MPLAB software 3. C18 Compiler
Precedure: Creating Project File 1. Create a New Project File. You will be using the PIC18F4550. Make sure this device is selected, and hit Next. 2. Make sure the Active Toolsuite is set Microchip C18 Toolsuite. Select Next 3. Select LAB4 as the project name 4. Now open a new file by clicking File>New, and key in the program below:
5. Activiti 1 - Write and run the test program given below. The test program is to toggle bit 0 of Port D continously.
#include<p18F4550.h> void MSDelay(void); void main (void) { TRISD = 0x00; //make PortD and output while (1) //repeat forever { PORTD = 0x01; MSDelay(); PORTD=0x00; MSDelay(); } } void MSDelay(void) { int i; int j; for (j=0;j<10;j++) { for (i=0;j<30000;i++); } } 6. Save this file using File>Save As. Select file type All Source File, and save as lab5.c 7. Right-click on the folder Source Files (Project window), and click Add Files to add lab4.c 8. Assemble the program by pressing Project>Build All. (If the error occurs, fix it and repeat this step again)
Simulating the program using Proteus 1. Simulate your program to the schematic shown below
U1
2 3 4 5 6 7 14 13 33 34 35 36 37 38 39 40 RA0/AN0 RC0/T1OSO/T1CKI RA1/AN1 RC1/T1OSI/CCP2/UOE RA2/AN2/VREF-/CVREF RC2/CCP1/P1A RA3/AN3/VREF+ RC4/D-/VM RA4/T0CKI/C1OUT/RCV RC5/D+/VP RA5/AN4/SS/LVDIN/C2OUT RC6/TX/CK RA6/OSC2/CLKO RC7/RX/DT/SDO OSC1/CLKI RB0/AN12/INT0/FLT0/SDI/SDA RB1/AN10/INT1/SCK/SCL RB2/AN8/INT2/VMO RB3/AN9/CCP2/VPO RB4/AN11/KBI0/CSSPP RB5/KBI1/PGM RB6/KBI2/PGC RB7/KBI3/PGD RD0/SPP0 RD1/SPP1 RD2/SPP2 RD3/SPP3 RD4/SPP4 RD5/SPP5/P1B RD6/SPP6/P1C RD7/SPP7/P1D RE0/AN5/CK1SPP RE1/AN6/CK2SPP RE2/AN7/OESPP RE3/MCLR/VPP 15 16 17 23 24 25 26
19 20 21 22 27 28 29 30 8 9 10 1
R1
220
D1
LED-YELLOW
18
VUSB PIC18F4550
R2
10k
Figure 4.1 2. Activity 2 Modify the circuit and program so that all the all LED connected to PORT D.
Using Hardware 1. Write and run the test program given below.
#include <p18f4550.h> /* --- BEGIN: changes required for bootloader ------------------------------ */ extern void _startup (void); // See c018i.c in your C18 compiler dir #pragma code _RESET_INTERRUPT_VECTOR = 0x000800 void _reset (void) { _asm goto _startup _endasm } #pragma code #pragma code _HIGH_INTERRUPT_VECTOR = 0x000808 void _high_ISR (void) { ; } #pragma code _LOW_INTERRUPT_VECTOR = 0x000818 void _low_ISR (void) { ; } #pragma code /* --- END: changes required for bootloader -------------------------------- */ /* This pragma forces the code below this line to be put into the code */ /* section (memory address >= 0x82A). See linker script for details. */ void MSDelay (void); /* ------------------------------------------------------------------------- */ /* main */ /* Delay function. */ /* ------------------------------------------------------------------------- */ void main(void) { TRISD=0x00; while(1) {
PORTD = 0x01; MSDelay(); PORTD = 0x00; MSDelay(); } } void MSDelay(void) { int i; int j; for (j = 0; j < 10; j++) { for (i = 0; i < 30000; i++); } }