0% found this document useful (0 votes)
186 views43 pages

Introduction To PIC16, Basic Concepts

pic16 programming and interfacing , for a complete newbie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views43 pages

Introduction To PIC16, Basic Concepts

pic16 programming and interfacing , for a complete newbie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Introduction to PIC16

Microcontroller Programming &


Interfacing

November 25-27, 2011


Engr. Franz Duran, MEP-ECE

OVERVIEW
DAY 1 (Morning)
Introduction to PIC Microcontroller
PICTrainer3 microcontroller
laboratory/training module
MPLAB IDE
Basic C Programming

25-Nov-2011 Franz Duran 2

OVERVIEW
DAY 1 (Afternoon)
Basic C Programming (cont.)
Intermediate C Programming
 C Functions & Structured Programming
 Modular Programming
Interfacing with 2x16 character LCD
Interfacing with 4x3 keypad

25-Nov-2011 Franz Duran 3

1
OVERVIEW
DAY 2 (Morning)
Basic of Interrupts
 Interrupt sources
 Interrupt service routine
RB0/INT interrupt
PORTB Interrupt on Change

25-Nov-2011 Franz Duran 4

OVERVIEW
DAY 2 (Afternoon)
PIC16 Timer module
 TMR0 architecture
 TMR0 as an interrupt source
Interfacing with 7-segment displays

25-Nov-2011 Franz Duran 5

OVERVIEW
DAY 3 (Morning)
Basic of Analog-to-Digital Conversion
Using the PIC16 A/D module
Interfacing LM35 temperature sensor

25-Nov-2011 Franz Duran 6

2
OVERVIEW
DAY 3 (Afternoon)
Basics of Serial Communication
PIC16 UART module
Basic string processing

25-Nov-2011 Franz Duran 7

MICROCONTROLLER
MCU, C
A single-chip computer
Invented in the 1970s
Used as embedded controller

25-Nov-2011 Franz Duran 8

MICROCONTROLLER
used as dedicated controllers
domestic appliances
consumer electronics
industrial equipments
automotive electronics
Naval/avionics/aerospace

25-Nov-2011 Franz Duran 9

3
MICROCONTROLLER
Why use?
Cheap
Flexible
Small outline & high integration
Low-power

25-Nov-2011 Franz Duran 10

PIC MICROCONTROLLER

PIC, PICMICRO
by MICROCHIP
Arizona, U.S.A.
1989 (offshoot of General Instrument)
http://www.microchip.com

25-Nov-2011 Franz Duran 11

PIC MICROCONTROLLER

Rank (8-bit microcontroller)


1990 20th
1993 8th
1996 - 5th
1997-2001 2nd
2002 Present 1st

25-Nov-2011 Franz Duran 12

4
PIC MICROCONTROLLER

Family
Popular among
PIC10, PIC12 students and hobbyists
PIC16
PIC17 / PIC18
PIC24 / DSPICs (16-bit)
PIC32(32-bit)

25-Nov-2011 Franz Duran 13

PIC16 ARCHITECTURE
General Purpose
Special Function
Registers
Registers
Data RAM
Program
Memory

Input/Output
CPU Ports
Power

Oscillator

Reset, Watchdog Internal Peripherals


Timer, etc

25-Nov-2011 Franz Duran 14

PIC16 ARCHITECTURE
General Purpose
Special Function
Registers
Registers
Data RAM
Program
Memory

Input/Output
CPU Ports
Power

Oscillator

Reset, Watchdog Internal Peripherals


Timer, etc

25-Nov-2011 Franz Duran 15

5
PIC16 ARCHITECTURE
CPU
Reduced Instruction Set Computer (RISC)
FOSC = 20MHz max. speed
FOSC/4 instruction clock

25-Nov-2011 Franz Duran 16

PIC16 ARCHITECTURE
General Purpose
Special Function
Registers
Registers
Program Data RAM

Memory

Input/Output
CPU Ports
Power

Oscillator

Reset, Watchdog Internal Peripherals


Timer, etc

25-Nov-2011 Franz Duran 17

PIC16 ARCHITECTURE
Program Memory
hard drive where fixed program is stored
flash-based memory
 reprogrammable at least 10,000x

25-Nov-2011 Franz Duran 18

6
PIC16 ARCHITECTURE
General Purpose
Special Function
Registers
Registers
Data RAM
Program
Memory

Input/Output
CPU Ports
Power

Oscillator

Reset, Watchdog Internal Peripherals


Timer, etc

25-Nov-2011 Franz Duran 19

PIC16 ARCHITECTURE
File Registers
General Purpose Registers (GPRs)
 Data RAM
Special Function Registers (SFRs)
 control device operation

25-Nov-2011 Franz Duran 20

PIC16 ARCHITECTURE
General Purpose
Special Function
Registers
Registers
Program Data RAM
Memory

Input/Output
CPU Ports
Power

Oscillator

Reset, Watchdog Internal Peripherals


Timer, etc

25-Nov-2011 Franz Duran 21

7
PIC16 ARCHITECTURE
General Purpose
Special Function
Registers
Data RAM Registers
Program
Memory

Input/Output
CPU Ports
Power

Oscillator

Reset, Watchdog Internal Peripherals


Timer, etc

25-Nov-2011 Franz Duran 22

PIC16 ARCHITECTURE
General Purpose
Special Function
Registers
Registers
Data RAM
Program
Memory

Input/Output
CPU Ports
Power

Oscillator

Reset, Watchdog Internal Peripherals


Timer, etc

25-Nov-2011 Franz Duran 23

PIC16 ARCHITECTURE
General Purpose
Special Function
Registers
Registers
Data RAM
Program
Memory

Input/Output
CPU Ports
Power

Oscillator

Reset, Watchdog Internal Peripherals


Timer, etc

25-Nov-2011 Franz Duran 24

8
PIC16 ARCHITECTURE
General Purpose
Special Function
Registers
Registers
Data RAM
Program
Memory

Input/Output
CPU Ports
Power
Oscillator

Reset, Watchdog Internal Peripherals


Timer, etc

25-Nov-2011 Franz Duran 25

PIC16 ARCHITECTURE
General Purpose
Special Function
Registers
Registers
Data RAM
Program
Memory

Input/Output
CPU Ports
Power

Oscillator

Reset, Watchdog Internal Peripherals


Timer, etc

25-Nov-2011 Franz Duran 26

PIC16 ARCHITECTURE
General Purpose
Special Function
Registers
Registers
Data RAM
Program
Memory

Input/Output
CPU Ports
Power

Oscillator

Reset, Watchdog Internal Peripherals


Timer, etc

25-Nov-2011 Franz Duran 27

9
PIC16 ARCHITECTURE
Input/Output pins
Internal Peripherals/Modules
Timers
A/D converter module
UART
SPI / I2C
Comparator

25-Nov-2011 Franz Duran 28

PIC16 ARCHITECTURE
Features:
Watchdog Timer
SLEEP mode
Power On Reset, Brown-out Reset
CPU
 RISC (reduced instruction set computer)
 FOSC = 20Mhz typical

25-Nov-2011 Franz Duran 29

PIC16 ARCHITECTURE
Instruction set
35 instructions (PIC16)
 Easy to memorize all instructions
75 instruction (PIC18)

25-Nov-2011 Franz Duran 30

10
PIC16F84A
8-bit microcontrolller
PIC16 family
F flash memory, i.e. reprogrammable
84 variant/model
A - revision
4Mhz (1MIPS), DIP18, +5V

25-Nov-2011 Franz Duran 31

PIC16F84A
Program Memory
1024 instruction words
1 word = 14 bit
File Registers (2 banks)
GPRs - 68 bytes RAM
SFRs 16 registers
Data EEPROM
64 bytes
25-Nov-2011 Franz Duran 32

PIC16F84A
13 I/O pins
 PORTB 8 pins
 PORTA 5 pins
2 power pins
 VDD, VSS
2 oscillator pins
 OSC1, OSC2
1 RESET pin
 MCLR

25-Nov-2011 Franz Duran 33

11
PIC16F877A
20Mhz (5MIPS), DIP40, +5V
8192 instruction word
368 bytes Data RAM / GPRs
56 SFRs
256 bytes Data EEPROM

25-Nov-2011 Franz Duran 34

PIC16F877A
33 I/O pins
PORTA 6 pins
PORTB 8 pins
PORTC 8 pins
PORTD 8 pins
PORTE 3 pins
4 power pins
1 Reset, 2 Clock pins
25-Nov-2011 Franz Duran 35

PIC16F877A TRAINER BOARD


Reset Button +5V supply

ICSP connector
SIL connectors

20Mhz Oscillator
PIC16F877A
25-Nov-2011 Franz Duran 36

12
PIC16F877A TRAINER BOARD
10 LEDs

3 potentiometers
7-seg. display

keypad
4x3 or 4x4
4 pushbuttons
DS1307
real-time IC

serial EEPROM IC

serial comm. ckt.


LM35 temp. sensor
character LCD
25-Nov-2011 Franz Duran 37

PIC16F877A & eICD2

eICD2

Connect wires to build application circuit ICSP connector


25-Nov-2011 Franz Duran 38

MPLAB IDE 8.xx


integrated development environment (IDE)
for PIC
 freely downloadable (~90MB)
 assembler (MPASM)
 HI-TECH C Compiler
 45-day full version (full optimization)
 Lite mode (no optimization)
 direct support for ICD2/PICKIT2
programmer/debugger
 eICD2, ePICKIT2

25-Nov-2011 Franz Duran 39

13
MPLAB IDE 8.xx
Download and Install MPLAB
http://www.microchip.com
Install HI-TECH Compiler
included in the MPLAB installer
or download separately from:
 http://www.htsoft.com
 HI-TECH Sofware
Brisbane, Australia
bought by Microchip (March 2009)
25-Nov-2011 Franz Duran 40

MPLAB IDE 8.53


Open MPLAB an example application

25-Nov-2011 Franz Duran 41

MPLAB IDE 8.53


To create new project:
 Project > Project Wizard

25-Nov-2011 Franz Duran 42

14
MPLAB IDE 8.53
Step 1: Select Device

25-Nov-2011 Franz Duran 43

MPLAB IDE 8.53


Step 2: Select Language

25-Nov-2011 Franz Duran 44

MPLAB IDE 8.53


Step 3: Select Project Name &
Directory

25-Nov-2011 Franz Duran 45

15
MPLAB IDE 8.53
Step 4: Add files

25-Nov-2011 Franz Duran 46

MPLAB IDE 8.53


Project Summary

25-Nov-2011 Franz Duran 47

MPLAB IDE 8.53


Empty Project
Project Window

Output Window

25-Nov-2011 Franz Duran 48

16
MPLAB IDE 8.53
Editor Window
Create source code

25-Nov-2011 Franz Duran 49

MPLAB IDE 8.53


#include <pic.h>

void main()
{
TRISB0 = 0;
RB0 = 1;

while(1)
{

}
}
25-Nov-2011 Franz Duran 50

MPLAB IDE 8.53


save as main.c in project
directory

25-Nov-2011 Franz Duran 51

17
MPLAB IDE 8.53

main.c

25-Nov-2011 Franz Duran 52

MPLAB IDE 8.53

Add
main.c
Build project
(F10)

25-Nov-2011 Franz Duran 53

MPLAB IDE 8.53

25-Nov-2011 Franz Duran 54

18
WHY C? (& not ASM?)
1. Easy to Use
 Easy to read
C uses human readable syntax
Assembly uses mnemonics (cryptic!)
 Shorter code
saves time & effort
easy math statements

2. Portable
 code can run in other target device
no or few modifications, saves time and effort
25-Nov-2011 Franz Duran 55

WHY C? (& not ASM?)


3. Easy to manage large, complex
programs
 code reuse of C modules (.h & .c)
 Easy to implement state machines
 Can use RTOS, not possible in ASM
4. Better performance
 C can be as fast as ASM
 well structured program
 C codes can include ASM codes
25-Nov-2011 Franz Duran 56

WHY C? (& not ASM?)


5. C is a universal language (almost!)
 Learning C will benefit the user down the
road
 Can be used in other 8-bit/16-bit/32-bit MCU
 implement USB, Ethernet & TCP-IP applications
 DSP
 Learn desktop programming
 foundation for C++, Java, C#, etc..

25-Nov-2011 Franz Duran 57

19
BASIC PIC16F84A CKT.
PIC16F84A CIRCUIT
1. PIC16F84A
2. +5v supply
3. Oscillator circuit
4. Reset circuit
5. External peripherals
6. In-circuit serial
programming
connector
25-Nov-2011 Franz Duran 58

BASIC PIC16F84A CKT.


PIC16F84A w/ +5V supply circuit

25-Nov-2011 Franz Duran 59

BASIC PIC16F84A CKT.


PIC16F84A w/ +5V supply circuit & crystal
oscillator and loading capacitors

25-Nov-2011 Franz Duran 60

20
BASIC PIC16F84A CKT.
oscillator circuit
generate a pulse train signal; used to
synchronize MCU internal operations

25-Nov-2011 Franz Duran 61

BASIC PIC16F84A CKT.


Reset circuit

25-Nov-2011 Franz Duran 62

BASIC PIC16F84A CKT.


Interfacing external peripherals

25-Nov-2011 Franz Duran 63

21
BASIC PIC16F84A CKT.

25-Nov-2011 Franz Duran 64

PIC16F877A Trainer Board

25-Nov-2011 Franz Duran 65

IO INTERFACING: LED
LED at RB0
RB0 is output

25-Nov-2011 Franz Duran 66

22
LED
5mm LED
IF = 5 35 mA
VF = 2V
3mm LED
IF = 1 30 mA
VF = 2V

25-Nov-2011 Franz Duran 67

LED
IF

+ VR=3V -
+
VF =
2v
If IF = 10mA, -
R = 3V/10mA
R = 300

25-Nov-2011 Franz Duran 68

LED
R should not be too large
LED will not turn on
R should not be too small
IF < 30mA
PIC Output pin source
current < 25mA

25-Nov-2011 Franz Duran 69

23
BASIC I/O PROGRAM
Example I/O Program

#include <pic.h> preprocessor directive

void main() main() function


{
TRISB0 = 0; initialization
RB0 = 1;

while(1)
{ program loop
- infinite loop
} - super loop
}

25-Nov-2011 Franz Duran 70

IO PROGRAMMING
Input/Output port - group of 8 pins typical
PORTA 6 I/O pins
 RA5, RA4, RA3, RA2, RA1, RA0
 PORTA<5:0>
PORTB 8 I/O pins
 RB7, RB6, RB5, RB4, RB3, RB2, RB1, RB0
 PORTB<7:0>

25-Nov-2011 Franz Duran 71

IO PROGRAMMING
PORTC 8 I/O pins
 RC7, RC6, RC5, RC4, RC3, RC2, RC1, RC0
 PORTC<7:0>
PORTD 8 I/O pins
 RD7, RD6, RD5, RD4, RD3, RD2, RD1, RD0
 PORTD<7:0>
PORTE 3 I/O pins
 RE2, RE1, RE0
 PORTE<2:0>
25-Nov-2011 Franz Duran 72

24
IO PROGRAMMING

Special Function Registers for I/O


TRISA, PORTA
TRISB, PORTB
TRISC, PORTC
TRISD, PORTD
TRISE, PORTE

25-Nov-2011 Franz Duran 73

IO PROGRAMMING

Consider PORTB..

25-Nov-2011 Franz Duran 74

IO PROGRAMMING: SFRs
PORTB port
 controlled by 2 special function registers
1. TRISB register
 PORTB Data Direction Register
 8-bit
2. PORTB register
 PORTB Data Latch Register
 8-bit
25-Nov-2011 Franz Duran 75

25
IO PROGRAMMING: SFRs
TRISB = XXXXXXXX 0 output
1 input

......... TRISB0
TRISB1
.
.
.
TRISB7
25-Nov-2011 Franz Duran 76

IO PROGRAMMING: SFRs

TRISB
X X X X X X X X
0
TRISB0

TRISB0 = 0; //RB0 is output

RB0 pin is an
output pin

25-Nov-2011 Franz Duran 77

IO PROGRAMMING: SFRs
PORTB = XXXXXXXX 0 Logic 0
1 Logic 1

......... RB0
RB1
.
.
.
RB7
25-Nov-2011 Franz Duran 78

26
IO PROGRAMMING: SFRs

PORTB
X X X X X X X X
1
RB0

RB0 = 1; //LED on
RB0 outputs a
Logic 1 signal;
~5V
25-Nov-2011 Franz Duran 79

IO PROGRAMMING: SFRs
TRISB0 = 0; //RB0 is output
RB0 = 1; //LED is on

TRISB X X X X X X X 0

PORTB X X X X X X X 1

25-Nov-2011 Franz Duran 80

IO PROGRAMMING
EXERCISE:
Create new project
 Led_demo_2
Turn on LEDs connected to the ff. I/O pins:
 RB0, RA1, RC3, RD7, RE2

25-Nov-2011 Franz Duran 81

27
IO PROGRAMMING
EXERCISE: (Solution)
TRISB0 = 0;
RB0 = 1; //LED1 on
TRISC3 = 0;
RC3 = 0; //LED2 on
TRISD7 = 0;
RD7 = 1; //LED3 on
ADCON1 = 0x06; //All PORTA & PORTE pins are digital I/O
TRISA1 = 0;
RA1 = 1; //LED4 on
TRISE2 = 0;
RE2 = 0; //LED5 on

25-Nov-2011 Franz Duran 82

IO PROGRAMMING
EXERCISE:
Turn on all 8 LEDs connected to PORTD

25-Nov-2011 Franz Duran 83

IO PROGRAMMING
SOLUTION:
TRISD = 0b00000000; //binary notation
PORTD = 0b11111111;
or
TRISD = 0x00; //hexadecimal notation
PORTD = 0xFF;
or
TRISD = 0; //decimal notation
PORTD = 255;

25-Nov-2011 Franz Duran 84

28
IO INTERFACING: Button
pushbutton
input device

Tack Switch

25-Nov-2011 Franz Duran 85

IO INTERFACING: Button
+5v
If button is not pressed
R
10k
RB2

RIN +
1M V = 5v x 1M / (R+1M)
V 5v (Logic 1)
-
25-Nov-2011 Franz Duran 86

IO INTERFACING: Button
If button is pressed

RB2

RIN +
~1M V = 0v (Logic 0)

-
25-Nov-2011 Franz Duran 87

29
IO INTERFACING: Button
void main()
{
TRISB0 = 0; //RB0 is an output pin
RB0 = 0; //LED is off
TRISB2 = 1; //RB2 is an input pin

while(1)
{
if(RB2==0) //If button is pressed,
RB0 = 1; // LED is on,
else //else,
RB0 = 0; // LED is OFF.
}
}

25-Nov-2011 Franz Duran 88

IO INTERFACING: Button
R should be large enough to limit the
current
when button is
I
pressed

25-Nov-2011 Franz Duran 89

IO INTERFACING: Button
R should be large enough to limit the
current
when I/O pin is
configured as I < 25mA

output and at
Logic 0 (I/O pin
is internally
connected
to ground)

25-Nov-2011 Franz Duran 90

30
BASICS OF C: #defines
#include <pic.h>

__CONFIG(HS & WDTDIS & PWRTDIS & UNPROTECT & LVPDIS);

#define LED RB0


#define BUTTON RB2
#define ON 1
#define OFF 0
#define PRESSED 0

void main()
{
TRISB0 = 0; //RB0 is an output pin
LED = OFF; //LED is initially off
TRISB2 = 1; //RB2 is an input pin

while(1)
{
if(BUTTON==PRESSED) //If pushbutton is pressed,
LED = ON; // turn on LED.
else //Else, LED is OFF.
LED = OFF;
}
}
25-Nov-2011 Franz Duran 91

BASICS OF C: Conditional
Statements
1. If()
If()-else()
If()-else-if()
2. switch()

25-Nov-2011 Franz Duran 92

BASICS OF C: IF Conditional
Statement
If()
 Simplest conditional statement
 if (condition)
statement1;
 Ex.
if(var>99)
var=0;

25-Nov-2011 Franz Duran 93

31
BASICS OF C: IF Conditional
Statement
If()
 use else clause (optional)
 if (condition)
statement1;
else
statement2;
 Ex.
if(BUTTON==PRESSED)
LED=1;
else
LED=0;
25-Nov-2011 Franz Duran 94

BASICS OF C: IF Conditional
Statement
If()-else-if()
 if (condition1)
statement1;
else if (condition2)
statement2;
else if (condition3)
statement3;
else
statement4;

25-Nov-2011 Franz Duran 95

BASICS OF C: Switch()
switch()
Allow comparison of a single variable (or
expression) to multiple values
Code associate with the matching value is
executed

25-Nov-2011 Franz Duran 96

32
BASICS OF C: Switch()
var1 = get_input_from_user()

switch(var1)
{
case 0x00:
statement1;
break;
case 0x01:
statement2;
break;
case 0x02:
statement3;
break;
case 0x03:
statement4;
break;
default:
statement5;
break
}
25-Nov-2011 Franz Duran 97

BASICS OF C: Loops

Loops
 used to repeatedly execute specific
statements
3 loop statements in C
1. for() loop
2. while() loop
3. do-while() loop

25-Nov-2011 Franz Duran 98

BASICS OF C: FOR Loop


void main()
{
unsigned int i; //a variable.
TRISB0 = 0; //RB0 pin is configured as an output
RB0 = 0; //LED is initially off

while(1)
{
RB0 = 1; //LED is ON
for(i=0;i<50000;i++) //delay
{
//empty body
}

RB0 = 0; //LED is OFF


for(i=0;i<50000;i++) //delay
{
//empty body
}
}
}
25-Nov-2011 Franz Duran 99

33
BASICS OF C: FOR Loop
start
for(i=0;i<50000;i++)
{
...//code goes here i=0
}

FALSE
i++ i<50000?

TRUE
exit
//codes

25-Nov-2011 Franz Duran 100

BASICS OF C: FOR Loop

i=0;
for( ;i<50000; )
{
//codes here
i++;
}

OR
for(i=50000;i>0;i--)
{
//codes here
}

25-Nov-2011 Franz Duran 101

BASICS OF C: WHILE Loop


while(condition)
start
{
...//code goes here
}
FALSE
condition?

TRUE
exit
//codes

25-Nov-2011 Franz Duran 102

34
BASICS OF C: WHILE Loop

EXERCISE:
Modify previous example to used while()
loop

25-Nov-2011 Franz Duran 103

BASICS OF C: DO-WHILE Loop


do
{ start
...//code goes here
}
while(condition);
//codes

FALSE
condition?

TRUE
exit

25-Nov-2011 Franz Duran 104

BASICS OF C: Loops

Which loop statements to use?


If number of iteration is controlled, use for()
loop
If a simple test of condition is used, use
while() loop
If a simple test of condition is used AND the
code block should be executed at least once,
use do-while() loop

25-Nov-2011 Franz Duran 105

35
BASICS OF C: Variables

Variables
program data that varies during run-time
temporary data
placed in volatile memory
 General Purpose Registers (GPR)
 PIC16F877A
368 Bytes GPR

25-Nov-2011 Franz Duran 106

BASICS OF C: Variables
unsigned char var1; //range of values: 0-255
var1 = 100; //OK
var1 = 500; //not OK!
var1 = -10; //not OK!

signed char var2; //range of values: -128 to 127


var2 = 100; //OK
var2 = -1000; //not OK!
var2 = 150; //not OK!

unsigned int temp = 100; // range of values: 0-65535


temp = 50000; //OK
temp = 100000; //not OK!

25-Nov-2011 Franz Duran 107

BASICS OF C: Variables
bit (1-bit) (0 1)
char (8-bit) (-128 127)
unsigned char (8-bit) (0 255)
short (16-bit) (-32768 32767)
unsigned short (16-bit) (0 65535)
int (16-bit) (-32768 32767)
unsigned int (16-bit) (0 65535)
short long (24-bit) (-8388608 8388607)
unsigned short long (24-bit) (0 16777215)
long (32-bit) (21474833648 2147483647)
unsigned long (32-bit) (0 4294967295)
float (24-bit) (1.17549435e-38 - 3.40277e+38)
double (24-bit) (1.17549435e-38 - 3.40277e+38 )
double (32-bit) (1.17549435e-38 - 3.40282347e+38 )

25-Nov-2011 Franz Duran 108

36
BASICS OF C: Operators
1. = (Assignment operator)
2. Mathematical operators
3. Relational operators
4. Logical operators
5. Bitwise operators

25-Nov-2011 Franz Duran 109

BASICS OF C: Operators
= (Assignment operator)
x = y; //assign the value of y
//to the variable x

Variable name = expression


Expression - anything that evaluates to
a number
 i.e.
int sum;
sum = a + b; //a + b is an expression
25-Nov-2011 Franz Duran 110

BASICS OF C: Operators
Mathematical operators
1. + (addition) ex. x + y
2. - (subtraction) ex. x - y
3. * (multiplication) ex. x * y
4. / (division) ex. x / y
5. % (modulus) ex. x % y
6. ++ (increment) ex. x++, ++x
7. -- (decrement) ex. x--, --x
25-Nov-2011 Franz Duran 111

37
BASICS OF C: Operators
Relational Operators
1. == (equal to) ex. x==y
2. > (greater than) ex. x>y
3. >= (greater than or equal to) ex. x>=y
4. < (lesser than) ex. x<y
5. <= (lesser than or equal to) ex. x<=y
6. != (not equal to) ex. x!=y

25-Nov-2011 Franz Duran 112

BASICS OF C: Operators
Logical Operators
1. && (Logical AND)
2. || (Logical OR)
3. ! (Logical NOT)

25-Nov-2011 Franz Duran 113

BASICS OF C: Operators
EXERCISE:
Turn on LED1 if BUTTON1 or BUTTON2
is pressed
Modify:
 Turn on LED1 if BUTTON1 and BUTTON2
are pressed

25-Nov-2011 Franz Duran 114

38
BASICS OF C: Operators
Bitwise Operators
1. & (Bitwise AND)
2. | (Bitwise OR)
3. ~ (Bitwise Complement)
4. ^ (Bitwise Exclusive-OR)
5. << (Leftshift)
6. >> (Rightshift)

25-Nov-2011 Franz Duran 115

BASICS OF C: & Operator


AND (&) operator truth table:

A B A& B
0 0 0
0 1 0
1 0 0
1 1 1
25-Nov-2011 Franz Duran 116

BASICS OF C: & Operator


Bitwise-AND operator
Example:
 RB0 = RB0 & 0; //clear RB0
Equivalent to:
 RB0 = 0; //clear RB0

25-Nov-2011 Franz Duran 117

39
BASICS OF C: & Operator
Example:
 Clear PORTB<3:0> and RB6
 Initial solution
RB0 = 0;
RB1 = 0;
RB2 = 0;
RB3 = 0;
RB6 = 0;
 Alternative (better solution):
PORTB = PORTB & 0b10110000;
PORTB &= 0b10110000;
PORTB &= ~0x4F;

25-Nov-2011 Franz Duran 118

BASICS OF C: | Operator
OR (|) operator truth table:

A B A| B
0 0 0
0 1 1
1 0 1
1 1 1
25-Nov-2011 Franz Duran 119

BASICS OF C: | Operator
Bitwise-OR operator
Example:
 RB0 = RB0 | 1; //set RB0
Equivalent to:
 RB0 = 1; //set RB0

25-Nov-2011 Franz Duran 120

40
BASICS OF C: Operators
Example:
Configure PORTD<6:5> and PORTD<2:1> as
input:
 Initial solution
TRISD6=1;
TRISD5=1;
TRISD2=1;
TRISD1=1;
 Alternative (better solution):
TRISD = TRISD | 0b01100110;
TRISD |= 0x66;

25-Nov-2011 Franz Duran 121

BASICS OF C: Operators
Masking technique
To clear a bit (or bits), AND this bit with 0
To set a bit (or bits), OR this bit with 1
Ex:
 PORTB &= 0b10110000;
 TRISD |= 0x66;

Mask values

25-Nov-2011 Franz Duran 122

BASICS OF C: ^ Operator
XOR (^) operator truth table:

A B A| B
0 0 0
0 1 1
1 0 1
1 1 0
25-Nov-2011 Franz Duran 123

41
BASICS OF C: ^ Operator
Bitwise-XOR operator
Toggle operator
Example:
 RB0 = RB0 ^ 1; //toggle RB0
Equivalent to:
 RB0 = ~RB0; //toggle RB0

 PORTB ^= 0b00000011;

25-Nov-2011 Franz Duran 124

BASICS OF C: ^ Operator
EXERCISE:
Create a LED blinker application using ^
operator
 Toggle two LEDs alternately

25-Nov-2011 Franz Duran 125

BASICS OF C: << and >>


Operators
<< (shift left)
>> (shift right)
Ex:
unsigned char var1 = 0b00000001;
PORTB = var1 << 2; //PORTB = 0b00000100

25-Nov-2011 Franz Duran 126

42
END

43

You might also like