Digital dial indicator interface – parte2


Dopo aver introdotto il tipo di codifica che usa il dispositivo in un articolo di qualche giorno fa.. ecco il codice del microcontrollore per eseguire la lettura dei dati sul nostro comparatore elettronico.

Come accade spesso, mi affido ad un arduino, quindi un ATmega168 o un ATmega328 possono fare al caso nostro.

Per leggere i dati con il microcontrollor sarà necessario cambiare i livelli di tensione come fatto nell’articolo sul calibro digital cinese; in ogni caso il circuito è quello riportato in figura, cambia solo che i segnali in ingresso sono a 3V invece che a 1.5V, ma non ci saranno problemi utilizzando gli stessi componenti.
Qui potete trovare i 2N3904

digital_dial_indicator_0_0_5.pde

/* DIGITAL DIAL INDICATOR

This is free software; you can redistribute it and/or

modify it under the terms of the GNU Lesser General Public

License as published by the Free Software Foundation; either

version 2.1 of the License, or (at your option) any later version.

reading the value from a chinese digital dianl indicator

not all kind of this device use the same encoding type

you need a proper circuit to convert signal from 3.0V to 5V

Created 2010

by Matteo Lampugnani

http://blog.lampugnani.org/tag/digital-dial-indicator/

*/

#define DEBUG 0 //setting debug message

volatile int count = 0;

int total = 0;

char data[28];

int dataPin = 5;

int powerPin = 4;

int ckPin = 2;

void setup(){

pinMode(dataPin, INPUT); //DATA line goes to Arduino digital pin 4

pinMode(powerPin, OUTPUT); //POWER line setup

digitalWrite(powerPin, HIGH);

pinMode(ckPin, INPUT); //ck line setup

digitalWrite(ckPin, HIGH);

Serial.begin(9600);

if(DEBUG) Serial.println("boot Up");

delay(500);

}

void loop(){

int first, second, third, fourth, fifth; //stores the calculated position of the caliper

int i,time;

for(i=0;i<28;i++){ //initialize array of bits to 0

data[i]=0;

}

attachInterrupt(0,getBit,FALLING); //start watching data line

while(count < 28){

} //wait till all bits are filled in

detachInterrupt(0);

if(DEBUG) Serial.println("Interrupt detached");

count = 0;

if (DEBUG) {

Serial.print("absolute data: ");

for(i=1;i<28;i++){

Serial.print(data[i], DEC); //print out all bits, either 1 or 0

if((i%4) == 0){

Serial.print(" ");

}

}

Serial.println();

}

for(i=1; i<=4; i++){

first += data[i]<<(i-1);

}

for(i=5; i<=8; i++){

second += data[i]<<(i-5);

}

for(i=9; i<=12; i++){

third += data[i]<<(i-9);

}

for(i=13; i<=16; i++){

fourth += data[i]<<(i-13);

}

for(i=17; i<=20; i++){

fifth += data[i]<<(i-17);

}

if (DEBUG) Serial.print(" Abs: ");

if (data[22] == 1) Serial.print("-");

if(fifth > 0 ) Serial.print(bcdToDec(fifth),DEC);

Serial.print(bcdToDec(fourth),DEC);

Serial.print(".");

Serial.print(bcdToDec(third),DEC);

Serial.print(bcdToDec(second),DEC);

Serial.print(bcdToDec(first),DEC);

delay(500);

Serial.println();

}

void getBit(){

char sample = 0; //variable used for "triple sampling"

if(digitalRead(dataPin) == LOW) //here the dataPin is checked three times for a HIGH value.

sample++;

if(digitalRead(dataPin) == LOW)

sample++;

if(digitalRead(dataPin) == LOW)

sample++;

if(sample > 1) //if the pin was HIGH at least twice, a 1 is recorded

data[count] = 1;

count++; //increment count so main() knows when the entire string of bits is ready

}

// Convert binary coded decimal to normal decimal numbers

byte bcdToDec(byte val)

{

return ( (val/16*10) + (val%16) );

}

Al momento il codice è in grado di leggere i dati dal comparatore quando in modalità millimetri e di riconoscere il segno della lettura (non è ad ora supportata la lettura in in).

Sfortunatamente, rispetto al calibro, i dati vengono spediti molto meno di frequente. Circa due o tre letture al secondo…

Lo sketch per processing, sarà molto simile all’interfaccia del calibro, solo con qualche funzione in più.

L’idea sarebbe di fare in modo di poter usare lo stesso sketch sia per il calibro che per il comparatore, e credo sia abbastanza abbordabile anche per le mie misere doti di programmatore.


Tag: , , , , , ,

Un Commento a “Digital dial indicator interface – parte2”

  1. [...] Vedi l’articolo successivo sul comparatore digitale [...]

Lascia un Commento