EC501 Embedded System Application Lab 4 Page | 1 PRACTICAL WORK 4 TITLE: PIC PROGRAMMING IN C OBJECTIVES: Upon completion of this practical work, student should be able; 1. Setting and use MPLAB C18 compiler to create project in C language, 2. To write I/O program in C, 3. To write PIC 18 Timer program used as a time delay, 4. 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) THEORY: A. I/O Programming In C: Ports PORTA-PORTD are byte accessible. We use the PORTA-PORTD labels as defined in C18 header file.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
EC501 Embedded System Application Lab 4
Page | 1
PRACTICAL WORK 4
TITLE: PIC PROGRAMMING IN C
OBJECTIVES:
Upon completion of this practical work, student should be able;
1. Setting and use MPLAB C18 compiler to create project in C language,
2. To write I/O program in C,
3. To write PIC 18 Timer program used as a time delay,
4. 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)
THEORY:
A. I/O Programming In C:
Ports PORTA-PORTD are byte accessible. We use the PORTA-PORTD labels as defined
in C18 header file.
EC501 Embedded System Application Lab 4
Page | 2
The I/O ports of PIC 18 are bit-addressable. We can access a single bit without disturbing
the rest of the port. We use PORTxbits.Rxy to access a single bit of Portx, where x is the port
A, B, C, D, and y is the bit (0-7) of that port.
For example, PORTBbits.RB7 indicates PORTB.7. We access the TRISx registers in the
same way where TRISBbits. TRISB7 indicates the D7 of the TRISB. The following table
shows the single-bit addresses of PIC18.
** Refer to reference book chapter 7 for more examples.
B. Logic Operation In C:
One of the most important and powerful features of the C language is its ability to perform bit
manipulation.
EC501 Embedded System Application Lab 4
Page | 3
Bit-wise operator in C
While every C programmer is familiar with the logical operators AND (&&), OR (||), and
NOT (!), many C programmer are less familiar with the bit-wise operators AND (&), OR (|),
EX-OR (^), inverter (~), shift right (>>), and shift left (<<). This bit-wise operators are
widely used in software engineering for embedded system and control.
The following shows some examples using the C bit-wise operators:
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:
EC501 Embedded System Application Lab 4
Page | 4
** Refer to reference book for more examples.
C. Data Conversion Programs In C
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:
EC501 Embedded System Application Lab 4
Page | 5
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.
EC501 Embedded System Application Lab 4
Page | 6
** refer to reference book for more examples.
D. TIMER PROGRAMMING IN C
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 18’s 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
EC501 Embedded System Application Lab 4
Page | 7
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.
EC501 Embedded System Application Lab 4
Page | 8
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.
EC501 Embedded System Application Lab 4
Page | 9
TMR0IF flag bit
Notice that the TMR0IF bit (Timer0 interrupt flag) is part of the INTCON (interrupt control)
register. See figure 9-3. As we will see, when the timer reaches its maximum value of
FFFFH, it rolls over to 0000, and TMR0IF is set to 1 (see figure 9-4)
Calculating Delay Length Using Timer
EC501 Embedded System Application Lab 4
Page | 10
Step To Program 8-bit Mode of Timer0
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 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.
** Refer to reference book for more examples of time delay using timer
EC501 Embedded System Application Lab 4
Page | 11
E. C Programming of Timer 0 and 1 as Counters
Timer can be used as counters if we provide pulses from outside the chip instead of using the
frequency of the crystal oscillator as the clock source. By feeding pulses to the T0CKI (RA4)
and T1CKI (RC0) pins, we turn Timer0 and Timer1 into Counter 0 and Counter 1,
selectively.
** Refer to reference book for more examples of counter programming using timer
EC501 Embedded System Application Lab 4
Page | 12
PROCEDURES:
** make sure MPLAB C18 or Hi-Tech C18 has been installed into your PC or