Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
PIC18 MICROCONTROLLER
PIC18 MICROCONTROLLER
{ LED and Button}
{…}
Mohamad Fauzi Zakaria
http://fkee.uthm.edu.my/mfauzi
Contents
2
Hardware Connection
Test1: LED ON
Software Delay
Test2: LED ON/OFF
Test2: LED ON/OFF
Test3: LED and Button
Exercise: LED, Button, and Software Delay
Hardware Connection
Hardware Connection
3
There are two output LEDs and two input buttons available in
SK40C.
OUTPUT INPUT
Write a complete program to light up LED1 when power ON.
First step: Develop a program flow‐charts.
Second step: Identify the commands involved.
Third step: Write the code
Start Program start in main routine
#include <p18f4550.h>
void main(void)
Registers
Registers {
TRISBbits.TRISB6 = 0
Configuration
Configuration TRISBbits.TRISB6 = 0; //Output
PORTBbit RB6 = 1
PORTBbits.RB6 1; //LED ON
}
LED1 ON PORTBbits.RB6 = 1
End
Variation in Programming
Variation in Programming
5
The difference style in programming:
n Flow-chart Design o Definition p Function
Put the register
Start definition at the
g g
beginning. Use subroutine or
Registers
Registers “#define LED1 function for
Configuration
Configuration PORTBbits.RB6” simplifying the
If any connection main routine.
change in the
LED1 ON future, only this “void LED1
definition is (unsigned char x)”
End altered.
Try modify “Test1b.c” and save as “Test1d.c” when LED2 is included in the program
Example
E l using
i for
f loop
l S l ti
Solution:
Write a C18 program to toggle all #include <P18F4550.h>
void msDelay(unsigned int);
the bits of Port B ports void main (void)
continuously with a 250 ins delay. {
Assume that the system is TRISB=0; //make Port B an output
while(1) //repeat forever
PlC18F4550 with XTAL = 20MHz. {
PORTB 0 55
PORTB=0x55;
msDelay(250);
PORTB=0xAA;
msDelay(250);
}
}
void msDelay(unsigned int iTime)
{
Disadvantage: unsigned int i; unsigned char j;
NEED TO for(i=0;i<iTime;i++)
VERIFY/MEASURE {
((TRIAL AND ERROR)) for (j=0;j<165; j++);
}
}
Time delay using function library (delays.h)
Name Description
Delay1TCY Delay one instruction cycle.
How to use the delays.h?
We have to know the required time delay and calculate based
on time for one instruction cycle (TCY).
i f i i l (TCY)
Example
Find the best function of delays.h for generating a 0.5s
delay if XTAL = 20MHz.
Calculation:
1 TCY = 1/(20MHz/4)=0.2us
1/(20MHz/4)=0 2us
For 0.5s = 0.5s/0.2us = 2,500,000 TCY
Therefore, we choose Delay10KTCYx(250).
Write a C18 program to toggle LED2 every 0.5s.
Start #include <p18f4550.h>
#include <delays.h>
#define LED2 PORTBbits.RB7
Registers
Registers
Configuration
Configuration TRISBbits.TRISB7= 0
void
id main(void)
i ( id)
while(1) {
LED2 ON LED2= 1 TRISBbits.TRISB7 = 0; //Output
while(1)
hil (1)
Delay 0.5s Delay10KTCYx(250) {
LED2 = 1; //LED ON
LED2 OFF LED2 = 0
LED2 OFF Delay10KTCYx(250);
LED2 = 0; //LED OFF
Delay 0.5s Delay10KTCYx(250) Delay10KTCYx(250);
}
End }
Test3.c {LED and Button}
Test3.c {LED and Button}
11
Write a C18 program to toggle LED2 every 0.5s if button SW1
is pressed. #include <p18f4550.h>
#include <delays
<delays.h>
h>
Start #define SW1 PORTBbits.RB0
#define LED2 PORTBbits.RB7
Registers
Registers
Registers
Registers TRISBbits.TRISB0
S b ts S 0=1
Configuration
Configuration TRISBbits.TRISB7 = 0 void main(void)
{
N SW1
SW1 TRISBbits TRISB0 = 1; //Input
TRISBbits.TRISB0
while(SW1==1) TRISBbits.TRISB7 = 0; //Output
pressed
while(SW1 == 1);
while(1)
( )
while(1)
LED2 Toggle LED2 = ~ LED2 {
LED2 = ~ LED2; //Toggle
Delay 0.5s
y y ( )
Delay10KTCYx(250) Delay10KTCYx(250);
y ( )
}
End }
Exercise
12
Write a complete C18 program for the following conditions:
1. When SW1 is pressed, LED1 ON for 2s
2. Wh SW2 i
When SW2 is pressed, LED2 ON for 1s
d LED2 ON f 1
3. When SW1 and SW2 are not pressed LED1 and LED2 are OFF.
& Follow the programming step and comments every important
instruction for future revision!
Program ROM Allocation
13
Program
g ROM Allocation
14
1. ROM program code space usage
Use the keyword rom
rom char mynum[] = “Hello”; //data string array
rom char weekdays = 7, month = 12;
Program Example 1
#include <P18F4520.h>
<P18F4520 h>
//ROM space for fixed (constant/static) data
rom const char mynum[]= “0123456789”;
void main(void)
{
unsigned char z;
;
TRISB = 0;
for (z=0; z<10 ; z++)
{PORTB=mynum[z];}
}
Program
g ROM Allocation
15
2. NEAR and FAR for Code Location
Storage Qualifier ROM (Data can be found anywhere)
near In program space of 0000 – FFFFH (64K bytes)
far In program space of 000000 – 1FFFFFH (2M bytes) - Default
Program Example 2
#include <P18F4520.h>
y [] “0123456789”;
near rom const char mynum[]=
void main(void)
{
unsigned char z;
TRISB = 0;
for (z=0; z<10 ; z++)
{PORTB=mynum[z];}
}
Program
g ROM Allocation
16
3. Fixed Address to Data and Code
#pragma Application
code For program contains executable instructions.
romdata For fixed data such as string and look-up tables.
Program
og a Example
a pe3
#include <P18F4520.h>
#pragma romdata mydata = 0x200 //place mydata at ROM addr 0x200
near rom const char mynum[]=
mynum[] “0123456789”;
0123456789 ;
void main(void)
{
unsigned char z;
TRISB = 0;
for (z=0; z<10 ; z++)
{PORTB=mynum[z];}
}
Program
g ROM Allocation
17
3. Fixed Address to Data and Code
Program Example 4
#include <P18F4520.h>
#pragma code main = 0x50 //place the main at ROM addr 0x50
void MSDelay(unsigned int);
void main (void)
{ unsigned char mydata[] = “HELLO”
unsigned char z;
TRISB = 0; //make Port B an output
for (z=0 ; z<5; z++)
{ PORTB = mydata[zl;
MSDelay(250); }
#pragma code MSDelay = 0x300 //place delay at ROM addr 0x300
void MSDelay(unsigned int itime)
{ unsigned int i;
unsigned char j;
f (i 0 i
for(i=0; i<itime;
i i i
i++);
)
{for(j=0;j<165;j++);}
}
Review Questions
18
2. True or false. The program (code) ROM space can be used for data
storage but the data space cannot be used for code
storage, code.
3. True or false. Using the program ROM space for data means the data
is fixed and static.
Program Example 1
#include <P18F4520.h>
void main(void)
{
unsigned char mynum[]=“0123456”; //uses RAM space
unsigned char z;
TRISB = 0;
for (z=0; z<7 ; z++)
{PORTB=mynum[z];}
}
Program Example 2
#include <P18F4520.h>
g
near unsigned char mydata[100];
y [ ] //100bytes
y space
p in the RAM
void main(void)
{
unsigned char x, z=0;
TRISB = 0;
for (x=0; x<100 ; x++)
{z--; mydata[x]= z; PORTB = z;}
}
#pragma Application
Data RAM Allocation idata For initialized data
3. Specific RAM Address udata For uninitialized data
21
Program Example 3
#include <P18F4520.h>
#
#pragma idata x = 0
0x100
100
unsigned char x = 5; //initialized data
#pragma idata y = 0x101
unsigned
i d char
h y = 9
9; //i i i li d d
//initialized data
#pragma udata z = 0x102
unsigned char z; //uninitialized data
unsigned
i d char
h mynum[]=
[] “0123456789”
“0123456789”;
void main(void)
{
TRISB = 00;
z = x + y;
PORTB = z;
}
and and
Unsigned char progB(void) Unsigned char progD(void)
{ overlay unsigned char y = 0; { overlay unsigned char y = 0;
y = y + 1; y = y + 2;
return y; return y;
} }
Comparison Program (b)
23 #include <p18f4520.h>
void main (void)
{ unsigned char mydata[]=“HELLO”
Program (a) unsigned char z;
#include <p18f4520.h> TRISB = 0;
void
id main
i ( (void)
id) for(z=0;z<5;z++);
{ TRISB = 0; {PORTB = mydata[z];}
PORTB = ‘H’; }
PORTB = ‘E’;
PORTB = ‘L’;
PORTB = ‘L’; Program (c)
PORTB = ‘O’; #include <p18f4520.h>
} void main (void)
{ rom unsigned char mydata[]=“HELLO”
unsigned char z;
TRISB = 0;
for(z=0;z<5;z++);
{PORTB = mydata[z];}
}
Review Questions
24
2. T or ffalse.
True l Th The ddata
t space can bbe usedd ffor code.
d
3. Which space
p would yyou use to declare the followingg values for C18?
(a) the number of days in a week ROM space
(b) the number of months in a year ROM space
(c) a counter for a delay RAM space
Bit-wise operators
1. AND: 0x35 & 0x0F = 0x05
2. OR: 0x04 | 0x68 = 0x6C
3. EX-OR: 0x54 ^ 0x78 = 0x2C
4. Inverter: ~0x55 = 0xAA
Bit-wise shift operation
Shift right (>>)
0x9A>>3 = 0x13 //shifting right 3 times
Shift left (<<)
0x06<<4 = 0x60 //shifting left 4 times
Bit-wise Logic
g Solution:
#include <P18F4520
<P18F4520.h>
h>
26
Operations void main(void)
{ unsigned char z;
TRISB = 0xFF; //make Port B an input
TRISD
S = 0;
0 //
//make
k Portt 0 an output
t t
Example 23 while(1) //repeat forever
Write a C18 program to read the RB0 and {z = PORTB; //read PORTB
RB1 bits and issue an ASCII character to z = z & 0x03; //mask the unused bits
Port D according to the following table: switch(z) //make decision
{
RB1 RB0 PORTD (ASCII) case (0):
{PORTD = '0';
0 ; //issue ASCII 0
0 0 ‘0’
break;}
0 1 ‘1’ case (1):
{PORTD = '1'; //issue ASCII 1
1 0 ‘2’
break;}
1 1 ‘3’ case (2):
{PORTD = '2'; //issue ASCII 2
break;}
case (3):
(3)
{PORTD = '3'; //issue ASCII 3
break;}
}}}
Review Questions
27
1. Find the content of PORTB after the following C code in each case:
a. PORTB = 0x37 & 0xCA; 0x02
b. PORTB = 0x37 | 0xCA; 0x0FF
c. PORTB = 0x37 0 CA 0xFD
0 37 ^ 0xCA; 0 FD
2. Zeros
To mask UNUSED bits we must AND them with ________.
3. One
To set high certain bits we must OR them with ________.
4. All zeros
EX-ORing a value with itself produces ________.