 |
Laboratorio di TDP
(anno
scolastico 2014/15)
Scheda
Arduino
|
|
Ultimo aggiornamento il
19/03/15
Documenti su OwnCloud
Sito
arduino 2009
Interfaccia USB
Specifiche documentazione prove di laboratorio
Scheda Arduino

Schema elettrico scheda

Layout versione 2012/13

Layout versione 2013/14

Come base di partenza utilizzare il file tch in dotazione su owncloud.
Per motivi di saldatura proibire l'accesso lato TOP ai terminali di: JP2,
JP1, JP7, JP2, JP3, JP4, S1, JP5, JP6.
Lo stabilizzatore U1 di tensione deve appoggiare su una zona di rame lato TOP
che avrà la funzione di dissipatore di calore
Fori versione 2014/15

dimensioni in mils
Schema adattatore seriale RS-232 scheda


| Connettore seriale 1 
|
Connettore seriale 2 
|
Circuito per la programmazione del bootloader

Shield domotico
Schema elettrico

Moduli
D' ingresso analogici




D' ingresso digitali




D'uscita digitali




PROVA 4




Procedure di collaudo
Sviluppo software
Protocollo RS485
DST Indirizzo destinazione (range 0..254)
MIT Indirizzo mittente (range 0..254)
CMD comando (range 0..255);
Comandi:
| Cmd |
n |
Descrizione |
Richiesta |
Risposta |
| rdp0 |
20 |
Legge porta P0 |
|
|
| rdp1 |
21 |
Legge porta P1 |
|
|
| rdp2 |
22 |
Legge porta P2 |
|
|
| rdp3 |
23 |
Legge porta P3 |
|
|
| wrp0 |
24 |
Scrive porta P0 |
|
|
| wrp1 |
25 |
Scrive porta P1 |
|
|
| wrp2 |
26 |
Scrive porta P2 |
|
|
| wrp3 |
27 |
Scrive porta P3 |
|
|
| RdVarBt |
30 |
Legge variabile byte (idx: l'indice) |
|
|
| WrVarBt |
31 |
Scrive variabile byte (idx: l'indice) |
|
|
| RdVarWd |
32 |
Legge variabile word (idx: l'indice) |
|
|
| WrVarWd |
33 |
Scrive variabile word (idx: l'indice) |
|
DST |
MITT |
33 |
idx |
byte L |
byte H |
|
|
| RdEEp |
34 |
Legge da EEprom (idx: l'indice) |
|
|
| WrEEp |
35 |
Scrive su EEprom (idx: l'indice) |
|
|
| SetMode |
36 |
Arduino SetMode |
|
|
| DigWr |
37 |
Arduino Digital Write |
|
|
| DigRd |
38 |
Arduino Digital Read |
|
|
| AnaWr |
39 |
Arduino Analog Write |
|
|
| AnaRd |
40 |
Arduino Analog Read |
|
|
Ardudomo
/*
Minodomotica 2014 libreria seriale gestione bus
*/
#include <SoftwareSerial.h>
/*
Ingressi analogici
Ch=0 A0 Pin 23
Ch=1 A1 Pin 24
Ch=2 A2 Pin 25
Ch=3 A3 Pin 26
Ch=4 A4 Pin 27
Ch=5 A5 Pin 28
Uscite Analogiche PWM
Ch= 3 I03 Pin 05
Ch= 5 I05 Pin 11
Ch= 6 IO6 Pin 12
Ch= 9 IO9 Pin 15
Ch=10 IO10 Pin 16
Ch=11 IO11 Pin 17
I/O digitali
Ch= 0 IO0(RX) Pin 02
Ch= 1 I01(TX) Pin 03
Ch= 2 I02 Pin 04
Ch= 3 I03 Pin 05
Ch= 4 I04 Pin 06
Ch= 5 I05 Pin 11
Ch= 6 IO6 Pin 12
Ch= 7 IO7 Pin 13
Ch= 8 IO8 Pin 14
Ch= 9 IO9 Pin 15
Ch=10 IO10 Pin 16
Ch=11 IO11 Pin 17
Ch=12 IO12 Pin 18
Ch=13 IO13 Pin 19
mode= 0 INPUT
mode= 1 OUTPUT
mode= 2 INPUT_PULLUP
*/
//:0201240D01# //Set Modo OUTPUT Linea Digital I/O 13
//:0201250D01# //DigWr 1 su Digital I/O 13
//:0201250D00# //DigWr 0 su Digital I/O 13
//Comandi protocollo comunicazione seriale tra schede 8051
#define rdp0 0x14 //Legge porta P0
#define rdp1 0x15 //Legge porta P1
#define rdp2 0x16 //Legge porta P2
#define rdp3 0x17 //Legge porta P3
#define wrp0 0x18 //Scrive porta P0
#define wrp1 0x19 //Scrive porta P1
#define wrp2 0x1A //Scrive porta P2
#define wrp3 0x1B //Scrive porta P3
#define RdVarBt 0x1E //Legge variabile byte
#define WrVarBt 0x1F //Scrive variabile byte
#define RdVarWd 0x20 //Legge variabile word
#define WrVarWd 0x21 //Scrive variabile word
#define RdEEp 0x22 //Legge EEprom
#define WrEEp 0x23 //Scrive EEprom
#define SetMode 0x24 //Arduino Set Digital mode
#define DigWr 0x25 //Arduino Digital Write
#define DigRd 0x26 //Arduino Digital Read
#define AnaWr 0x27 //Arduino Analog Write
#define AnaRd 0x28 //Arduino Analog Read
//SoftwareSerial RaspiSerial(2, 3); // RX, TX Seriale verso RaspberryPI
SoftwareSerial DomoSerial (2, 3); // RX, TX Seriale verso BusMiniDomo
#define DomTxEn 4 //Linea di controllo bus
#define MyAddr 5 //Indirizzo scheda
char VarBt[10];
int VarWd[10];
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
DomoSerial.begin(9600);
pinMode(DomTxEn,OUTPUT);
}
//---------------------------------------------------------------------------
void loop() // run over and over
{
raspicom();
}
Libreria serraspi
//---------------------------------------------------------------------------
/*
Decodfica la richiesta RASPI interroga il bus e comunica la risposta
*/
char rxbuf[10];
byte txbuf[10];
void CmdBySerialToDomo(String s)
{
int ls,lr,n,i;
String u;
if (MyAddr== esadec(s.substring(0,2)))
{//Locale
//Serial.print(s+";");
i=esadec(s.substring(4,6));
switch (i)
{
case SetMode: //Arduino Set Digital mode
pinMode(esadec(s.substring(6,8)),esadec(s.substring(8,10)));
Serial.println(s.substring(2,4)+s.substring(0,2)+s.substring(4,6));
break;
case DigWr: //Arduino Digital Write
digitalWrite(esadec(s.substring(6,8)),esadec(s.substring(8,10)));
Serial.println(s.substring(2,4)+s.substring(0,2)+s.substring(4,6));
break;
case DigRd: //Arduino Digital Read
n=digitalRead(esadec(s.substring(6,8)));
Serial.println(s.substring(2,4)+s.substring(0,2)+decesa(n,2));
break;
case AnaWr: //Arduino Analog Write
analogWrite(esadec(s.substring(6,8)),esadec(s.substring(8,10)));
Serial.println(s.substring(2,4)+s.substring(0,2)+s.substring(4,6));
break;
case AnaRd: //Arduino Analog Read
n=analogRead(esadec(s.substring(6,8)));
Serial.println(s.substring(2,4)+s.substring(0,2)+decesa((n & 0xFF),2)+decesa((n >> 8),2));
break;
case RdVarBt: //Legge variabile byte
n=VarBt[esadec(s.substring(6,8))];
Serial.println(s.substring(2,4)+s.substring(0,2)+decesa(n,2));
break;
case WrVarBt: //Scrive variabile byte
VarBt[esadec(s.substring(6,8))]=esadec(s.substring(8,10));
Serial.println(s.substring(2,4)+s.substring(0,2)+s.substring(4,6));
break;
case RdVarWd: //Legge variabile word
n=VarWd[esadec(s.substring(6,8))];
Serial.println(s.substring(2,4)+s.substring(0,2)+decesa((n & 0xFF),2)+decesa((n >> 8),2));
break;
case WrVarWd: //Scrive variabile word
VarWd[esadec(s.substring(6,8))]=esadec(s.substring(8,10))+esadec(s.substring(10,12))<<8;
Serial.println(s.substring(2,4)+s.substring(0,2)+s.substring(4,6));
break;
}
}
else
{//Remoto
txbuf[0]=esadec(s.substring(0,2));
txbuf[1]=esadec(s.substring(2,4));
txbuf[2]=esadec(s.substring(4,6));
ls=(s.length()-6)/2;
for (n=0;n<ls;n++)
{
i=6+n*2;
txbuf[3+n]=esadec(s.substring(i,i+2));
}
if ((txbuf[2]<rdp0) || (txbuf[2]>AnaRd))
return;
switch (txbuf[2])
{
case RdVarWd:
case AnaRd:
lr=2;
break;
default:
lr=1;
break;
}
DomoSerial.listen();
digitalWrite(DomTxEn,true);
DomoSerial.write(txbuf,ls+3);
digitalWrite(DomTxEn,false);
DomoSerial.readBytes(rxbuf,2+lr);
u=decesa(rxbuf[0],2)+decesa(rxbuf[1],2)+decesa(rxbuf[2],2);
if (lr==2)
u=decesa(rxbuf[3],2)+u;
Serial.println(u);
}
}
//---------------------------------------------------------------------------
#define stx ':'
#define etx '#'
String scmd;
bool crd=false;
char c;
int pb;
long int oms,oms1;
//---------------------------------------------------------------------------
void domoBusTx(int ls)
{
txbuf[0]=rxbuf[1];
txbuf[1]=rxbuf[0];
digitalWrite(DomTxEn,true);
DomoSerial.write(txbuf,ls);
digitalWrite(DomTxEn,false);
pb=0;
}
//---------------------------------------------------------------------------
void raspicom()
{
int n;
DomoSerial.listen();
//TestCmd();
//delay(100);
if (Serial.available())
{
c=Serial.read();
if (c==stx)
{
scmd="";
crd=true;
oms=millis();
}
if (crd)
{
if (c!=stx)
{
//Serial.println(c);
if (c==etx)
{
CmdBySerialToDomo(scmd);
//Serial.println("Comando: "+scmd);
crd=false;
}
else
scmd+=c;
}
}
}
if (DomoSerial.available())
{
c=DomoSerial.read();
rxbuf[pb]=c;
//Serial.println(c,HEX);
//Serial.println(pb);
if (rxbuf[0]==MyAddr)
{
oms1=millis();
if (pb>=2)
switch(rxbuf[2])
{
case SetMode: //Arduino Set Digital mode
if (pb>=4)
{
pinMode(rxbuf[3],rxbuf[4]);
txbuf[2]=rxbuf[2];
domoBusTx(3);
pb=0;
}
break;
case DigWr: //Arduino Digital Write
if (pb>=4)
{
digitalWrite(rxbuf[3],rxbuf[4]);
txbuf[2]=rxbuf[2];
domoBusTx(3);
pb=0;
}
break;
case DigRd: //Arduino Digital Read
if (pb>=3)
{
n=digitalRead(rxbuf[3]);
txbuf[2]=n;
domoBusTx(3);
pb=0;
}
break;
case AnaWr: //Arduino Analog Write
if (pb>=4)
{
analogWrite(rxbuf[3],rxbuf[4]);
txbuf[2]=rxbuf[2];
domoBusTx(3);
pb=0;
}
break;
case AnaRd: //Arduino Analog Read
if (pb>=3)
{
n=analogRead(rxbuf[3]);
txbuf[2]=(n & 0xFF);
txbuf[3]=(n >> 8);
domoBusTx(4);
pb=0;
}
break;
case RdVarBt: //Legge variabile byte
if (pb>=3)
{
txbuf[2]=VarBt[rxbuf[3]];
domoBusTx(3);
pb=0;
}
break;
case WrVarBt: //Scrive variabile byte
if (pb>=4)
{
VarBt[rxbuf[3]]=rxbuf[4];
txbuf[2]=rxbuf[2];
domoBusTx(3);
pb=0;
}
break;
case RdVarWd: //Legge variabile word
if (pb>=3)
{
n=VarWd[rxbuf[3]];
txbuf[2]=(n & 0xFF);
txbuf[3]=(n >> 8);
domoBusTx(4);
pb=0;
}
break;
case WrVarWd: //Scrive variabile word
if (pb>=5)
{
VarWd[rxbuf[3]]=rxbuf[4]+((int)rxbuf[5]<<8);
txbuf[2]=rxbuf[2];
domoBusTx(3);
pb=0;
}
break;
default:
pb=0;
break;
}
pb++;
}
}
if ((crd) && ((millis()-oms)>1000))
crd=false;
if ((pb) && ((millis()-oms1)>1000))
pb=0;
}
void SerView(int lr)
{
String u;
u=decesa(rxbuf[0],2)+decesa(rxbuf[1],2)+decesa(rxbuf[2],2);
if (lr==2)
u=decesa(rxbuf[3],2)+u;
Serial.println(u);
}
//---------------------------------------------------------------------------
boolean DomoCmd(int ls,int lr)
{
int rb;
int c=3;
boolean Er;
DomoSerial.listen();
do
{
Er=false;
digitalWrite(DomTxEn,true);
DomoSerial.write(txbuf,ls+3);
digitalWrite(DomTxEn,false);
rb=DomoSerial.readBytes(rxbuf,2+lr);
//Serial.println(rb);
if (rb<2+lr) //Controlla se è arrivata tutta la risposta
Er=true;
if ((rxbuf[0]!=txbuf[1]) || (rxbuf[1]!=txbuf[0])) //Controlla se la provenienza è giusta;
Er=true;
c--;
}
while ((c>0) && Er);
return !Er;
}
//---------------------------------------------------------------------------
void TestCmd()
{
txbuf[0]=0x03;
txbuf[1]=0x05;
txbuf[2]=0x24;
txbuf[3]=0x0D;
txbuf[4]=0x01;
if (DomoCmd(3,1))
SerView(1);
else
Serial.println("Errore");
}
//---------------------------------------------------------------------------
boolean LampOnOff(int Lamp,boolean On)
{
byte b;
txbuf[0]=0x03;
txbuf[1]=0x05;
txbuf[2]=0x14;
if (DomoCmd(1,1))
{
b=rxbuf[2];
if (On)
b|=(1<<Lamp);
else
b&=(~(1<<Lamp));
txbuf[2]=0x18;
txbuf[3]=b;
if (DomoCmd(2,1))
return true;
else
return false;
}
else
return false;
}
//---------------------------------------------------------------------------
boolean LampsOnOff(int Lamp,boolean On)
{
byte b;
txbuf[0]=0x03;
txbuf[1]=0x05;
if (On)
b=0xFF;
else
b=0x00;
txbuf[2]=0x18;
txbuf[3]=b;
if (DomoCmd(2,1))
return true;
else
return false;
}
//---------------------------------------------------------------------------
boolean readBts(int nbts)
{
int i=0;
byte readByte;
DomoSerial.listen();
long l = millis();
while ((millis() - l < 1000) && (i<nbts))
{
while ((DomoSerial.available() > 0) && (i<nbts)) {
readByte = DomoSerial.read();
rxbuf[i] = readByte;
i++;
}
}
return i>=nbts;
}
//---------------------------------------------------------------------------
int esadec(String esa)
{
unsigned int n,l,esad;
// Serial.print("-"+esa+"-");
esad=0;
l=esa.length();
for (n=0;n<l;n++)
{
//Serial.print(esa[n]);
if (esa[n]<65)
esad=esad+ ((esa[n]-48) << ((l-n-1)*4));//round(pot(16,l-n))*(ord(esa[n])-48)
else
esad=esad+((esa[n]-55) << ((l-n-1)*4));//round(pot(16,l-n))*(ord(esa[n])-55);
}
return esad;
}
//---------------------------------------------------------------------------
String decesa(unsigned int deca, int lung)
{
int n,a;
String str;
n=0;
str="";
for (n=0;n<lung;n++)
{
a=deca % 16;
deca=deca / 16;
if (a>9)
str=(char)(a+55)+str;
else
str=(char)(a+48)+str;
//Serial.println(str);
}
return str;
}