You are on page 1of 72

 Specially designed microprocessors

 A microcontroller is a complete computer system on a single chip


 Highly integrated chip
includes all or most parts needed for controller
 A typical microcontroller has:
 bit manipulation
 easy and direct access to I/O
 quick and efficient interrupt processing
 Microcontroller drastically reduces design cost
 What we can do with microcontroller
 Any control problem. Just let it go your imagination.

1/175
 Many people are already familiar with
microprocessors as microprocessors are the
heart of Personal Computers (PCs). Most
people, even some in the technical
community, are not familiar with
microcontrollers although microcontrollers are
encountered in modern life much more than
microprocessors are. Microcontrollers are what
make modern appliances intelligent and
programmable. It is often found in washing
machines, video games, telephones,
automobiles, copiers, elevators, CD ROM
drives, hard disk drives, keyboards, remote
control units, computer screens, and many
other equipments and consumer electronics

2/175
 Appliances
(microwave oven, refrigerators, television and VCRs, stereos)
 Computers and computer equipment
(laser printers, modems, disk drives)
 Automobiles
(engine control, diagnostics, climate control),
 Environmental control
(greenhouse, factory, home)
 Instrumentation
 Aerospace
 Robotics, etc...

3/175
 Computer engineering, computer science, electrical engineering
(power and communications), applied electronics, and applied
physics students and professionals all need to well know
microprocessors and microcontrollers. Mechanical engineers also
can make better jobs with good knowledge of microprocessors
and microcontrollers.

4/175
 8048 (Intel)
 8051 (Intel and others)
 80c196 (MCS-96)
 80186,80188 (Intel)
 80386 EX (Intel)
 65C02/W65C816S/W65C134S (Western Design Center)
 MC14500 (Motorola)

5/175
 68HC05 (Motorola)
 68HC11 (Motorola and Toshiba)
 683xx (Motorola)
 PIC (MicroChip)
 COP400 Family (National Semiconductor)
 COP800 Family (National Semiconductor)
 HPC Family (National Semiconductor)
 Project Piranha (National Semiconductor)

6/175
 Z8 (Zilog)
 HD64180 (Hitachi)
 TMS370 (Texas Instruments)
 1802 (RCA)
 MuP21 (Forth chip)
 F21 (Next generation Forth chip)

7/175
 Machine/Assembly language
 Ansi C
 C Basic/ Turbo C
 Embedded java

8/175
Part 1

 Simulators
 Resident Debuggers
 Emulators
 Java on Embedded Systems

9/175
 The main difference between them is that a
microprocessor needs several accompanying chips to
operate. The microcontroller has most of these chips
integrated on the same chip.
 The types of this chips
 ADC
 DAC
 Serial communication technique
 External memory

10/175
 The good knowledge of digital electronics will help you much and
will speed up your learning curve. However, the basic concepts will
be reviewed during the course and a basic understanding will do. If
you are familiar with analog electronics, this will help you do
better analog interfacing projects but it is not at all necessary for
this course.

11/175
12/175
13/175
14/175
15/175
High-level language Low-level language

Ease of learning Easy Difficult

Ease of programming Easy Difficult

Development time shorter longer

Code efficiency Less efficient high efficient

Tool price Very expensive Free

16/175
17/175
 C is a computer programming language.
 C is what is called a compiled language. This means that
once you write your C program, you must run it through a
C compiler to turn your program into an executable that
the computer can run (execute). The C program is the
human readable form, while the executable that comes
out of the compiler is the machine-readable and
executable form. What this means is that to write and run
a C program, you must have access to a C compiler. You
can use Turbo C or Turbo C++ Compilers for this purpose

18/175
 Small size
 · Extensive use of function calls
 · Loose typing -- unlike PASCAL
 · Structured language
 · Low level (Bitwise) programming readily available
 · Pointer implementation - extensive use of pointers for memory, array,
 structures and functions.
C has now become a widely used professional language for various
reasons.
 be used to write any complex program.
 · It can handle low-level activities.
 · It produces efficient programs.
 · It is fast. (much faster than BASIC).
 · It can be compiled on a variety of computers

19/175
Documentation Section
Link Section
Definition Section
Global Declaration Section

Main() Function Section{


Declaration Part
Executable Part }

Subprogram Section
Function 1
Function 2
…..
……..
Function n

20/175
 1. Documentation Section :
 This section consists of a set of comments lines giving the name of the program,
 the author and other details which the programmer would like to use later.
 2. Link Section:
 This section provides instruction to the complier to link functions from the system
 library.
 3. Definition section:
 This section defines all symbolic constants.
 4. Global Section:
 Here those variables are declared which are used in more than one function.
 5. main() Function Section:
 Every program must have one main () function section It contains declaration
 part and executable part.
 6. Subprogram Section:
 It contains all the user-defined functions that are called in the main function.

21/175
#include <stdio.h> /* preprocessor directive */
#define PI 3.142 / * include standard C header file */
float area; /* global declaration */
int square (int r); /* prototype declaration */
main()
{ / * beginning of main function */
int radius_squared; /* local declaration */
int radius = 3; /* declaration and initialization */
radius_squared = square (radius);
/* pass a value to a function */
area = PI * radius_squared;
/* assignment statement */
printf(“Area is %6.4f square units\n”,area);
} /* end of main function & program */
square(int r) /* function head */
{
int r_squared; / * declarations here are known */
/* only to square */
r_squared = r * r;
return(r_squared); /* return value to calling statement
*/
}

22/175
 #define directive, which substitutes text for the specified identifier
 #include directive, which includes the text of an external file into a
program. the header file is denoted by a .h extension
 #include <16c71.h> //# include <ctype.h> => This information
is used by the compiler to link all the hardware specifics and source
programs together.
 #pragma, The pragma command instructs the compiler to perform a
particular action at the compile time such as specifying the
PICmicro®MUC being used or the file format generated.
Like : #pragma device PIC16C54

23/175
 Variables: A variable is a name for a specific memory location. This memory
location can hold various values depending on how the variable was declared.
In C, all variables must be declared before they are used. A variable
declaration tells the compiler what type of variable is being used. All variable
declarations are statements in C and therefore must be terminated with a
semicolon.
Ex: char, int, float, and long.
The format
type variable_name;

24/175
 Constants: A constants is a fixed value which cannot be changed by the
program . For example, 25 is a constant. Integer constants are specified
without any fractional components, such as –100 or 40. Floating point constants
require the decimal point followed by the number‟s fractional component. The
number 45.65 is a floating point constant. Character constants are enclosed
between single quotes such as „A‟ or„&‟.
 #define <label> value
 The <label> defines the name you will use throughout your program, value is
the value you are assigning to <label>.
 EX: #define TRUE 1
#define pi 3.14159265359
 #define data is not stored in memory, it is resolved at compile time.
 To save constants in the chip ROM, use the const keyword in a variable
 declaration. For example:
 char const id[5]={“1234”};

25/175
main()
{
function1(int a,int b )
{
Return()
}
function2()
{
}
}
 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.

26/175
 #define is a powerful directive as illustrated in the previous section. C
allows defines to have parameters making them even more powerful.
When parameters are used it is called a macro. Macros are used to
enhance readability or to save typing. A simple macro:

#define var(x,v) unsigned int x=v;


var(a,1)
var(b,2)
var(c,3)
is the same as:
unsigned int a=1;
unsigned int b=2;
unsigned int c=3;

27/175
 The ANSI C standard defines 32 keywords for use in the C language.
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsignd
continue for signed void

28/175
 Type Bit Width Range
 short 1 0 or 1
 short int 1 0 or 1
 int 8 0 to 255
 Char 8 0 to 255
 unsigned 8 0 to 255
 unsigned int 8 0 to 255
 Signed 8 -128 to 127
 signed int 8 -128 to 127
 long 16 0 to 65536
 long int 16 0 to 65536
 signed long 16 -32768 to 32767
 float 32 3.4E-38 to 3.4E+38

29/175
+ 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

30/175
> greater than
 >= greater than or equal to
 < less than
 <= less than or equal to
 == equal to
 != not equal to

31/175
 AND OR NOT
p q p&&q p||q !p
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0

32/175
& bitwise AND
 | bitwise OR
 ^ bitwise XOR
 ~ 1‟s complement
 >> right shift
 << left shift#
 EX: The general format for using the shift operators is:
 variable << expression
 variable >> expression

33/175
if (expression)
{
.
statement;

34/175
if (expression1)
{
.
statement(s)
.
}
else if(expression2)
{
.
statement(s)
.
}
else
{
.
statement(s)
.
}

35/175
 for( initialization ; conditional_test ;
increment )
 void main(void)
{
int i;
for(i=0; i<10; i++)
printf(“%d “,i);
printf(“done”);
}

36/175
 while (expression)
{
statement;
}

37/175
 do
{
statements
}
while(expression)

38/175
 void main(void)
{
int i;
for(i=0;i<50;i++)
{
printf(“%d “,i);
if(i==15)
break;
}
}

39/175
 switch (variable)
{
case constant1:
statement(s);
break;
case constant2:
statement(s);
break;
case constantN:
statement(s);
break;
default:
statement(s);
}

40/175
 for (i=0;i<10;i++)
;
 Nothing happens when the null statement is executed – unlike the NOP in
assembler, which introduces a one cycle delay. Statements such as do,
for, if and while require that an executable statement appears as the
statement body. The null statement satisfies the syntax in those cases.

41/175
 type var_name [size];

 intheight[50];
 height[24] = 60;

42/175
 type array_name[size] = {value_list};

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

43/175
 int number[5][5];

 int num[3][3]={ 1,2,3,


4,5,6,
7,8,9};

44/175
 We can mix c and assembly by using the
command
 #asm

statements

 #End asm

45/175
 The Input and Output ports on a PICmicro®MCU
are made up from two registers – PORT and PORT
DIRECTION – and are designated PORTA,B,C,D,E
and TRISA,B,C,D,E.
 Ex:

unwanted bits

46/175
 Technical support
 Development tools
 Documentation
 Purchasing more devices at one manufacturer
(A/D, memory, etc.)
 Additional features
(EEPROM, FLASH, LCD driver, etc.)

47/175
 Basic parts are: external
inerrupts
 Central Processing Unit
 RAM
interrupt ROM timer 1 counter
 EPROM/PROM/ROM or FLASH control
RAM
timer 0 inputs
Memory
 I/O serial or/and parallel
 timers CPU

 interrupt controller
 Optional parts are: OSC
bus 4 I/O serial
control ports port
 Watch Dog Timer
 AD Converter
TxD RxD
 LCD driver P0 P2 P1 P3
 etc. address/
data

48/175
 MICRO C PRO
 With the program

49/175
50/175
51/175
 RISC architecture
Only 35 instructions to learn
All single-cycle instructions except branches
 Operating frequency 0-20 MHz
Precision internal oscillator Factory calibrated
Software selectable frequency range of 8MHz to 31KHz
 Power supply voltage 2.0-5.5V
Consumption: 220uA (2.0V, 4MHz), 11uA (2.0 V, 32 KHz) 50nA (stand-by mode)
 Power-Saving Sleep Mode
 Brown-out Reset (BOR) with software control option
 35 input/output pins
High current source/sink for direct LED drive
software and individually programmable pull-up resistor
Interrupt-on-Change pin
52/175
 8K ROM memory in FLASH technology
Chip can be reprogrammed up to 100.000 times
In-Circuit Serial Programming Option
Chip can be programmed even embedded in the target device
 256 bytes EEPROM memory
Data can be written more than 1.000.000 times
 368 bytes RAM memory
 A/D converter:
14-channels
10-bit resolution
 3 independent timers/counters
 Watch-dog timer
 Analog comparator module with
Two analog comparators
Fixed voltage reference (0.6V)
Programmable on-chip voltage reference
 PWM output steering control
 Enhanced USART module
Supports RS-485, RS-232 and LIN2.0
Auto-Baud Detect
 Master Synchronous Serial Port (MSSP)
supports SPI and I2C mode

53/175
1-Central Processor Unit (CPU):
 CPU is made in RISC technology because this fact can affect you to buy
exactly this microcontroller.
 RISC stands for Reduced Instruction Set Computer, which gives the
PIC16F887 two advantages:
- Its CPU can recognize and execute only 35 simple instructions
- Execution time is the same for all of them and lasts 4 clock
cycles
-The only exceptions are jump and branch instructions whose
execution time is 8 cycles

54/175
This microcontroller has three types of memory- ROM, RAM and EEPROM
1-ROM :
 ROM memory is used to permanently save program being executed. That is
why it is often called “program memory”. The PIC16F887 has 8Kb ROM (in
total of 8192 locations). Since, in this very case, ROM is made in FLASH
technology, its contents can be changed by providing special programming
voltage (13V).
2-EEPROM:
 Similar to program memory, the contents of EEPROM is permanently saved, even
upon the power goes off. ,but unlike ROM, the contents of EEPROM can be changed
during operation of the microcontroller. That is why this memory (256 locations) is a
perfect one for permanently saving results created and used during the operation.

55/175
RAM Memory :
 The most complex part of microcontroller memory. In this very
case, it consists of two parts: general-purpose registers and
special-function registers (SFR).
 SFR: special-function registers :
 there is two types: 1-Core (CPU) registers - control and monitor
operation and processes in the central processor. Even though there are only a few
of them, the operation of the whole microcontroller depends on their contents.
 2-Peripheral SFRs- control the operation of peripheral units (serial communication
module, A/D converter etc.).

 Note: unlike general-purpose registers, SFR purpose is predetermined


during manufacturing process and cannot be changed.

56/175
 RAM Memory Banks :The data memory is partitioned into four banks.
Prior to access some register during program writing (in order to read or change
its contents), it is necessary to select bank which contains that register. Two
bits of the STATUS register are used for bank selecting.

57/175
58/175
 Stack :
 A part of RAM used for stack consists of eight 13-bit registers.
Before the microcontroller starts to execute a subroutine (CALL
instruction) or when an interrupt occurs, the address of first next
instruction being currently executed is pushed onto the stack,
the microcontroller knows from where to continue regular
program execution. This address is cleared upon return to the
main program because there is no need to save it any longer, and
one location of the stack is automatically available for further
use.

59/175
 The first thing that the microcontroller does upon an interrupt
request arrives is to execute the current instruction and then
stop regular program execution. Immediately after that, the
current program memory address is automatically pushed onto
the stack and default address is written to the program counter.
 The difference between interrupts and polling
 Interrupts: external ,physical interrupts,
 Polling :software interrupts.
 When an interrupt request arrives it does not mean that interrupt
will automatically occur, because it must be also enabled by the
user by using .

60/175
61/175
 One of the most important feature of the microcontroller is a
number of input/output pins used for with peripherals.

 TRIS register: TRISA, TRISB, TRISC etc.which determines


performance, but not the contents of the port bits.

 By clearing some bit of the TRIS register (bit=0), the


corresponding port pin is configured as output. Similarly, by
setting some bit of the TRIS register (bit=1), the corresponding
port pin is configured as input. This rule is easy to remember 0 =
Output, 1 = Input.

62/175
 There are three completely independent timers/counters marked as
TMR0, TMR1 and TMR2.
 The timer TMR0 module is an 8-bit timer/counter with the
following features:
 · 8-bit timer/counter register
 · 8-bit prescaler (shared with Watchdog timer)
 · Programmable internal or external clock source
 · Interrupt on overflow
 · Programmable external clock edge selection
 In order to make an initial value for the timer
TMR0 Initial = 256 - (Delay Time * Clock Frequency / 8)

63/175
 1. Write to ADCON1 indicating what are the digital I/O pins and
which are the analog I/O pins. At this time, if a 10-bit conversion
is going to be done, set the format flag in ADCON 1 appropriately.
 2. Write to ADCON0, setting ADON, resetting ADIF and GO/_DONE
and specifying the ADC TAD clock and the pin to be used.
 3. Wait for the input signal to stabilize.
 4. Set the GO/_DONE bit. If this is a high-accuracy measurement,
ADIE should be enabled for interrupts and then the PICmicro®
MCU put to “sleep”.
 5. Poll “GO/_DONE” until it is reset (conversion done). 6. Read
the result form “ADRES” and optionally “ADRESH”.

64/175
 EUSART:
 Enhanced Universal Synchronous Asynchronous Receiver
Transmitter (EUSART) module is a serial I/O communication
peripheral. It is also known as Serial Communications Interface
(SCI). It contains all clock generators, shift registers and data
buffers necessary to perform an input or output serial data
transfer independently of device program execution.
 the PIC16F887 microcontroller has the following
features:
 · Full-duplex asynchronous transmit and receive
 · Programmable 8- or 9-bit character length
 · Address detection in 9-bit mode
 · Input buffer overrun error detection
 · Half-duplex communication in synchronous mode (master or slave)

65/175
 Each data is transferred in the following way:
 · In idle state, data line has high logic level (1).
 · Each data transmission starts with START bit which is always a
zero (0).
 · Each data is 8- or 9-bit wide (LSB bit is first transferred)
 · Each data transmission ends with STOP bit which always has
logic level which is always one (1).

66/175
 In order to enable data transmission via EUSART module, it is necessary to configure it to operate
 as a transmitter. I other words, it is necessary to define the state of the following bits:
 TXEN = 1 - EUSART transmitter is enabled by setting this bit of the TXSTA register.
 SYNC = 0 - EUSART is configured to operate in asynchronous mode by clearing this bit of the TXSTA
register.
 SPEN = 1 - By setting this bit of the RCSTA register, EUSART is enabled and the TX/CK pin is
automatically configured as output. If this bit is simultaneously used for some analog function, it
must be disabled by clearing the corresponding bit of the ANSEL register.

 Byte will be immediately transferred to the shift register TSR. TXREG register
remains empty, which is indicated by setting flag bit TXIF of the PIR1 register. If the
TXIE bit of the PIE1 register is set, an interrupt will be generated. Besides, the flag
is set regardless of whether an interrupt is enabled or not. Also, it cannot be
cleared by software, but by writing new data to the TXREG register.

67/175
 Similar to the activation of EUSART transmitter, in order to enable receiver it is necessary to
 define the following bits:

 CREN = 1 - EUSART receiver is enabled by setting this bit of the RCSTA register.

 SYNC = 0 - EUSART is configured to operate in asynchronous mode by clearing this bit stored in the
TXSTA register.

 SPEN = 1 - By setting this bit of the RCSTA register, EUSART is enabled and the RX/DT pin is
automatically configured as input. If this bit is simultaneously used for some analog function, it must be
disabled by clearing the corresponding bit of the ANSEL register .

 · Data is automatically transferred to the RCREG register (if empty). ·


 The flag bit RCIF is set and an interrupt, if enabled by the RCIE bit of the PIE1 register,
occurs. Similar to transmitter, the flag bit is cleared by software only, i.e. by reading the RCREG register.
Have in mind that this is a two character FIFO memory (first-in, first-out) which allows reception of two
characters simultaneously.
 If the RCREG register is occupied (contains two bytes) and the shift register detects STOP bit, the
overflow bit OERR will be set. In this case, a new coming data is lost, and the OEER bit must be cleared by
software. It is done by clearing and resetting the CREN bit.

68/175
 MSSP module (Master Synchronous Serial Port) is a very useful,
but at the same time one of the most complex circuit within the
microcontroller. It enables high speed communication between a
microcontroller and other peripherals or microcontroller devices
by using few input/output lines (maximum two or three).
Therefore, it is commonly used to connect the microcontroller to
LCD displays, A/D converters, serial EEPROMs, shift registers etc.
The main feature of this communication is that it is synchronous
and suitable for use in systems with a single master and one or
more slaves. A master device contains the necessary circuitry for
baud rate generation and supplies the clock for all devices in the
system. Slave devices may in that way eliminate the internal
clock generation circuitry

69/175
 The MSSP module can operate in one of two modes:
SPI mode (Serial Peripheral Interface)
I2C mode (Inter-Integrated Circuit)

70/175
 SPI Mode
 The SPI mode allows 8 bits of data to be transmitted and received simultaneously
using 3 input/output lines:
 · SDO - Serial Data Out - transmit line.
 · SDI - Serial Data In - receive line.
 · SCK - Serial Clock - synchronization line.
 In addition to these three lines, in case the microcontroller exchanges data with
several peripheral devices, the forth line (SS) may be also used. SS - Slave Select -
is additional pin used for specific device selection. It is active only in case the
microcontroller is in slave mode, i.e. when the external - master device requires
data exchange.
 When operating in SPI mode, MSSP module uses in total of 4 registers:
 · SSPSTAT - status register
 · SSPCON - control register
 · SSPBUF - buffer register
 · SSPSR - shift register (not directly available)

71/175
 Step 1. Data to transmit should be written to the buffer register SSPBUF .Immediately after that, if the
SPI module operates in master mode the microcontroller will automatically perform the following step 2, 3
and 4. If the SPI module operates as Slave, the microcontroller will not perform these steps until the SCK
pin detects clock signal.

 Step 2. This data is now moved to the SSPSR register and the SSPBUF register is not cleared
 Step 3. Synchronized with clock signal, this data is shifted to the output pin (MSB bit first) while the
register is simultaneously being filled with bits through input pin. In Master mode, the microcontroller
itself generates clock, while the Slave mode uses external clock (pin SCK).

 Step 4. The SSPSR register is full once the 8 bits of data have been received. It is indicated by setting
the BF and SSPIF bits. The received data (that byte) is automatically moved from the SSPSR register to the
SSPBUF register. Since data transfer via serial communication is performed automatically, the rest of the
program is normally executed while data transfer is in progress. In that case, the function of the SSPIF bit
is to generate interrupt when one byte transmission is completed.

 Step 5.At last, the data stored in the SSPBUF register is ready for use and moved to any register
available

72/175

You might also like