Sei sulla pagina 1di 14

4

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

Test1.c {LED ON}


Test1.c {LED ON}
4

… 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.

Test1a.c Test1b.c Test1c.c

Try modify “Test1b.c” and save as “Test1d.c” when LED2 is included in the program

Software Delay {1}


Software Delay {1}
6

There are two ways to create a time delay in C18:


1. Using a simple “for” loop or “delays.h” library.
2 Using
2. U i the
th PIC18 titimers.

Two factors that can affect the accuracy of the delay:


1. Crystal frequency of PIC18 system
2. Compiler used to compile the C program.
Software Delay {2}
Software Delay {2}
7

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

Software Delay {3}


Software Delay {3}
8

… Time delay using function library (delays.h)

Name Description
Delay1TCY Delay one instruction cycle.

Delay10TCYx Delay in multiples of 10 instruction cycle.

Delay100TCYx Delay in multiples of 100 instruction cycle.

Delay1KTCYx Delay in multiples of 1000 instruction cycle.

Delay10KTCYx Delay in multiples of 10,000 instruction cycle.


Software Delay {4}
Software Delay {4}
9

… 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).

Test2.c {LED ON/OFF}


Test2.c {LED ON/OFF}
10

… 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

PIC18F On-Chip Code Address


ROM (Bytes) Range (Hex)
2220 4K 00000 – 00FFF
2410 16K 00000 – 03FFF
2M bytes

4520 32K 00000 – 07FFF


4550 32K 00000 – 07FFF
4580 32K 00000 – 07FFF
4685 96K 00000 – 17FFF
8722 128K 00000 – 1FFFF

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

1. 2M bytes of program ROM


The PlC 18 family has a maximum of________
space.

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.

4. If we have a message string with a size of over 1000 bytes, then we


use ________ (program ROM or data RAM ) to store it.
Data RAM Allocation PIC18 Familyy have a maximum of 4K
bytes of data RAM
19
1. RAM data space usage
Use without keyword rom
char mynum[] = “Hello”; //data string array
char weekdays = 7, month = 12;

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

Data RAM Allocation


2. NEAR and FAR for Data Location
20

Storage Qualifier RAM


near In access bank
far Anywhere in data RAM file register (default)

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

Data RAM Allocation Allowing 2 variables to share the same


physical address as long as they are
4. Overlay
O storage class not active at the same time
22

Program Example 4 Program Example 5


Unsigned char progA(void) Unsigned char progC(void)
{ overlay unsigned char x = 0; { overlay unsigned char x = 0;
x = x + 1; x = progD();
return x; return x;
} }

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

1. 4K bytes of data RAM.


The PlC 18 has a maximum of_________

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

4. True or false. The near storage


g qqualifier is used to pplace the variables in
access bank.
Bit-wise
Bit wise Logic Operations
25

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 ________.

5. Find the contents of PORTC after execution of the following code:


PORTC = 0;
PORTC = PORTC | 0x99;
PORTC = ~ PORTC; PORTC = 0x66

Potrebbero piacerti anche