0% found this document useful (0 votes)
8 views

Microcontroller-Process

Introduction to 8-bit microcontroller

Uploaded by

Kent Roferos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Microcontroller-Process

Introduction to 8-bit microcontroller

Uploaded by

Kent Roferos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Microcontroller

PROCESS
Input Microcontroller Output
signal (Process) signal

• 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 a
program for PIC18F4550.
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
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
Standard Method
b. Include Header File
o It enables the compiler to include file which define
standard keyword for PIC based on model.
o Eg.:
Standard Method
c. Configuration Bits
o A macro that writes a value into the special register
that controls the core executing operations of the
microcontroller.

o Configuration bit can be set in development software


(eg.: MPLAB IDE).

o If the configuration bits are not specified correctly, the


PIC MCUs might not run the application code properly.
Configuration Bits
 Eg.:
 1. Configuration bit for PIC16F877A
___CONFIG 0Xf32
 2. Configuration bit for PIC18F4550
Standard Method
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 basis 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 (finishing running).
Basic Rules for Programming in C
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 semicolon after the
closing brace.
Exercise
 Identify the following from the program above:
1. Comment
2. Include header file
3. Configuration bit
4. Function
5. Function body
Radix Format
 Is the way a value is being presented in C language.
 Radix Format

Binary 0bnumber or 0Bnumber

Octal 0number 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:
c) PORTA = 15
d) PORTA = 0x0F;
Data Type
 Is type assigned to variable to determine the size and how
the variable being interpreted.
 Fundamental Type
Type Description Bits
char Single character 8
int Integer 16
float Single precision floating point number 32
double Double precision floating number 64
Data Type
 Modified Integer Types.
 Qualifies: unsigned, signed, short and long.
Qualified Type Min Max Bits
unsigned char 0 255 8
char, signed char -128 127 8
unsigned short int 0 65535 16
short int, singned short int -32768 32767 16
unsigned int 0 65535 16
int, signed int -32768 32767 16
unsigned long int 0 2³² - 1 32
long int, signed long int -2³¹ 2³¹ - 1 32
unsigned long long int 0 2⁶⁴ - 1 64
long long int, signed long long int -2⁶³ 2⁶³ - 1 64
Data Type
 Modified Floating Point Types

Qualifies Type Absolute Min Absolute Max Bits


float ± ~ 10 ¯⁴⁴·⁸⁵ ± ~ 10 ³⁸·⁵³ 32
double ¹ ± ~ 10 ¯⁴⁴·⁸⁵ ± ~ 10 ³⁸·⁵³ 32
long double ± ~ 10 ¯³²³·³ ± ~ 10 ³⁰⁸·³ 64
Unsigned Char
o The unsigned int is an 8-bit data type that takes a value in the range of 0 to 255 (00 –
FFH).
o C compilers use the signed char as the default unless we put the keyword unsigned in
front of the char.
o 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<=100000;z++)
{
PORTB=z;
while (1);
}
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);
}
Signed Char
o It is an 8-bit data type that uses the most significant bit
(D7 of D7-D0) to represent the – or + value.

o 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

magnitude
Sign bit
Data Type – Signed Char
Example 5.4
Write a C18 program to send values -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
}
Data Type - Unsigned Int
o Is a 16-bit data type that takes a value in the range of 0 to
65,535 (0000 – FFFFH).

o It also used to set counter value of more than 256.

o Example:

unsigned int i;
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
o 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.

o Example:
signed int i;
Or
int i;
Other Data Types
o The unsigned int is limited to value 0-65,535 (0000 –
FFFFH).

o The C18 C compiler supports both short long and long data
types, if we want values greater than 16-bit.

o 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?

You might also like