Lecture-6-PIC Programming in C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

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
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)

10
PORTB = 0b 0011 0000 (pins)
Unsigned char array
(0 to 255)
PINS
Direction Pin Value
(TRISB) (PORTB)
0 0
0 0
0 1
0 1
0 0
0 0
0 0
0 0

z=0
PORTB = ‘0’ (in code)
PORTB = 0x30 = 48 (actual)

11
PORTB = 0b 0011 0000 (pins)
Unsigned char array
(0 to 255)

z=1
PORTB = ‘1’ (in code)
PORTB = 0x31 = 49 (actual)

12
PORTB = 0b 0011 0001 (pins)
Unsigned char array
(0 to 255)

z=6
PORTB = ‘A’ (in code)
PORTB = 0x41 = 65 (actual)

13
PORTB = 0b 0100 0001 (pins)
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𝑀𝑀𝑀𝑀𝑀𝑀
= = 2.5MHz = 2,500,000 Ins/sec
4

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 1ms 23
Instruction Cycle
FOSC = Oscillator Frequency FOSC
= 10 MHz = 10,000,000 cycles/sec
Each instruction takes 4 clock cycles (ticks)

FCY = Instruction Cycle Frequency


F 10𝑀𝑀𝑀𝑀𝑀𝑀
= OSC = = 2.5MHz = 2,500,000 Ins/sec
4 4

TCY = Instruction Cycle Time FCY


1 1
= = = 0.0000004 sec per Ins
FCY 2.5MHz
= 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 24


 2,500 Instructions can complete in 1ms (generalizing since most instructions only take 1 Ins. Cycle)
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)\Microchip\xc8\v1.20\include

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)\Microchip\xc8\v1.20\include\plib 34
– C:\Program Files (x86)\Microchip\xc8\v1.20\sources\pic18\plib
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

You might also like