Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Figure2:SPIbussignalsduringthewritingintotheslave(top),andreadingfromtheslave(bottom)
D
3
1 0
A
4
D
2
0
D
5
A
1
0 0
SCK
1 0
D
4
A
0
0 0 MOSI
A
3
A
5
D
1
D
7
0 0 0
A
2
0
D
0
D
6
0
R
W
0
SS
M
S
A
5
A
1
D
2
D
7
R
W
1
D
1
D
5
1
A
0
1 X
D
3
SS
0
D
0
A
3
SCK
0 0
A
4
D
6
0
M
S
MOSI
A
2
D
4
D
6
0
D
1
1 0
D
7
D
5
MISO
D
2
D
4
1 1 1 0
D
0
0
D
3
0
1
WRITE FROM MASTER TO SLAVE
READ FROM SLAVE TO MASTER
PlayingwithSTM32F407testboardSPICommunication
3
readingfromslavewecansendadummyvalueinsteadofeightdatabitsovertheMOSIsignal.The
completelistingofthefunctionisgiveninFig.3.
The combined 16 bits to be sent over the MOSI signal are the argument AandD of the function.
The function returns a char value as received over the MISO signal during the transmission. The
function starts with some definitions of bits and the declaration and initialization of a local variable
Ret.NexttheSlaveSelectsignalispulsedlowandkeptassuchuntiltheendofthefunction.
Sixteenclockpulsesareneededtotransmitorreceiveabyte,sothefunctioncontinuesbyafor
loop to be repeated 16 times. In the loop the bit 15 of the argument is first checked, and then the
signal MOSI is set to the value of this bit. Next the value of the argument is shifted left for one bit
preparingtheargumentforthenextloop,andtheclockpulseisgeneratedbypullingthesignalSCK
firstlow,andthenhighagain.Somedelayisaddedtoadapttothespeedoftheslavedevice.Thelast
thing to do within the loop is to read the value of the signal MISO and shiftaccumulate bits in the
variableRet.
In order to write to the slave device the above function should be called, and the combined
addressanddatabytesaretobepassedastheargument.ThelistingofthefunctionSPIwritetodo
thisisshowninFig.4,top.OnlysixbitsofargumentAdrareused(thisassuresthefirsttwoofthe
string of 16 bits bits are low), they are shifted eight bits to the left, and then byte of data is added.
Thereturnvalueisignored.
Inordertoreadfromtheslavedevicetheabovefunctioniscalledinasimilarway(SPIread)but
most significant of the 16 bits is set to high by adding 8000
h
. This causes reading from the slave, so
thereturnedvalueisusedhereandreturnedtothecallingprogram.
char SPI t r ansact i on ( i nt AandD) { / / about 7us
#def i ne MOSI 0x8000
#def i ne MI SO 0x4000
#def i ne SCK 0x2000
#def i ne SEL 0x1000
i nt Ret = 0;
GPI OB- >ODR &= ~SEL; / / SPI sel ect
f or ( char k = 0; k<16; k++) { / / f or 16 bi t s
i f ( AandD & 0x8000) GPI OB- >ODR | = MOSI ; / / send addr ess & dat a
el se GPI OB- >ODR &= ~MOSI ; / /
AandD <<= 1; / / next bi t of addr ess & dat a
GPI OB- >ODR &= ~SCK; / / SCL l o
f or ( i nt i = 0; i <10; i ++) {}; / / wast e 150ns
Ret <<= 1; / / shi f t bi t s r ead r i ght
GPI OB- >ODR | = SCK; / / SCL hi
i f ( GPI OB- >I DR & MI SO) Ret ++; / / r ead bi t and add t o st r i ng
};
GPI OB- >ODR | = SEL; / / SPI done
r et ur n ( ( char ) ( Ret & 0xf f ) ) ; / / r et ur n r esul t
}
Figure3:AlistingofthefunctiontowriteorreadabyteofdatausingSPIbus
voi d SPI wr i t e( i nt Adr , i nt Dat a) {
SPI t r ansact i on( ( ( Adr & 0x3f ) << 8) + Dat a) ; / / pr epar e 16 bi t s
}
char SPI r ead( i nt Adr ) {
char Ret = SPI t r ansact i on( ( ( Adr & 0x3f ) << 8) + 0x8000) ; / / 16 bi t s
r et ur n ( Ret ) ; / / r et ur n r esul t
}
Figure4:Alistingoffunctions toutilizetheabovefunctionforwritingandreading
PlayingwithSTM32F407testboardSPICommunication
4
The complete program to read data from the accelerometer, two axess, is given in Fig. 5. The
program starts with the initialization of ports where the slave device is connected, and continues
with the initialization of the slave device and the LCD screen. Then the execution of the program
enters the endless loop, where the two bytes representing the readout from each axis of the
accelerometerarecombined(seedatasheetontheaccelerometerfordetailsandregisters)intoa16
bit result and sent to the screen. Some delay is added to ease the reading of results from the LCD
screen.
Astheinitializationisconcerned,theSPIbusisavailableattheconnectorK485.Thisisconnected
toportB(MOSIsignalatbit15,MISOsignalatbit14,SCKsignalatbit13,andSSsignalatbit12).This
portmustfirstbeactivatedbyenablingtheclockforportB,thenbits15,13,and12mustbedefined
asoutputs.Thisisdobeintwosteps;onresetofthemicrocontrollerthebit15isdefinedasalternate
function for programming purposes, and this must be disabled by clearing MS bit of the register
MODE,thenallthreebitsoftheportcanbemadeoutputs.Thefinalstepoftheinitilizationistoset
SlaveSelectandclocksignaltohighinitially.
As the initialization of the accelerometer chip is concerned, the relevan info is available in the
datasheetoftheaccelerometer.Hereweonlystatethattheaccelerometerchipmustbepoweredup
bywritingC7
h
toaddres20
h
,andthentheinternalupdatingofresultsshouldbedefinedbywritingto
address21
h
,aswellasfilteringdisabledbywritingtoaddress22
h
forthepurposeofthisexperiment.
Otherrequirementsmaybesuitableforotherpurposes.