0% found this document useful (0 votes)
6 views22 pages

C3. PIC Programming in C

Chapter 3 covers programming the PIC18 microcontroller using the C language, focusing on data types, I/O operations, logic operations, and data serialization. It highlights the advantages of using C over Assembly, such as ease of writing and portability. The chapter includes various examples and objectives aimed at understanding C data types, time delays, and bit manipulation for effective programming.

Uploaded by

huyhna.24itb
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)
6 views22 pages

C3. PIC Programming in C

Chapter 3 covers programming the PIC18 microcontroller using the C language, focusing on data types, I/O operations, logic operations, and data serialization. It highlights the advantages of using C over Assembly, such as ease of writing and portability. The chapter includes various examples and objectives aimed at understanding C data types, time delays, and bit manipulation for effective programming.

Uploaded by

huyhna.24itb
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/ 22

CHAPTER 3

PIC PROGRAMMING IN C
OUTLINE
• Microcontrollers
• Overview of the PIC18 Family
•…
Objectives
• Examine C data types for the PIC18
• Code C18 programs for time delay and I/O operations
• Code C18 programs for I/O bit manipulation
• Code C18 programs for logic and arithmetic operations
• Code C18 programs for data serialization
• Understand C18 compiler RAM and ROM allocation
Why program the PIC18 in C?
• Hex file produced by the compiler will be downloaded into the arom of the
microcontroller.
• The size of hex file is the main concerns of programmers for 2 reasons:
• Microcontrollers have limited on-chip ROM.
• The code space from the PIC18 is limited to 2M.
• Writing programs in C instead of Assembly, because:
• It is easier and less time consuming to write in C than in Assembly.
• C is easier to modify and update.
• You can use code available in function libraries.
• C code is portable to other microcontrollers with little or no modification.
3.1 Data types and Time delays in C
• C data types for the PIC18
• One of the goals of C18 programmers is to create smaller hex files → a good understanding
of C data types for the C18.
• Some Data types widely Used by C18
Data Type Size in Bits Data Range/Usage
Unsigned char 8-bit 0 to 255
Char 8-bit -128 to +127
Unsigned int 16-bit 0 to 65535
Int 16-bit -32768 to +32767
Unsigned short 16-bit 0 to 65535
Short 16-bit -32768 to +32767
Unsigned short long 24-bit 0 to 1677215
Short long 24-bit -8388608 to +8388607
Unsigned long 32-bit 0 to 4294967295
long 32-bit -2147483648 to +2147483647
3.1 Data types and Time delays in C
• C data types for the PIC18
3.1 Data types and Time delays in C
• C data types for the PIC18
• Unsigned char
• An 8-bit data type that takes a value in the range of 0-255 (00-FFH)
• When setting a counter value, we should use the unsigned char instead of the signed char.
• C compilers use the signed char as the default unless we put the keyword unsigned in front of the char.
• Use the unsigned char for a string of ASCII characters.
• Must pay attention to the size of the data in declaring variables and try to use unsigned char instead of int
if possible, because using int can lead to a large-size hex file.
• Example 1: Write a C18 program to send values 00-FF to Port B.
• Example 2: Write a C18 program to send hex values for ASCII character of 0, 1, 2, 3, 4, 5, A, B, C and D
to Port B.
• Example 3: Write a C18 program toously. toggle all the bits of Port b continuously.
3.1 Data types and Time delays in C
• C data types for the PIC18
• Signed char
• An 8-bit data type that uses the most significant bit to represent the – or + value, 7 bits for the magnitude
of the signed number from -128 to +127.
• + and – represent a given quanity (temperature) → use the signed char data type
• If we do not use the keyword unsigned, the default is the signed value.
• Example 4: Write a C18 program to send values -4 to +4 to Port B.
3.1 Data types and Time delays in C
• C data types for the PIC18
• Unsigned int
• An 16-bit data type that takes a value in the range of 0-65535 (0000-FFFFH)
• Used to define 16-bit variables such as memory addresses.
• Set counter values of more than 256
• Not use the int data in PIC18 unless we have to.
• C compiler uses signed int as the default unless use keyword unsigned.
• Signed int
• An 16-bit data type that uses the most significant bit to represent the – or + value.
• 15 bits for the magnitude of number or value frone -32768 to +32767.
3.1 Data types and Time delays in C
• C data types for the PIC18
• Other data types
• Values greater than 16-bit.
• The short long value is 24 bits wide.
• The long value is 32 bits wide.
• Example 5: Write a C18 program to toggle all bits of Port B 50000 times.
• Example 6: Write a C18 program to toggle all bits of Port B 100000 times.
3.1 Data types and Time delays in C
• Time delay
• Two ways to create a time relay in C18
• Using a simple FOR loop.
• Using the PIC18 timers.
• Two ways to create a time relay in C18
• Two factors that can affect the accuracy of the time delay using a FOR loop:
• The crystal frequency connected to the OSC1-OSC2 input pins
• The compiler used to compile the C program: each compiler produces different-size hex code.
• Example 7: Write a C18 program to toggle all bits of Port B continuously with a 250 ms
delay. Assume that the system is PIC18F458 with XTAL=10 MHz.
• Example 8: Write a C18 program to toggle all bits of Port C and Port D continuously with a
250 ms .
3.2 I/O programming in C
• Byte size I/O
• Ports PORTA-PORTD are byte accessible.
• We use the PORTA-PORTD labels as defined in the C18 header file.
• Example 9: LEDs are connected to bits in Port B and Port C. Write a C18 program that
shows the count from 0 to FFh (0000 0000 to 1111 1111 in binary) on the LEDs.
• Example 10: Write a C18 program to get a byte of data from Port B, wait ½ second and then
se
• Example 11: Write a C18 program to get a byte of data from Port C. If it is less than 100,
send it to Port B; otherwise, send it to Port D.
3.2 I/O programming in C
• Bit-addressable I/O programming
• The I/O Ports of PIC 18 are bit-addressable.
• PORTxbits.Rxy : access a single bit of Portx
• x: Port A, B, C, D
• y: bit (0-7) of that port
• Ex:PORTBbits.RB7 indicates PORTB.7
• TRISBbits.TRISB7 : indicates the D7 of the TRISB.
3.3 Logic Operations in C
• One of the most important and powerful features of the C language is its ability to
perform bit manipulation.
Bit-wise logic In C Example Result
Operators
Bit-wise AND & 0x35 & 0x0F 0x05
logic OR | 0x04 | 0x68 0x6C
Operators
XOR ^ 0x54 ^ 0x78 0x2C
INVERTER ~ ~ 0x55 0xAA
Bit-wise SHIFT RIGHT >> 0x9A >> 3 0x13
shift SHIFT LEFT << 0x6 << 4 0x60
Operators
3.3 Logic Operations in C
• Example 9: Run the following program on your simulator and examine the result
3.3 Logic Operations in C
• Example 10: Write a C18 program to toggle all the bits of Port B and Port C
continuously with a 250 ms delay. Use the inverting operator.
• Example 11: Write a C18 program to toggle all the bits of Port B, Port C and Port
D continuously with a 250 ms delay. Use the XOR operator.
3.4 Data serialization in C
• Serialing data is a way of sending a byte of data one bit at a time through a single
pin of a microcontroller.
• The serial versions are becoming popular because:
• Not all devices supports connection standard such as I2C and CAN.
• Serial version take less space on a printed circuit board.
• Two ways to transfer a byte of data serially:
• Using the serial port
• To transfer data one bit a time and control the sequence of data and spaces between them.
3.4 Data serialization in C
• Example 12: Write a C18 program to send out the value 44H serially one bit at a
time via RC0. The LSB should go out first.
• Example 13: Write a C18 program to send out the value 44H serially one bit at a
time via RC0. The MSB should go out first.
• Example 14: Write a C18 program to bring in a byte of data serially one bit at a
time via the RB0 pin. Place the byte on the Port D. The LSB should come in first.
• Example 15: Write a C18 program to bring in a byte of data serially one bit at a
time via the RB0 pin. The MSB should come in first.
3.5 Program ROM Allocation in C18
Summary
•…
•…
•…
Quiz

You might also like