Chapter 5 - Pic Programming in C
Chapter 5 - Pic Programming in C
PIC PROGRAMMING IN C
Chapter Objective
Understand C programming for embedded
system
Understand input output (I/O) programming in C
Understand programming timer
C Vs. Assembly
C Language
Assembly Language
Time consuming
Tedious to modified
Functions
void main (void):
It represents that "main" function does not return any
value as well as does not accept any
parameter/arguments.
Bit Size
Data Range
unsigned char
8-bit
0 to 255
char
8-bit
-128 to +127
unsigned int
16-bit
0 to 65535
int
16-bit
-32768 to
+32767
*PIC18 has a limited
number of registers and
data RAM locations
#include<htc.h>
void main()
{
unsigned int z;
TRISB = 0;
for (z=0; z<=50000; z++)
{
PORTB = 0x55;
PORTB = 0xAA;
}
while(1);
}
Example 2
#include<htc.h>
void main()
{
unsigned short long z;
unsigned int x;
TRISB = 0;
for (z=0; z<=100000; z++)
{
PORTB = 0x55;
PORTB = 0xAA;
}
while(1); //stay here forever
}
Time delay
Example
Let's say we have 4MHz oscillator
connected to our microcontroller. Then,
one instruction cycle will take
4/4Mhz = 1us(micro second).
Example
Write a C18 program to toggle all bits of
Port B ports continuously with a 250 ms
delay. Assume that the system is
PIC184550 with XTAL=10MHz
#include<htc.h>
__CONFIG(0x3F3A);
void delay(unsigned int);
void main(void)
{
TRISB=0;
PORTB=0;
while(1)
{
PORTB = 0X55;
delay(250);
PORTB = 0xAA;
delay(250);
}
}
Answer
Exercise
Assume that a system is PIC18F4550 with
XTAL=20MHz.
Answer
#include<htc.h>
int a;
void main()
{
TRISB = 0xFF;
PORTB = 0x00;
TRISC=0x00;
while(1)
{
a = PORTB;
PORTC = a;
}
}
Answer
#include<htc.h>
void main(void)
{
unsigned char z;
TRISC = 0xFF;
TRISB = 0x00;
TRISD = 0x00;;
while(1)
{
z = PORTC;
if (z < 100)
PORTB = z;
else
PORTD = z;
}
}
Bit-addressable I/O
programming
The l/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, or 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.
Operation
Symbol
Example
Equal to
==
if(a == 0) b=b+5;
Not equal to
!=
if(a != 1) b=b+4;
Greater than
>
Less than
<
>=
<=
OPERATION
OPERATOR
DESCRIPTION
SOURCE CODE
EXAMPLE
RESULT
Increment
++
result = num1++;
0000 0000
0000 00001
Decrement
--
result = num1--;
1111 1111
1111 1110
Complement
result = ~num1;
0000 0000
1111 1111
Add
Integer or Float
Subtract
Integer or Float
Multiply
Integer or Float
Divide
Integer or Float
Logical AND
&
Integer Bitwise
Logical OR
Integer Bitwise
Logical OR
Integer Bitwise
Arithmetic Operation
0000 1010
*0000 0011
0001 1110
Logical Operation
1001 0011
& 0111 0001 0001 0001
1001 0011 1111 0011
|0111 0001
1001 0011
^ 0111 0001
1110 0010
Data conversion in C
Recall that BCD numbers were discussed earlier. As
stated there, many newer microcontrollers have a realtime clock (RTC) where the time and date are kept 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. In this section we show the application of logic
and rotate instructions in the conversion of BCD and
ASCII.
ASCII numbers
On ASCII keyboards, when the key "0" is activated, "0 II
0000" (30H) is provided to the computer. Similarly, 31H
(0110001) is provided for the key "1", and so on.
Programming Timer
The PICI8 has two to five timers depending on the family
member. They are referred to as Timers 0, I, 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.
Programming Timer
PIC18 has two to five timers
Depending on the family number
Example
4. After the timer reaches its limit and rolls over, in order to
repeat the process, the registers TMR0H and TMR0L
must be reloaded with the original value, and the
TMR0IF flag must be reset to 0 for the next round.
COUNTER PROGRAMMING
We used the timers of the PICI8 to generate time delays.
These timers can also be used as counters to count
events happening outside the PIC 18.
When it is used as a counter, however, it is a pulse
outside the PIC 18 that increments the TH, TL registers.
In counter mode, notice that registers such as T0CON,
TMR0H, and TMR0L are the same as for the timer
discussed in the last section; they even have the same
names.