67
INTRODUCTION TO MICROCONTROLLERS AND EMBEDDED SYSTEMS Prepared by: Islam Samir

Introduction to Embedded Systems and Microcontrollers

Embed Size (px)

Citation preview

Page 1: Introduction to Embedded Systems and Microcontrollers

INTRODUCTION TO MICROCONTROLLERS AND EMBEDDED SYSTEMS

Prepared by:

Islam Samir

Page 2: Introduction to Embedded Systems and Microcontrollers

- Islam Samir Mohamed

- Electronics and Communications department, Faculty

of Engineering, Cairo University

- Embedded Software Engineer.

Page 3: Introduction to Embedded Systems and Microcontrollers

INTRODUCTION Prequisties:- Digital logic design, basics of combinational and

sequntial circuits.- Basics of microprocessors.

(A brief review will be given in the course)- C programming language.

Page 4: Introduction to Embedded Systems and Microcontrollers

INTRODUCTION Why do we study this course? As engineers:- Learning how microprocessors work and how to use them is

very essential for every electrical engineer in many ways:

1. Implementing mathmatical algorithms:- To implement an algorithm for a communication system,

control system, or any real time system we usually write a program and load it on the target microprocessor.

- Then, this microprocessor is used as a part of this system.

Page 5: Introduction to Embedded Systems and Microcontrollers
Page 6: Introduction to Embedded Systems and Microcontrollers

INTRODUCTION

2. Minimizing the cost and power:- If this system is a part of a battery-powered device, or it’s

required to keep the device’s cost low as much as possible, we’ve to use a cheap microprocessor.

- To do so, the program should be minimized as possible and the designer should make use of every element of the used microprocessor/microcontroller.

Page 7: Introduction to Embedded Systems and Microcontrollers

INTRODUCTION As Egyptians:- The development of the field of electrical engineering

in Egypt is our responsibility, not anyone else.

- To face the challenges facing our country these days, we have to be at a technical level that allows us to create new products and compete in the global market.

- To do so, we’ve to be equipped with the required knowledge and experience to innovate and bring new ideas that gives us the advantage in the market.

Page 8: Introduction to Embedded Systems and Microcontrollers

AGENDA

What are embedded systems? How do we implement them?

What is a microcontroller? Developing embedded applications using

MCU’s. Basics of architecture. PIC microcontrollers. Basics of PICmicro devices architecture. Memory organization and addressing modes. C programming language.

Page 9: Introduction to Embedded Systems and Microcontrollers

AGENDA

What are embedded systems? How do we implement them?

What is a microcontroller? Developing embedded applications using

MCU’s. Basics of architecture. PIC microcontrollers. Basics of PICmicro devices architecture. Memory organization and addressing modes. C programming language.

Page 10: Introduction to Embedded Systems and Microcontrollers

EMBEDDED SYSTEMS An embedded system is a special purpose

system that is used to perform one or few dedicated functions.

Simply, we can call any computer system embedded inside an electronic device an embedded system.

Page 11: Introduction to Embedded Systems and Microcontrollers

EMBEDDED SYSTEMS (CONT.) Embedded systems are

made to perform few tasks only, after implementation you can’t use them for another purposes.

Ex. You can’t watch movies using the microprocessor of your microwave oven!!

Page 12: Introduction to Embedded Systems and Microcontrollers

EMBEDDED SYSTEMSExamples:

Digital and analog televisions

Set-top boxes (DVDs, VCRs, Cable boxes)

Personal digital assistants (PDAs)

MP3’s and iPod's

Kitchen appliances (refrigerators ,microwave ovens)

Telephones/cell phones

Cameras

Global positioning systems

And many others.

Page 13: Introduction to Embedded Systems and Microcontrollers

SO.. HOW DO WE IMPLEMENT THEM?

We do so by using microcontrollers (or microprocessor based systems).

Or simply by using the digital circuits that perform the function we want.

Page 14: Introduction to Embedded Systems and Microcontrollers

COMPARISON

Digital circuits Microprocessor based systems

Faster- only propagation delay.

Slower- Technology dependent.

Inflexible-Functions they perform can’t be changed easily.

Flexible- We need only to update the software.

Example- Communication systems ciphering algorithms.

Example- Personal computers, PDA’s, Mobile phones and PLC’s.

Page 15: Introduction to Embedded Systems and Microcontrollers

AGENDA

What are embedded systems? How do we implement them?

What is a microcontroller? Developing embedded applications using

MCU’s. Basics of architecture. PIC microcontrollers. Basics of PICmicro devices architecture. Memory organization and addressing modes. C programming language.

Page 16: Introduction to Embedded Systems and Microcontrollers

WHAT IS A MICROCONTROLLER?

It’s a full computer system on a chip, even if its resources are far more limited than of a desktop personal computer.

Designed for stand alone operations.

Page 17: Introduction to Embedded Systems and Microcontrollers

WHAT IS A MICROCONTROLLER? (CONT.) So.. What’s the difference between a

microcontroller and a microprocessor system?

Page 18: Introduction to Embedded Systems and Microcontrollers

WHAT IS A MICROCONTROLLER? (CONT.)

A microcontroller has a processor and many peripherals integrated with it on the same chip, like a flash memory, RAM, I/O ports, serial communication ports, ADC …Etc.

Page 19: Introduction to Embedded Systems and Microcontrollers

WHAT IS A MICROCONTROLLER? (CONT.)

A timer module to allow the MCU to perform tasks for certain time periods.

A serial I/O port to allow data to flow between the MCU and other devices such as a PC or another MCU.

An ADC to allow the MCU to accept analog inputs for processing.

Page 20: Introduction to Embedded Systems and Microcontrollers

WHAT IS A MICROCONTROLLER? (CONT.)

But a microprocessor can’t do all the functions of a computer system on its own, and needs another circuits to support it like:

I/O devices, RAM, ROM, DMA controllers, Timers, ADC, LCD drivers.. Etc.

Page 21: Introduction to Embedded Systems and Microcontrollers

COMPARISONMicrocontroller General purpose microprocessor

Depend mainly on its peripherals like:Program memory, I/O ports, timers, interrupt circuitry, ADC…Etc.

Depend mainly on other devices like:I/O devices, memory, DMA controllers ..Etc.

Used for a few dedicated functions determined by the system designer.

Used in many applications, according to the program running on it

Usually used as a part of a larger system

It’s in the heart of our PC’s.

Page 22: Introduction to Embedded Systems and Microcontrollers

POPULAR MICROCONTROLLERS

8051 (Intel and others) 80386 EX (Intel) PIC (Microchip) 68HC05 (Motorola) Z8 (Zilog)

Page 23: Introduction to Embedded Systems and Microcontrollers

AGENDA

What are embedded systems? How do we implement them?

What is a microcontroller? Developing embedded applications using

MCU’s. Basics of architecture. PIC microcontrollers. Basics of PICmicro devices architecture. Memory organization and addressing modes. C programming language.

Page 24: Introduction to Embedded Systems and Microcontrollers

DEVELOPING EMBEDDED SYSTEMS

Embedded systems development

Software development

Hardware development

Page 25: Introduction to Embedded Systems and Microcontrollers

HARDWARE DEVELOPMENT This includes choosing the right MCU for your

application, so that it can satisfy the required specifications.

The criteria for choosing a microcontroller is:

1- Number of I/O ports.

2- Serial communication modules.

3- Peripherals like (Timer, ADC, PWM ..Etc.)

4- Memory requirements.

5- Processing speed required.

6- Power requirements.

Page 26: Introduction to Embedded Systems and Microcontrollers

SOFTWARE DEVELOPMENT• Writing the required

algorithm using assembly or a high level language.

• Using a compiler or assembler and a linker.

• Debugging your code.

Page 27: Introduction to Embedded Systems and Microcontrollers

SOFTWARE DEVELOPMENT

Compiler Assembler

Page 28: Introduction to Embedded Systems and Microcontrollers

SOFTWARE DEVELOPMENT Using assembly involves learning the used

microcontroller's specific instruction set but results in the most compact and fastest code.

Using C programming language makes your code portable, which means that you can use it for another target microcontroller without learning its instruction set, this eases the process of software development (short time to market) with acceptable quality.

Page 29: Introduction to Embedded Systems and Microcontrollers

C VS ASSEMBLY

Assembly programs are optimized more than C programs, but to develop more complicated programs, using C is more practical and also efficient.

Page 30: Introduction to Embedded Systems and Microcontrollers

LINKER

The linker’s function is to link code modules saved in different files together into a single final program .

At the same time it takes care of the chip's memory allocation by assigning each instruction to a microcontroller memory addresses in such a way that different modules do not overlap.

Page 31: Introduction to Embedded Systems and Microcontrollers

DEBUGGER

Common debugging features include:

1. The capability to examine and modify the microcontroller's  on-chip registers, data- and program-memory.

2. Pausing or stopping program executing at defined program locations by setting breakpoints.

3. Single-stepping (execute one instruction at a time) through the code; and looking at a history of executed code (trace).

Page 32: Introduction to Embedded Systems and Microcontrollers

SOFTWARE DEVELOPMENT

An Integrated Development Environment (IDE) puts all of the previously discussed software components under one common unified user interface.

Page 33: Introduction to Embedded Systems and Microcontrollers

AGENDA

What are embedded systems? How do we implement them?

What is a microcontroller? Developing embedded applications using

MCU’s. Basics of architecture. PIC microcontrollers. Basics of PICmicro devices architecture. Memory organization and addressing modes. C programming language.

Page 34: Introduction to Embedded Systems and Microcontrollers

ARCHITECTURE (MEMORY ACCESSING)Von Neumann One memory for data and program, with one bus for both.

CPU provides address to get data or instructions.

Data and instructions must have the same width.

Harvard-Separate program and data memories and separate busses, can be accessed simultaneously.

separated buses allow one instruction to execute while the next instruction is fetched.

Data and instructions mustn’t have the same width.

Page 35: Introduction to Embedded Systems and Microcontrollers

ARCHITECTURE (NUMBER OF INSTRUCTIONS)CISC Processors:-A large no. of instructions requiring different no. of clock cycles.

Support many addressing modes.

More complex operations are implemented in hardware and more elaborate way of accessing data.

RISC Processors:-Less no. of instructions.

-Instructions are of fixed length. This facilitates ins. pipelining as when the CPU fetches a new ins. It will depend only on the address not on the pervious ins.

-Few addressing modes.

Page 36: Introduction to Embedded Systems and Microcontrollers

AGENDA

What are embedded systems? How do we implement them?

What is a microcontroller? Developing embedded applications using

MCU’s. Basics of architecture. PIC microcontrollers. Basics of PICmicro devices architecture. Memory organization and addressing modes. C programming language.

Page 37: Introduction to Embedded Systems and Microcontrollers

PIC MICROCONTROLLERS One of the leading architectures for

low end applications (app.'s that require only 4-bit, 8-bit or 16-bit processors).

They are RISC, Harvard architecture processors.

Easier implementation to pipelining without having a complex hardware, less silicon area and less power consumption.

Page 38: Introduction to Embedded Systems and Microcontrollers

PIC MICROCONTROLLERS PICmicro devices architectural features:1 – Harvard, RISC architecture.

2 - Single Word Instructions.

Each instruction takes one word of memory (14-bits).

3 - Single Cycle Instructions.

Each instruction is fetched in one instruction cycle, decoded and executed in the next instruction cycle.

4 - Instruction Pipelining

An instruction is fetched and another instruction is executed at the same time every single TCY.

Page 39: Introduction to Embedded Systems and Microcontrollers

PIC MICROCONTROLLERS

The one we’ll use is: PIC 16f877A

Page 40: Introduction to Embedded Systems and Microcontrollers

AGENDA

What are embedded systems? How do we implement them?

What is a microcontroller? Developing embedded applications using

MCU’s. Basics of architecture. PIC microcontrollers. Basics of PICmicro devices architecture. Memory organization and addressing modes. C programming language.

Page 41: Introduction to Embedded Systems and Microcontrollers

OSCILLATOR You’ve to choose the suitable oscillator mode you’ll use

in your application, according to:

1. The required processing speed.

2. The required timing precision.

Page 42: Introduction to Embedded Systems and Microcontrollers

OSCILLATOR

1. External RC Oscillator:- Depend on the value of Rext, Cext, the supply

voltage and the temperature.- Less cost compared to using a crystal.

2. Internal 4 MHz RC Oscillator:- provides a fixed 4 MHz (nominal) system clock at

VDD = 5V and 25°C.- The value in the OSCCAL register is used to tune the

frequency of the internal RC oscillator.

Page 43: Introduction to Embedded Systems and Microcontrollers

OSCILLATOR

3-Crystal oscillator:-A crystal or ceramic resonator is connected to the OSC1 and OSC2 pins to establish oscillation.

- Used for high precession timing requirements.

-The capacitors are chosen according to the frequency and the preferred values in the datasheet of the used device.

Page 44: Introduction to Embedded Systems and Microcontrollers

INSTRUCTION CYCLE

We call the time of fetching, decoding and executing an instruction, the instruction cycle.

For PICmicro devices:

Instruction cycle = 4 oscillator clock cycles

Page 45: Introduction to Embedded Systems and Microcontrollers

INSTRUCTION CYCLE

Actually, each instruction is fetched in one instruction cycle, and then decoded and executed in another ins. cycle.

Due to using pipelining, while the current instruction is fetched, the pervious instruction is executed. So, you can say that each instruction is fetched, decoded and executed in one instruction cycle (4 clock cycles).

Page 46: Introduction to Embedded Systems and Microcontrollers

INSTRUCTION CYCLE The instruction fetch begins with the program

counter incrementing in Q1.

In the execution cycle, the fetched instruction is latched into the “Instruction Register (IR)” in cycle Q1. This instruction is then decoded and executed during the Q2, Q3 and Q4 cycles. Data memory is read during Q2 (operand read) and written during Q4 (destination write).

Page 47: Introduction to Embedded Systems and Microcontrollers

INSTRUCTION CYCLE

Ex. If you use a 4MHZ oscillator, the MCU will execute 1Million instruction per second (1 MIPS)

Clock frequency (4MHZ) = Instruction execution (1MHZ)

Page 48: Introduction to Embedded Systems and Microcontrollers

AGENDA

What are embedded systems? How do we implement them?

What is a microcontroller? Developing embedded applications using

MCU’s. Basics of architecture. PIC microcontrollers. Basics of PICmicro devices architecture. Memory organization and addressing modes. C programming language.

Page 49: Introduction to Embedded Systems and Microcontrollers

MEMORY ORGANIZATION

There are two memory blocks:- Program memory.- Data memory. Each one has its own bus. so that access to each

block can occur during the same oscillator cycle.

Page 50: Introduction to Embedded Systems and Microcontrollers

MEMORY ORGANIZATION Program memory:- 8 K×14 program memory space, capable of carrying 8K

instructions.- Reset vector 0000h

the place in memory the CPU branches to when a reset occurs.

- Interrupt vector 0004h- the place in memory the CPU branches to when there is an

interrupt signal.- 13-bit program counter.- Program memory is partitioned to 4 banks, chosen by writing

the PCLATH<4:3> bits before executing any CALL or GOTO instruction.

[11bit from op-code + 2 paging bits]

Page 51: Introduction to Embedded Systems and Microcontrollers
Page 52: Introduction to Embedded Systems and Microcontrollers

MEMORY ORGANIZATION

Stack:- 8 level stack allow up to 8 program calls /

interrupts to occur.- The return address (13-bit) will be PUSHed into

the stack so that the CPU knows from to continue.

- When a return instruction is executed, the whole 13bits of PC are POPed from the stack.

- The stack pointer is not readable or writable.- There are no PUSH or POP instructions.

Page 53: Introduction to Embedded Systems and Microcontrollers

MEMORY ORGANIZATION

Data memory:- Contains of general purpose registers (GPRs), and

special function registers (SFRs).- SFRs control the functions of the core and the

peripherals.- GPRs are not initialized by a power up on reset,

and unchanged on all other resets.- Data memory is partitioned to 4 banks, each

bank is 128 byte.

Page 54: Introduction to Embedded Systems and Microcontrollers

MEMORY ORGANIZATION

Direct addressing:- The bank selection bits are used [STATUS <6:5>].

Indirect addressing:- Data memory address is not fixed.- The address is first defined in the SFR register. then any access to the INDF register will access the register pointed to by the SFR register.

Page 55: Introduction to Embedded Systems and Microcontrollers

AGENDA

What are embedded systems? How do we implement them?

What is a microcontroller? Developing embedded applications using

MCU’s. Basics of architecture. PIC microcontrollers. Basics of PICmicro devices architecture. Memory organization and addressing modes. C programming language.

Page 56: Introduction to Embedded Systems and Microcontrollers

C PROGRAMMING LANGUAGE

Features of C: Extensive use of function calls Low level (Bitwise) programming readily available Pointer implementation - extensive use of pointers for

memory and arrays. Structures and functions. Can handle low-level activities. Produces efficient programs. Fast. It can be compiled on a variety of computers.

Page 57: Introduction to Embedded Systems and Microcontrollers

C PROGRAMMING LANGUAGEGeneral form for any C program:

Page 58: Introduction to Embedded Systems and Microcontrollers

DATA TYPESType Bit Width Range:

Char 8 0 to 255

unsigned Char 8 0 to 255

Signed Char 8 -128 to 127

short 16 0 or 65536

long 32 0 to 65536

float 32 3.4E-38 to 3.4E+38

- For each variable we should Know: Sign, Size and the variable’s name.

Page 59: Introduction to Embedded Systems and Microcontrollers

OPERATORS

+ addition

- subtraction

* multiplication

/ division

% modulus a*=b is the same as a=a*b a/=b a=a/b a+=b a=a+b a-=b a=a-b a%=b a=a%b a<<=b a=a<<b a>>=b a=a>>b a&=b a=a&b a|=b a=a|b a^=b a=a^b

Page 60: Introduction to Embedded Systems and Microcontrollers

OPERATORS

Relational operators:

>,<,<=,>=,==,!=

Logical operators:

&&,||,!

Bitwise operators:

&, |, ^ (XOR), <<,>>,~

Precedence:

1. Casting

2. Parentheses

3. Negative

4. Multiplication and division

5. Addition and subtraction

Page 61: Introduction to Embedded Systems and Microcontrollers

CONDITIONAL STATEMENTSIf statement:

if (expression)

{

statement(s);

}

If-else statement:

if (expression1)

{

statement(s)

}

else if(expression2)

{

statement(s)

}

else

{

statement(s)

}

Page 62: Introduction to Embedded Systems and Microcontrollers

SWITCH STATEMENT switch (variable)

{

case constant1:

statement(s);

break;

case constant2:

statement(s);

break;

default:

statement(s);

}

Page 63: Introduction to Embedded Systems and Microcontrollers

FOR LOOP for( initialization ; conditional_test ; increment )

Ex. void main(void)

{

int i;

for(i=0; i<10; i++)

printf(“%d “,i);

printf(“done”);

}

Page 64: Introduction to Embedded Systems and Microcontrollers

WHILE & DO-WHILE LOOP

While:

while (expression)

{

statement;

}

do-while Loop do

{

statements

}

while(expression)

Page 65: Introduction to Embedded Systems and Microcontrollers

ARRAYS

type array_name[size] = {value list};

Ex. int i[5] = {1,2,3,4,5};

Multidimensional arrays: int num[3][3]={ 1,2,3,

4,5,6,

7,8,9};

Page 66: Introduction to Embedded Systems and Microcontrollers

FUNCTIONS main() is the first function called when the program is executed. The

other functions, function1() and function2(), can be called by any function in the program.

Example.main()

{

int x=0;

X=Function1(5,6);

}

int function1(int a,int b )

{

Return (a+b);

}

Page 67: Introduction to Embedded Systems and Microcontrollers

MIX C AND ASSEMBLY

We can mix c and assembly by using the command

#asm

statements

#End asm