Microcontroller-Process
Microcontroller-Process
PROCESS
Input Microcontroller Output
signal (Process) signal
Decimal number
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
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.
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 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
2. Give the magnitude of unsigned char and signed char data types.