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

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
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
 

Recently uploaded (20)

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
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 )
 

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