day 1 am introduction
TRANSCRIPT
-
7/30/2019 Day 1 AM Introduction
1/38
19-Aug-2010 Paranz 1
Introduction to PICMicrocontroller Programming &
Interfacing
August 19-21, 2010Franz Duran
19-Aug-2010 Paranz 2
OVERVIEW
DAY 1 (Morning)Introduction to PIC Microcontroller
PIC16F877A trainer board andeICD2/ePickit2 programmer/debugger
MPLAB IDE
Basic C Programming
19-Aug-2010 Paranz 3
OVERVIEW
DAY 1 (Afternoon)Basic C Programming (cont.)
C Functions
Modular Programming
Interfacing with 2x16 character LCD
Interfacing with 4x3 keypad
-
7/30/2019 Day 1 AM Introduction
2/38
19-Aug-2010 Paranz 4
OVERVIEW
DAY 2 (Morning)Basic of Interrupts
Interrupt sources
Interrupt service routine
RB0 interrupt
PORTB Interrupt on Change
19-Aug-2010 Paranz 5
OVERVIEW
DAY 2 (Afternoon)Timer module
TMR0 architecture
TMR0 as an interrupt source
Interfacing with 7-segment displays
TMR1
TMR2
19-Aug-2010 Paranz 6
OVERVIEW
DAY 3 (Morning)Basic of Analog-to-Digital Conversion
Using the A/D module
Interfacing LM35 temp. sensor
-
7/30/2019 Day 1 AM Introduction
3/38
19-Aug-2010 Paranz 7
OVERVIEW
DAY 3 (Afternoon)Basics of Serial Communication
UART module
Basic string processing
Interfacing with Bluetooth module
Interfacing with GSM module
19-Aug-2010 Paranz 8
MICROCONTROLLER
MCU, C
A single-chip computer
Invented in the 1970s
Used as embedded controller
19-Aug-2010 Paranz 9
MICROCONTROLLER
used as dedicated controllers
domestic appliances
consumer electronicsindustrial equipments
automotive electronics
-
7/30/2019 Day 1 AM Introduction
4/38
19-Aug-2010 Paranz 10
MICROCONTROLLER
Why use?cheap
flexible
easy to develop applications
easy to maintain applications
small outline high integration
low-power
19-Aug-2010 Paranz 11
PIC MICROCONTROLLER
PIC, PICMICRO
by MICROCHIP
Arizona, U.S.A.
1989 (offshoot of General Instrument)
http://www.microchip.com
19-Aug-2010 Paranz 12
PIC MICROCONTROLLER
Rank (8-bit microcontroller)1990 20th
1993 8
th
1996 - 5th
1997-2001 2nd
2002 Present 1st
-
7/30/2019 Day 1 AM Introduction
5/38
19-Aug-2010 Paranz 13
PIC MICROCONTROLLER
FamilyPIC10, PIC12
PIC16
PIC17/PIC18
PIC24/DSPICs (16-bit)
PIC32(32-bit)
Popular amongstudents and hobbyists
19-Aug-2010 Paranz 14
PIC16 ARCHITECTURE
Program Memory hard drive where fixed program is stored
File RegistersGeneral Purpose Registers (GPRs)
Data RAM
Special Function Registers (SFRs)
control device operation
DATA EEPROMnon-volatile storage
19-Aug-2010 Paranz 15
PIC16 ARCHITECTURE
Input/Output pins
Internal Peripherals/Modules
TimersA/D converter module
UART
SPI / I2C
Comparator
-
7/30/2019 Day 1 AM Introduction
6/38
19-Aug-2010 Paranz 16
PIC16 ARCHITECTURE
Features:Watchdog Timer
SLEEP mode
Power On Reset, Brown-out Reset
CPU
RISC (reduced instruction set computer)
FOSC = 20Mhz typical
19-Aug-2010 Paranz 17
PIC16 ARCHITECTURE
Instruction set35 instructions (PIC16)
Easy to memorize all instructions
75 instruction (PIC18)
19-Aug-2010 Paranz 18
PIC16F84A
8-bit microcontrolllerPIC16 family
F flash memory, i.e. reprogrammable
84 variant/model
A - revision
4Mhz (1MIPS), DIP18, +5V
-
7/30/2019 Day 1 AM Introduction
7/38
19-Aug-2010 Paranz 19
PIC16F84A
Program Memory
1024 instruction words1 word = 14 bit
File Registers (2 banks)GPRs - 68 bytes RAM
SFRs 16 registers
Data EEPROM64 bytes
19-Aug-2010 Paranz 20
PIC16F84A
13 I/O pinsPORTB 8 pins
PORTA 5 pins
2 power pinsVDD, VSS
2 oscillator pinsOSC1, OSC2
1 RESET pinMCLR
19-Aug-2010 Paranz 21
PIC16F877A
20Mhz (5MIPS), DIP40, +5V
8192 instruction word
368 bytes Data RAM / GPRs56 SFRs
256 bytes Data EEPROM
-
7/30/2019 Day 1 AM Introduction
8/38
19-Aug-2010 Paranz 22
PIC16F877A
33 I/O pins
PORTA 6 pinsPORTB 8 pins
PORTC 8 pins
PORTD 8 pins
PORTE 3 pins
4 power pins
1 Reset, 2 Clock pins
19-Aug-2010 Paranz 23
PIC16F877A TRAINER BOARD
PIC16F877A
Reset Button
20Mhz Oscillator
+5V supply
ICSP connector
SIL connectors
19-Aug-2010 Paranz 24
PIC16F877A TRAINER BOARD10 LEDs
4 pushbuttons
3 potentiometers
serial comm. ckt.
character LCD
7-seg. display
keypad
4x3 or 4x4
DS1307real-time IC
serial EEPROM IC
LM35 temp. sensor
-
7/30/2019 Day 1 AM Introduction
9/38
19-Aug-2010 Paranz 25
PIC16F877A & eICD2
Connect wires to build application circuit
eICD2
ICSP connector
19-Aug-2010 Paranz 26
MPLAB IDE 8.xx
integrated development environment
(IDE) for PIC freely downloadable (~90MB)
assembler (MPASM)
HI-TECHC Compiler 45-day full version (full optimization)
Lite mode (no optimization)
direct support for ICD2/PICKIT2programmer/debugger eICD2, ePICKIT2
19-Aug-2010 Paranz 27
MPLAB IDE 8.xx
Download and Install MPLABhttp://www.microchip.com
Install HI-TECH
Compilerincluded in the MPLAB installer
or download separately from:
http://www.htsoft.com
HI-TECH Sofware
Brisbane, Australia
bought by Microchip (March 2009)
-
7/30/2019 Day 1 AM Introduction
10/38
19-Aug-2010 Paranz 28
MPLAB IDE 8.53
Open MPLAB an example application
19-Aug-2010 Paranz 29
MPLAB IDE 8.53
To create new project:Project > Project Wizard
19-Aug-2010 Paranz 30
MPLAB IDE 8.53
Step 1: Select Device
-
7/30/2019 Day 1 AM Introduction
11/38
19-Aug-2010 Paranz 31
MPLAB IDE 8.53
Step 2: Select Language
19-Aug-2010 Paranz 32
MPLAB IDE 8.53
Step 3: Select Project Name &Directory
19-Aug-2010 Paranz 33
MPLAB IDE 8.53
Step 4: Add files
-
7/30/2019 Day 1 AM Introduction
12/38
19-Aug-2010 Paranz 34
MPLAB IDE 8.53
Project Summary
19-Aug-2010 Paranz 35
MPLAB IDE 8.53
Empty ProjectProject Window
Output Window
19-Aug-2010 Paranz 36
MPLAB IDE 8.53
Create source codeEditor Window
-
7/30/2019 Day 1 AM Introduction
13/38
19-Aug-2010 Paranz 37
MPLAB IDE 8.53#include
void main()
{
TRISB0 = 0;
RB0 = 1;
while(1)
{
}
}
19-Aug-2010 Paranz 38
MPLAB IDE 8.53
save as main.c in project directory
19-Aug-2010 Paranz 39
MPLAB IDE 8.53
main.c
-
7/30/2019 Day 1 AM Introduction
14/38
19-Aug-2010 Paranz 40
MPLAB IDE 8.53
Addmain.c Build project
(F10)
19-Aug-2010 Paranz 41
MPLAB IDE 8.53
19-Aug-2010 Paranz 42
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
-
7/30/2019 Day 1 AM Introduction
15/38
19-Aug-2010 Paranz 43
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
19-Aug-2010 Paranz 44
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..
19-Aug-2010 Paranz 45
BASIC PIC16F84A CKT.
PIC16F84A CIRCUIT1. PIC16F84A
2. +5v supply
3. Oscillator circuit
4. Reset circuit
5. External peripherals
6. In-circuit serial
programming
connector
-
7/30/2019 Day 1 AM Introduction
16/38
19-Aug-2010 Paranz 46
BASIC PIC16F84A CKT.
PIC16F84A w/ +5V supply circuit
19-Aug-2010 Paranz 47
BASIC PIC16F84A CKT.
PIC16F84A w/ +5V supply circuit & crystaloscillator and loading capacitors
19-Aug-2010 Paranz 48
BASIC PIC16F84A CKT.
oscillator circuitgenerate a pulse train signal; used to
synchronize MCU internal operations
-
7/30/2019 Day 1 AM Introduction
17/38
19-Aug-2010 Paranz 49
BASIC PIC16F84A CKT.
Reset circuit
19-Aug-2010 Paranz 50
BASIC PIC16F84A CKT.
Interfacing external peripherals
19-Aug-2010 Paranz 51
BASIC PIC16F84A CKT.
-
7/30/2019 Day 1 AM Introduction
18/38
19-Aug-2010 Paranz 52
PIC16F877A Trainer Board
19-Aug-2010 Paranz 53
IO INTERFACING: LED
LED at RB0RB0 is output
19-Aug-2010 Paranz 54
LED
5mm LEDIF = 5 35 mA
VF = 2V
3mm LEDIF = 1 30 mA
VF = 2V
-
7/30/2019 Day 1 AM Introduction
19/38
19-Aug-2010 Paranz 55
LED
IF
+
-
VF =
2v
+ -VR=3V
If IF = 10mA,R = 3V/10mA
R = 300
19-Aug-2010 Paranz 56
LED
R should not be too largeLED will not turn on
R should not be too smallIF < 30mA
PIC Output pin source
current < 25mA
19-Aug-2010 Paranz 57
BASIC I/O PROGRAM Example I/O Program
#include
void main()
{
TRISB0 = 0;
RB0 = 1;
while(1)
{
}
}
preprocessor directive
main() functioninitialization
program loop- infinite loop- super loop
Example#1
-
7/30/2019 Day 1 AM Introduction
20/382
19-Aug-2010 Paranz 58
IO PROGRAMMING
Input/Output port - group of 8 pinstypicalPORTA 6 I/O pins
RA5, RA4, RA3, RA2, RA1, RA0
PORTA
PORTB 8 I/O pins
RB7, RB6, RB5, RB4, RB3, RB2, RB1, RB0
PORTB
19-Aug-2010 Paranz 59
IO PROGRAMMING
PORTC 8 I/O pins
RC7, RC6, RC5, RC4, RC3, RC2, RC1, RC0
PORTC
PORTD 8 I/O pins
RD7, RD6, RD5, RD4, RD3, RD2, RD1, RD0
PORTD
PORTE 3 I/O pins
RE2, RE1, RE0
PORTE
19-Aug-2010 Paranz 60
IO PROGRAMMING
Special Function Registers for I/OTRISA, PORTA
TRISB, PORTB
TRISC, PORTC
TRISD, PORTD
TRISE, PORTE
-
7/30/2019 Day 1 AM Introduction
21/382
19-Aug-2010 Paranz 61
IO PROGRAMMING
Consider PORTB..
19-Aug-2010 Paranz 62
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
19-Aug-2010 Paranz 63
IO PROGRAMMING: SFRs
TRISB
TRISB0
TRISB1
TRISB7
. . . . . . . . .
.
.
.
0 output1 input
= XXXXXXXX
-
7/30/2019 Day 1 AM Introduction
22/382
19-Aug-2010 Paranz 64
IO PROGRAMMING: SFRs
TRISB0
X X X X X X X X0
TRISB0 = 0; //RB0 is output
TRISB
RB0 pin isan output pin
19-Aug-2010 Paranz 65
IO PROGRAMMING: SFRs
PORTB
RB0
RB1
RB7
. . . . . . . . .
.
.
.
0 Logic 01 Logic 1
= XXXXXXXX
19-Aug-2010 Paranz 66
IO PROGRAMMING: SFRs
RB0
X X X X X X X X1
RB0 = 1; //LED on
PORTB
RB0 outputs aLogic 1 signal;
~5V
-
7/30/2019 Day 1 AM Introduction
23/382
19-Aug-2010 Paranz 67
IO PROGRAMMING: SFRs
TRISB0 = 0; //RB0 is output
RB0 = 1; //LED is on
X X X X X X X
X X X X X X X
0
1
TRISB
PORTB
19-Aug-2010 Paranz 68
IO PROGRAMMING
EXERCISE:Create new project
Led_demo_2
Turn on LEDs connected to the ff. I/O pins: RB0, RA1, RC3, RD7, RE2
Example#2
19-Aug-2010 Paranz 69
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
Example#2
-
7/30/2019 Day 1 AM Introduction
24/382
19-Aug-2010 Paranz 70
IO PROGRAMMING
EXERCISE:Turn on all 8 LEDs connected to PORTD
Example#3
19-Aug-2010 Paranz 71
IO PROGRAMMING
SOLUTION:TRISD = 0b00000000; //binary notation
PORTD = 0b11111111;
or
TRISD = 0x00; //hexadecimal notation
PORTD = 0xFF;
or
TRISD = 0; //decimal notation
PORTD = 255;
Example#3
19-Aug-2010 Paranz 72
IO INTERFACING: Button
pushbuttoninput device
Tack Switch
-
7/30/2019 Day 1 AM Introduction
25/382
19-Aug-2010 Paranz 73
IO INTERFACING: Button
If button is not pressed
RIN
10k
+5v
+
-
1MV = 5v x 1M / (R+1M)
R
V 5v
RB2
(Logic 1)
19-Aug-2010 Paranz 74
IO INTERFACING: Button
If button is pressed
RIN +
-
~1M V = 0v
RB2
(Logic 0)
19-Aug-2010 Paranz 75
IO INTERFACING: Buttonvoid 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.
}
}
Example#4
-
7/30/2019 Day 1 AM Introduction
26/382
19-Aug-2010 Paranz 76
IO INTERFACING: Button
R should be large enough to limitthe currentwhen button is
pressed I
19-Aug-2010 Paranz 77
IO INTERFACING: Button
R should be large enough to limitthe currentwhen I/O pin is
configured as
output and at
Logic 0 (I/O pinis internally
connected
to ground)
I < 25mA
19-Aug-2010 Paranz 78
BASICS OF C: #defines#include
__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;
}
}
Example#5
-
7/30/2019 Day 1 AM Introduction
27/382
19-Aug-2010 Paranz 79
BASICS OF C: ConditionalStatements
1. If() If()-else()
If()-else-if()
2. switch()
19-Aug-2010 Paranz 80
BASICS OF C: IF ConditionalStatement
If() Simplest conditional statement
if (condition)
statement1;
Ex.if(var>99)
var=0;
19-Aug-2010 Paranz 81
BASICS OF C: IF ConditionalStatement If()
use elseclause (optional)
if (condition)
statement1;
else
statement2;
Ex.if(BUTTON==PRESSED)
LED=1;
else
LED=0;
-
7/30/2019 Day 1 AM Introduction
28/382
19-Aug-2010 Paranz 82
BASICS OF C: IF ConditionalStatement
If()-else-if() if (condition1)
statement1;
else if (condition2)
statement2;
else if (condition3)
statement3;
else
statement4;
19-Aug-2010 Paranz 83
BASICS OF C: Switch()
switch()Allow comparison of a single variable (or
expression) to multiple values
Code associate with the matching value isexecuted
19-Aug-2010 Paranz 84
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
}
-
7/30/2019 Day 1 AM Introduction
29/382
19-Aug-2010 Paranz 85
BASICS OF C: Loops
Loops used to repeatedly execute specific
statements
3 loop statements in C1. for() loop
2. while() loop
3. do-while() loop
19-Aug-2010 Paranz 86
BASICS OF C: FOR Loopvoid 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
-
7/30/2019 Day 1 AM Introduction
30/383
19-Aug-2010 Paranz 88
BASICS OF C: FOR Loop
i=0;
for( ;i0;i--)
{
//codes here
}
19-Aug-2010 Paranz 89
BASICS OF C: WHILE Loop
while(condition)
{
...//code goes here
}
condition?
TRUE
FALSE
//codes
exit
start
19-Aug-2010 Paranz 90
BASICS OF C: WHILE Loop
EXERCISE:Modify previous example to used while()
loop
Example#7
-
7/30/2019 Day 1 AM Introduction
31/383
19-Aug-2010 Paranz 91
BASICS OF C: DO-WHILE Loop
do
{
...//code goes here
}
while(condition);
condition?
TRUE
FALSE
//codes
exit
start
19-Aug-2010 Paranz 92
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, usewhile() loop
If a simple test of condition is used ANDthecode block should be executed at least once,use do-while() loop
19-Aug-2010 Paranz 93
BASICS OF C: Variables
Variables
program data that variesduring run-time
temporary dataplaced in volatile memory
General Purpose Registers (GPR)
PIC16F877A
368 Bytes GPR
-
7/30/2019 Day 1 AM Introduction
32/383
19-Aug-2010 Paranz 94
BASICS OF C: Variables
unsigned char var1; //range of values: 0-255
var1 = 100; //OK
var1 = 500; //not OK!
Var1 = -10; //not OK!
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!
19-Aug-2010 Paranz 95
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-bi t) (1.17549435e-38 - 3.40277e+38) double (24-bi t) (1.17549435e-38 - 3.40277e+38 ) double (32-bit) (1.17549435e-38 - 3.40282347e+38 )
19-Aug-2010 Paranz 96
BASICS OF C: Operators
1.= (Assignment operator)
2. Mathematical operators
3. Relational operators
4. Logical operators
5. Bitwise operators
-
7/30/2019 Day 1 AM Introduction
33/383
19-Aug-2010 Paranz 97
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 anumber i.e.
int sum;
sum = a + b; //a + b is anexpression
19-Aug-2010 Paranz 98
BASICS OF C: Operators
Mathematical operators1. + (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
19-Aug-2010 Paranz 99
BASICS OF C: Operators
Relational Operators
1.== (equal to) ex. x==y
2.> (greater than) ex. x>y3.>= (greater than or equal to) ex. x>=y
4.< (lesser than) ex. x
-
7/30/2019 Day 1 AM Introduction
34/383
19-Aug-2010 Paranz 100
BASICS OF C: Operators
Logical Operators1. && (Logical AND)
2. || (Logical OR)
3. ! (Logical NOT)
19-Aug-2010 Paranz 101
BASICS OF C: Operators
EXERCISE:Turn on LED1 if BUTTON1 or BUTTON2
is pressed
Modify: Turn on LED1 if BUTTON1 and BUTTON2
are pressed
19-Aug-2010 Paranz 102
BASICS OF C: Operators
Bitwise Operators1. & (Bitwise AND)
2. | (Bitwise OR)3. ! (Bitwise Complement)
4. ^ (Bitwise Exclusive-OR)
5. > (Rightshift)
-
7/30/2019 Day 1 AM Introduction
35/383
19-Aug-2010 Paranz 103
BASICS OF C: & Operator
111
001
010
000
A & BBA
AND (&) operator truth table:
19-Aug-2010 Paranz 104
BASICS OF C: & Operator
Bitwise-AND operator
Example: RB0 = RB0 & 0; //clear RB0
Equivalent to: RB0 = 0; //clear RB0
19-Aug-2010 Paranz 105
BASICS OF C: & Operator
Example:Clear PORTD and RB6
Initial solution
RB0 = 0;RB1 = 0;
RB2 = 0;
RB3 = 0;
RB6 = 0;
Alternative (better solution):
PORTB = PORTB & 0b10110000;
PORTB &= 0b10110000;
PORTB &= ~0x4F;
-
7/30/2019 Day 1 AM Introduction
36/383
19-Aug-2010 Paranz 106
BASICS OF C: | Operator
111
101
110
000
A | BBA
OR (|) operator truth table:
19-Aug-2010 Paranz 107
BASICS OF C: | Operator
Bitwise-OR operator
Example: RB0 = RB0 | 1; //set RB0
Equivalent to: RB0 = 1; //set RB0
19-Aug-2010 Paranz 108
BASICS OF C: Operators
Example:Configure PORTD and PORTD as
input:
Initial solutionTRISD6=1;
TRISD5=1;
TRISD2=1;
TRISD1=1;
Alternative (better solution):TRISD = TRISD | 0b01100110;
TRISD |= 0x66;
-
7/30/2019 Day 1 AM Introduction
37/383
19-Aug-2010 Paranz 109
BASICS OF C: Operators
Masking techniqueTo 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
19-Aug-2010 Paranz 110
BASICS OF C: ^ Operator
011
101
110
000
A | BBA
XOR (^) operator truth table:
19-Aug-2010 Paranz 111
BASICS OF C: ^ Operator
Bitwise-XOR operatorToggle operator
Example: RB0 = RB0 ^ 1; //toggle RB0
Equivalent to: RB0 = !RB0; //toggle RB0
-
7/30/2019 Day 1 AM Introduction
38/38
19-Aug-2010 Paranz 112
BASICS OF C: ^ Operator
EXERCISE:Create a LED blinker application using ^
operator
Toggle two LEDs alternately every 500ms
19-Aug-2010 Paranz 113
BASICS OF C: >
Operators
> (shift right)
Ex:unsigned char var1 = 0b00000001;
PORTB = var1