1.0 PIC Programming in C
1.0 PIC Programming in C
1.0 PIC Programming in C
INTRODUCTION TO
EMBEDDED SYSTEM AND PIC
PROGRAMMING IN C
The embedded system could be defined as a processor based system designed to perform a
few dedicated functions, often in real time
• A special purpose computer system designed to perform one or few dedicated function
often with real time
• Usually refer to the product that controlled by its own internal microprocessor or
microcontroller.
• An embedded system is a system that has software embedded into computer-hardware,
which makes a system dedicated for an application(s) or specific part of an application or
product or part of a larger system.
• Typically, the microcontroller’s ROM is burned with a purpose for specific functions needed
for the system.
• Characteristics of Embedded System
o Designed to do a specific task
o The program instructions written for embedded systems are referred to as firmware,
and are stored in read-only memory of flash memory chips.
• Examples: Remote control, telephone, Video Game, Printer, Camera, Security System etc
• Microcontroller is an IC chip that takes input process data according to program written
in its memory and gives output as control signal for controlling other machines and
devices.
1.1.3 Name the real life applications that use embedded system.
Camera
uC s
CD/DV
D
Players
Security Microwave
Alarms Oven.
Microprocessor :
Microprocessor is the heart of any processing device. It’s a basic building block of
modern processors and controllers.
It is a register based multi-purpose electronics device which takes input from us, process
that input data according to the program written in external memory and gives us useful
results.
This device is only consists of processing unit, that is Memory and I/O devices are need
to be connected externally.
As it requires external memory and I/O devices so it requires large space and is larger in
size. It is of no use without interfacing with external memory and I/O ports.
CPU, ROM, RAM, timer and I/O CPU, ROM, RAM, timer and I/O devices
devices are all in separated unit are all in a chip (Embedded in a chip)
System designer can determine the Capacity of RAM,ROM and I/O devices
capacity for RAM, ROM and I/O have been determined during
devices fabrication
Capacity of memory and I/O port can Capacity of memory and I/O port
be added (expansive) at any time cannot be added (expansive) at any
time.
Suitable for project which has
expanded space. Suitable for project which has limited
space
For general-purposes usages
For specific-purposes usages
CPU, ROM, RAM, timer and I/O devices are all in a chip (Embedded in a chip), hence it
saves money and space
It is for specific-purposes usages, hence it is easy to use for designing a system
It is suitable for dangerous and repeating tasks and tasks request highly accuracy
There are five major 8-bit microcontrollers. They are Freescale Semiconductor’s
(formerly Motorola) 68HC08/68HC11, Intel’s 8051, Atmel’s AVR, Zilog’s Z8, and PIC form
Microchip Technology.
Each of the above microcontrollers has unique instruction set and register set; therefore
they are not compatible with each other
1. Must accomplish the task assigned efficiently and cost effectively. Considerations in this
category are:
Introduction
The following are some of major reason for writing programs in C instead of Assembly:
o It is easier and less time consuming.
o C is easier to modify and update.
o You can use code available in function libraries.
o C code is portable to other microcontrollers with little or no modification.
#include<p18F4550.h>
#include<p18F4550.h>
TRISB = 0x00;
PORTB = 0xFF
One of the goals of C18 programmer is to create smaller hex files, so it is worthwhile to
re-examine C data type for C18
A good understanding of C types for the C18 can help programmers to create smaller
hex files
C data type that are most useful and widely used for the PIC18 microcontroller is shown
in Table below
The unsigned char is an 8-bit data type that takes value in range 0 – 255 (00-FFH)
It is one of the most widely used data types for PIC18
C compilers use the signed char as default unless we put the keyword unsigned in front
of the char
Example 5-1
Write a C program to send values 00 – FF to Port B
Solution:
#include<p18F4550.h>
void main (void)
{
unsigned char z;
TRISB = 0;
for (z=0; z<=255; z++)
PORTB = z;
while(1);
}
Example 5-2
Write a C18 program to toggle all the bits of Port B continuously
Solution:
#include<p18F4550.h>
void main (void)
{
TRISB = 0;
for (;;)
{
PORTB = 0x55;
PORTB = 0xAA;
}
}
Signed Char
The signed Char is an 8-bit data type that uses the most significant bit (D7 of D7 – D0) to
represent the - or + value
D7 D6 D5 D4 D3 D2 D1 D0
Sign
Bit
Example 5-3
Write a C18 program to send values -4 to +4 to Port B
Solution:
#include<p18F4550.h>
void main (void)
{
char mynum[ ]= {+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
TRISB = 0;
for (z=0;z<8;z++)
{
PORTB =mynum[1];
}
while(1);
}
Unsigned Int
The unsigned int is a 16-bit data type that takes a value range of 0 to 65,535 (0 – FFFF H)
It is also used to set counter value more than 256
Example 5-4
Write a C program to toggle all bits of port B 50,000 times
Solution
#include<p18F4550.h>
void main (void)
{
unsigned int z;
TRISB = 0;
for (z=0; z<=50000; z++)
{
PORTB = 0x55;
PORTB = 0xAA;
}
while(1);
}
Signed int is a 16-bit data type that uses the most significant bit (D15 of D15-D0) to represent
the – or + value. As a result, we have only 15bits for the magnitude of the number, or value from
-32,768 to + 32,767.
Example 5-5
Write a C18 program to toggle all bits of Port B 100,000 times
Solution:
#include<p18F4550.h>
void main (void)
{
unsigned short long z;
TRISB = 0;
for (z=0; z<=100000; z++)
{
PORTB = 0x55;
PORTB = 0xAA;
}
while(1);
}
Review Question
In the PIC18 family, there are many ports for I/O operations.
The PIC18 microcontroller have 40 pins, the pin with a label “RA”, “RB”, “RC”, “RD” and
“RE” can be uses as a PORT. PIC18F4550 has a total of 33 pins set aside for the five
ports PORTA, PORTB, PORTC, PORTD and PORTE.
Some pins of the I/O ports are multiplexed with an alternate function from the
peripheral features on the device.
In general, when a peripheral is enabled, that pin may not be used as a general purpose
I/O pin.
The rest of the pins are designated as Vdd (Vcc), Vss (GND) OSC1, OSC2, MCLR (reset)
and another of Vdd and Vss.
PIC18F4550 has a total of 35 I/O (input-output) pins which are distributed among 5
Ports. The following table shows the names and numbers of I/O pins of these 5 ports:
The 35 I/O pins of PIC18F4550 are also multiplexed with one or more alternative
functions of controller’s various peripherals.
Because the size of SFR in PIC18F4550 is 8-bits, we can read/write the data to SFR by
two methods:
o Byte input output
o Bit input output
Example 5.5
i. Port A as an input
ii. RB0 and RB1 an input
iii. RC7 and RC1 an output
iv. Port D as Output
v. RE1 and RE2 as an output
#include<p18F4550.h>
Viod main (void)
{
TRISA = 0b11111111;
TRISB = 0b00000011;
TRISC = 0b01111101;
TRISD = 0x00;
TRISE =
while(1);
}
Example 5.6
LEDs are connected to bits in Port B and Port C. Write a C program to show the count from 0 to
FFH on the LEDS
Solution:
Example 5.7
Write a C program to toggle only RB4 continuously without distributing the rest of the bits of
Port B.
Example 5.8
Write a C program to monitor bit RC5. If HIGH, send 55H to Port D ; otherwise, send AAH to
Port D
One of the most important and powerful features of the C language is its ability to
perform bit manipulation
Many C programmers are less familiar with the bitwise operators AND (&), OR (|), EX-
OR (^), inverter (~), shift right (>>), and shift left(<<)).
These bit-wise operators are widely used in software engineering for embedded
systems and control; consequently, their understanding and mastery are critical in
microprocessor-based system design and interfacing
Example 5.9
#include <p18F4550.h>
void main (void)
{
TRISB = 0;
TRISC = 0;
TRISD = 0;
PORTB = 0x35 & 0x0F;
PORTC = 0x04 | 0x68;
PORTD = 0x54 ^ 0x78;
PORTB = ~0x55;
PORTC = 0x9A >> 3;
PORTD = 0x77 >>4;
Arthmetic Operators
#include <p18F4550.h>
void main (void)
{
unsigned char x= 5, y=9;
unsigned char p,q,r;
ADCON1 = 0X0E;
TRISB = 0;
TRISC = 0;
TRISD = 0;
p = x + y;
PORTB = p;
q = y – x;
PORTC = q;
r = x * y;
PORTD = r;
while(1);
}
Review Question