EKSAMENSOPPGAVE.
Emne: IRE35115 / Kraftelektronikk og mikrokontrollere Lærer: Even Arntsen /
Åge T Johansen /
Grupper: 12ELE-D+ 12ELEY-D Dato: 16.12.2016 Tid: 09.00 —13.00 Antall vedleggsider: 19
Antall oppgavesider: 6(med denne)
Sensurfrist: 15.01.2017 Hjelpemidler:
Vedlegg 1:Formler (3 sider)
Vedlegg 2:Oversikt 74HC595 (1 side) Vedlegg 3:Oversikt 74HC165 (1 side)
Vedlegg 4: MBED API(sammendrag) (14 sider)
* Selvskrevet formelsamling og godkjent kalkulator
* Dokumentasjon av MBED API vedlagt som Vedlegg 4.
KANDIDATENMÅ SELVKONTROLLEREAT OPPGAVESETrETER FULLSTENDIG
Med hensyn til bedømming av besvarelsen, vil oppgave 2 og 3 (kraftelektronikk) til sammen telle likt med oppgave 4 (mikrokontrollere).
Oppgave 1
Prosjektoppgave uten navn merkes med studentens eksamensnummer og leveres sammen med resten av besvarelsen.
Høgskolen i Østfold —Avdeling for ingeniørfag
Oppgave 2
Ä
Figur 1. Enfase likeretter
Figur 1 viser en likeretter som tilfører likestrøm til en belastning R = 15 ohm.
Det skal utvikles 3,1 kW i lasten. Hvor stor blir likestrømmen?
Hvor stor blir vekselstrømmen?
Hvor stor blir vekselspenningen?
Hva blir laveste overharmoniske komponent i nettstrømmen?
Det blir for lite effekt i lasten, så likeretteren utvides med to dioder og en glattedrossel som vist i figur 2. Den kobles til trefasenett med samme spenning som i c) Hva blir effekten i lasten nå, når vi ser bort fra Ls?
Figur 2. Trefase likeretter
g) Hvor stor blir effektivverdien av vekselstrømmen?
Oppgave 3
•
Figur 3. Chopper
Figur 3 viser en motordrift for en kjølevifte. Motoren har merkespenning 36 V. For å spare energi, er den turtallsstyrt ved at motorspenningen kan reguleres.
Reguleringsområdet er fra 12 —36 V. Ul = 12 V. Styringsprinsippet er baser på Puls Bredde Modulasjon.
Innefor hvilket område må duty-cyclen kunne varieres?
Det er valgt en MOSFET som svitsje-element. Andre alternativ er bipolar transistor eller IGBT. Sett opp noen positive og negative egenskaper for de tre alternativene.
Hvilken oppgave har D2 i kretsen? Forklar hvorfor den er nødvendig.
I en tilsvarende krets med step-ned chopper, kunne man ha sløyfet Cl . Hvorfor er det ikke så gunstig å gjøre det i denne kretsen.
Motoren belaster kretsen med 108 W ved merkespenning. Hva er middelverdien av strømmen som kilden Ul leverer.
Transistoren svitsjer med 20 kHz. Induktansen L, er 800 LAH.Beregn
rippelstrømmen i L, og opp tegn opp en skisse av strøm i L, og spenning over
L. Påfør verdier av strøm, spenning og tid i skissen.
Høgskolen i Østfold —Avdeling for ingeniørfag
Oppgave 4
VEDLEGG4 til slutt i oppgavesettet gir en kort oversikt over de mest aktuelle MBED-klassene. Vær oppmerksom på at pinnereferansene i teksten i vedlegget gjelder for MBED LPC1768, mens spørsmålene i denne prøven gjelder enheten NUCLEO-L432KC,som vist i figur 4:
=
cn
o.)o o o (I'
= -
Figur 4. Pinnereferanser for NUCLEO L432KC
nr,
VEDLEGG2 inneholder en kort oversikt over skiftregisteret 74HC595 som kan benyttes for å utvide antall digitale utganger på mikrokontrollermodulen.
VEDLEGG3 inneholder en kort oversikt over skiftregisteret 74HC165 som kan benyttes for å utvide antall digitale innganger på mikrokontrollermodulen.
a) Antall digitale inn- og utganger for en mikrokontollerenhet NUCLE032-L432KC skal utvides med 8 innganger og 8 utganger. Til dette skal det benyttes et serie-inn/parallell-ut skiftregister 74HC595
Begge skiftregistrene skal kobles til via et av de tilgjengelige SPI-interfacene på NUCLEO,med mikrokontrolleren som master. Merk at i tillegg til SPI-signalene, vil det blant annet også være nødvendig med signaler for å klokke ut parallelle data til utportene, og for å klokke inn parallelle data til innportene.
Lag en skisse som viser sammenkoblingen av de 3 enhetene. Påfør navn som tilsvarer komponentenes inn/utganger på skissen. Vis også hvordan kontrollinnganger på skifteregistrene som ikke er tilknyttet NUCLEO,må kobles til Vcc eller GND.
b) For å realisere et program som skal benytte de 2 tilkoblede registrene, skal nødvendige MBED- objekter deklareres globalt. Kall SPI-objektet, spil.
Skriv ned kodelinjene som viser hvordan de MBED-objektene kan opprettes, i henhold til den valgte skissen fra punkt a). Disse kodelinjene er altså ment å komme før main() og eventuelle andre funksjonsdefinisjoner.
Skriv også en funksjon som konfigurerer SPI-interfacet med bitrate 400000 bps, 8 bits ordlengde og «mode» 0.
Funksjonsprototype:
void init_spil();
Skriv to funksjoner. Lifunksjon for å skrive en byte (8 bit) til utgangene på 74HC595, ogén funksjon for å lese in en byte (8 bit) fra inngangene på 74HC165.
Husk å få med styringen av alle nødvendige kontrollsignaler.
Funksjonsprototyper:
void write_SPIbytel(unsigned char byte);
unsigned char read_SPIbytel();
Skriv et main()-program der du benytter funksjonene og definisjonene ovenfor. Programmet skal styre 4 bit på 74HC595 etter mønsteret som vist i figur 5 nedenfor. Sekvensen er repetert hvert 80 ms (periodetid = 80 ms). Se bort fra tiden det tar å overføre data fra NUCLEOtil skiftregisteret.
Signalene D1, D2, D3 og D4 i diagrammet tilsvarer utganger på skiftregisteret. Før disse navnene inn i skissen i punkt a).
Har du ikke skrevet egne funksjoner som angitt i punkt b) og c), kan du benytte funksjonene fra MBED-klassen direkte, eller gå ut fra at de angitte funksjonene er definert.
Hint: Benytt en tabell for å registrere de 8 signalkombinasjonene.
Høgskolen i østfold —Avdeling for ingeniørfag
D1
02
03
D4
10 10 10 10 10 10 10 10 tid [ms]
Figur 5. Sekvensdiagram
e) Styresignalene D1 —D4 fra punkt d) er tenkt å benyttes for å styre hastigheten til en motor.
(Ingen regulering ved hjelp av tilbakekobling her.) Med tidsangivelsene som er vist i sekvensdiagrammet, antas det at dette tilsvarer en omdreiningshastighet på 750 rpm.
Hvis tidsavstanden mellom de enkelte flankene avtar, antar vi at hastigheten øker, dvs. pulsene kommer tettere. Forlenges tiden mellom flankene, vil hastigheten avta.
Vi ønsker nå å styre hastigheten til motoren ved hjelp av et potensiometer tilknyttet en analog inngang på NUCLEO. Potensiometeret vil gi en spenning mellom 0 V og 3.3 V.
Vis hvordan potensiometeret kobles inn i kretsen. Bruk skissen fra punkt a).
Modifiser programmet fra punkt d) slik at periodetiden, ved hjelp av potensiometeret, kan justeres mellom 40 ms og 120 ms.
Husk å vise hvordan interfacingen til potensiometeret utføres i programmet også.
Hint: Vurder å benytte et Ticker-objekt til dette.
f) NUCLEO-enheten skal også kobles til et annet datautstyr ved hjelp av et ledig SPI-interface. Kall dette siste SPI-interfacet spi2. I dette tilfellet skal NUCLEO-enheten ta imot kommandoer fra det tilkoblede datautstyret, og må derfor settes opp i slavemodus.
For å teste ut systemet, vil datautstyret veksle mellom å sende to kommandoer STARTog STOP for å starte og stoppe motoren (omtalt i punkt e)). Kommandoene sendes via SPI
Verdien til STOP-kommando er 1, verdien til START-kommando er 2. Alle andre kommandoer skal neglisjeres. Når motoren stoppes, skal alle styresignaler (Dl —D4) settes til 0. Ved start, skal den opprinnelige sekvensen med 80 ms periode settes opp.
Skriv et program for NUCLEOsom vil utføre testen. Her må det blant annet tas med kontroll og oppsett av begge aktuelle SPI-interfacer. Spi2 skal ha tilsvarende parametere som spil.
Du kan gjenbruke deler av programmene fra andre deloppgaver, men vær da nye med å forklare og vise hvordan programfragmentene passer sammen.
VEDLEGG 1
Formelark. Kraftelektronikk
Dette er ment som en hjelp tHde som har "jernteppe". Således er ikke forutsetning for bruk av formelen tatt med.
Ud= .,f0Tu(t)dt Middelverdi
URMS=
u rt-\
7
1 roT
2dt
..1-..;
iEffektivverdi
Ud= 0,9Us Middelverdi, diodebru, ånfase
Ud=0,9Uscos(a) Middelverdi, tyristorbru, ånfase
2coLsId
2Xkld
Kommuteringsspenningsfall, ånfase
= Id Vekselstrøm, ånfase
°J91d Grunnharmonisk, ånfase
Ud= 1,35Us Middelverdi, diodebru, trefase
Ud=1,35Uscos(a) Middelverdi, tyristorbru, trefase
36..)L5Id 3Xkld
Kommuteringsspenningsfall, trefase n-
2xkld
cosii = 1 Kommuteringsvinkel. (Merk
Xk =wl,$)
/s = 0,816/d Effektivverdi. Trefase vekselstrøm
= 0,781d Grunnharmonisk. Trefase vekselstrøm
U2 = U1D Step-ned chopper
AU2 Ts2
U2
=
—8LC —D) Rippel, step
-ned
U2
= 1– DStep
-up
Høgskolen i Østfold —Avdeling for ingeniørfag
AU2
=
DTSu2
RCD N21—D
Rippel, step-up
Fly-back
"
U2 = uul N2
Forward
Asynkronmaskin:
cos= Klf s =
f2 = sfi
%Pr =
f f-2-f2
Ui K2Øagf
Ks
elagf2
Tem KM, g
f2
In =
K8kg
= j.
2+ 1m 2
Usinus
ma =
Utrekantm
ftrekantf
f
sinusPBM, amplitudemodulasjonsindeks
PBM, frekvensmodulasjonsindeks
Likestrømsmaskin:
E = coKlet ø = K217a Tem= K31,0
U = Raia + La..1 + E di dt
P = -N/"Ulcoscp Generell trefase
Q = .NIUIsincp Generell trefase
PF =,.
PEffektfaktor
DPF = coscp1 Forskyvningseffektfaktor
1 1 1
is = -\/' • 0,78Id (sin(wt) ——5 sin(5wt) + —7 sin(7wt) —i-1 sin(11w0 + i-sin(13wt) 1 .. —+)
Fourerrekke til vekselstrøm i trefase likeretter
-
\/2 • 0,78/ 1 1 1
i s = N d (sin(wt) ——11sin(11wt)+ —13sin(13w0——23sin(23w0 + 1
Fourierrekke til vekselstrøm i tolvpuls likeretter
Høgskolen i Østfold —Avdeling for ingeniørfag
VEDLEGG 2
74HC595 - FUNKSJONSOVERSIKT
14 DS 11 SHCP 10 MR
12 STCP
13 OE
8-STAGE SHIFT REGISTER
8-BIT STORAGE REGISTER
3-STATE OUTPUTS
075 9
00 01 Q2 03 04 05 06 07
15 1 2 3 4 5 6 7 mna554
MR 10 master reset (activeLOW)
SHCP 11 shift register clock input
STCP 12 storage register clock input
OE 13 output enable input (active LOW)
DS 14 serial data input
Table 3. Function tableili Control
SHCP STCP
X X
X t
X X
? X
X
i'
Input Output
OE MR DS Q7SQn
L L X L NC
L L X L L
H L X L Z
L H H 06S NC
L H X NC QnS
L H X 065 OnS
[1] H = HIGH voltage state:
L = LOW voltage state:
= LOW-to-HIGH transition:
X = don't care:
NC = no change:
Z = high-impedance OFF-state.
,Function
a LOW-level on MR only affects the shift registers empty shift register loaded into storage register
shift register clear: paralleI outputs in high-impedance OFF-state logic HIGH-Ievel shifted into shift register stage 0. Contents of all shift register stages shifted through. e.g. previous state of stage 6 (internal 065) appears on the serial output (075).
contents of shift register stages (internal QnS) are transferred to the storage register and parallel output stages
contents of shift register shifted through: previous contents of the shift register is transferred to the storage register and the parallel output stages
VEDLEGG 3
74E1C165- FUNKSJONSOVERSIKT
Table 3. Function table111 Operating modes inputs
PL CE CP DS DO to D7
parallel load L X X X L
L X ,X IX [F1
serial shift H IL ,T I X
Fl ' L II h X
Fi T L I X
H T L h X
hold "do nothing" H 1H iX X X
H X i H iX IX
Qn registers Outputs
00 ,01 to Q6 07 07
L L to L L H
1H H to H H L
1L q0 to q5 q6 q6
H q0 to q5 q6 It.
L Iq0 to q5 q6 c7
H qt) to q5 q6 cI7
q0 ql to q6 q7
(7,
q0 ql to q6 q7
[1] H = HIGH vottage level;
h = HIGH vottage level one set-up time prior to the LOW-to-HIGH clock transition:
L = LOW voltage level:
I = LOW vottage level one set-up time prior to the LOW-to-HIGH clock transition:
q = state of the referenced output one set-up time prior to the LOW-to-HIGH clock transition;
X = don't care:
= LOW-to-HIGH clock transition.
11 12 13 14 3 4 5 6
DO D1 D2 03 04 D5 06 D7
1 P1
10 DS
07 9
2 CP 8-BIT SHIFT REGISTER
PARALLEL-IN/SERIAL-OUT 07 7
15 CE
mna992
Table 2.
Symbol PL CP Q7 GND 07 DS DOto D7 CE Vcc
Pin 1 2 7 8 9 10 11, 15 16 Pin description
12, 13, 14, 3, 4, 5, 6
Description
asynchronous parallel load input (active LOW) clock input (LOW-to-HIGH edge-triggered) complementary output from the last stage ground (0 V)
serial output from the last stage serial data input
parallel data inputs (also referred to as Dn) clock enable input (active LOW)
positive supply voltage
VEDLEGG 4
MBED API (sammendrag)
DigitalOut
A digital output, used for setting the state of a pin.
The DigitalOut Interface can be used on all pin marked blue in the figure above.
The DigitalOut Interface can be used to set the state of the output pin, and also read back the current output state. Set the DigitalOut to zero to turn it off, or 1 to turn it on.
The pin output is Ov and 3.3v (0 and 1).
Example:
DigitalOut my1ed(LED1);
int main() ( while(1)
myled = 1;
wait(0.2);
myled = 0;
wait(0.2);
Functions
DigitalOut (PinName pin)
Create a DigitalOut connected to the specified pin.
DigitalOut (PinName pin, int value)
Create a DigitalOut connected to the specified pin.
void write (int value)
Set the output, specified as 0 or 1 (int)
int read 0
Return the output setting, represented as 0 or 1 (int)
int is_connected 0
Return the output setting, represented as 0 or 1 (int) DigitalOut & operator= (int value)
A shorthand for write() operator int 0
A shorthand for read()
The pin input is logic '0' for any voltage on the pin below 0.8v, and '1' for any voltage above 2.0v. By default, the Digitalln is setup with an internal pull-down resistor.
Example:
DigitalIn enable(p5);
DigitalOut led(LED1);
int main() { while(1)
if(enable) { led = !led;
wait(0.25);
Functions
Digitalln (PinName pin)
Create a Digitalln connected to the specified pin.
Digitalln (PinName pin, PinMode mode)
Create a Digitalln connected to the specified pin.
int read 0
Read the input, represented as 0 or 1 (int) void mode (PinMode pull)
Set the input pin mode. PullUp, PullDown, NoPull, OpenDrain int is_connected 0
Return the output setting, represented as 0 or 1 (int) operator int 0
An operator shorthand for read()
BusOut
The BusOut interface is used to create a number of DigitalOut pins that can be written as one value.
Example:
BusOut myleds(LED1, LED2, LED3, LED4);
int main() { while(1) {
for(int i=0; i <;16;
myleds = i;
wait(0.25);
BusIn
The BusIn interface is used to create a number of Digitalln pins that can be read as one value. Any of the numbered mbed pins can be used as a Digitalln in the Busln.
Example:
BusIn nibble(p5, p6, p18, p11);
int main() { while(1) {
switch(nibble) {
case 0x3: printf("Hello!\n"); break;
case 0x8: printf("World!\n"); break;
InterruptIn
Let a signal change on a pin trig a function independent of the state for the main- loop.
Example:
InterruptIn button(p5);
DigitalOut led(LED1);
DigitalOut flash(LED4);
void flip() { led =
int main()
button.rise(&flip); - - -
while(1) {
flash = !flash;
wait(0.25);
Functions
Interruptin (PinName pin)
Create an Interruptin connected to the specified pin.
int read 0
Read the input, represented as 0 or 1 (int) operator int 0
An operator shorthand for read() void rise (Callback< void()> func)
Attach a function to call when a rising edge occurs on the input.
void
enable_irq() Enable IRQ.
void
disable_irq() Disable IRQ.
AnalogIn
An analog input, used for reading the voltage on a pin.
The AnalogIn Interface can be used on mbed pins p15-p20.
The 0.0v to 3.3v range of the Analogin is represented in software as a normalized floating point number from 0.0 to 1.0.
gl.nclude "mbed.h"
AnalogIn ain(A0);
DigitalOut dout(LED1);
int main(void) while (1) (
if(ain > 0.3f) { dout = 1;
} else { dout = 0;
printf("percentage: %3.3f%%\n", ain.read()*100.0f);
printf("normalized: Ox%04X \n", ain.read_u16());
wait(0.2f);
Functions
Analogin
(PinName pin)
Create an
Analogin ,connected to the specified pin.
float
read 0Read the input voltage, represented as a float in the range [0.0, 1.0].
unsigned short
read_u16 0Read the input voltage, represented as an unsigned short in the range [0x0, OxFFFF].
operator float 0
An operator shorthand for read()
AnalogOut
An analog output, used for setting the voltage on a pin.
The AnalogOut Interface can be used on mbed pin p18 (for the MBED LPC1768).
The AnalogOut Interface can be used to set the voltage on the analog output pin somewhere in the range of 0.0v to 3.3v.
The 0.0v to 3.3v range of the AnalogOut can be represented in software as a normalized floating point number from 0.0 to 1.0, or directly as volts or millivolts.
-=
AnalogOut aout(p18);
DigitalOut dout(LED1);
int main(void) while (1) {
eje r:
=he
for (float i = 0.0f; i < 1.0f; i += 0.1f) { aout = i;
printf("aout = %1.2f volts\n", aout.read() * 3.3f);
- lei
dout = (aout > 0.5f) ? 1 : 0;
wait(1.0f);
Functions
AnalogOut
(PinName pin)
Create an
AnalogOutconnected to the specified pin.
void
write(float value)
Set the output voltage, specified as a percentage (float) void
write_u16(unsigned short value)
Set the output voltage, represented as an unsigned short in the range [0x0, OxFFFF].
float
readReturn the current output voltage setting, measured as a percentage (float)
AnalogOut & operator=(float percent)
An operator shorthand for write()
operator floatAn operator shorthand for read()
train. The default period is 0.020s, and the default pulsewidth is 0.
The PwmOut interface can express the pulse train in many ways depending on how it is to be used. The period and pulse width can be expressed directly in units of seconds, millisecond or microseconds. The pulsewidth can also be expressed as a percentage of the period.
Functions
PwmOut (PinName pin)
Create a PwmOut connected to the specified pin.
void write (float value)
Set the ouput duty-cycle, specified as a percentage (float)
float read ()
Return the current output duty-cycle setting, measured as a percentage (float) void period (float seconds)
Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
void period_ms (int ms)
Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
void period_us (int us)
Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
void pulsewidth (float seconds)
Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
void puisewidth_ms (int ms)
Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
void pulsewidth_us (int us)
Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
PwmOut & operator= (float value)
A operator shorthand for write() operator float 0
An operator shorthand for read()
wait
Generic wait functions.
Functions:
void wait(float s); Waj.t number of seconcs void wait ms(int ms); -,2,"rbP'"
void waitlus(int us);
Waits for a number of seconds, with microsecond resolution (within the accuracy of single precision floating point). Alternative functions with milliseconds and microseconds as arguments are also available.
Timer
A general purpose timer Example:
Timer timer;
DigitalOut led(LED1);
int begin, end;
int main() { timer.start();
begin = timer.read_us();
led = !led;
end = timer.read_us();
printf("Toggle the led takes %d us", end - begin);
Functions:
void start();
void stop();
void reset();
float read();
int read_ms();
int read_us();
Timeout
The Timeout interface is used to setup an interrupt to call a function after a specified delay.
Any number of Timeout objects can be created, allowing multiple outstanding interrupts at the same time.
Example:
A simple program to setup a Timeout to invert an LED after a given timeout...
Timeout flipper;
DigitalOut ledl(LED1);
DigitalOut led2(LED2);
int main() { led2 = 1;
flipper.attach(&flip, 2.0); (. set:JF,f cpi _
while(1) {
ledl = !1edi;
wait(0.2);
Functions
void
attach
(Callback< voidO> func, float t)Attach a function to be called by the Ticker, , specifiying the interval in seconds.
Ticker
The Ticker interface is used to setup a recurring interrupt to repeatedly call a function at a specified rate. Any number of Ticker objects can be created, allowing multiple outstanding interrupts at the same time. The function can be a static function, or a member function of a particular object.
A Ticker is used to call a function at a recurring interval.
Example:
Ticker flipper;
DigitalOut ledl(LED1);
DigitalOut led2(LED2);
void flip() { led2 = !led2;
int main() { led2 = 1;
flipper.attach(&flip, 2.0);
while(1) (
ledl = !ledi;
wait(0.2);
Functions
Public Member Functions
void
attach
(Callback< voidO> func, float t)Attach a function to be called by the Ticker , specifiying the interval in seconds.
void detach ()
Detach the function
Serial
Serial is a generic protocol used by computers and electronic modules to send and receive control
information and data. The Serial link has two unidirection channels, one for sending and one for receiving.
The link is asynchronous, and so both ends of the serial link must be configured to use the same settings.
One of the Serial connections goes via the mbed USB port, allowing you to easily communicate with your host PC.
Example:
Serial pc(USBTX, USBRX);
int main() { pc.printf(rh while(1) {
pc.putc(pc.getc() + 1);
Functions
(a selection)
Serial (PinName tx, PinName rx, const char*name=NULL,
int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) Create a Serial port, connected to the specified transmit and receive pins.
Serial (PinName tx, PinName rx, int baud)
Create a Serial port, connected to the specified transmit and receive pins, with the specified baud.
void baud (int baudrate)
Set the baud rate of the serial port.
void format (int bits=8, Parity parity=SerialBase::None, int stop_bits=1) Set the transmission format used by the serial port.
int readable ()
Determine if there is a character available to read.
int writeable 0
Determine if there is space available to write a character.
void attach(Callback< void()> func, IrqType type=RxIrq)
Attach a function to call whenever a serial interrupt is generated.
void set_flow_control (Flow type, PinName flow1=NC, PinName flow2=NC) Set the flow control type on the serial port.
This interface can be used for communication with SPI slave devices, such as FLASH memory, LCD screens and other modules or integrated circuits.
Eksempel:
SPI spi(p5, p6, p7);
DigitalOut cs(p8);
int main() I cs = 1;
spi.format(8,3);
spi.frequency(1000000);
cs = 0;
spi.write(0x8F);
int whoami = spi.write(0x00);
printf("WHOAMI register = Ox%X\n", whoami);
cs = 1;
Functions
SPI (PinName mosi, PinName miso, PinName sclk, PinName sse1=NC) Create a SPI master connected to the specified pins.
void format (int bits, int mode=0)
Configure the data transmission format.
void frequency (int hz=1000000) Set the spi bus clock frequency.
virtual int write (int value)
Write to the SPI Slave and return the response.
virtual void lock (void)
Acquire exclusive access to this SPI bus.
virtual void unlock (void)
Release exclusive access to this SPI bus.
SPISlave
A SPI slave, used for communicating with a SPI Master device.
The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz Example:
// •eply Lo a SPI mas.:_eras s]av.e 4iinclude "Tbed.h"
SPISlave device(p5, p6, p7, p8);
int main() {
device.reply(0x00);
while(1) {
if(device.receive()) { int v = device.read();
v = (v + 1) % Ox100;
device.reply(v);
1
-
Make his he n-xt reoli;
Functions
SPISlave
(PinName mosi, PinNarne miso, PinName sclk, PinName ssel) Create a SPI slave connected to the specified pins.
void
format(int bits, int mode=0)
Configure the data transmission format.
void
frequency(int hz=1000000) Set the spi bus clock frequency.
int
receive(void)
Polls the
SPIto see if data has been received.
int
read(void)
Retrieve data from receive buffer as slave.
void
reply(int value)
Fill the transmission buffer with the value to be written out as slave on the next received
message from the master.
The I2C interface provides I2C Master functionality.
This interface can be used for communication with a I2C devices, such as serial memories, sensors and other modules or integrated circuits.
Warning:
Remember, you will need a pull-up resistor on sda and scl. All drivers on the I2C bus are required to be open collector, and so it is necessary for pull up resistors to be used on the two signals. A typical value for the pullup resistors is around 2.2k ohms, connected between the pin and 3v3.Note: The mbed API uses 8 bit addresses, so make sure to take that 7 bit address and left shift it by 1 before passing it.
Example:
#1nclude "mbe(1.5"
I2C i2c(p28, p27);
const int addr = 0x90;
int main() { char cmd[2];
while (1) [
cmd[0] = 0x01;
cmd[1] = Ox00;
i2c.write(addr, cmd, 2);
wait0{5);
cmd[0] = Ox00;
i2c.write(addr, cmd, 1);
i2c.read(addr, cmd, 2);
float tmp = (float((cmd[0]<<8) lcmd[1]) / 256.0 printf{")ems = , tmp);
Functions
I2C (PinName sda, PinName scl)
Create an I2C Master interface, connected to the specified pins.
void frequency (int hz)
Set the frequency of the I2C interface.
int read (int address, char *data, int length, bool repeated=false) Read from an I2C slave.
int read (int ack)
Read a single byte from the I2C bus.
int write (int address, const char *data, int length, bool repeated=false) Write to an I2C slave.
int write (int data)
Write single byte out on the I2C bus.
void start (void)
Creates a start condition on the I2C bus.
void stop (void)
Creates a stop condition on the I2C bus.
virtual void lock (void)
Acquire exclusive access to this I2C bus.
virtual void unlock (void)
Release exclusive access to this I2C bus.
int transfer (int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t &callback,
int event=12C_EVENT_TRANSFER_COMPLETE, bool repeated=false) Start non-blocking I2C transfer.
void abort_transfer ()
Abort the on-going I2C transfer.
Example:
// S=ie responder I2CSlave slave(p9, p10);
int main()
char buf[10];
char msg[] = "Slave!";
slave.address(OxA0);
while (1) {
int i = slave.receive();
switch (1) (
case I2CSlave::ReadAddressed:
slave.write(msg, strlen(msg) + 1);
break;
case I2CSlave::WriteGeneral:
slave.read(buf, 10);
printf("Read G: 95s\n", buf);
break;
case I2CSlave::WriteAddressed:
slave.read(buf, 10);
printf("Read A: %s\n", buf);
break;
for(int i = 0; i < 10; i++) buf[i] = 0;
Functions
12CSlave (PinName sda, PinName scl)
Create an I2C Slave interface, connected to the specified pins.
void frequency (int hz)
Set the frequency of the I2C interface.
int receive (void)
Checks to see if this I2C Slave has been addressed.
int read (char *data, int length) Read from an I2C master.
int read (void)
Read a single byte from an I2C master.
int write (const char *data, int length) Write to an I2C master.
int write (int data)
Write a single byte to an I2C master.
void address (int address) Sets the I2C slave address.
void stop (void)
Reset the I2C slave back into the known ready receiving state.