2015_16 - lecture for rts laboratory 2 (1)
TRANSCRIPT
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
1/34
RTS Laboratory 2Getting to grips with devices, data sheets and
graphics (as an example of a device library)
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
2/34
Looking at PICs (in general) Getting a working system
Todays material
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
3/34
PICs (yo will remember) range in cost, si!e,speed and complexity "o pick the chip best sited to the task in hand
#nce yo have decided on yor device thepractical work can begin It may be that yo start with a development board
$nd then move to a bespoke board
#r design the board based on yor analysis of the
task it will be addressing
What does a pic look like
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
4/34
% pins & PIC'(L)'*+' -ote.
/ome pins have mltiple labels
Some examples of PICs
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
5/34
To slightly(! more complexo"es
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
6/34
0here will always be a few common things Power (1dd and 1ss)
Inpt#pt ports $s the chips get 2bigger3 yo get more
4i5erent chips will have di5erent devices integratedinto them It is p to yo, the programmer, to specify what yo
need the pins to do
$ 2reset3 6 7CL8 "o may sacri9ce this is the pin dobles p for a
fnction yo re:ire more
#o$e%er&
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
7/34
We ca" ha%e circ'its as simpleas
http.circit&!one;comediy
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
8/34
"or board is a little more complex with anmber of peripherals bilt on the PC@ 0ri&axial $naloge $ccelerometer
/peaker
#LA4 4isplay ('+x? pixels)
$4C
0o make se of these we need to doknow afew things Dnow what is connected to PI-s on the chip
Con9gre the integrated device(s) pins
o'r PIC board
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
9/34
Ehen working with development boardssch as the ones yo have it is a good ideato look at the schematics for the kit 0his will allow s to see what is bilt in
Fow things are connected
"o can 9nd the schematics in the 2sersgide3 (on moodle) Let s take a look at them and explain how they
describe the board
I"%estigati"g yo'r board
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
10/34
The )o%er%ie$
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
11/34
The Peripherals
0hese are links backto the processor
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
12/34
The a'dio
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
13/34
sefl as we will be writing code to monitor themH
-ote the names on the
chip 8C 8$% #n the switches note
the line over the name
&J negative logic
*"d o"e more + thes$itches
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
14/34
Consider the switches, they are linked to 8C (/witch
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
15/34
0he 9rst task is to indicate how the pin is towork (will it be an inpt or an otpt
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
16/34
Consider we have P#80$ (an % bit port) andwe wish 8$>,', to be an inpt
$ll other pins (8$'&8$+) to be otpt
Ee create a the binary representation as
/o in binary we need to set the 08I/$ as >b>>>>>''' or >x>+ or +
This is best do"e byexample
R*. R*/ R*0 R*1 R* R*2 R*3 R*4
> > > > > ' ' '
'% ? '? % '
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
17/34
#ne wold set the register as movlw b3>>>>'''3
movwf 08I/$
-ote. Ee wold not need to de9ne 208I/$3 as the
selection of device wold provide this to thecompiler
0he good news Ee are not sing assemblerH
If 'si"g assembler ($e are"ot!
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
18/34
0he xc'? compiler 2knows3 abot thevarios devices and pins on the chip via theheader 9le yo inclde (the2devicex>+N
08I/$ M >b>>>>'''N
So ho$ do I do this i" C
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
19/34
Ee can make se of the portH0he (basic) ports act like memory
$ vale we write to them 2latches3 ntil wechange it
Ee see the vale presented across the (inpt)pins as a vale
/o, if we had set 08I/$ M >b>>>>>>>>N $ll otpt
0hen P#80$ M >b'>'>'>'>N
Latches alternate pins at *v, >v, *v etc; (if1ddM*v)
5"ce co"6g'red +$riti"go't
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
20/34
Ee can read the vale across the pins and store this ina sitable variable
/o, if we had set 08I/$ M >b''''''''N $ll Inpt (binary notation)
08I/$ M >x5N $ll Inpt (hex notation)
08I/$ M **N $ll Inpt (decimal notation)
0hen Connect *v (1dd) to pins 8$>,',,
Ee wold see this across the port as
>>>>'''' /o if we had (assming 1on$ is an integer)
1on$ M P#80$N
0hen 1on$ wold (if displayed) contain '*
Readi"g i"
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
21/34
If we wanted to (say) read only one pin wecold 8ead the vale on the port
7ask ot the other bits (se an $-4)
Look at the reslt
0o change the otpt state of one pin 8ead the vale on the port #8 the relevant pin (trn 2on3)
$-4 the relevant pin (trn 2o53)
,'t $hat abo't i"di%id'alpi"s-
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
22/34
0he pins too have names and we can access these of Port $
Ode9ne P#80$bits;8$>
There is a" easier $ay
@t what are theseK
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
23/34
"o will remember we had strctres
* remi"der from C
strct 4ata/tct int aN int bN
int cN int dNQN
int main(void)
strct 4ata/tct 4/N 4/;a M 'N 4/;b M N retrn >NQ
In this example we have for
separate variables within thestrctre
0he total si!e of the strctreis that of for integers;
$n integer is (on a PC) bytes so '? bytes in total
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
24/34
@it9eld strctres. we de9ne variable si!esin terms of bits (integer types #-L")
* "e$ bit of C
strct 4ata/tct int a.'N ' bit (> or ')
int b.N bits (>,',,) int c.N bits (> to +) int d.N bits (>,',,)QNint main(void)
strct 4ata/tct 4/N 4/;a M 'N 4/;b M N retrn >NQ
In this example we have forseparate variables, all integers which
we can se as re:ired
0he /A4 si!e of each (and so range)is de9ned by the nmber of @I0/
In this example the 0#0$L 2sed3 si!eis 'RRR M % bits M ' @yte
It will take on the si!e of an integer asthis is 2base3 nit
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
25/34
@it9eld strctres. we de9ne variable si!esin terms of bits (integer types #-L")
We co'ld 'se a C#*R
strct 4ata/tct char a.'N ' bit (> or ')
char b.N bits (>,',,) char c.N bits (> to +) char d.N bits (>,',,)QNint main(void)
strct 4ata/tct 4/N 4/;a M 'N 4/;b M N retrn >NQ
In this example we have forseparate variables, all CF$8s which
we can se as re:ired
0he /A4 si!e of each (and so range)is de9ned by the nmber of @I0/
In this example the 0#0$L 2sed3 si!eis 'RRR M % bits M ' @yte M ' char
It will take on the /$7A si!e of anchar as this is 2base3 nit
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
26/34
0his is a techni:e in Cwhere we allow variables to/F$8A memory
78I58Snion 4atanion int a,bN Soat c,dNQN
int main(void)
nion 4atanion 4N 4;a M 'N printf (TUn4;a M Vd T, 4;a)N printf (TUn4;b M Vd T, 4;b)N4;b M N
printf (TUn4;a M Vd T, 4;a)N printf (TUn4;b M Vd T, 4;b)N
retrn >NQ
In this example we have
de9ned for separatevariables
0he address of each ishowever the /$7AH I;e; a MM b MM c MM d
Ee change one, we a5ectthe others
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
27/34
78I58S + i" actio"
nion 4atanion int a,bN Soat c,dNQN
int main(void) nion 4atanion 4N 4;a M 'N printf (TUn4;a M Vd T, 4;a)N printf (TUn4;b M Vd T, 4;b)N
4;b M N printf (TUn4;a M Vd T, 4;a)N printf (TUn4;b M Vd T, 4;b)N
retrn >NQ
#tpt (PC)
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
28/34
$s yo will remember, we control PICs (andsch devices) by setting register vales; $ register may control a nmber of related
parameters, each perhaps needing a
di5erent nmber of bits0he header for the chip 2de9nes3 each of
these sing the same name Ee then set these vales as re:ired
@0. 0his was we need to set everything in one go
Why is he telli"g 's this-
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
29/34
Co"sider
0o set this we might se
0'C#- M >b'>'>>>>>''>>''>N0'C#- M >x*>??N0'C#- M >*%N
0o change a bit, we need to 2know3 the crrent state andthen modify as re:ired (sing #8 and W#8 to setclearbits)
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
30/34
Ee create a @I0IAL4 strctre thatdescribes the register Ee -I#- this 2over3 the register address
0he header 9le de9nes the name for s
The )Cle%er trick
strct nsigned .'N nsigned 0C/.'N nsigned 0/"-C.'N nsigned .'N nsigned 0CDP/.N
nsigned 0G$0A.'N nsigned .?N nsigned 0/I4L.'N nsigned .'N nsigned 0#-.'N QN
Ee can then /et the whole register in one
go
0'C#- M >x*>??N /et -4I1I4$L items as
re:ired 0'C#-bits;0CPD/ M N
0'C#-bits;0#- M >N
0'C#-bits;0#- M 'N
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
31/34
$s well as basic I# ports, PICs have manyother (often complex) devices 0imers
PE7 modles
$80/ $ll of which we con9gre throgh the se of
registers sing the method detailed todayH
$nd will talk more abot next weekH
9or more complex de%ices
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
32/34
Ee are often given 2pre&written3 code that provides swith a nmber of fnctions we then make se of
7icrochip provide a large nmber of sch libraries
#ne sch is the graphics library 6 and it is that yo will besing this week
Ehile it 2looks3 complicated if yo read the 'serg'ides o" :oodlein conBnction with the sample
code provided yo will soon get to grips with it
Anabling yo to modify things and so make yor ownapplciation;
9or )%ery complex thi"gs
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
33/34
0his code has a nmber of samples showinghow the graphics fnctions are called
It incldes 4isplaying text on the screen
/ome simple shapes
/how how the slider can be sed
"or tasks $re to se these to display information on the
screen (nmbers)
0o bild a little 2egg timer3
The sample code
-
7/25/2019 2015_16 - Lecture for RTS Laboratory 2 (1)
34/34
Ee dig deeper Look how the Check/witch/' fnctions work
Consider techni:es inclding Polling
Interrpts 0imers
$nd (alas) I have to let yo se global variables
@t in a very controlled mannerH
8ext $eek (lect're ; labs!