Programming Pic Microcontrollers PDF
Programming Pic Microcontrollers PDF
Slide 1
James Grimbleby
Slide 2
http://www.mikroelektronika.co.yu/english/product/books/
htt
//
ik l kt ik
/
li h/ d t/b k /
PICbook/0_Uvod.htm
James Grimbleby
Slide 3
Slide 4
Slide 5
Assessment
This unit
nit will
ill be assessed b
by a m
multiple-choice
ltiple choice test
The multiple-choice
multiple choice test will last for 30 minutes
minutes, during which
20 questions must be answered
You will be permitted to bring your notebooks and the course
notes into the test
The test will be held at the end of the Autumn term
The marks from this test will contribute to the overall mark for
the module EE2A2
James Grimbleby
Slide 6
Multi-Choice
Multi
Choice Test Example
This question relates to the use of the CCS PIC C
compiler.
A variable q is declared:
long int q;
q can take
k on any value
l
in
i the
h range:
(a)
(b)
(c)
(d)
(e)
-128 to +127
0 to 255
2
-32768 to +32767
0 to 65535
-2147483648 to + 2147483647
Answer:
James Grimbleby
Slide 7
Lecture 1
PIC Architecture
James Grimbleby
Slide 8
Program and
P
d
data memory
CPU
Data
memory
Harvard Architecture:
Program
memory
James Grimbleby
Slide 9
Program
counter
Instruction
register
Stack
Counter/
ti
timers
RAM
g
file registers
Address MUX
FSR
I/O ports
g
Status register
Instruction
decode
Watchdog
Start up timer
Timing
generation
James Grimbleby
MUX
Peripherals
W register
Slide 10
Slide 11
Clock Generator
PICs use a fully static design so that any clock frequency up
to the specified maximum can be used
There are 4 possible clock configurations:
- external clock (eg crystal oscillator module)
- self-oscillating with external crystal or ceramic resonator
- external or self-oscillating with phase-locked loop
- self-oscillating with external RC
In practice the choice will normally be a compromise
between cost and clock speed or clock stability
James Grimbleby
Slide 12
Reset
A reset p
puts the PIC in a well-defined initial state so that the
processor starts executing code from the first instruction
Resets can result from:
- external reset by MCLR pulled low
- reset on power-up
- reset by watchdog timer overflow
- resett on power supply
l b
brown-outt
Reset can be used
sed as a last resort for reco
recovering
ering from some
catastrophic software event but all current data will be lost
James Grimbleby
Slide 13
James Grimbleby
Slide 14
Stack level 1
Stack level 2
..
..
..
.
Stack level 31
James Grimbleby
Slide 15
Program memory
contains the Reset and
Interrupt vectors
The PIC18F452 has 32k
(0x8000) locations of
program memory
Reset vector
0x0000
0x0008
0x0018
P
Program
memory
0x7FFF
James Grimbleby
Slide 16
GPR bank 0
GPR bank 1
GPR bank 2
GPR bank 3
GPR bank 4
GPR bank 5
Unused
SFRs
James Grimbleby
0x000
0x100
0x200
0x300
0x400
0x500
0x600
0xF80
0xFFF
Slide 17
James Grimbleby
Port A
Port B
Port C
Port D
Port E
...
..
Tris A
Tris B
Tris C
Tris D
Tris E
0xF80
0
F80
0xF81
0xF82
0xF83
0xF84
0xF92
0xF93
0xF94
0xF95
0xF96
SPBRG
...
..
Timer1L
Timer1H
...
..
Timer0L
Timer0H
...
..
Wreg
...
..
StkPtr
0 FAF
0xFAF
0xFCE
0xFCF
0xFD6
0xFD7
0xFE8
0xFFC
Slide 18
Slide 19
Slide 20
Add W and
d f:
f resultlt in
i W or f
Clear f
Decrement f
Move contents of f to f or W
James Grimbleby
Clear bit in f
Test bit in f; skip if clear
Slide 21
Branch unconditionally
Call subroutine (function)
Return from subroutine (function)
Branch if not zero
Literal instructions :
MOVLW
ADDLW
Move literal to W
Add literal
lit l tto W
Data memor
memory program memor
memory operations
operations:
TBLRD*+
James Grimbleby
post-increment
Table read with p
Slide 22
Status Register
The 8-bit status register
g
is set during
g arithmetic operations
p
N
OV
Z
DC
C
OV
DC
Slide 23
Lecture 2
CCS Compiler
James Grimbleby
Slide 24
What is C ?
In 1970 a team at Bell Labs led by
y Brian Kernighan
g
were
developing the UNIX computer operating system
They required a high-level computer language for writing
computer operating systems
Starting from an existing language called BCPL they
developed C
C was used
d tto write
it th
the nextt version
i off UNIX system
t
software
UNIX eventually became the world's first portable operating
system
James Grimbleby
Slide 25
What is C ?
C has now become a widely
y used professional
p
language
g g for
various reasons:
It has
h high
hi h llevell constructs
t t
It can handle low level activities
It produces efficient programs
It can be compiled on a wide variety of computers
The standard for C programs was originally the features set
by Brian Kernighan
Later an international standard was developed: ANSI C
(American National Standards Institute)
James Grimbleby
Slide 26
What is C++
C ?
More recently another group at AT&T led by Bjarne Stroustrup
developed C to reflect modern programming techniques
The new language was called C++
C++ has stronger type checking and supports object-oriented
programming
C++ may be considered in several ways.:
An extension of C
A "data
data abstraction
abstraction" improvement on C
A base for "object oriented" programming
James Grimbleby
Slide 27
Slide 28
Slide 29
PC with
CCS compiler
PIC
USB
ICD2
debugger
PIC system
Slide 30
James Grimbleby
Slide 31
#inline
#priority
Slide 32
1 bit
8 bit
16 bit
32 bit
8 bit
32 bit
0 or 1
0 to 255
0 to 65535
0 tto 4294967295
0 to 255
38 to 310+38
38
310-38
Slide 33
1 bit
8 bit
16 bit
32 bit
8 bit
32 bit
0 or 1
-128 to +127
-32768 to +32767
-2147M
2147M tto +2147M
2147M
0 to 255
38 to 310+38
38
310-38
Slide 34
Constants
Constants can be specified in either decimal, octal,
hexadecimal or binary, or as a special character:
123
0123
0x123
0b010010
Decimal
Octal
Hex
Binary
'x'
\010
'\010'
'\0xA5
Character
Octal character
Hex character
James Grimbleby
'\n'
\n
'\r'
'\t'
\t
'\b'
'\f'
\f
'\a'
\v
'\v'
'\?'
'\''
\
'\"'
'\\'
Line Feed
Return Feed
TAB
Backspace
Form Feed
Bell
Vertical Space
Question Mark
Single
S
g e Quote
Double Quote
A Single
g Backslash
Slide 35
Slide 36
Multi-Precision
Multi
Precision Operations
It is often necessary to process data words that are larger
than can be operated on by a single instruction
PIC instructions only operate on 8-bit words
Multi-precision arithmetic uses a sequence of basic
instructions on existing data types
In CCS C the long int (16 bit) and int32 (32 bit) types are
processed using multi-precision arithmetic
This is much more expensive in time and code size than
single instructions
James Grimbleby
Slide 37
Multi-Precision
Multi
Precision Operations
Ams
Als
+=
Bms
Bls
Als += Bls
Overflow?
Yes
Ams ++
No
Ams += Bms
James Grimbleby
Slide 38
Multi-Precision
Multi
Precision Operations
16-bit addition using 8-bit operations:
Ams
Als
+=
Bms
Bls
MOVF Bls, W
ADDWF Als
MOVF Bms
Bms, W
ADDWFC Ams
James Grimbleby
Slide 39
Multi-Precision
Multi
Precision Operations
32-bit addition using 8-bit operations:
Ams
A2
A1
Als
+=
Bms
B2
B1
Bls
MOVF Bls, W
ADDWF Als
MOVF B1, W
ADDWFC A1
MOVF B2, W
ADDWFC A2
MOVF Bms
Bms, W
ADDWFC Ams
James Grimbleby
Slide 40
Reference Parameters
CCS C provides C++
C like reference parameters to functions:
Traditional C:
CCS C ((C++):
)
int j = 5, k = 8;
swap(&j,
(&j &k);
&k)
int j = 5, k = 8;
swap(j,
(j k);
k)
James Grimbleby
Slide 41
Built-In
Built
In Functions
RS-232 I/O:
Discrete I/O:
getc()
putc()
fgetc()
gets()
puts()
f t ()
fgets()
fputc()
fputs()
printf()
kbhit()
p
()
fprintf()
set_uart_speed()
perror()
assert()
getchar()
putchar()
setup uart()
setup_uart()
read_bank()
setup_spi()
spi read()
spi_read()
spi_write()
spi_data_is_in()
output_low()
output_high()
output float()
output_float()
output_bit()
input()
output_X()
t t ()
output_toggle()
input_state()
input_X()
port_b_pullups()
_
_ ()
set_tris_X()
James Grimbleby
Slide 42
Built-In
Built
In Functions
Parallel Slave I/O:
I2C I/O
Processor control:
setup_psp()
psp_input_full()
psp_output_full()
psp_overflow()
i2c_start()
i2c_stop()
i2C_read
i2c_write()
i2c poll()
i2c_poll()
sleep()
reset_cpu()
restart_cause()
disable_interrupts()
enable interrupts()
enable_interrupts()
ext_int_edge()
read_bank()
write bank()
write_bank()
label_address()
goto_address()
getenv()
t
()
clear_interrupts
setup_oscillator()
James Grimbleby
Slide 43
Built-In
Built
In Functions
Bit/Byte Manipulation:
Standard C Math:
shift_right()
shift_left()
rotate right()
rotate_right()
rotate_left()
bit_clear()
bit
bit_set()
t()
bit_test()
swap()
make8()
make16()
()
make32()
abs()
acos()
asin()
atan()
ceil()
cos()
()
exp()
floor()
labs()
sinh()
g()
log()
log10()
pow()
sin()
cosh()
tanh()
James Grimbleby
fabs()
fmod()
atan2()
frexp()
ldexp()
df()
modf()
sqrt()
tan()
div()
ldiv()
Slide 44
Built-In
Built
In Functions
Standard C Char:
atoi()
atoi32()
atol()
atof()
tolower()
toupper()
isalnum()
isalpha()
i
isamoung()
()
isdigit()
islower()
isspace()
isupper()
g ()
isxdigit()
strlen()
strcpy()
strncpy()
James Grimbleby
strcmp()
stricmp()
strncmp()
strcat()
strstr()
strchr()
strrchr()
isgraph()
i
iscntrl()
t l()
strtok()
strspn()
strcspn()
strpbrk()
strlwr()
()
sprintf()
isprint()
strtod()
strtol()
strtoul()
strncat()
strcoll()
strxfrm()
Slide 45
Built-In
Built
In Functions
A/D Conversion:
Timers:
Standard C memory:
setup_vref()
setup_adc_ports()
d
()
setup_adc()
set_adc_channel()
read_adc()
setup_timer_X()
set_timer_X()
i
()
get_timer_X()
p
setup_counters()
setup_wdt()
restart_wdt()
memset()
memcpy()
()
offsetof()
offsetofbit()
malloc()
calloc()
free()
realloc()
memmove()
memcmp()
memchr()
Analog Compare:
setup_comparator()
James Grimbleby
Slide 46
Built-In
Built
In Functions
Capture/Compare/PWM:
Internal EEPROM:
setup_ccpX()
set pwmX duty()
set_pwmX_duty()
setup_power_pwm()
setup_power_pwm_pins()
set power pwmx duty()
set_power_pwmx_duty()
set_power_pwm_override()
read_eeprom()
write eeprom()
write_eeprom()
read_program_eeprom()
write_program_eeprom()
read calibration()
read_calibration()
write_program_memory()
read_program_memory()
write_external_memory()
i
l
()
erase_program_memory()
setup_external_memory()
Delays:
delay_us()
delay_ms()
delay cycles()
delay_cycles()
Standard C Special:
rand()
srand()
James Grimbleby
Slide 47
Slide 48
PIN_A0 31744
PIN
PIN_A1
A1 31745
. . . .
PIN_B0 31752
PIN
PIN_B1
B1 31753
. . . .
T1_DISABLED
T1
T1_INTERNAL
INTERNAL
T1_EXTERNAL
T1_EXTERNAL_SYNC
. . . .
CCP_OFF
CCP_CAPTURE_FE
CCP_CAPTURE_RE
CCP_CAPTURE_DIV_4
. . . .
0
0x85
0x87
0x83
0
4
5
6
Slide 49
Fuses
CCS C p
provides a fuse directive:
#fuses HS,NOWDT,NOBROWNOUT,NOPROTECT,PUT
Slide 50
Delays
CCS C p
provides functions for g
generating
g delays:
y
delay_us()
delay ms()
delay_ms()
Slide 51
Slide 52
Lecture 3
Data Ports
James Grimbleby
Slide 53
Data Ports
Simplified
p
diagram
g
representing
p
g a single
g data I/O p
pin of one
of the ports A-E:
Data bus
D Q
I/O pin
Data write
D Q
Tri-state
s a e write
e
Data read
James Grimbleby
Slide 54
Data Ports
Data I/O p
port functions:
Data write - this latches the data written to the pin which
should be configured as an output
Tri-state write - this latches the data direction for the pin
(0 = output, 1 = input)
Data read - this reads the current value of the pin which
should
h ld b
be configured
fi
d as an iinputt
Each data port (A-E)
(A E) consists of a n
number
mber of pins
pins, each of
which can individually be configured as an input or output
James Grimbleby
Slide 55
Hardware Access in C
Memory-mapped hardware is traditionally accessed in C using
pointers
If the hardware is byte (8-bit) organised then char or int (PIC)
pointers are used
Example:
a pe a
an 8
8-bit
b t input
put po
portt memory-mapped
e o y apped to location
ocat o
0xF81:
#d fi
#define
portb
tb (int
(i t *) 0xF81
0 F81
Slide 56
Hardware Access in C
Th portt is
The
i accessed
d by
b the
th use off the
th indirection
i di ti operator
t **:
int p;
p = *portb;
Slide 57
Slide 58
void main()
()
{
int q = 0x0F;
set_portb_output();
t
tb
t t()
for (;;) {
write_portb(q
_p
(q = ~q);
q);
delay_ms(100);
}
}
Although
Alth
h thi
this code
d iis llonger th
than th
the previous
i
example
l it iis
better structured
James Grimbleby
Slide 59
Slide 60
Slide 61
Standard I/O
#use standard
standard_io(port)
io(port) affects how the compiler will generate
code for input and output instructions that follow
This directive takes effect until another #use xxx_io directive
is encountered
The
e sta
standard
da d method
et od o
of I/O
/O will cause tthe
e co
compiler
p e to
generate code to set the direction register for each I/O
operation
p
Standard_io is the default I/O method for all ports.
Examples: #use standard_io(A)
James Grimbleby
Slide 62
Fast I/O
#use fast
fast_io(port)
io(port) affects how the compiler will generate code
for input and output instructions that follow
This directive takes effect until another #use xxxx_io directive
is encountered
The
e fast
ast method
et od of
o doing
do g I/O
/O will cause tthe
e co
compiler
p e to
perform I/O without programming of the direction register
The user must ensure the direction register is set correctly via
set_tris_X() .
Example: #use fast_io(A)
James Grimbleby
Slide 63
Fixed I/O
#use fixed
fixed_io(port)
io(port) affects how the compiler will generate code
for input and output instructions that follow
This directive takes effect until another #use xxx_io
directive is encountered
The fixed method of I/O will cause the compiler to generate
code to set the direction register for each I/O operation
The pins are programmed according to the information in this
directive (not the operations actually performed)
Examples: #use fixed_io(a_outputs=PIN_A2,PIN_A3)
James Grimbleby
Slide 64
Slide 65
Standard I/O
#use standard
standard_io(b)
io(b)
void main()
{
int q;
for (q = 0b00000001;; q ^
^= 0b00000101) {
output_b(q);
delay_ms(100);
y
}
}
output_b(q);
CLRF 0xf93
MOVFF 0x6, 0xf8a
James Grimbleby
Set DDR
Write port
Slide 66
Fast I/O
#use fast_io(b)
fast io(b)
void main()
{
int q;
set tris b(0b11111010);
set_tris_b(0b11111010);
for (q = 0b00000001;; q ^= 0b00000101) {
output_b(q);
p
q
delay_ms(100);
}
}
output_b(q);
MOVFF 0x6, 0xf8a
James Grimbleby
Write port
Slide 67
Fixed I/O
#use fixed
fixed_io(b_outputs=pin_b2,pin_b0)
io(b outputs pin b2 pin b0)
void main()
{
int q;
for (q = 0b00000001;; q ^
^= 0b00000101) {
output_b(q);
delay_ms(100);
y
}
}
output_b(q);
MOVLW 0xfa
MOVWF 0xf93
MOVFF 0x6, 0xf8a
James Grimbleby
Set DDR
Write port
Slide 68
Slide 69
Standard I/O
#use standard_io(b)
void main()
{
boolean q;
for (q = false;; q = !q) {
if (q)
output_high(pin_b2);
else
output_low(pin_b2);
delay_ms(100);
}
}
output_high(pin_b2);
output
high(pin b2);
BCF 0xf93, 0x2
BSF 0xf8a,
, 0x2
James Grimbleby
S t DDR
Set
Write pin
Slide 70
Fast I/O
#use fast_io(b)
void main()
{
boolean q;
set_tris_b(0b11111010);
for (q = false;; q = !q) {
if (q)
output_high(pin_b2);
else
output_low(pin_b2);
delay ms(100);
delay_ms(100);
}
}
output_high(pin_b2);
BSF 0xf8a, 0x2
James Grimbleby
Write pin
Slide 71
Fixed I/O
#use fixed_io(b_outputs=pin_b2,pin_b0)
void main()
{
boolean q;
for (q = false;; q = !q) {
if (q)
output_high(pin_b2);
else
output low(pin b2);
output_low(pin_b2);
delay_ms(100);
}
}
p
g (p
);
output_high(pin_b2);
MOVLW 0xfa
MOVWF 0xf93
BSF 0xf8a,
0xf8a 0x2
James Grimbleby
S t DDR
Set
Write pin
Slide 72
output_toggle(pin_b2);
output
toggle(pin b2);
BTG 0xf8a, 0x2
James Grimbleby
Toggle pin
Slide 73
Pull-ups
Pull
ups
Some data p
ports have optional
p
internal weak p
pull-ups
p which
pull the I/O lines high by default
A switch used as input can pull the line low (against the weak
pull-ups) and no further hardware is required
These are only available on ports A and B
The commands to activate the pull-ups are:
port_a_pullups(value)
port_b_pullups(value)
Slide 74
Lecture 4
Timer/Counter/PWM
James Grimbleby
Slide 75
Timer/Counters
The PIC 18F452 has 4 timer/counters: Timer0, Timer1,
Timer2, Timer3
Timer 0:
Ti
0
Timer 1:
Timer 2:
Timer 3:
8 or 16
16-bit
bit ((selectable)
l t bl )
16-bit
8 bit
8-bit
16-bit
James Grimbleby
Slide 76
Timer/Counters
Fosc/4
0
0
1
I/O pin
Pre-scaler
T0SE
T0CS
Timer/
counter
PSA
D-bus
Slide 77
TMR0ON
T08BIT
T0CS
T0SE
PSA
T0PS0-2
T0SE
PSA
Slide 78
Counters
Program
g
to count p
pulses on external input
p to timer/counter 0:
#define t0con (int *) 0xFD5
#define tmr0l (int *) 0xFD6
void main()
{
*t0con = 0xE0;
*tmr0l = 0;
lcd_init();
for (;;) {
printf(lcd_putc, "\f%d", *tmr0l);
delay_ms(200);
}
}
James Grimbleby
Slide 79
Counters
Fortunatelyy it is not necessary
y to manipulate
p
the registers
g
directly because special functions are provided in CCS C:
setup_timer_0(mode)
setup
timer 0(mode)
setup_timer_1(mode)
. . . . . . . . . .
where mode depends on the timer, but for timer 0 can be:
RTCC INTERNAL RTCC_EXT_L_TO_H
RTCC_INTERNAL,
RTCC EXT L TO H or RTCC_EXT_H_TO_L
RTCC EXT H TO L
RTCC_DIV_2, RTCC_DIV_4, RTCC_DIV_8, RTCC_DIV_16,
RTCC DIV 32 RTCC_DIV_64,
RTCC_DIV_32,
RTCC DIV 64 RTCC_DIV_128
RTCC DIV 128 or RTCC_DIV_256
RTCC DIV 256
RTCC_OFF, RTCC_8_BIT
One constant may be used from each group ORed together with
the | operator
James Grimbleby
Slide 80
Counters
To set the counter:
set_timer0(value)
set_timer1(value)
(
)
. . . . . . . . .
Slide 81
Counters
Program to count pulses on external input to timer/counter 0:
void main()
{
setup_timer_0(RTCC_EXT_L_TO_H | RTCC_8_BIT);
set_timer0(0);
lcd init();
lcd_init();
for (;;) {
printf(lcd_putc,
p
(
p
, "\f%d",
\
, (
(int)
) get_timer0());
g
());
delay_ms(200);
}
}
James Grimbleby
Slide 82
Timer Interrupts
Microprocessors
p
normally
y execute code sequentially
q
y
Sometimes execution must be suspended temporarily to
perform some other task
In PICs this happens as the result of interrupt requests
An interrupt is raised when a particular condition occurs:
-
timer/counter overflow
change in the state of an input line
data received on the serial bus
completion of an analogue-to-digital conversion
power supply
p
pp y brown-out
James Grimbleby
Slide 83
Timer Interrupts
An interrupt can be generated each time a counter/timer
overflows
This generates interrupts at a frequency determined by the
clock speed
p
and the timer/counter configuration
g
The clock, divided by 4 and pre-scaled, is applied to the
counter which counts to 2n-1 before overflowing back to 0
clock
l k frequency
f
interrupt rate =
4 65536 prescaler
(This assumes that a 16-bit timer/counter is being used)
James Grimbleby
Slide 84
Timer Interrupts
Suppose that
S
th t the
th clock
l k frequency
f
is
i 20 M
Mz and
d a pre-scaler
l
ratio of 16 is used:
20000000
interrupt rate =
4 65536 16
20000000
=
4194304
= 4.768 Hz
Note that only a limited number of discrete interrupt rates are
possible with a given clock frequency
James Grimbleby
Slide 85
Timer Interrupts
CCS C provides the following functions to configure interrupts:
disable_interrupts()
enable_interrupts()
clear_interrupt()
#INT_TIMER0
#INT
TIMER0
Counter/timer 0 oflo
#INT_AD
A/D conversion complete
#INT RB
#INT_RB
Change on B port
#INT_SSP
I2C Activity
. . . . . . . . . . . . . . . . .
Slide 86
Timer Interrupts
#INT_TIMER0
#
0
void timer_irq()
{
output_toggle(pin_b1);
}
void main()
{
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_16);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
bl i
(
)
for (;;) {
}
}
James Grimbleby
Slide 87
Timer Interrupts
Starting with a 20 MHz clock there is no power-of-2 prescaler ratio that gives an interrupt rate close to 1 Hz
A pre-scaler
l ratio
ti off 128 gives:
i
20000000
interrupt rate =
4 65536 128
= 0.59605
Interrupts occur when the counter overflows from 65535 to 0
If the counter is pre-loaded with a value n when an interrupt
occurs then the counter only has to count from n to 65535
James Grimbleby
Slide 88
Timer Interrupts
Pre-loading with a value n:
clock frequency
p rate =
interrupt
4 (65536 n ) prescaler
l
To generate
T
t a 1 Hz
H iinterrupt
t
t with
ith a clock
l k ffrequency off 20 MH
MHz
and a pre-scaler ratio of 128:
20000000
1=
4 (65536 n ) 128
20000000
n = 65536
4 128
= 26474
James Grimbleby
Slide 89
1 Hz Timer Interrupts
#INT_TIMER0
#
0
void timer_irq()
{
set_timer0(26474);
output_toggle(pin_b1);
}
void main()
{
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_128);
enable_interrupts(INT_TIMER0);
bl i
(
0)
enable_interrupts(GLOBAL);
for (;;) {
}
}
James Grimbleby
Slide 90
Pulse-Width
Pulse
Width Modulation
Pulse-width modulation (PWM) can be used to create an n-bit
digital-to-analogue converter (DAC)
A rectangular
t
l wave with
ith a given
i
mark-space
k
ratio
ti (d
(duty
t cycle)
l ) iis
generated and this is applied to a 1-bit DAC
DACs of this type have only a limited bandwidth because of the
need to filter out the rectangular wave
wave.
Typical applications are in dc motor control
control, brightness control
of lights and in dc-dc converters
The PIC18F452 has a PWM generator that make use of
counter/timer 2
James Grimbleby
Slide 91
Pulse-Width
Pulse
Width Modulation
Simplified
p
diagram
g
of PWM g
generator:
Duty-cycle
Duty
cycle register
Comparator
Counter/timer
R Q
reset
RC2
S
Comparator
P i d register
Period
i t
D-bus
James Grimbleby
Slide 92
Pulse-Width
Pulse
Width Modulation
PWM waveform:
clock frequency
PWM frequency =
4 prescaler (1 + period register )
For example, 20 MHz clock, prescaler = 1, period register =
255:
255
20000000
PWM frequency =
= 19.53 kHz
4 1 (1 + 255)
James Grimbleby
Slide 93
Pulse-Width
Pulse
Width Modulation
CCS C provides the following functions to control the PWM
generator:
setup_ccp1(CCP_PWM)
(
)
set_pwm1_duty(q)
sets
t PWM mode
d
sets duty-cycle register to q
Note that q should not exceed the value of the period register
It is also necessary to configure counter/timer 2:
setup timer 2(pre scaler period,
setup_timer_2(pre-scaler,
period 1);
Slide 94
Pulse-Width
Pulse
Width Modulation
#define period 100
void main()
{
int q;
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_4,period,1);
for(;;) {
if (++q >= period)
q = 0;
set_pwm1_duty(q);
delay_ms(100);
}
}
20000000
PWM frequency =
= 12.38 kHz
4 4 (1 + 100)
James Grimbleby
Slide 95
Lecture 5
LCD,, RS232,, ADC,,
2
I C, EEPROM
James Grimbleby
Slide 96
#define
#define
#define
#define
d4
d5
d6
d7
pin_d0
pin_d1
pin d2
pin_d2
pin_d3
R ti
Routines
tto d
drive
i th
the LCD are available
il bl iin a fil
file lcd.c:
l d
#include "lcd.c"
James Grimbleby
Slide 97
Slide 98
Slide 99
s
d
Ld
X
LX
g
w
String or character
Signed int
Long signed int
Hex int (upper case)
Hex long int (upper case)
Float (rounded decimal)
Int with decimal p
point
Slide 100
Slide 101
RS232
The PIC18F452 has a built-in Universal Synchronous
Asynchronous Receiver Transmitter (USART)
This allows it to communicate using the RS232, RS422 and
RS485 protocols
The
e 5 V logic-level
og c e e receive
ece e a
and
d ttransmit
a s t ssignals
g aso
of tthe
e PIC
Ca
are
e
converted to RS232 levels by a MAX232 device
Baud rates are generated by dividing down the system clock
The USART receive and transmit pins are c7 and c6
respectively
James Grimbleby
Slide 102
RS232
CCS C p
provides the following
g functions to control RS2323
communications:
getc()
kbhit()
putc(char)
printf(form,..)
Slide 103
RS232
#use rs232(baud=38400,
, xmit=PIN_C6,
, rcv=PIN_C7,
,
parity=n, bits=8)
void main()
{
float p;
lcd_init();
for (;;) {
p = 5.0 * read_adc() / 1024.0;
printf("\n\rVoltage
p
( \ \
g = %01.2fV",
, p);
if (kbhit())
printf(lcd_putc, "%c", fgetc());
delay ms(100);
delay_ms(100);
}
}
James Grimbleby
Slide 104
Analogue-to-Digital
Analogue
to Digital Converter
The PIC18F452 has a single 10-bit successive-approximation
ADC with up
p to 8 multiplexed
p
analogue
g inputs
p
The reference voltage
g Vref is software selectable to be either
the supply rail or the analogue input AN3
Analogue inputs should have a source resistance of less than
2.5 k to allow for charging of the sample-hold capacitor
Conversion takes 11 cycles of the ADC clock which can be
either a RC oscillator (2-6 S) or the pre-scaled system clock
James Grimbleby
Slide 105
Analogue-to-Digital
Analogue
to Digital Converter
AN7
AN6
Vin
AN5
AN4
AN3
AN2
VDD
10-bit
ADC
Vref
AN1
AN0
James Grimbleby
Db
D-bus
Slide 106
Analogue-to-Digital
Analogue
to Digital Converter
CCS C provides
id the
th following
f ll i functions
f
ti
tto control
t l th
the ADC
ADC:
setup_adc(mode)
setup_adc_ports(value)
set_adc_channel(channel)
read_adc()
where
h
xx can b
be 8 or 10 ((when
h sett tto 8 th
the ADC will
ill return
t
th
the
most significant byte)
James Grimbleby
Slide 107
Analogue-to-Digital
Analogue
to Digital Converter
#device ADC=10
void main()
{
long int q;
float p;
p
setup_adc(ADC_CLOCK_DIV_64);
setup_adc_ports(AN0);
set_adc_channel(0);
lcd init();
lcd_init();
for (;;) {
q = read_adc();
p = 5
5.0
0 * q / 1024.0;
1024 0
printf(lcd_putc, "\fADC = %4ld", q);
printf(lcd_putc,
p
(
_p
, "\nVoltage
\
g = %01.2fV",
, p);
delay_ms(100);
}
}
James Grimbleby
Slide 108
EEPROM
The PIC18F452 has 256 byte of internal data eeprom
EEPROM is not directly mapped to the data space but is
accessed indirectly through the SFR: EEADR
This memory is non-volatile and can be used to store, for
example,
e
a p e, setup pa
parameters
a ete s
CCS C p
provides the following
g functions to read and write to
the EEPROM:
read_eeprom(address)
read
eeprom(address)
write_eeprom(address, value)
James Grimbleby
Slide 109
Inter-Integrated
Inter
Integrated Circuit (I2C) Bus
The PIC18F452 has a Master Synchronous Serial Port (MSSP)
which can operate in either SPI or I2C mode
SPI is
i a synchronous
h
serial
i l protocol
t
l th
thatt uses 3 wires:
i
SDO
SDO,
SDI and SCK
I2C is a synchronous serial protocol that uses 2 wires: SDA and
SCL
The PicDem2 board used in the PIC laboratory has 2 devices
connected to the I2C bus:
- a TC74 digital thermometer with I2C address 0x9A
- a 24LC256 EEPROM (32 kbytes) with I2C address 0xA0
James Grimbleby
Slide 110
Inter-Integrated
Inter
Integrated Circuit (I2C) Bus
CCS C provides the following functions to control I2C
communications:
i2c_start()
_
()
i2c_write(data)
i2c_read()
i2c_stop()
This directive also assigns the SDA and SCL pins used for the
I2C interface
James Grimbleby
Slide 111
24LC256 EEPROM
The Microchip Technology 24LC256/ is a 32K x 8 (256 Kbit)
serial EEPROM
It has
h been
b
developed
d
l
d ffor advanced,
d
d llow-power applications
li ti
such as personal communications or data acquisition
This device is capable of operation across a broad voltage
range (1.8V
(1 8V to 5
5.5V)
5V)
Functional address lines allow up to eight devices on
the same bus, for up to 2 Mbit address space
It is available in the standard 8-pin plastic DIP,SOIC, TSSOP,
MSOP and DFN packages.
packages
James Grimbleby
Slide 112
24LC256 EEPROM
To p
perform a read operation
p
the master g
generates a Start with
R/W=0 and sends the word address (MS byte first)
Then the master generates a Start with R/W=1 and reads the
data
Finally the master generates a Stop
James Grimbleby
Slide 113
24LC256 EEPROM
#use i2c(master, sda=pin
sda pin_c4,
c4, scl
scl=pin
pin_c3)
c3)
#define eeprom_addr 0xa0
int read_ext_eeprom(long
i
d
(l
int
i
i)
{
int q;
i2c_start();
i2c_write(eeprom_addr & 0xfe);
i2
i2c_write(i
i (i >> 8);
8)
i2c_write(i & 0xff);
i2c start();
i2c_start();
i2c_write(eeprom_addr | 0x01);
q = i2c_read(0);
i2
i2c_stop();
()
return q;
}
James Grimbleby
Slide 114
24LC256 EEPROM
To p
perform a write operation
p
the master g
generates a Start with
R/W=0 and sends the word address (MS byte first) and data
Then the master generates a Stop
To prevent further writes while the device is busy the master
should wait for acknowledge (ack=0) before proceeding
James Grimbleby
Slide 115
24LC256 EEPROM
boolean busy()
{
boolean ack;
i2c_start();
ack
k = i2c_write(eeprom_addr
i
i (
dd & 0xfe);
f )
i2c_stop();
return ack;
;
}
void write_ext_eeprom(long int i, int d)
{
i2c_start();
i2c_write(eeprom_addr
_
( p
_
& 0xfe);
);
i2c_write(i >> 8);
i2c_write(i & 0xff);
i2c write(d);
i2c_write(d);
i2c_stop();
while (busy());
}
James Grimbleby
Slide 116
James Grimbleby
Slide 117