Vous êtes sur la page 1sur 18

1

INTRODUCTION TO
EMBEDDED SYSTEM AND PIC
PROGRAMMING IN C

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

Copyright Politeknik Kota Bharu Page 1


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
Phones
Measurement
Instruments.

Camera
uC s
CD/DV
D
Players

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

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

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. Define the terminologies below:
a. Embedded System
b. Microcontroller
c. Microprocessor
4. List TWO (2) Characteristic of Embedded System
5. Explain why microcontroller is called as heart of embedded system
6. Draw the block diagram of microcontroller unit including support devices
7. Determine FOUR (4) criteria in choosing microcontroller
8. What does the term “embedded system’ mean and give TWO (2) examples of
embedded system applications
9. State TWO (2) criteria in choosing microcontroller

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

1.2.2 Describe the Structure of C program for PIC

#include<p18F4550.h>

void main (void)

Main program here

Copyright Politeknik Kota Bharu Page 7


example:

#include<p18F4550.h>

void main (void)

TRISB = 0x00;

PORTB = 0xFF

1.2.3 List C Data type commonly used by PIC18

 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

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

Copyright Politeknik Kota Bharu Page 8


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

Copyright Politeknik Kota Bharu Page 9


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

Copyright Politeknik Kota Bharu Page 10


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.

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

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


2. Give the magnitude of unsigned char and signed char data type
3. If we declaring a variable for person’s age, we should use the ________ data type

Copyright Politeknik Kota Bharu Page 11


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

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

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

Copyright Politeknik Kota Bharu Page 12


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.

 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

 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

Byte Size Input Output

 Port A – Port D are byte accessible

Example 5.5

Write a C18 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

Copyright Politeknik Kota Bharu Page 13


Solution:

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

Copyright Politeknik Kota Bharu Page 14


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

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

Copyright Politeknik Kota Bharu Page 15


Solution:

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 5.9

Run the following program and examine the result

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

Copyright Politeknik Kota Bharu Page 16


PORTB = 0x06 <<4;
While(1);
}

Arthmetic Operators

 There are seven arithmetic operators:


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

Copyright Politeknik Kota Bharu Page 17


Example

Run the following program and examine the result

#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

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 18

Vous aimerez peut-être aussi