Laboratorio CAD di circuiti elettronici Esercitazione

Download Report

Transcript Laboratorio CAD di circuiti elettronici Esercitazione

Laboratorio CAD di circuiti elettronici
Esercitazione 3
Esercitazione 3
Protocolli seriali
1) SPI Master
Scrivere in mbed il codice di un programma in grado di impostare il microcontrollore come Master
SPI ed inviare ogni 100µs la seguente parola di 8 bit: 0xA1.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
/* Program Example 1: Sets up the mbed as SPI master, and continuously sends a single
byte
*/
#include "mbed.h"
SPI ser_port(D11, D12, D13); // mosi, miso, sclk
char switch_word ; //word we will send
int main()
{
ser_port.format(8,0); // Setup the SPI for 8 bit data, Mode 0 operation
ser_port.frequency(1000000); // Clock frequency is 1MHz
while (1) {
switch_word=0xA1; //set up word to be transmitted
ser_port.write(switch_word); //send switch_word
wait_us(100);
}
}


Visualizzare su due canali di un oscilloscopio l’andamento dei segnali sui pin mosi e sclk.
Verificare se viene inviato prima il MSB o il LSB.
2) SPI Data Link
Scrivere in mbed il codice di due programmi in grado di impostare due microcontrollori come
Master SPI e Slave SPI rispettivamente. Fare in modo che alla pressione del pulsante B1 (PC_13 =
low) sulla board alloggiata con il µC Master, quest ultimo invii la parola 0xB1, diversamente invierà
la parola 0xA6. Il µC Slave, risponderà con una parola di ack=0xA0 nel riconoscere la pressione del
pulsante sul Master ed accenderà un LED. Diversamente risponderà con un no_ack = 0xC1.
Prevedere inoltre, che il µC Master alla ricezione della risposta dello Slave, a seconda di un ack
oppure no_ack accenda rispettivamente un LED Verde oppure Rosso.
1
Laboratorio CAD di circuiti elettronici
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
Esercitazione 3
/* Program Example 2A: Master
*/
#include "mbed.h"
SPI ser_port(D11, D12, D13); // mosi, miso, sclk
DigitalOut cs(D2);
DigitalIn button(PC_13);
DigitalOut LEDR(D4);
DigitalOut LEDG(D7);
DigitalOut buttonOut(D9);
char switch_word ; //word we will send
char rec_word;
const char ack = 0xA0;
int main()
{
ser_port.format(8,0); // Setup the SPI for 8 bit data, Mode 0 operation
ser_port.frequency(1000000); // Clock frequency is 1MHz
while (1) {
buttonOut = button;
if (button) {
switch_word=0xA6; //set up word to be transmitted
} else {
switch_word=0xB1;
}
cs = 0;
rec_word = ser_port.write(switch_word); //send switch_word
cs = 1;
wait(0.01);
if (rec_word==ack) {
LEDR=0;
LEDG=1;
} else {
LEDR=1;
LEDG=0;
}
2
Laboratorio CAD di circuiti elettronici
35.
36. }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
Esercitazione 3
}
/* Program Example 2B: Slave
*/
#include "mbed.h"
SPISlave ser_port(D11, D12, D13, PA_15); // mosi, miso, sclk, ssel
DigitalOut LED(D4);
char switch_word ; //word we will send
char rec_word;
const char key = 0xB1;
const char ack = 0xA0;
const char no_ack = 0xC1;
int a;
int main()
{
while (1) {
a = ser_port.receive();
if (a) {
rec_word = ser_port.read();
if (rec_word==key) {
ser_port.reply(ack);
LED=1;
} else {
ser_port.reply(no_ack);
LED=0;
}
}
}
}
3) Accelerometro ADXL345
Scrivere in mbed il codice di un programma in grado di interrogare ogni 400ms l’accelerometro
ADXL345 tramite protocollo SPI e mostrare a terminale i valori ricevuti.
3
Laboratorio CAD di circuiti elettronici
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
Esercitazione 3
/*Program Example 3: Reads values from accelerometer through SPI, and outputs
continuously to terminal screen.
*/
#include "mbed.h"
SPI acc(D11,D12,D13); // set up SPI interface on pins 11,12,13
DigitalOut cs(PA_15); // use pin PA_15 as chip select
Serial pc(USBTX, USBRX); // set up USB interface to host terminal
char buffer[6]; //raw data array type char
int16_t data[3]; // 16-bit twos-complement integer data
float x, y, z; // floating point data, to be displayed on-screen
int main() {
cs=1; //initially ADXL345 is not activated
acc.format(8,3); // 8 bit data, Mode 3
acc.frequency(2000000); // 2MHz clock rate
cs=0; //select the device
acc.write(0x31); // data format register
acc.write(0x0B); // format +/-16g, 0.004g/LSB
cs=1; //end of transmission
cs=0; //start a new transmission
acc.write(0x2D); // power ctrl register
acc.write(0x08); // measure mode
cs=1; //end of transmission
while (1) { // infinite loop
wait(0.4);
cs=0; //start a transmission
acc.write(0x80|0x40|0x32); // RW bit high, MB bit high, plus address
for (int i = 0;i<=5;i++) {
buffer[i]=acc.write(0x00); // read back 6 data bytes
}
cs=1; //end of transmission
data[0] = buffer[1]<<8 | buffer[0]; //combine MSB and LSB
data[1] = buffer[3]<<8 | buffer[2];
data[2] = buffer[5]<<8 | buffer[4];
x=0.004*data[0]; y=0.004*data[1]; z=0.004*data[2]; // convert to float,
//actual g value
pc.printf("x = %+1.2fg\t y = %+1.2fg\t z = %+1.2fg\n\r", x, y,z); //print
}
}
4) Accelerometro ADXL345 (lib)
Riscrivere in mbed il codice di del programma al punto precedente, facendo uso della libreria
presente all’indirizzo: https://developer.mbed.org/cookbook/ADXL345-Accelerometer.
5) I2C: Sensore di temperatura TCN75A
Scrivere in mbed il codice di un programma in grado di interrogare ogni secondo il sensore di
temperatura TCN75A tramite protocollo I2C e mostrare a terminare i valori ricevuti.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
/*
*
*
*
*
*
*
*
*
*
Read temperature from a TCN75 I2C thermometer
I2C address: 1 0 0 1 A2 A2 A0
Wiring scheme:
TCN75
uC
------------------------1. SDA
D14 (SDA - 10k Pull-up)
2. SCL
D15 (SCL - 10k Pull-up)
4
Laboratorio CAD di circuiti elettronici
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.

* 3. ALERT
* 4. GND
* 8. VCC
*/
Esercitazione 3
-- (not used)
GND
3V3
#include "mbed.h"
I2C i2c(D14, D15);
const int addr = 0x90;
// Arduino compatible pins
// TCN75 address: 0x48<<1
int main()
{
char cmd[2];
// data buffer
printf("\r\nTCN75 I2C thermometer\r\n");
cmd[0] = 0x01;
// Pointer to CONFIG register
cmd[1] = 0x00;
// Data for CONFIG register (Normal operation, comparato
r mode)
i2c.write(addr, cmd, 2);
// Send Address/command and two bytes
while (1) {
wait(1);
cmd[0] = 0x00;
// Pointer to TEMP register
i2c.write(addr, cmd, 1); // Write adress/command byte, then register address
i2c.read(addr, cmd, 2);
// Read 2 bytes from TEMP register
float temp = cmd[0]<<8|cmd[1];
printf("Temperatue = %.4f C\r\n", temp);
}
}
Cambiare il codice per avere diverse risoluzioni del convertitore A/D e verificarne l’impatto
sul valore di temperatura mostrato.
5