SlideShare a Scribd company logo
PIC PROGRAMMING
IN C
DEPARTMENT OF ELECTRICAL ENGINEERING
POLITEKNIK SULTAN HAJI AHMAD SHAH
CHAPTER 5 – Part 1
EC501 EMBEDDED SYSTEM APPLICATIONS
Outcomes of today’s class
 Student should be able to:
1. Identify C programming languages for
PIC18
2. Explain the structure of C program for
PIC18
3. Discuss C Data type widely use by PIC
Review
• Microcontroller process input data base on a
program (hex file) stored in microcontroller.
• Compilers produce hex files that we download
into the ROM of the microcontroller.
• In this chapter we will learn C programming
language to write program for PIC18F4550.
Input
signal
Microcontroller
(Process)
Output
signal
Why Program the PIC18 in C?
Reasons For Writing Programs In C Instead Of Assembly:
1. It is easier and less time consuming.
2. C is easier to modify and update.
3. You can use code available in function libraries.
4. C code is portable to other microcontrollers with
little or no modification.
Standard Method
 There is no 100% correct ways to write C program,
anyway there are guide lines to follow.
Standard Method
a. C Comments
2 ways to write comment:
i. Comment for multiple line:
/* Chapter 5
Title: C programming for PIC18F4550 */
ii. Comment for one line only:
// wait until SW1 press
b. Include Header File
• It enables the compiler to include file which
define standard keyword for PIC based on
model.
• Eg.:
Standard Method
c. Configuration Bits
• A macro that writes a value into the special register that
controls the core executing operations of the
microcontroller.
• Configuration bit can be set in development software
(eg.: MPLAB IDE).
• If the configuration bits are not specified correctly, the
PIC MCUs might not run the application code properly
Standard Method
Configuration Bits
 Eg.:
 1. Confuguration bit for PIC16F877A
__CONFIG 0Xf32
 2. Configuration bit for PIC18F4550
 d. Include Libraries, Functions declaration and
Global variables
 Placed:
- include other header file,
- libraries,
- to declare functions prototype
- global variables.
 This section can be leaved blank depend on
program writer.
Standard Method
 e. Function Name
 It is the basic building block in C programming.
 C program must have at least the function
main( ).
 Eg.: void main (void)
 The void means the main( ) function doesn't
return any value when it exit (finish running).
Standard Method
 f. Function Body
 Every function, including main( ), must have a
body enclosed in braces { }.
 The curly braces are to signify the block of
codes belonging to main( ).
 Do take note that there MUST NOT be a
semicolon after the closing brace.
Basic rules for programming in C
Exercise
 Identify the following from the program above :
1. Comment
2. Include header file
3. Configuration bit
4. Function
5. Function body
Tem_str
#include<xc.h>
//Configuration bits for PIC18f4550
#pragma config PLLDIV = 5 //20MHz devide by 5 to obtain 4MHz to enter PLL and become 96MHz
#pragma config CPUDIV = OSC1_PLL2 //Primary Oscillator source is 96MHz/2
#pragma config USBDIV = 2 //USB peripheral clock source from 96MHz/2
#pragma config FCMEN = ON //Fail-Safe Clock Monitor Enable bit
#pragma config IESO = ON //Internal/External
#pragma config PWRT = ON //Power Up Timer Enable bit
#pragma config BOR = OFF //Brown-out Reset Enable bit
#pragma config WDT = OFF //Watch-dog Timer Enable bit
#pragma config VREGEN = ON //Enable USB Voltage Regulator
#pragma config CCP2MX = ON //CCP2 at RC1
#pragma config PBADEN = ON //PORTB analog pin as analog input after Reset
#pragma config LPT1OSC = OFF //Timer 1 configured for high power operation
#pragma config MCLRE = ON //PCLR pin enable, RE3 input disable
#pragma config STVREN = ON //Stack overflow will cause Reset
#pragma config LVP = OFF //Low voltage programming disable
#pragma config XINST = OFF //Instruction set extension and Indexed Addressingmode enabled
#pragma config DEBUG = OFF //Debug disable, RB6 and RB7 as normal IO
#pragma config WRTB = ON //Boot Block Write Protect enable
void main(void)
{
TRISA2=0; //make pin RA2 as output (LED)
LATA2=0; //LED on RA2 "OFF"
while(1);
}
Radix Format
 Radix format is the way a value is being presented in
C language
 There are four methods to present value:
Radix Format
Binary 0bnumber or 0Bnumber
Octal Onumber or number
Decimal number
Hexadecimal 0xnumber or 0Xnumber
Radix Format - example
1. Write a statement to sent binary value 00001111 to Port A.
Solution:
PORTA= 0b00001111;
2. Convert the value in Question 1 into the following radix
format:
a) Decimal
b) Hexadecimal
Solution:
a) PORTA=15;
b) PORTA=0x0F;
Data Type
 Data Type is type assigned to variable to determine
the size and how the variable being interpreted.
 Fundamental Type
 Modified Integer Types.
 Qualifiers: unsigned, signed, short and long
Data Type
 Modified Floating Point Types
Data Type
– The unsigned char is an 8-bit data type that takes value
in range of 0-255 (00-FFH)
– C compilers use the signed char as the default unless we
put the keyword unsigned in front of the char.
– Example 5.1:
• Write a C program to send value 0-255 to Port B
Solution:
#include <P18F4550.h>
void main(void)
{
unsigned char z;
TRISB=0;
for(z=0;z<=255;z++)
PORTB=z;
while(1);
}
Unsigned Char
Data Type - Unsigned Char
• Example 5.2
Write a C program to send hex values 8D to Port B.
Solution:
#include <P18F4550.h>
unsigned char z=0x8D;
void main(void)
{
TRISB=0;
PORTB=z;
while(1);
}
• The signed char is an 8-bit data type that
uses the most significant bit (D7 of D7-D0) to
represent the – or + value.
• As a result, we have only 7 bits for the
magnitude of the signed number, giving us
value from -128 to +127.
D7 D6 D5 D4 D3 D2 D1 D0
Sign
bit magnitude
Signed Char
Data Type - Signed Char
• Example 5-4
Write a C18 program to send values of -4 to
+4 to Port B.
Solution:
#include <P18F4550.h>
void main(void)
{
char mynum[ ]={+1,-1,+2,-2,+3,-3,+4,-4};
signed char z;
TRISB=0; //make Port B an output
for (z=0;z<8;z++)
{
PORTB=mynum[z];
}
while(1); //stay here forever
• The unsigned int is a 16-bit data type that takes a value
in the range of 0 to 65,535 (0000 – FFFFH).
• It also used to set counter value of more than 256.
• Example:
unsigned int i;
Data Type - Unsigned Int
• Example 5-5
Write a C18 program to toggle all bits of Port
B 50,000 times.
Solution:
#include <P18F4550.h>
void main(void)
{
unsigned int z;
TRISB=0; //make Port B an output
for (z=0;z<=50000;z++)
{
PORTB=0x55;
PORTB=0xAA;
}
while(1); //stay here forever
}
• Signed int is a 16-bit data type that uses the most
significant bit (D15 of D15-D0) to represent the – or +
value. As a result, we have only 15bits for the magnitude
of the number, or value from -32,768 to + 32,767.
• Example:
signed int i;
Or
int i;
Signed Int
Other Data Types
• The unsigned int is limited to value 0-65,535
(0000 – FFFFH).
• The C18 C compiler supports both short long
and long data types, if we want values greater
than 16-bit.
• See Table 5.1. The short long value is 24 bits
wide, while the long value is 32 bits wide.
EXERCISE
Write a C18 program to toggle all bits of Port B
100,000 times.
Solution:
#include <P18F4550.h>
void main(void)
{
unsigned short long z;
TRISB=0; //make Port B an output
for (z=0;z<=100000;z++)
{
PORTB=0x55;
PORTB=0xAA;
}
while(1); //stay here forever
}
Review question
1. Give the reason why you choose C programming
instead of assembly?
2. Give the magnitude of unsigned char and signed
char data types.
3. If we declaring a variable for person’s age, we
should use the ___data type.
Conclusion – outcomes of the day
Students should be able to:
1. Identify C programming languages for PIC18
2. Explain the structure of C program for PIC18
3. Discuss C Data type widely use by PIC
Have you achieved the outcomes?

More Related Content

PPTX
Arm instruction set
PDF
8051 assembly programming
PDF
Embedded system (Chapter 3) io_port_programming
PPTX
555 timer ppt by vishnu
PDF
Embedded system (Chapter 2) part 2
PPT
8255 presentaion.ppt
PPTX
Lcd interfacing with microprocessor 8051
PPTX
LCD Interacing with 8051
Arm instruction set
8051 assembly programming
Embedded system (Chapter 3) io_port_programming
555 timer ppt by vishnu
Embedded system (Chapter 2) part 2
8255 presentaion.ppt
Lcd interfacing with microprocessor 8051
LCD Interacing with 8051

What's hot (20)

PPTX
I/O port programming in 8051
PPTX
Emitter Coupled Logic (ECL)
DOCX
8085 interfacing with memory chips
PDF
Arm instruction set
PPTX
Race around and master slave flip flop
PPTX
synchronous state machine design
PPTX
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PPT
Menjelaskan prinsip register
PDF
interfacing of temperature sensor LM 35 with 8051.pdf
PPTX
Data Communication Basics
PDF
Serial communication in 8085
PPTX
Interfacing external memory in 8051
PPTX
80386-1.pptx
PPTX
Decoders-Digital Electronics
PPSX
8085 Interfacing with I/O Devices or Memory
PPT
8051 ch9-950217
PPTX
Pll in lpc2148
PPTX
Encoders and decoders
PPT
Bus system using Multiplexer in COA .ppt
I/O port programming in 8051
Emitter Coupled Logic (ECL)
8085 interfacing with memory chips
Arm instruction set
Race around and master slave flip flop
synchronous state machine design
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
Menjelaskan prinsip register
interfacing of temperature sensor LM 35 with 8051.pdf
Data Communication Basics
Serial communication in 8085
Interfacing external memory in 8051
80386-1.pptx
Decoders-Digital Electronics
8085 Interfacing with I/O Devices or Memory
8051 ch9-950217
Pll in lpc2148
Encoders and decoders
Bus system using Multiplexer in COA .ppt
Ad

Viewers also liked (17)

DOC
Isi kandungan (latihan industri)
DOCX
Report latihan industri
PPTX
Embedded system (Chapter 5) part 1
PDF
Embedded system (Chapter 1)
DOCX
report latihan industri politeknik ( Bab 1 )
PDF
CMOS Topic 5 -_cmos_inverter
PDF
Introduction To Embedded Systems
PDF
CMOS Topic 4 -_the_wire
PDF
CMOS Topic 6 -_designing_combinational_logic_circuits
DOCX
Panduan menulis report akhir
PDF
CMOS Topic 7 -_design_methodology
PPT
Introduction to Embedded Systems and its Applications
PDF
Embedded system (Chapter 2) part A
PDF
CMOS Topic 3 -_the_device
DOCX
Pengakuan
PPT
CMOS Topic 2 -manufacturing_process
PDF
INTRODUCTION_TO_IC
Isi kandungan (latihan industri)
Report latihan industri
Embedded system (Chapter 5) part 1
Embedded system (Chapter 1)
report latihan industri politeknik ( Bab 1 )
CMOS Topic 5 -_cmos_inverter
Introduction To Embedded Systems
CMOS Topic 4 -_the_wire
CMOS Topic 6 -_designing_combinational_logic_circuits
Panduan menulis report akhir
CMOS Topic 7 -_design_methodology
Introduction to Embedded Systems and its Applications
Embedded system (Chapter 2) part A
CMOS Topic 3 -_the_device
Pengakuan
CMOS Topic 2 -manufacturing_process
INTRODUCTION_TO_IC
Ad

Similar to Embedded system (Chapter ) (20)

PDF
Embedded C programming based on 8051 microcontroller
PDF
Lecture-6-PIC Programming in C-good.pdf
PDF
MC_module_3_2022.pdf
PPTX
Intel 8051 Programming in C
PPTX
Microcontroller lec 3
PPT
Picmico
PPTX
Introduction to Embedded system programming using 8051
PDF
Lenguaje de Programación en C Presentacion
PDF
Embedded_C_1711824726engéiiiring_with_the_best.pdf
PDF
Embedded c
PDF
PIC Programming and Concept for P184550 Book
PDF
An introduction to_programming_the_microchip_pic_in_ccs_c
PDF
Embedded C Programming Module 5 Presentation
PPTX
8051 Programing in C.pptx this is ppt on 8051 programming
PPTX
Data Type in C Programming
PPTX
Programming 8051 with C and using Keil uVision5.pptx
PDF
Embedded-C.pdf
PDF
Introduction to Embedded C Programming.pdf
PPT
C language programming
PPT
EMBEDDED SYSTEMS 4&5
Embedded C programming based on 8051 microcontroller
Lecture-6-PIC Programming in C-good.pdf
MC_module_3_2022.pdf
Intel 8051 Programming in C
Microcontroller lec 3
Picmico
Introduction to Embedded system programming using 8051
Lenguaje de Programación en C Presentacion
Embedded_C_1711824726engéiiiring_with_the_best.pdf
Embedded c
PIC Programming and Concept for P184550 Book
An introduction to_programming_the_microchip_pic_in_ccs_c
Embedded C Programming Module 5 Presentation
8051 Programing in C.pptx this is ppt on 8051 programming
Data Type in C Programming
Programming 8051 with C and using Keil uVision5.pptx
Embedded-C.pdf
Introduction to Embedded C Programming.pdf
C language programming
EMBEDDED SYSTEMS 4&5

Embedded system (Chapter )

  • 1. PIC PROGRAMMING IN C DEPARTMENT OF ELECTRICAL ENGINEERING POLITEKNIK SULTAN HAJI AHMAD SHAH CHAPTER 5 – Part 1 EC501 EMBEDDED SYSTEM APPLICATIONS
  • 2. Outcomes of today’s class  Student should be able to: 1. Identify C programming languages for PIC18 2. Explain the structure of C program for PIC18 3. Discuss C Data type widely use by PIC
  • 3. Review • Microcontroller process input data base on a program (hex file) stored in microcontroller. • Compilers produce hex files that we download into the ROM of the microcontroller. • In this chapter we will learn C programming language to write program for PIC18F4550. Input signal Microcontroller (Process) Output signal
  • 4. Why Program the PIC18 in C? Reasons For Writing Programs In C Instead Of Assembly: 1. It is easier and less time consuming. 2. C is easier to modify and update. 3. You can use code available in function libraries. 4. C code is portable to other microcontrollers with little or no modification.
  • 5. Standard Method  There is no 100% correct ways to write C program, anyway there are guide lines to follow.
  • 6. Standard Method a. C Comments 2 ways to write comment: i. Comment for multiple line: /* Chapter 5 Title: C programming for PIC18F4550 */ ii. Comment for one line only: // wait until SW1 press
  • 7. b. Include Header File • It enables the compiler to include file which define standard keyword for PIC based on model. • Eg.: Standard Method
  • 8. c. Configuration Bits • A macro that writes a value into the special register that controls the core executing operations of the microcontroller. • Configuration bit can be set in development software (eg.: MPLAB IDE). • If the configuration bits are not specified correctly, the PIC MCUs might not run the application code properly Standard Method
  • 9. Configuration Bits  Eg.:  1. Confuguration bit for PIC16F877A __CONFIG 0Xf32  2. Configuration bit for PIC18F4550
  • 10.  d. Include Libraries, Functions declaration and Global variables  Placed: - include other header file, - libraries, - to declare functions prototype - global variables.  This section can be leaved blank depend on program writer. Standard Method
  • 11.  e. Function Name  It is the basic building block in C programming.  C program must have at least the function main( ).  Eg.: void main (void)  The void means the main( ) function doesn't return any value when it exit (finish running). Standard Method
  • 12.  f. Function Body  Every function, including main( ), must have a body enclosed in braces { }.  The curly braces are to signify the block of codes belonging to main( ).  Do take note that there MUST NOT be a semicolon after the closing brace. Basic rules for programming in C
  • 13. Exercise  Identify the following from the program above : 1. Comment 2. Include header file 3. Configuration bit 4. Function 5. Function body Tem_str
  • 14. #include<xc.h> //Configuration bits for PIC18f4550 #pragma config PLLDIV = 5 //20MHz devide by 5 to obtain 4MHz to enter PLL and become 96MHz #pragma config CPUDIV = OSC1_PLL2 //Primary Oscillator source is 96MHz/2 #pragma config USBDIV = 2 //USB peripheral clock source from 96MHz/2 #pragma config FCMEN = ON //Fail-Safe Clock Monitor Enable bit #pragma config IESO = ON //Internal/External #pragma config PWRT = ON //Power Up Timer Enable bit #pragma config BOR = OFF //Brown-out Reset Enable bit #pragma config WDT = OFF //Watch-dog Timer Enable bit #pragma config VREGEN = ON //Enable USB Voltage Regulator #pragma config CCP2MX = ON //CCP2 at RC1 #pragma config PBADEN = ON //PORTB analog pin as analog input after Reset #pragma config LPT1OSC = OFF //Timer 1 configured for high power operation #pragma config MCLRE = ON //PCLR pin enable, RE3 input disable #pragma config STVREN = ON //Stack overflow will cause Reset #pragma config LVP = OFF //Low voltage programming disable #pragma config XINST = OFF //Instruction set extension and Indexed Addressingmode enabled #pragma config DEBUG = OFF //Debug disable, RB6 and RB7 as normal IO #pragma config WRTB = ON //Boot Block Write Protect enable void main(void) { TRISA2=0; //make pin RA2 as output (LED) LATA2=0; //LED on RA2 "OFF" while(1); }
  • 15. Radix Format  Radix format is the way a value is being presented in C language  There are four methods to present value: Radix Format Binary 0bnumber or 0Bnumber Octal Onumber or number Decimal number Hexadecimal 0xnumber or 0Xnumber
  • 16. Radix Format - example 1. Write a statement to sent binary value 00001111 to Port A. Solution: PORTA= 0b00001111; 2. Convert the value in Question 1 into the following radix format: a) Decimal b) Hexadecimal Solution: a) PORTA=15; b) PORTA=0x0F;
  • 17. Data Type  Data Type is type assigned to variable to determine the size and how the variable being interpreted.  Fundamental Type
  • 18.  Modified Integer Types.  Qualifiers: unsigned, signed, short and long Data Type
  • 19.  Modified Floating Point Types Data Type
  • 20. – The unsigned char is an 8-bit data type that takes value in range of 0-255 (00-FFH) – C compilers use the signed char as the default unless we put the keyword unsigned in front of the char. – Example 5.1: • Write a C program to send value 0-255 to Port B Solution: #include <P18F4550.h> void main(void) { unsigned char z; TRISB=0; for(z=0;z<=255;z++) PORTB=z; while(1); } Unsigned Char
  • 21. Data Type - Unsigned Char • Example 5.2 Write a C program to send hex values 8D to Port B. Solution: #include <P18F4550.h> unsigned char z=0x8D; void main(void) { TRISB=0; PORTB=z; while(1); }
  • 22. • The signed char is an 8-bit data type that uses the most significant bit (D7 of D7-D0) to represent the – or + value. • As a result, we have only 7 bits for the magnitude of the signed number, giving us value from -128 to +127. D7 D6 D5 D4 D3 D2 D1 D0 Sign bit magnitude Signed Char
  • 23. Data Type - Signed Char • Example 5-4 Write a C18 program to send values of -4 to +4 to Port B. Solution: #include <P18F4550.h> void main(void) { char mynum[ ]={+1,-1,+2,-2,+3,-3,+4,-4}; signed char z; TRISB=0; //make Port B an output for (z=0;z<8;z++) { PORTB=mynum[z]; } while(1); //stay here forever
  • 24. • The unsigned int is a 16-bit data type that takes a value in the range of 0 to 65,535 (0000 – FFFFH). • It also used to set counter value of more than 256. • Example: unsigned int i; Data Type - Unsigned Int
  • 25. • Example 5-5 Write a C18 program to toggle all bits of Port B 50,000 times. Solution: #include <P18F4550.h> void main(void) { unsigned int z; TRISB=0; //make Port B an output for (z=0;z<=50000;z++) { PORTB=0x55; PORTB=0xAA; } while(1); //stay here forever }
  • 26. • Signed int is a 16-bit data type that uses the most significant bit (D15 of D15-D0) to represent the – or + value. As a result, we have only 15bits for the magnitude of the number, or value from -32,768 to + 32,767. • Example: signed int i; Or int i; Signed Int
  • 27. Other Data Types • The unsigned int is limited to value 0-65,535 (0000 – FFFFH). • The C18 C compiler supports both short long and long data types, if we want values greater than 16-bit. • See Table 5.1. The short long value is 24 bits wide, while the long value is 32 bits wide.
  • 28. EXERCISE Write a C18 program to toggle all bits of Port B 100,000 times. Solution: #include <P18F4550.h> void main(void) { unsigned short long z; TRISB=0; //make Port B an output for (z=0;z<=100000;z++) { PORTB=0x55; PORTB=0xAA; } while(1); //stay here forever }
  • 29. Review question 1. Give the reason why you choose C programming instead of assembly? 2. Give the magnitude of unsigned char and signed char data types. 3. If we declaring a variable for person’s age, we should use the ___data type.
  • 30. Conclusion – outcomes of the day Students should be able to: 1. Identify C programming languages for PIC18 2. Explain the structure of C program for PIC18 3. Discuss C Data type widely use by PIC Have you achieved the outcomes?