30
PIC PROGRAMMING IN C DEPARTMENT OF ELECTRICAL ENGINEERING POLITEKNIK SULTAN HAJI AHMAD SHAH CHAPTER 5 – Part 1 EC501 EMBEDDED SYSTEM APPLICATIONS

Embedded system (Chapter )

Embed Size (px)

Citation preview

Page 1: Embedded system (Chapter )

PIC PROGRAMMING IN C

DEPARTMENT OF ELECTRICAL ENGINEERING

POLITEKNIK SULTAN HAJI AHMAD SHAH

CHAPTER 5 – Part 1

EC501 EMBEDDED SYSTEM APPLICATIONS

Page 2: Embedded system (Chapter )

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

Page 3: Embedded system (Chapter )

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

Microcontroll

er (Proce

ss)

Output signal

Page 4: Embedded system (Chapter )

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.

Page 5: Embedded system (Chapter )

Standard Method There is no 100% correct ways to write C program,

anyway there are guide lines to follow.

Page 6: Embedded system (Chapter )

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

Page 7: Embedded system (Chapter )

b. Include Header File• It enables the compiler to include file which

define standard keyword for PIC based on model.

• Eg.:

Standard Method

Page 8: Embedded system (Chapter )

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

Page 9: Embedded system (Chapter )

Configuration Bits Eg.: 1. Confuguration bit for PIC16F877A

__CONFIG 0Xf32 2. Configuration bit for PIC18F4550

Page 10: Embedded system (Chapter )

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

Page 11: Embedded system (Chapter )

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

Page 12: Embedded system (Chapter )

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

Page 13: Embedded system (Chapter )

Exercise Identify the following from the program above :

1. Comment

2. Include header file

3. Configuration bit

4. Function

5. Function body

Tem_str

Page 14: Embedded system (Chapter )

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

}

Page 15: Embedded system (Chapter )

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

Page 16: Embedded system (Chapter )

Radix Format - example1. 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:

c) PORTA=15;

d) PORTA=0x0F;

Page 17: Embedded system (Chapter )

Data Type Data Type is type assigned to variable to determine

the size and how the variable being interpreted. Fundamental Type

Page 18: Embedded system (Chapter )

Modified Integer Types. Qualifiers: unsigned, signed, short and long

Data Type

Page 19: Embedded system (Chapter )

Modified Floating Point Types

Data Type

Page 20: Embedded system (Chapter )

– 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

Page 21: Embedded system (Chapter )

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

Page 22: Embedded system (Chapter )

• 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

Page 23: Embedded system (Chapter )

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 outputfor (z=0;z<8;z++) {PORTB=mynum[z];}while(1); //stay here forever}

Page 24: Embedded system (Chapter )

• 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

Page 25: Embedded system (Chapter )

• 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 outputfor (z=0;z<=50000;z++) {PORTB=0x55;PORTB=0xAA;}while(1); //stay here forever}

Page 26: Embedded system (Chapter )

• 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

Page 27: Embedded system (Chapter )

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.

Page 28: Embedded system (Chapter )

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 outputfor (z=0;z<=100000;z++) {PORTB=0x55;PORTB=0xAA;}while(1); //stay here forever}

Page 29: Embedded system (Chapter )

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.

Page 30: Embedded system (Chapter )

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?