Upload
engislam90
View
5.197
Download
7
Tags:
Embed Size (px)
Citation preview
INTRODUCTION TO MICROCONTROLLERS AND EMBEDDED SYSTEMS
Prepared by:
Islam Samir
- Islam Samir Mohamed
- Electronics and Communications department, Faculty
of Engineering, Cairo University
- Embedded Software Engineer.
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.
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.
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.
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.
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.
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.
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.
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!!
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.
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.
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.
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.
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.
WHAT IS A MICROCONTROLLER? (CONT.) So.. What’s the difference between a
microcontroller and a microprocessor system?
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.
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.
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.
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.
POPULAR MICROCONTROLLERS
8051 (Intel and others) 80386 EX (Intel) PIC (Microchip) 68HC05 (Motorola) Z8 (Zilog)
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.
DEVELOPING EMBEDDED SYSTEMS
Embedded systems development
Software development
Hardware development
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.
SOFTWARE DEVELOPMENT• Writing the required
algorithm using assembly or a high level language.
• Using a compiler or assembler and a linker.
• Debugging your code.
SOFTWARE DEVELOPMENT
Compiler Assembler
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.
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.
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.
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).
SOFTWARE DEVELOPMENT
An Integrated Development Environment (IDE) puts all of the previously discussed software components under one common unified user interface.
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.
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.
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.
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.
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.
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.
PIC MICROCONTROLLERS
The one we’ll use is: PIC 16f877A
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.
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.
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.
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.
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
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).
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).
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)
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.
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.
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]
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.
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.
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.
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.
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.
C PROGRAMMING LANGUAGEGeneral form for any C program:
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.
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
OPERATORS
Relational operators:
>,<,<=,>=,==,!=
Logical operators:
&&,||,!
Bitwise operators:
&, |, ^ (XOR), <<,>>,~
Precedence:
1. Casting
2. Parentheses
3. Negative
4. Multiplication and division
5. Addition and subtraction
CONDITIONAL STATEMENTSIf statement:
if (expression)
{
statement(s);
}
If-else statement:
if (expression1)
{
statement(s)
}
else if(expression2)
{
statement(s)
}
else
{
statement(s)
}
SWITCH STATEMENT switch (variable)
{
case constant1:
statement(s);
break;
case constant2:
statement(s);
break;
default:
statement(s);
}
FOR LOOP for( initialization ; conditional_test ; increment )
Ex. void main(void)
{
int i;
for(i=0; i<10; i++)
printf(“%d “,i);
printf(“done”);
}
WHILE & DO-WHILE LOOP
While:
while (expression)
{
statement;
}
do-while Loop do
{
statements
}
while(expression)
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};
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);
}
MIX C AND ASSEMBLY
We can mix c and assembly by using the command
#asm
statements
#End asm