Professional Documents
Culture Documents
1 Introduction To Embedded System and Pic Programming in C: Learning Outcomes
1 Introduction To Embedded System and Pic Programming in C: Learning Outcomes
INTRODUCTION TO
EMBEDDED SYSTEM AND
PIC PROGRAMMING IN C
Learning Outcomes
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.
• 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
Player
s
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 consisting 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 Z80, 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:
3. List FOUR (4) the real life applications that use embedded system
c. Microprocessor
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 <xc.h>
example:
#include <xc.h>
TRISB = 0x00;
PORTB =
0xFF
Unsigned Char
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 1-1
Write a C program to send values 00 – FF to Port B
Solution:
#include <xc.h>
void main (void)
{
unsigned char z;
TRISB = 0;
for (z=0; z<=255; z++)
PORTB = z;
while(1);
}
Solution:
#include <xc.h>
void main (void)
{
TRISB = 0;
for (;;)
{
PORTB = 0x55;
PORTB = 0xAA;
}
}
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
As a result, we have only 7 bits for the magnitude of the signed number, giving us value from -
128 to +127
D7 D6 D5 D4 D3 D2 D1 D0
Sign
Bit
Example 1-3
Write a C18 program to send values -4 to +4 to Port B
Solution:
#include <xc.h>
void main (void)
{
char mynum[ ]=
{0,+1,-1,+2,-
2,+3,-3,+4,-4};
char z;
TRISB = 0;
for (z=0;z<9;z++)
{
PORTB
=mynum[1];
}
while(1);
}
The unsigned int is a 16-bit data type that takes a value range of 0 to 65,535 (0 – FFFFH)
It is also used to set counter value more than 256
Example 1-4
Write a C program to toggle all bits of port B 50,000 times
Solution
#include <xc.h>
Signed Int
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.
Solution:
#include<xc.h>
void main (void)
{
unsigned short z;
TRISB = 0;
for (z=0;
z<=100000; z++)
{
PORTB = 0x55;
PORTB = 0xAA;
}
while(1);
}
Review Question
In the PIC18 family (PIC18F1220, PIC18F4220, PIC18F4550……), 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.
The 35 I/O pins of PIC18F4550 are also multiplexed with one or more alternative
functions of controller’s various peripherals.
To use any of I/O ports as an input or output port, it must be programmed. Each port in
pic18f4450 is associated with three 8 bit registers for IO operations.
o TRISx (8 bit)
o PORTx (8 bit)
o LATx (8 bit)
TRIS
TRISx: where x is the name of the ports either of A, B, C, D, E. Every port has its own TRIS
register, thus there are 5 TRISXs to be configured:
o TRISA
o TRISB
o TRISC
o TRISD
o TRISE
TRIS is used to control data direction (Input or Output) of each IO pin. TRIS register
needs to be set before the I/O operation
o 1 - to make a port an input
o 0 - to make a port an output
o After a Reset all port pins are defined as inputs
PORTx : Reads/write the levels on the pins of the device and bidirectional
PORT is used to Write or Read data from IO pins.
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
LAT
LATx : useful for read, modify and write operations on the I/O port
Example 1-6
Solution:
#include<xc.h>
void main (void)
{
TRISA = 0b11111111;
TRISB =
0b00000011; TRISC
= 0b01111101;
TRISD = 0x00;
TRISE = 0xF9;
while(1);
}
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:
#include<xc.h>
void main (void)
{
TRISB = 0;
TRISC = 0;
PORTB=0;
PORTC=0;
while(1)
{
PORTB ++;
PORTC +
+;
}
}
Example 1-8
Write a C program to toggle only RB4 continuously without distributing the rest of the bits of
Port B.
Solution:
#include<xc.h>
void main (void)
{
TRISB4=0; //make RB4 an output while(1)
{
RB4 = 1;
RB4 = 0;
Example 1-9
Write a C program to monitor bit RC5. If HIGH, send 55H to Port D ; otherwise, send AAH to
Port D
Solution:
#include<xc.h>
#define mybit PORTB5 //declare
single bit void main (void)
{
TRISB5 = 1; //make
RB5 an input
TRISD = 0; // port D as output while(1)
{
if (mybit == 1)
PORTD = 0x55;
else
PORTD = 0xAA;
}
Example 1-10
A programmer decides to make a home security system using a touch card at pin RB3, a smoke
sensor at pin RC5, a temperature sensor at pin RC6 while magnetic solenoid, buzzer and LED at
PORT D. Complete the pins input and output declaration using bit addressable format in C
langguage
Solution:
#include<xc.h>
void main (void)
{
TRISB3 = ;
TRISC5 = ;
TRISC6 = ;
TRISD =
;
1. Show how to configure TRIS register to make PORTB as an input and PORTC as an output.
4. Figure below shows the connection between LDR, LAMP and MOTOR with PIC18F. Build a C
language to configure the input output pins using byte addressable
7. A programmer decides to use two DC motors at pin RB6 and RB7; while the LED at RA5 and
two digital sensors at pin RB0 and RB1 in his circuit. Write the input output initialization for
the program using bit addressable format in C language.
8. A PIC microcontroller is used to control a system where the following devices are connected
to PORTD as stated in Table 1
Determine the value to be written on the TRISD register accordingly to the devices in Table 1
10. Write a program statement to set bit5 of Port B to logic HIGH by:
a. Bit addressable
b. Byte addressable
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 1.11
#include <xc.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;
PORTB = 0x06 <<4;
while(1);
}
Example
1.12
Run the following program and examine the result
#include <xc.h>
void main (void)
{
unsigned char x= 5, y=9;
unsigned char p,q,r;
TRISB = 0;
TRISC = 0;
TRISD = 0;
p = x + y;
PORTB = p;
q = y – x;
PORTC = q;
r = x * y;
PORTD = r;
while(1);
}
c. PORTC=0xF0^0x90; d. PORTC=0xF0^0x76;
e. PORTC=0xF0|0x90; f. PORTC=0xF0|0x99;
#include <xc.h>
void main (void)
{
TRISA = 0;
TRISB = 0;
TRISC = 0;
A = 0b01010101; B
= 0b10101010;
PORTA = A + B;
PORTB = A | B;
PORTC = A & B;
}
Time Delay
Example 1.13
Write a C program to toggle all the bits of Port B continuously with 250 ms delay.
Solution:
#include <xc.h>
void MSDelay(unsigned int);
void main(void)
{
TRISB = 0;
while (1)
{
P
O
R
T
B
0
x
5
5
;
MSDelay(250);
PORTB =
0xAA;
MSDelay(250);
}
}
void MSDelay(unsigned int
itime)
{
unsigned int i;
unsigned char j;
for
(i=0;i<itime;i++)
for
(j=0;j<1
65;j++);
}
Copyright Politeknik Kota Bharu Page 31
Using Delay Function
Example 1.14
#include <xc.h>
#define _XTAL_FREQ 64000000
void main (void)
{
TRISD = 0;
while (1)
// do it forever
{
PORTD = 0x55;
delay_ms(250);
PORTD = 0xAA;
delay_ms(250);
}
}
Review Question
2. Built a program using C language that will alternate blinking eight (8) LED at PORT D. The
initial condition for LED is 00001111. Use function delay given in figure below