You are on page 1of 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

2 Describe the microcontroller as the heart of embedded system. Washing Machines. Almost all general purpose devices such as Digital Watches. • Microcontroller is called as heart of embedded systems because a microcontroller is the data processing “brain” of an embedded system.1. and programmable input/output peripherals. Copyright Politeknik Kota Bharu Page 2 .  In today's world of technology.1. • A microcontroller (sometimes abbreviated µC. we found Microcontrollers in almost every electronic device we use. uC or MCU) is a small computer on a single integrated circuit containing a processor core.1. Mobile Phones and Microwave Ovens works on the bases of Microcontroller. memory.3 Name the real life applications that use embedded system. It receive input signals. • 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. CD/DVD Players. 1. processes the input signals and generates output signals.

 This device is only consists of processing unit. 1.  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.4 List differences between microcontroller and microprocessor.  It is a register based multi-purpose electronics device which takes input from us. It’s a basic building block of modern processors and controllers.1. Camera uC s CD/DV D Players Security Microwave Alarms Oven. Microprocessor :  Microprocessor is the heart of any processing device. Copyright Politeknik Kota Bharu Page 3 . process that input data according to the program written in external memory and gives us useful results. that is Memory and I/O devices are need to be connected externally. Mobile Electronic Phones Measurement Instruments.

 It doesn't requires external ROM. Programming of both Microcontroller And Microprocessor is almost similar. I/O ports for its operation. thus it is small in size.Microcontroller :  Microcontroller is also like a Microprocessor except that a Microcontroller made by Integrating Memory and I/O ports on a single chip.  It is basically used for controlling various machines. As memory such as ROM/RAM is integrated on a single IC chip. Copyright Politeknik Kota Bharu Page 4 .

Zilog’s Z8. 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. RAM. Suitable for project which has limited space For general-purposes usages For specific-purposes usages 1. ROM. Intel’s 8051. hence it saves money and space  It is for specific-purposes usages.6 List the common microcontrollers that available in the market.  Each of the above microcontrollers has unique instruction set and register set.1.  CPU. RAM. Atmel’s AVR. They are Freescale Semiconductor’s (formerly Motorola) 68HC08/68HC11.ROM and I/O devices capacity for RAM. Suitable for project which has expanded space.5 Describe the advantages of microcontroller application. Microprocessor Microcontroller CPU. ROM. and PIC form Microchip Technology. timer and I/O CPU. hence it is easy to use for designing a system  It is suitable for dangerous and repeating tasks and tasks request highly accuracy 1. timer and I/O devices are all in a chip (Embedded in a chip). 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.1. therefore they are not compatible with each other Copyright Politeknik Kota Bharu Page 5 .  There are five major 8-bit microcontrollers.

Does it come in a 40-pin DIP (dual inline package) or a QFP (quad flat package). emulator. What is the highest speed that the microcontroller supports? • Packaging. Must accomplish the task assigned efficiently and cost effectively. • The third criterion in choosing a microcontroller is its ready availability in required quantities both now and in the future. and prototyping the end product. • a code-efficient C language compiler. Key considerations include the availability of : • an assembler. Wide availability and reliable sources of the microcontroller.Criteria in choosing microcontrollers 1. • technical support. This is important in terms of the final cost of the product in which a microcontroller is used. • debugger. 3. • Power consumption. • Cost per unit. • The second criterion in choosing a microcontroller is how easy it is to develop products around it. Or some other packaging format? This important in terms of space. Availability of software and hardware development. assembling. • The number of I/O pins and timer on the chip. Copyright Politeknik Kota Bharu Page 6 . For example. some microcontrollers cost 50 cents per unit when purchased 100. This is especially critical for battery-powered products.000 units at a time. • The amount of RAM and ROM on the chip. Considerations in this category are: • Speed. and • both in-house and outside expertise. 2. • Ease of upgrade to higher-performance or lower-power-consumption versions.

h> void main (void) { Main program here } Copyright Politeknik Kota Bharu Page 7 . o You can use code available in function libraries. State FOUR (4) benefits to human life by using microcontroller 3.2.Review Question 1. Microcontroller c. o C is easier to modify and update. Microprocessor 4. Determine FOUR (4) criteria in choosing microcontroller 8. Embedded System b. o C code is portable to other microcontrollers with little or no modification. State TWO (2) criteria in choosing microcontroller 1. What does the term “embedded system’ mean and give TWO (2) examples of embedded system applications 9. Define the terminologies below: a. Explain why microcontroller is called as heart of embedded system 6. 1. List FOUR (4) embedded products using microcontroller 2. List TWO (2) Characteristic of Embedded System 5.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. Draw the block diagram of microcontroller unit including support devices 7.2 Describe the Structure of C program for PIC #include<p18F4550.

648 to +2.777.535 Short 16-bit -32.483.967.3 List C Data type commonly used by PIC18  One of the goals of C18 programmer is to create smaller hex files.388.147.215 Short long 24 bit -8.535 Int 16-bit -32.147.294. PORTB = 0xFF } 1.768 to +32.388.767 Unsigned short long 24 bit 0 to 16.767 Unsigned short 16 bit 0 to 655.647 Copyright Politeknik Kota Bharu Page 8 .2.483.example: #include<p18F4550.608 to +8.h> void main (void) { TRISB = 0x00. 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.607 Unsigned long 32 bit 0 to 4.768 to +32.295 long 32 bit -2.

h> void main (void) { TRISB = 0.or + value Copyright Politeknik Kota Bharu Page 9 . z<=255. } Example 5-2 Write a C18 program to toggle all the bits of Port B continuously Solution: #include<p18F4550.) { PORTB = 0x55. } } Signed Char  The signed Char is an 8-bit data type that uses the most significant bit (D7 of D7 – D0) to represent the .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.. PORTB = 0xAA. z++) PORTB = z. for (. while(1).

for (z=0.-1. } Copyright Politeknik Kota Bharu Page 10 .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.+4. } Unsigned Int  The unsigned int is a 16-bit data type that takes a value range of 0 to 65.h> void main (void) { unsigned int z.z<8.-3.z++) { PORTB =mynum[1].h> void main (void) { char mynum[ ]= {+1. TRISB = 0. we have only 7 bits for the magnitude of the signed number.000 times Solution #include<p18F4550.-2. } while(1). for (z=0.+2.+3. PORTB = 0xAA.-4}. unsigned char z. TRISB = 0. } while(1). z++) { PORTB = 0x55.  As a result. 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. z<=50000.

TRISB = 0. or value from -32.  The C18 C compiler supports both short long and long data types. we should use the ________ data type Copyright Politeknik Kota Bharu Page 11 .000 times Solution: #include<p18F4550. Give the magnitude of unsigned char and signed char data type 3. z<=100000. Give the reason why we choose C programming instead of assembly 2. if we want values greater than 16-bit. while the long value is 32 bits wide.  The short long value is 24 bits wide. } while(1).767. for (z=0. we have only 15bits for the magnitude of the number.h> void main (void) { unsigned short long z. } Review Question 1.768 to + 32.535 (0000 – FFFFH).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. If we declaring a variable for person’s age. z++) { PORTB = 0x55. Other Data Types  The unsigned int is limited to value 0-65. PORTB = 0xAA. As a result. Example 5-5 Write a C18 program to toggle all bits of Port B 100.

“RC”. “RB”. the pin with a label “RA”. PORTC. Indicate what data type you would use for the following variables: a.  Some pins of the I/O ports are multiplexed with an alternate function from the peripheral features on the device. PORTB. that pin may not be used as a general purpose I/O pin. The number of days in year d. 4.  The PIC18 microcontroller have 40 pins. “RD” and “RE” can be uses as a PORT.3 Understand digital input and output (I/O) Programming in C  In the PIC18 family. MCLR (reset) and another of Vdd and Vss. when a peripheral is enabled.  In general. PORTD and PORTE.  The rest of the pins are designated as Vdd (Vcc). OSC2. The number of days in week c. The counter to keep the number of people getting on a bus 1. PIC18F4550 has a total of 33 pins set aside for the five ports PORTA. The temperature b. Copyright Politeknik Kota Bharu Page 12 . there are many ports for I/O operations. Vss (GND) OSC1.

 PIC18F4550 has a total of 35 I/O (input-output) pins which are distributed among 5 Ports. RB0 and RB1 an input iii. and  serial communication pins. RC7 and RC1 an output iv.I/O Ports pin and their function  Each port in PIC18F4550 can be used as digital Input/Output.  interrupts. 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. Input and Output (I/O) programming in C  Because the size of SFR in PIC18F4550 is 8-bits.  Some pins of the I/O ports are multiplexed with an alternate functions such as:  ADC. 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. Port A as an input ii. RE1 and RE2 as an output Copyright Politeknik Kota Bharu Page 13 . 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. Port D as Output v.5 Write a C18 program to make i.  timers.

Solution: #include<p18F4550. TRISC = 0b01111101. } Example 5.h> Viod main (void) { TRISA = 0b11111111. TRISD = 0x00. TRISB = 0b00000011. Write a C program to show the count from 0 to FFH on the LEDS Solution: Copyright Politeknik Kota Bharu Page 14 . TRISE = while(1).6 LEDs are connected to bits in Port B and Port C.

7 Write a C program to toggle only RB4 continuously without distributing the rest of the bits of Port B. and y is the bit (0-7) of that port.  We can access a single bit without disturbing the rest of the port.Rx to access a single bit of Portx. Example 5. C. We access the TRISx registers in the same way where TRISBbits. where x is the port A. otherwise. We use PORTxbits. send 55H to Port D .7.  For example. B.8 Write a C program to monitor bit RC5. PORTBbits. Example 5.Bit-addressable I/O programming  The l/O ports of PIC 18 are bit-addressable. send AAH to Port D Copyright Politeknik Kota Bharu Page 15 .TRISB7 indicates the D7 of the TRISB. If HIGH.RB7 indicates PORTB. or D.

PORTB = 0x35 & 0x0F. their understanding and mastery are critical in microprocessor-based system design and interfacing Example 5. TRISC = 0. shift right (>>). Copyright Politeknik Kota Bharu Page 16 . PORTC = 0x04 | 0x68.h> void main (void) { TRISB = 0.9 Run the following program and examine the result #include <p18F4550. inverter (~). PORTB = ~0x55. PORTD = 0x54 ^ 0x78. PORTD = 0x77 >>4.  These bit-wise operators are widely used in software engineering for embedded systems and control. consequently. EX- OR (^).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 (&). and shift left(<<)). OR (|). PORTC = 0x9A >> 3. TRISD = 0.

While(1). } Arthmetic Operators  There are seven arithmetic operators: o + Add o .decrement Copyright Politeknik Kota Bharu Page 17 .subtract o * multiply o / divide o ++ increment o -. PORTB = 0x06 <<4.

PORTB=0xF0&0x56. TRISB = 0. PORTC=0xF0|0x99. r = x * y. ADCON1 = 0X0E. PORTC=0xF0|0x90. TRISD = 0. y=9. Indicate the data on the ports for each of the following: a. PORTC=0xF0^0x90. TRISC = 0. while(1). f.Example Run the following program and examine the result #include <p18F4550. Copyright Politeknik Kota Bharu Page 18 . b. PORTD = r.h> void main (void) { unsigned char x= 5. PORTC = q. unsigned char p. c.q. PORTC=0xF0^0x76.r. e. } Review Question 1. p = x + y. q = y – x. PORTB=0xF0&0x45. PORTB = p. d.