Professional Documents
Culture Documents
PRACTICAL WORK 4
TITLE:
PIC PROGRAMMING IN C
OBJECTIVES: Upon completion of this practical work, student should be able; 1. 2. 3. 4. Setting and use MPLAB C18 compiler to create project in C language, To write I/O program in C, To write PIC 18 Timer program used as a time delay, Build, simulate and debug program code using MPLAB SIM and test the code created in simulation software and then in target board.
EQUIPMENT: 1) PC or Laptop with MPLAB IDE, MPLAB C18/Hi-Tech C18 and Proteus Isis installed. 2) SK40C (controller board) 3) Target Board 4) UIC00B (programmer) 5) AC to DC adaptor (output:7.5v 15v dc) 6) Reference Book: PIC Microcontroller and Embedded System (Chapter 7 & Chapter 9)
Ports PORTA-PORTD are byte accessible. We use the PORTA-PORTD labels as defined in C18 header file.
Page | 1
One of the most important and powerful features of the C language is its ability to perform bit manipulation.
Page | 2
Bit-wise shift operation in C There are two bit-wise shift operators in C: (1) shift right (>>), and (2) shift left (<<). Their format in C is as follows: Data >> number of bits to be shifted right Data << number of bits to be shifted left The following shows some examples of shift operators in C:
Page | 3
Many newer microcontroller have a real-time clock (RTC) where the time and date are keep even when the power is off. Very often the RTC provides the time and date in packed BCD. To display them, however, it must convert them to ASCII. ASCII Number On ASCII keyboard, when the key 0 is activated, 011 0000 (30H) is provided to the computer. Similarly, 31H (011 0001) is provided for the key 1 and so on, as shows in the table below:
Page | 4
Packed BCD to ASCII Conversion: To convert packed BCD to ASCII, you must first convert it to unpacked BCD. Then the unpacked BCD is tagged with 001 0000 (30H). The following demonstrates converting from packed BCD to ASCII:
ASCII to Packed BCD Conversion: To convert ASCII to packed BCD, you first convert it to unpacked BCD (to get rid of the 3), and then combine to make packed BCD. For example, 4 and 7 on the keyboard give 34H and 37H, respectively, the goal is to produce 47H or 0100 0111 which is packed BCD.
Page | 5
PIC 18 has two to five timers depending on the family member. They are referred to as Timers 0, 1, 2, 3 and 4. They can be used either as timers to generate a time delay or as counters to count events happening outside the microcontroller. 1. Programming Timer 0 and 1
Every timer need a clock pulse to tick. The clock source can be internal or external. If we use the internal clock source, then 1/4th of the frequency of the crystal oscillator on the OSC 1 and OSC 2 pins (Fosc/4) is fed into the timer. Therefore, it is used for time delay generation and for that reason is called a timer. By choosing the external clock option, we feed pulses through one of the PIC 18s pins: this is called counter. Basic Register of the Timer Many of the PIC 18 timers are 16 bits wide. Because the PIC 18 has an 8-bit architecture, each 16-bit timer is accessed as two separate registers of low byte (TMRxL) and high byte (TMRxH). Each timer also has the TCON (timer control) register for setting modes of operation. Timer0 Registers and Programming Timer0 can be used as an 8-bit or a 16-bit timer. The 16-bit register of Timer0 is accessed as low byte and high byte, as shown in figure 9-1. The low-byte register is called TMR0L (Timer0 low byte) and the high-byte register is referred to as TMR0H (Timer0 high byte).
T0CON (Timer0 control) register Each timer has a control register, called TCON, to set the various timer operation modes. T0CON are shown in Figure 9-2
Page | 6
T0CS (Timer0 clock source) This bit in the T0CON register is used to decide whether the clock source is internal (Fosc/4) or external. If T0CS = 0, then the Fosc/4 is used as clock source. In this case, the timers are often used for time delay generation. See example 9-1. If T0CS = 1, the clock source is external and comes from the RA4/T0CKI, which is pin 6 on the DIP package of PIC18 18f4580/4520. When the clock source comes from external source, the timer is used as an event counter.
Page | 7
Step To Program Timer0 in 16-bit Mode 1. Load the value into the T0CCON register indicating which mode (8-bit or 16-bit) is to be used and the selected prescaler option. 2. Load register TMR0H followed by register TMR0L with initial count values. 3. Turn on T0CON, TMR0ON 4. Keep monitoring the timer flag (TMR0IF)) to see if it is raised. Get out of the loop when TMR0IF become high. 5. Turn off T0CON,TMR0ON. 6. Clear the TMR0IF flag for the next round. ** In C18 we can access timer registers such as TMR0H, TMR0L, and T0CON directly using the PIC18Fxxx.h header file.
Page | 8
Page | 9
** Refer to reference book for more examples of time delay using timer Page | 10
** Refer to reference book for more examples of counter programming using timer
Page | 11
ACTIVITY 1.0: Basic Project (byte I/O programming )and MPLAB IDE Configuration 1.1 Creating file (source code) Start MPLAB IDE and select File>New to bring up a new empty source file. Type the source text given below into this new file. Use File>Save As to save this file. Browse to or create a new folder location to store projects. Click Save.
Note: Creating a new source file can be done either before or after creating a new project. The order is not important. Creating a new file does not automatically add that file to the currently open project. 1.2 Creating Project
1. Select Project>Project Wizard to create a new project. When the Welcome screen displays, click Next> to continue. 2. At Step One: Select a device, use the pull-down menu to select the device. Device used is PIC18F4580.
Page | 12
3. At Step Two: Select a language toolsuite, choose Microchip C18 Toolsuite as the Active Toolsuite. Then click on each language tool in the toolsuite (under Toolsuite Contents) and check or set up its associated executable location (Figure 3-2).
Click Next> to continue. 4. At Step Three: Name your project (Figure 3-3), enter the name of the project and use Browse to select the folder where the project will be saved. Then click Next> to continue.
Page | 13
5. At Step Four: Add any existing files to your project, navigate to the source file to be added to the project. First, select the source file created earlier. If source files have not yet been created, they can be added later (see Figure 3-4). Click ADD>> to add it to the list of files to be used for this project (on the right).
Second, a linker script file must be added to tell the linker about the memory organization of the selected device. Linker scripts are located in the lkr subfolder of the installation directory for MPLAB C18. Scroll down to the .lkr file for the selected device, click on it to highlight and click ADD>> to add the file to the project. See example in Figure 3-5. Select Next> to continue. Note: There are also linker scripts delivered with MPASM when it is installed with MPLAB IDE. Make sure to use the linker scripts in the \mcc18\lkr folder.
Page | 14
6. At the Summary screen, review the Project Parameters to verify that the device, toolsuite and project file location are correct. Use <Back to return to a previous wizard dialog. Click Finish to create the new project and workspace. Click OK to exit.
1.3
1.4
For MPLAB C18 compiler executable, verify its location is C:\mcc18\bin\mcc18.exe as shown in Figure 3-8.
Page | 16
And for the MPLINK Linker, ensure that its location is C:\mcc18\bin\MPLink.exe as shown in Figure 3-10.
1.5
Page | 17
2. One option may need to be changed from the default. Click on the MPLINK Linker tab. If it is not checked, click on the box labeled Suppress COD-file generation:
Page | 18
Shortcut keys, Ctrl+F10 and F10, can be used instead of selecting items from the menu. There are icons on the toolbar for these functions as well, so either one function key or one mouse click will build the project:
If the message Errors : 0 is not shown from both MPLINK (the Linker) and MP2HEX (the .hex file converter), something may have been mistyped. Expand the Output window and look for the first error. If it was a mistype, then double click on that error line in the Output window to edit the error in the file main.c. 1.6.2 Testing with MPLAB SIM To test these programs in MPLAB IDE, use the built-in simulator, MPLAB SIM.
Page | 19
After the simulator is selected, the Debug Toolbar (Figure 3-16) appears under the MPLAB menus.
Page | 20
The Stopwatch also shows the current processor frequency setting of 20 MHz. To measure the time between the LEDs flashing off and on, breakpoints will be set at the appropriate places in the code. Use the right mouse button to set breakpoints. 1.6.4 Set Breakpoints Click on in the word PORTB and press the right mouse button. The debug menu will appear as shown in Figure 4-13.
Select Set Breakpoint from the menu, and the screen should now show a breakpoint on this line, signified by a red icon with a B in the left gutter (see Figure 4-14).
Page | 21
Put a second breakpoint on line 15, the line to send a value of 0x5A to PORTB. The Editor window should have two breakpoints and look similar to Figure 4-15:
1.6.5 Run Program 3 Select the Run icon and the program should execute, then will stop at a breakpoint indicated by a green arrow on the first breakpoint. Note that the Stopwatch has measured how much time it has taken to get to this point (see Figure 4-16).
Page | 22
The Stopwatch reading is 7.000000 microseconds, indicating it took seven microseconds to start from reset to run to this point in the program.
The Stopwatch now reads 7.200000 microseconds, indicating it took 0.2 microseconds to get here from the last breakpoint. Select Run again to go around the loop back to the first breakpoint (Figure 4-18):
1.7
Page | 23
ACTIVITY 2.0: Bit I/O Programming A door sensor is connected to RB1 pin, and a buzzer is connected to RC7. When the door is open, the buzzer will sound (ON). You can sound the buzzer by sending a square wave of few hundred Hz frequency to it. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 Open new file and write C program code to monitor the door sensor, and when it opens, sound the buzzer. Save as your code as activity2.c Create new project and save it as activity2. (follow step 1.2) Configure Language Tools location if needed (follow step 1.4) Verify installation and build option if needed (Follow step 1.5) Build and Testing your project using MPLAB SIM (Follow step 1.6) Simulate your project using Proteus Isis Run your project in target board. Show your project to lecturer
ACTIVITY 3.0: Write PIC18 timers in C language to generate time delays 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 Open new file and write C program as shown in examples 9-28 in theory section. Save as your code as activity3.c Create new project and save it as activity3. Configure Language Tools location if required. Verify installation and build option if required. Build and Testing your project using MPLAB SIM. Simulate your project using Proteus Isis. Run your project in target board. Show your project to lecturer.
ACTIVITY4.0: Write PIC18 timers in C language to be used event counter 4.1 4.2 4.3 4.4 Open new file and write C program as shown in examples 9-34 in theory section. Save as your code as activity4.c Create new project and save it as activity4. Configure Language Tools location if required. Page | 24
DISCUSSION: Discuss in detail each activity. CONCLUSION: Conclude the practical with reference to the objectives.
Page | 25