SlideShare a Scribd company logo
1 of 43
Download to read offline
The University of Texas at Arlington
Lecture 6
PIC Programming in C
CSE 3442/5442
Embedded Systems 1
Based heavily on slides by Dr. Gergely Záruba and Dr. Roger Walker
Code Space Limitations
• On a general purpose PC, we don’t
usually care about our program’s size
• MB/GB/TB range for general purpose PCs
– Ex: 1300 line .C file 50 KB  40 KB .hex file
• 2MB max in PIC18’s Program ROM
• For our PIC18F452  Only 32KB
– See datasheet
2
Why C over ASM?
• While Assembly Language produces a
much smaller .HEX file than C…
– More human-readable in C
• Easier to write and less time consuming
– C is easier to modify and update
• Don’t care about absolute ROM locations
– Access to many C function libraries
– C code is portable and can be used on other
microcontrollers with little or no modification
3
C Integer Data Types
(Generic)
4
C Integer Data Types
(C18 Compiler)
5
6
C Integer Data Types
(XC8 Compiler)
6
Unsigned char
(0 to 255)
• PIC18 is 8-bit architecture, char type (8 bits) is the
most natural choice
• C compilers use signed char (-128 to +127) by
default unless we put “unsigned”
– char == signed char
7
Unsigned char array
(0 to 255)
8
Unsigned char array
(0 to 255)
9
Unsigned char array
(0 to 255)
z = 0
PORTB = ‘0’ (in code)
PORTB = 0x30 = 48 (actual)
PORTB = 0b 0011 0000 (pins) 10
Unsigned char array
(0 to 255)
z = 0
PORTB = ‘0’ (in code)
PORTB = 0x30 = 48 (actual)
PORTB = 0b 0011 0000 (pins)
Direction
(TRISB)
0
0
0
0
0
0
0
0
Pin Value
(PORTB)
0
0
1
1
0
0
0
0
PINS
11
Unsigned char array
(0 to 255)
z = 1
PORTB = ‘1’ (in code)
PORTB = 0x31 = 49 (actual)
PORTB = 0b 0011 0001 (pins) 12
Unsigned char array
(0 to 255)
z = 6
PORTB = ‘A’ (in code)
PORTB = 0x41 = 65 (actual)
PORTB = 0b 0100 0001 (pins) 13
Signed char
(-128 to +127)
• Still 8-bit data type but MSB is sign value
14
Unsigned int
(0 to 65,535)
• PIC18 is 8-bit architecture, int type (16 bits) takes
two bytes of RAM (only use when necessary)
• C compilers use signed int (-32,768 to +32,767) by
default unless we put “unsigned”
– int == signed int
15
Larger Integer Types
(short, long, short long)
16
Floating-Point Data Types
• Can store and calculate numbers with
decimals (precision)
• Always signed, can’t be unsigned
2.5, 32.05898, -1.00232, .2600313, 51156.01, etc.
• Further info: Text and Video Explanation 17
Modulus
• In C can use % to perform a modulus of
two numbers (find the whole number
remainder from a “repeated subtraction”)
• 25 % 5 = 0
• 25 % 7 = 4
• 25 % 10 = 5
• 428 % 100 = 28
• 1568 % 10 = 8 18
Casting to Prevent Data Loss
?
?
19
Casting to Prevent Data Loss
20
Time Delay
• Want to have exact time differences or
spacing between certain instructions
• Three methods:
– Using a simple loop (for/while) (crude)
– Using PIC18 timer peripheral (later)
– Built-in delay functions (reliable and accurate)
21
Two Factors for
Delay Accuracy in C
1. The crystal’s frequency (int. or ext.)
– Duration of clock period for instruction cycle
2. The compiler used for the C program
– In ASM, we control the exact instructions
– Different compilers produce different ASM code
22
Time Delay Example
FOSC = 10 MHz = 10,000,000 cycles/sec
Each instruction takes 4 clock cycles (ticks)
FCY = Instruction Cycle Frequency
=
10𝑀𝑀𝑀𝑀𝑀𝑀
4
= 2.5MHz = 2,500,000 Ins/sec
TCY = Instruction Cycle Time
= 1 / 2.5MHz = 0.0000004 sec per Ins
= 0.0004 ms = 0.4 µs
How many IC (instructions) fit into 1ms?
1ms / 0.0004ms = 2,500
 2,500 Instruction Cycles take place in 1ms
 2,500 Instructions can complete in 1ms23
Instruction Cycle
FOSC = Oscillator Frequency
= 10 MHz = 10,000,000 cycles/sec
Each instruction takes 4 clock cycles (ticks)
FCY = Instruction Cycle Frequency
=
FOSC
4
=
10𝑀𝑀𝑀𝑀𝑀𝑀
4
= 2.5MHz = 2,500,000 Ins/sec
TCY = Instruction Cycle Time
=
1
FCY
=
1
2.5MHz
= 0.0000004 sec per Ins
= 0.0004 ms = 0.4 µs
How many IC (instructions) fit into 1ms?
1ms / 0.0004ms = 2,500
 2,500 Instruction Cycles take place in 1ms
 2,500 Instructions can complete in 1ms (generalizing since most instructions only take 1 Ins. Cycle)
FOSC
FCY
24
Delay Functions in the
XC8 Compiler
1. Include the “xc.h” header file
2. Define your crystal’s frequency
• _XTAL_FREQ
3. Can now use these 2 delay functions:
– __delay_us(x); //unsigned long (0 - 4294967295)
– __delay_ms(x); //unsigned long (0 - 4294967295)
25
26
PORT I/O Programming in C
• Btye-Size Register Access
– Labels still the same
– PORTA – PORTD
– TRISA – TRISD
– INTCON
• Bit-Addressable Register Access
– PORTBbits.RB3
– TRISCbits.RC7 or TRISCbits.TRISC7
– INTCONbits.RBIE
27
PORT I/O Programming in C
28
PORTxbits.Rxy
29
PORT I/O Programming in C
30
31
.ASM Generated from C
32
Header Files
• Remember that certain register/variable
names are not native C keywords
• They are PIC-specific
– PORTB, TRISA, TMR0H, PRODL, etc.
• Defined and mapped in header file
– Using regular data types (char, int, struct, etc.)
• Regular P18Fxxx.h (device) header files
– C:Program Files (x86)Microchipxc8v1.20include
33
Header Files
• Other functional headers are available
– adc.h
– delays.h
– i2c.h
– pwm.h
– timers.h
– usart.h
• Peripheral library Header Files
– C:Program Files (x86)Microchipxc8v1.20includeplib
– C:Program Files (x86)Microchipxc8v1.20sourcespic18plib
34
Logic Operations in C
• Bit-Wise Operators
• Bit-Wise Shift Operators
– Can shift right/left by X bits
Shift right >>
Shift left << 35
Logic Operations in C
36
Binary (hex) to Decimal and
ASCII Conversion
• Sometimes we can’t handle multiple-digit
decimals natively in C for display purposes
• printf() is standard for generic C but
requires more memory space than a
PIC18 is willing to sacrifice
• Best to build your own “custom” print or
display functions in C
37
Extract Single Decimal Digits
• Want each digit of 253 (0b11111101, 0xFD)
and convert to ASCII for displaying
38
Extract Single Decimal Digits
• Want each digit of 253 (0b11111101, 0xFD)
and convert to ASCII for displaying
39
Extract Single Decimal Digits
• Want each digit of 253 (0b11111101, 0xFD)
and convert to ASCII for displaying
40
Extract Single Decimal Digits
• Want each digit of 253 (0b11111101, 0xFD)
and convert to ASCII for displaying
41
#define Directive
• Can associate labels with numbers or
registers as a constant
#define LED_OUTPUT PORTBbits.RB2
#define MAX_USERS 50
42
Questions?
• For PIC C Programming
– Textbook Ch. 7 for more details
• Start looking over Arithmetic/Logic
– Textbook Ch. 5
43

More Related Content

Similar to Lecture-6-PIC Programming in C-good.pdf

Introduction2_PIC.ppt
Introduction2_PIC.pptIntroduction2_PIC.ppt
Introduction2_PIC.pptAakashRawat35
 
Microprocessors-based systems (under graduate course) Lecture 5 of 9
Microprocessors-based systems (under graduate course) Lecture 5 of 9 Microprocessors-based systems (under graduate course) Lecture 5 of 9
Microprocessors-based systems (under graduate course) Lecture 5 of 9 Randa Elanwar
 
B.sc cs-ii-u-2.2-overview of register transfer, micro operations and basic co...
B.sc cs-ii-u-2.2-overview of register transfer, micro operations and basic co...B.sc cs-ii-u-2.2-overview of register transfer, micro operations and basic co...
B.sc cs-ii-u-2.2-overview of register transfer, micro operations and basic co...Rai University
 
Advanced Processor Power Point Presentation
Advanced Processor  Power Point  PresentationAdvanced Processor  Power Point  Presentation
Advanced Processor Power Point PresentationPrashantYadav931011
 
Bca 2nd sem-u-2.2-overview of register transfer, micro operations and basic c...
Bca 2nd sem-u-2.2-overview of register transfer, micro operations and basic c...Bca 2nd sem-u-2.2-overview of register transfer, micro operations and basic c...
Bca 2nd sem-u-2.2-overview of register transfer, micro operations and basic c...Rai University
 
Arm architecture chapter2_steve_furber
Arm architecture chapter2_steve_furberArm architecture chapter2_steve_furber
Arm architecture chapter2_steve_furberasodariyabhavesh
 
My seminar new 28
My seminar new 28My seminar new 28
My seminar new 28rajeshkvdn
 
Advanced Microprocessors
Advanced MicroprocessorsAdvanced Microprocessors
Advanced MicroprocessorsBuddiesSairamit
 
Introduction to computer architecture .pptx
Introduction to computer architecture .pptxIntroduction to computer architecture .pptx
Introduction to computer architecture .pptxFatma Sayed Ibrahim
 
Arduino by yogesh t s'
Arduino by yogesh t s'Arduino by yogesh t s'
Arduino by yogesh t s'tsyogesh46
 
Session01_Intro.pdf
Session01_Intro.pdfSession01_Intro.pdf
Session01_Intro.pdfRahnerJames
 
Introduction to Processor Design and ARM Processor
Introduction to Processor Design and ARM ProcessorIntroduction to Processor Design and ARM Processor
Introduction to Processor Design and ARM ProcessorDarling Jemima
 

Similar to Lecture-6-PIC Programming in C-good.pdf (20)

Introduction2_PIC.ppt
Introduction2_PIC.pptIntroduction2_PIC.ppt
Introduction2_PIC.ppt
 
Microprocessors-based systems (under graduate course) Lecture 5 of 9
Microprocessors-based systems (under graduate course) Lecture 5 of 9 Microprocessors-based systems (under graduate course) Lecture 5 of 9
Microprocessors-based systems (under graduate course) Lecture 5 of 9
 
B.sc cs-ii-u-2.2-overview of register transfer, micro operations and basic co...
B.sc cs-ii-u-2.2-overview of register transfer, micro operations and basic co...B.sc cs-ii-u-2.2-overview of register transfer, micro operations and basic co...
B.sc cs-ii-u-2.2-overview of register transfer, micro operations and basic co...
 
TMS320C6X Architecture
TMS320C6X ArchitectureTMS320C6X Architecture
TMS320C6X Architecture
 
Advanced Processor Power Point Presentation
Advanced Processor  Power Point  PresentationAdvanced Processor  Power Point  Presentation
Advanced Processor Power Point Presentation
 
Processors selection
Processors selectionProcessors selection
Processors selection
 
Bca 2nd sem-u-2.2-overview of register transfer, micro operations and basic c...
Bca 2nd sem-u-2.2-overview of register transfer, micro operations and basic c...Bca 2nd sem-u-2.2-overview of register transfer, micro operations and basic c...
Bca 2nd sem-u-2.2-overview of register transfer, micro operations and basic c...
 
PILOT Session for Embedded Systems
PILOT Session for Embedded Systems PILOT Session for Embedded Systems
PILOT Session for Embedded Systems
 
Arm architecture chapter2_steve_furber
Arm architecture chapter2_steve_furberArm architecture chapter2_steve_furber
Arm architecture chapter2_steve_furber
 
Dsp ajal
Dsp  ajalDsp  ajal
Dsp ajal
 
My seminar new 28
My seminar new 28My seminar new 28
My seminar new 28
 
Arm processor
Arm processorArm processor
Arm processor
 
Unit I_MT2301.pdf
Unit I_MT2301.pdfUnit I_MT2301.pdf
Unit I_MT2301.pdf
 
Advanced Microprocessors
Advanced MicroprocessorsAdvanced Microprocessors
Advanced Microprocessors
 
Introduction to computer architecture .pptx
Introduction to computer architecture .pptxIntroduction to computer architecture .pptx
Introduction to computer architecture .pptx
 
M&amp;i(lec#01)
M&amp;i(lec#01)M&amp;i(lec#01)
M&amp;i(lec#01)
 
Lecture 03 basics of pic
Lecture 03 basics of picLecture 03 basics of pic
Lecture 03 basics of pic
 
Arduino by yogesh t s'
Arduino by yogesh t s'Arduino by yogesh t s'
Arduino by yogesh t s'
 
Session01_Intro.pdf
Session01_Intro.pdfSession01_Intro.pdf
Session01_Intro.pdf
 
Introduction to Processor Design and ARM Processor
Introduction to Processor Design and ARM ProcessorIntroduction to Processor Design and ARM Processor
Introduction to Processor Design and ARM Processor
 

More from AvinashJain66

Graphs and waveforms.ppt
Graphs and waveforms.pptGraphs and waveforms.ppt
Graphs and waveforms.pptAvinashJain66
 
Image processing tool box.pptx
Image processing tool box.pptxImage processing tool box.pptx
Image processing tool box.pptxAvinashJain66
 
Loop Concept and Array.ppt
Loop Concept and Array.pptLoop Concept and Array.ppt
Loop Concept and Array.pptAvinashJain66
 
Looping concept.pptx
Looping concept.pptxLooping concept.pptx
Looping concept.pptxAvinashJain66
 
Virtual Instrumentation & LabVIEW-lini.ppt
Virtual Instrumentation & LabVIEW-lini.pptVirtual Instrumentation & LabVIEW-lini.ppt
Virtual Instrumentation & LabVIEW-lini.pptAvinashJain66
 
PIC18F458_Ritula Thakur.pptx.pdf
PIC18F458_Ritula Thakur.pptx.pdfPIC18F458_Ritula Thakur.pptx.pdf
PIC18F458_Ritula Thakur.pptx.pdfAvinashJain66
 
Introduction_PIC18F458_Ritula Thakur.pptx
Introduction_PIC18F458_Ritula Thakur.pptxIntroduction_PIC18F458_Ritula Thakur.pptx
Introduction_PIC18F458_Ritula Thakur.pptxAvinashJain66
 

More from AvinashJain66 (9)

FACTS.pdf
FACTS.pdfFACTS.pdf
FACTS.pdf
 
Graphs and waveforms.ppt
Graphs and waveforms.pptGraphs and waveforms.ppt
Graphs and waveforms.ppt
 
Image processing tool box.pptx
Image processing tool box.pptxImage processing tool box.pptx
Image processing tool box.pptx
 
Loop Concept and Array.ppt
Loop Concept and Array.pptLoop Concept and Array.ppt
Loop Concept and Array.ppt
 
Looping concept.pptx
Looping concept.pptxLooping concept.pptx
Looping concept.pptx
 
Virtual Instrumentation & LabVIEW-lini.ppt
Virtual Instrumentation & LabVIEW-lini.pptVirtual Instrumentation & LabVIEW-lini.ppt
Virtual Instrumentation & LabVIEW-lini.ppt
 
PIC18F458_Ritula Thakur.pptx.pdf
PIC18F458_Ritula Thakur.pptx.pdfPIC18F458_Ritula Thakur.pptx.pdf
PIC18F458_Ritula Thakur.pptx.pdf
 
Introduction_PIC18F458_Ritula Thakur.pptx
Introduction_PIC18F458_Ritula Thakur.pptxIntroduction_PIC18F458_Ritula Thakur.pptx
Introduction_PIC18F458_Ritula Thakur.pptx
 
Relay-ppt.pptx
Relay-ppt.pptxRelay-ppt.pptx
Relay-ppt.pptx
 

Recently uploaded

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 

Recently uploaded (20)

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 

Lecture-6-PIC Programming in C-good.pdf

  • 1. The University of Texas at Arlington Lecture 6 PIC Programming in C CSE 3442/5442 Embedded Systems 1 Based heavily on slides by Dr. Gergely Záruba and Dr. Roger Walker
  • 2. Code Space Limitations • On a general purpose PC, we don’t usually care about our program’s size • MB/GB/TB range for general purpose PCs – Ex: 1300 line .C file 50 KB  40 KB .hex file • 2MB max in PIC18’s Program ROM • For our PIC18F452  Only 32KB – See datasheet 2
  • 3. Why C over ASM? • While Assembly Language produces a much smaller .HEX file than C… – More human-readable in C • Easier to write and less time consuming – C is easier to modify and update • Don’t care about absolute ROM locations – Access to many C function libraries – C code is portable and can be used on other microcontrollers with little or no modification 3
  • 4. C Integer Data Types (Generic) 4
  • 5. C Integer Data Types (C18 Compiler) 5
  • 6. 6 C Integer Data Types (XC8 Compiler) 6
  • 7. Unsigned char (0 to 255) • PIC18 is 8-bit architecture, char type (8 bits) is the most natural choice • C compilers use signed char (-128 to +127) by default unless we put “unsigned” – char == signed char 7
  • 10. Unsigned char array (0 to 255) z = 0 PORTB = ‘0’ (in code) PORTB = 0x30 = 48 (actual) PORTB = 0b 0011 0000 (pins) 10
  • 11. Unsigned char array (0 to 255) z = 0 PORTB = ‘0’ (in code) PORTB = 0x30 = 48 (actual) PORTB = 0b 0011 0000 (pins) Direction (TRISB) 0 0 0 0 0 0 0 0 Pin Value (PORTB) 0 0 1 1 0 0 0 0 PINS 11
  • 12. Unsigned char array (0 to 255) z = 1 PORTB = ‘1’ (in code) PORTB = 0x31 = 49 (actual) PORTB = 0b 0011 0001 (pins) 12
  • 13. Unsigned char array (0 to 255) z = 6 PORTB = ‘A’ (in code) PORTB = 0x41 = 65 (actual) PORTB = 0b 0100 0001 (pins) 13
  • 14. Signed char (-128 to +127) • Still 8-bit data type but MSB is sign value 14
  • 15. Unsigned int (0 to 65,535) • PIC18 is 8-bit architecture, int type (16 bits) takes two bytes of RAM (only use when necessary) • C compilers use signed int (-32,768 to +32,767) by default unless we put “unsigned” – int == signed int 15
  • 16. Larger Integer Types (short, long, short long) 16
  • 17. Floating-Point Data Types • Can store and calculate numbers with decimals (precision) • Always signed, can’t be unsigned 2.5, 32.05898, -1.00232, .2600313, 51156.01, etc. • Further info: Text and Video Explanation 17
  • 18. Modulus • In C can use % to perform a modulus of two numbers (find the whole number remainder from a “repeated subtraction”) • 25 % 5 = 0 • 25 % 7 = 4 • 25 % 10 = 5 • 428 % 100 = 28 • 1568 % 10 = 8 18
  • 19. Casting to Prevent Data Loss ? ? 19
  • 20. Casting to Prevent Data Loss 20
  • 21. Time Delay • Want to have exact time differences or spacing between certain instructions • Three methods: – Using a simple loop (for/while) (crude) – Using PIC18 timer peripheral (later) – Built-in delay functions (reliable and accurate) 21
  • 22. Two Factors for Delay Accuracy in C 1. The crystal’s frequency (int. or ext.) – Duration of clock period for instruction cycle 2. The compiler used for the C program – In ASM, we control the exact instructions – Different compilers produce different ASM code 22
  • 23. Time Delay Example FOSC = 10 MHz = 10,000,000 cycles/sec Each instruction takes 4 clock cycles (ticks) FCY = Instruction Cycle Frequency = 10𝑀𝑀𝑀𝑀𝑀𝑀 4 = 2.5MHz = 2,500,000 Ins/sec TCY = Instruction Cycle Time = 1 / 2.5MHz = 0.0000004 sec per Ins = 0.0004 ms = 0.4 µs How many IC (instructions) fit into 1ms? 1ms / 0.0004ms = 2,500  2,500 Instruction Cycles take place in 1ms  2,500 Instructions can complete in 1ms23
  • 24. Instruction Cycle FOSC = Oscillator Frequency = 10 MHz = 10,000,000 cycles/sec Each instruction takes 4 clock cycles (ticks) FCY = Instruction Cycle Frequency = FOSC 4 = 10𝑀𝑀𝑀𝑀𝑀𝑀 4 = 2.5MHz = 2,500,000 Ins/sec TCY = Instruction Cycle Time = 1 FCY = 1 2.5MHz = 0.0000004 sec per Ins = 0.0004 ms = 0.4 µs How many IC (instructions) fit into 1ms? 1ms / 0.0004ms = 2,500  2,500 Instruction Cycles take place in 1ms  2,500 Instructions can complete in 1ms (generalizing since most instructions only take 1 Ins. Cycle) FOSC FCY 24
  • 25. Delay Functions in the XC8 Compiler 1. Include the “xc.h” header file 2. Define your crystal’s frequency • _XTAL_FREQ 3. Can now use these 2 delay functions: – __delay_us(x); //unsigned long (0 - 4294967295) – __delay_ms(x); //unsigned long (0 - 4294967295) 25
  • 26. 26
  • 27. PORT I/O Programming in C • Btye-Size Register Access – Labels still the same – PORTA – PORTD – TRISA – TRISD – INTCON • Bit-Addressable Register Access – PORTBbits.RB3 – TRISCbits.RC7 or TRISCbits.TRISC7 – INTCONbits.RBIE 27
  • 31. 31
  • 33. Header Files • Remember that certain register/variable names are not native C keywords • They are PIC-specific – PORTB, TRISA, TMR0H, PRODL, etc. • Defined and mapped in header file – Using regular data types (char, int, struct, etc.) • Regular P18Fxxx.h (device) header files – C:Program Files (x86)Microchipxc8v1.20include 33
  • 34. Header Files • Other functional headers are available – adc.h – delays.h – i2c.h – pwm.h – timers.h – usart.h • Peripheral library Header Files – C:Program Files (x86)Microchipxc8v1.20includeplib – C:Program Files (x86)Microchipxc8v1.20sourcespic18plib 34
  • 35. Logic Operations in C • Bit-Wise Operators • Bit-Wise Shift Operators – Can shift right/left by X bits Shift right >> Shift left << 35
  • 37. Binary (hex) to Decimal and ASCII Conversion • Sometimes we can’t handle multiple-digit decimals natively in C for display purposes • printf() is standard for generic C but requires more memory space than a PIC18 is willing to sacrifice • Best to build your own “custom” print or display functions in C 37
  • 38. Extract Single Decimal Digits • Want each digit of 253 (0b11111101, 0xFD) and convert to ASCII for displaying 38
  • 39. Extract Single Decimal Digits • Want each digit of 253 (0b11111101, 0xFD) and convert to ASCII for displaying 39
  • 40. Extract Single Decimal Digits • Want each digit of 253 (0b11111101, 0xFD) and convert to ASCII for displaying 40
  • 41. Extract Single Decimal Digits • Want each digit of 253 (0b11111101, 0xFD) and convert to ASCII for displaying 41
  • 42. #define Directive • Can associate labels with numbers or registers as a constant #define LED_OUTPUT PORTBbits.RB2 #define MAX_USERS 50 42
  • 43. Questions? • For PIC C Programming – Textbook Ch. 7 for more details • Start looking over Arithmetic/Logic – Textbook Ch. 5 43