You are on page 1of 4

Title: PIC18F4580TIMERPROGRAMMING IN C

Objectives
We able to:
i. Write a C program to configure I/O pins of the PIC18F4580 according to the given
problem using MPLAB IDE software.
ii. UsePIC18F4580 to process/read digital inputand produce/write digital output.
iii. Configure Timer0 to produce time delay.
iv. Configure Timer0 to produce counter.
Equipment
a. PC installed with software:
i. MPLAB IDE integrated with Microchip XC8 compiler version 1.31.
ii. PICKit2.
b. UIC00B programmer.
c. PIC18F4580 chip.
d. Jumper wires.
e. Lab power supply.
f. Project board.
g. Push button switch.
h. 200 resistor x 2.
i. LED x 8.
j. Buzzer.
Procedures
1. We create a new project file for PIC18F4580 with the following setting:
i. We set a project workspace name as PRACTICAL WORK 4 (GROUP NO.)
ii. And we add a source file to project, then we set the source file name as Practical
Work 4.c
2. We copy and paste the given programcode (Appendix A) into our source file and then build
we the program until successful. If any error occurs, we try to fix it.
3. We refer the program at Appendix A, at Main function; we write the initialization of
input/output port based on the schematic diagram shown in Appendix B. We write our
initialization code in the result section.
4. We write a C program to configure TIMER 0 by setting T0CON, TMR0H and TMR0L
registers by referring the tables below:

Table 1: Registers Associated with TIMER0

Table 2: T0CON Control Register

Configuration Setting:
TIMER0 in 16-bit mode, prescaler 1:2,use internal clock, increment on positive-edge.
T0CON=

We build our program until successful.


5. According to setting TIMER 0 at step 4, we calculate the values need to be loaded into the
TMR0H and TMR0L registers in order to produce a time delay of 1 miliseconds. Given Fosc
= 20MHz.
6. Under section Function Prototype, declare voiddelay_ms(unsigned int x); .
We try completing the following function:
void delay_ms(unsigned int x)
{
for( ;x>0;x- -)
{
T0CON=0b________________;
TMR0H=0x_____;

TMR0L=0x_____;
T0CONbits.TMR0ON= ___;
while(INTCONbits.TMR0IF= = ___);
T0CONbits.TMR0ON= ___;
INTCONbits.TMR0IF= ___;
}
}
7. We refer to the schematic shown in Appendix B, we write the C program to:
a) Activate the buzzer with the following pattern:
Loop BUZZER= ON Delay = 20000ms BUZZER=OFFDelay=20000msLoop
We write our code in the result section and we run our circuit and observe the result.
b) create a sound pattern with the following sequence:
Loop BUZZER= ON Delay1BUZZER=OFF Delay2BUZZER= ON
Delay3BUZZER=OFF Delay4BUZZER= ON Delay5BUZZER=OFF
Delay6
BUZZER= ON Delay7BUZZER=OFF Delay8 Loop
Delay1=10000ms
Delay2=10000ms
Delay3=1000ms
Delay4=1000ms

Delay5=1000ms
Delay6=1000ms
Delay7=500ms
Delay8=500ms

8. Now, we want to use TIMER0 to act as a counter. We connect a push button switch
(connection in pull-up setting) to pin RA4 with the following operation:
Operation:
The buzzer will sound continuously for 5 seconds then OFF when the switch is pressed for 5
times, then the system will repeat again.
We write a new program code to run the above operation.
9. We draw a flow chart to illustrate the working principle of the whole system based on
operation in Step 8.

Discussion

What is the main difference of a timer and counter?

What is the function of a pre-scaler?

Repeat again Step 7(b), now connect 8 LEDs at all pins of Port D. Write the Part 1 &
Part 2 code to get the same operation as observed in your lecturers trainer demo.

According to Procedure Step No.4 6, what is a new complete program code if we


want to produce a truly 1 second delay in real time.
(you may test your code by using the same hardware operation as in Step 7(a))

Conclusion
Thanks to our lecturer during our previous class, he gave us briefing about the upcoming lab
work. He provides us guidance during our lab work so we can finish it with the correct way.
During the lab work we can write a C program to configure I/O pins of the PIC18F4580
according to the given problem using MPLAB IDE software. Next, we use PIC18F4580 to
process/read digital input and produce/write digital output. Besides that, we learn to configure
Timer0 to produce time delay and we learn configure Timer0 to produce counter. Finally, we
know that we still lacking with knowledge, but still with our lecturer giving us guidance
during the lab, we manage to finish the lab successfully.

You might also like