Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

1

INTRODUCTION TO
EMBEDDED SYSTEM AND
PIC PROGRAMMING IN C

Learning Outcomes

1. Know the concept of embedded system


2. Know C programming for PIC microcontroller.
3. Understand digital input and output (I/O) programming in C
4. Apply C program for time delay and I/O operations.

1.1 Know the concept of embedded system

 The embedded system could be defined as a processor based system designed to perform a
few dedicated functions, often in real time

1.1.1 The meaning of embedded system.

• 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.

Copyright Politeknik Kota Bharu Page 1


• 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

1.1.2 Describe the microcontroller as the heart of embedded 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.

• Microcontroller is called as heart of embedded systems because a microcontroller is the


data processing “brain” of an embedded system. It receive input signals, processes the
input signals and generates output signals.

• A microcontroller (sometimes abbreviated µC, uC or MCU) is a small computer on a


single integrated circuit containing a processor core, memory, and programmable
input/output peripherals.

1.1.3 Name the real life applications that use embedded system.

 In today's world of technology, we found Microcontrollers in almost every electronic


device we use. Almost all general purpose devices such as Digital Watches, Washing
Machines, CD/DVD Players, Mobile Phones and Microwave Ovens works on the bases of
Microcontroller.

Copyright Politeknik Kota Bharu Page 2


Mobile
Electronic
Measurement Phones
Instruments.

Camera
uC s
CD/DV
D
Player
s
Security Microwave
Alarms Oven.

1.1.4 List differences between microcontroller and microprocessor.

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.

Copyright Politeknik Kota Bharu Page 3


Microcontroller :

 Microcontroller is also like a Microprocessor except that a Microcontroller made by


Integrating Memory and I/O ports on a single chip.
 It doesn't requires external ROM, I/O ports for its operation. As memory such as
ROM/RAM is integrated on a single IC chip, thus it is small in size.
 It is basically used for controlling various machines. Programming
of both Microcontroller and Microprocessor is almost similar.

Copyright Politeknik Kota Bharu Page 4


Microprocessor Microcontroller

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

1.1.5 Describe the advantages of microcontroller application.

 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

1.1.6 List the common microcontrollers that available in the market.

 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

Copyright Politeknik Kota Bharu Page 5


Criteria in choosing microcontrollers

1. Must accomplish the task assigned efficiently and cost effectively. Considerations in this
category are:

• Speed. What is the highest speed that the microcontroller supports?


• Packaging. Does it come in a 40-pin DIP (dual inline package) or a QFP (quad flat package).
Or some other packaging format? This important in terms of space, assembling, and
prototyping the end product.
• Power consumption. This is especially critical for battery-powered products.
• The amount of RAM and ROM on the chip.
• The number of I/O pins and timer on the chip.
• Ease of upgrade to higher-performance or lower-power-consumption versions.
• Cost per unit. This is important in terms of the final cost of the product in which a
microcontroller is used. For example, some microcontrollers cost 50 cents per unit when
purchased 100,000 units at a time.

2. Availability of software and hardware development.

• The second criterion in choosing a microcontroller is how easy it is to develop products


around it. Key considerations include the availability of :
• an assembler,
• debugger,
• a code-efficient C language compiler, emulator,
• technical support, and
• both in-house and outside expertise.

3. Wide availability and reliable sources of the microcontroller.

• The third criterion in choosing a microcontroller is its ready availability in required


quantities both now and in the future.

Copyright Politeknik Kota Bharu Page 6


Review Question

1. List FOUR (4) embedded products using microcontroller

2. State FOUR (4) benefits to human life by using microcontroller

3. List FOUR (4) the real life applications that use embedded system

4. Describe the meaning of terminologies below:


a. Embedded System

Copyright Politeknik Kota Bharu Page 7


b. Microcontroller

c. Microprocessor

5. List TWO (2) Characteristic of Embedded System

6. List THREE (3) advantages of microcontroller applications

7. Explain why microcontroller is called as heart of embedded system

Copyright Politeknik Kota Bharu Page 8


8. Draw the block diagram of microcontroller unit including support devices

9. State THREE (3) differences between microcontroller and microprocessor

10. Determine FOUR (4) criteria in choosing microcontroller

Copyright Politeknik Kota Bharu Page 9


11. What does the term “embedded system’ mean and give TWO (2) examples of
embedded system applications

12. State TWO (2) criteria in choosing microcontroller

2. Know C Programming for PIC microcontroller

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.

Copyright Politeknik Kota Bharu Page 10


1.2.2 Describe the Structure of C program for PIC

#include <xc.h>

void main (void)

Main program here

example:

#include <xc.h>

void main (void)

TRISB = 0x00;

PORTB =

0xFF

1.2.3 List C Data type commonly used by PIC18

 One of the goals of XC programmer is to create smaller hex files, so it is worthwhile to


re-examine C data type for XC
 A good understanding of C types for the XC 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

Copyright Politeknik Kota Bharu Page 11


Data Type Size in Bits Data Range/Usage
Unsigned char 8-bit 0 to 255
Char 8-bit -128 to +127
Unsigned int 16-bit 0 to 65,535
Int 16-bit -32,768 to +32,767
Unsigned short 16 bit 0 to 655,535
Short 16-bit -32,768 to +32,767
Unsigned short long 24 bit 0 to 16,777,215
Short long 24 bit -8,388,608 to +8,388,607
Unsigned long 32 bit 0 to 4,294,967,295
long 32 bit -2,147,483,648 to +2,147,483,647

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);
}

Copyright Politeknik Kota Bharu Page 12


Example 1-2
Write a C18 program to toggle all the bits of Port B continuously

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);
}

Copyright Politeknik Kota Bharu Page 13


Unsigned Int

 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>

void main (void)


{
unsigned int z;
TRISB = 0;
for (z=0;
z<=50000; z++)
{
PORTB = 0x00;
PORTB =
0xFF;
}
while(1);
}

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.

Other Data Types

 The unsigned int is limited to value 0-65,535 (0000 – FFFFH).


 The C18 C compiler supports both short long and long data types, if we want values greater than
16-bit.
 The short long value is 24 bits wide, while the long value is 32 bits wide.

Copyright Politeknik Kota Bharu Page 14


Example 1-5
Write a C18 program to toggle all bits of Port B 100,000 times

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

1. Give the reason why we choose C programming instead of assembly language

2. Write the structure of C program for PIC

Copyright Politeknik Kota Bharu Page 15


3. List FIVE (5) data type commonly used by PIC

4. Give the magnitude of “unsigned char” and “char” data type

5. If we declaring a variable for person’s age, we should use the data


type
6. Indicate what data type you would use for the following variables:
a. The temperature

b. The number of days in week

c. The number of days in year

d. The counter to keep the number of people getting on a bus

Copyright Politeknik Kota Bharu Page 16


3. Understand digital input and output (I/O) Programming in C

 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.

I/O Ports pin and their function

 Each port in PIC18F4550 can be used as digital Input/Output.


 Some pins of the I/O ports are multiplexed with an alternate functions such as:
 ADC,
 timers,
 interrupts, and
 Serial communication pins.

Copyright Politeknik Kota Bharu Page 17


 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:

Port Name Number of pins Pins


PORTA 7 RA0-RA6
PORTB 8 RB0-RB7
PORTC 7 RC0-RC2, RC4-RC7
PORTD 8 RD0-RD7
PORTE 4 RE0-RE3

 The 35 I/O pins of PIC18F4550 are also multiplexed with one or more alternative
functions of controller’s various peripherals.

Input and Output (I/O) programming in C

 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

Copyright Politeknik Kota Bharu Page 18


PORT

 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

Byte Size Input Output

 Port A – Port D are byte accessible

Example 1-6

Write a c language program to make


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

Solution:
#include<xc.h>
void main (void)
{
TRISA = 0b11111111;
TRISB =
0b00000011; TRISC
= 0b01111101;
TRISD = 0x00;
TRISE = 0xF9;
while(1);
}

Copyright Politeknik Kota Bharu Page 19


Example 1-7

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 +
+;
}
}

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 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, RB7 indicates PORTB.7. We access the TRISx registers in the same way
where TRISB7 indicates the D7 of the TRISB.

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;

Copyright Politeknik Kota Bharu Page 20


}
}

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 =
;

Copyright Politeknik Kota Bharu Page 21


Review Question

1. Show how to configure TRIS register to make PORTB as an input and PORTC as an output.

2. Write the C language instruction to set all PORTC pins as input

3. Explain briefly the meaning of bit addressability in PIC18 family

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

Copyright Politeknik Kota Bharu Page 22


5. Use instruction in C language to configure TRIS register to make pin 0, 1, 2 and 3 of PORTB
as an input and pin 4, 5, 6 and 7 of PORTC as an output

Copyright Politeknik Kota Bharu Page 23


6. State the difference between bit addressability with byte addressability in PIC18F

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

Bit I/O Device


0 LED1
1 Water pump
2 Switch S1
3 Water level sensor
4 LED2
5 Switch S2
6 Switch S3
7 Not Used

Determine the value to be written on the TRISD register accordingly to the devices in Table 1

Copyright Politeknik Kota Bharu Page 24


9. Figure B2 shows a new helmet design with embedded system. One active low switch is
placed in the inner shell (SW1) and the chain strap (SW2) of the helmet. Two LEDs are
placed at the outer shell namely LED1 and LED2. Build a C language to configure the input
output pins. Use PORTB as input and PORTC as output port

10. Write a program statement to set bit5 of Port B to logic HIGH by:

a. Bit addressable

b. Byte addressable

Copyright Politeknik Kota Bharu Page 25


Copyright Politeknik Kota Bharu Page 26
Logic Operation and data conversion in C

 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

Run the following program and examine the result

#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);
}

Copyright Politeknik Kota Bharu Page 27


Arthmetic Operators

 There are six arithmetic operators:


o + Add
o - subtract
o * multiply
o / divide
o ++ increment
o -- decrement

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);
}

Copyright Politeknik Kota Bharu Page 28


Review Question

1. Indicate the data on the ports for each of the following:


a. PORTB=0xF0&0x45; b.
PORTB=0xF0&0x56;

c. PORTC=0xF0^0x90; d. PORTC=0xF0^0x76;

e. PORTC=0xF0|0x90; f. PORTC=0xF0|0x99;

Copyright Politeknik Kota Bharu Page 29


2. Referring to the program below, calculate the value of registers PORTA, PORTB and
PORTC after each instruction

#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

 Delays are very essential in the operation of a microcontroller. Because microcontrollers


execute instructions at a very fast rate (in microseconds), programmers need to apply delay
so that it can interact properly with humans.
 Two factor that effect the accuracy of delay:
o The crystal frequency
o Compiler used to compile C program
 There are three basic ways to create a time delay
o Using for loop
o Using PIC timers
o Delay Function

Copyright Politeknik Kota Bharu Page 30


Using For Loop

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

1. State TWO (2) factors that can affect the


delay size

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

Copyright Politeknik Kota Bharu Page 32


3. Built a C program for main ( ) function to make LED1 on port RB0 and LED2 on port RB1
blink alternately for each 2 second continuously. You are given a sub function for time delay
in figure below

Copyright Politeknik Kota Bharu Page 33

You might also like