Вы находитесь на странице: 1из 3

djkirkendall wrote 08/16/2018 at 02:43

I tried the PIC method on the same Indicator, and it just didn't work out for me.
I wound up coding up a arduino nano to do the same thing, and it was much
easier/faster/cheaper.
Here's the code:

#define DataPin A1 //nano pin A1


#define ClkPin A0 //nano pin A0

//UART Speed
#define UARTBaudRate 9600

#define ADC_Threshold 160

//Data Format
#define DATA_BITS_LEN 24

unsigned short clk;


unsigned short clock_lpv=1;
unsigned long data_bit;
unsigned long data_word;
unsigned long measurement;
unsigned int timeout_counter=0;
unsigned short bit_count;
float total = 0;

//Arduino setup and main loop

//Defines for setting and clearing register bits

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

void setup(){
//set ADC prescale to 16 (set ADC clock to 1MHz)
//this gives as a sampling rate of ~77kSPS
sbi(ADCSRA, ADPS2);
cbi(ADCSRA, ADPS1);
cbi(ADCSRA, ADPS0);

Serial.begin(UARTBaudRate);
}

void loop(){

//reset counters and data


total = 0;
bit_count=0;
timeout_counter=0;
data_word=0;
data_bit=0;
int analogReading = 0;
//wait for clock to go low
while(analogRead(ClkPin) > ADC_Threshold);

//read in data
while( bit_count < DATA_BITS_LEN && timeout_counter < 50 ){
clk=(analogRead(ClkPin) > ADC_Threshold);

if(clk==1 && clock_lpv==0) {


//read data bit

data_bit=(analogRead(DataPin) > ADC_Threshold);


//Serial.print(data_bit);

//store data bit into word


data_bit = data_bit << 24;
data_word = data_word + data_bit ;
data_word = data_word >> 1;

//reset timeout counter


timeout_counter=0;

//increment bit counter


bit_count++;
}

//increment timeout counter


timeout_counter++;

clock_lpv=clk;
}

//Serial.println();
//if (timeout_counter >= 50){
//Serial.println("timed out");
//}

//extract measurement data from message (bits 0..19)


measurement = data_word & 0b00000000000011111111111111111111;

bool bit1 = data_word & 0b00000000000000000000000000000001;


bool bit2 = (data_word & 0b00000000000000000000000000000010) >> 1;
bool bit3 = (data_word & 0b00000000000000000000000000000100) >> 2;
bool bit4 = (data_word & 0b00000000000000000000000000001000) >> 3;
bool bit5 = (data_word & 0b00000000000000000000000000010000) >> 4;
bool bit6 = (data_word & 0b00000000000000000000000000100000) >> 5;
bool bit7 = (data_word & 0b00000000000000000000000001000000) >> 6;
bool bit8 = (data_word & 0b00000000000000000000000010000000) >> 7;
bool bit9 = (data_word & 0b00000000000000000000000100000000) >> 8;
bool bit10 = (data_word & 0b00000000000000000000001000000000) >> 9;
bool bit11 = (data_word & 0b00000000000000000000010000000000) >> 10;
bool bit12 = (data_word & 0b00000000000000000000100000000000) >> 11;

total += (bit1*1);
total += (bit2*2);
total += (bit3*4);
total += (bit4*8);
total += (bit5*16);
total += (bit6*32);
total += (bit7*64);
total += (bit8*128);
total += (bit9*256);
total += (bit10*512);
total += (bit11*1024);
total += (bit12*2048);

total /= 100;

//Serial.print("total:");
Serial.println(total);
delay(100);

It works, cost under $10 to make (assuming you already have the indicator).
Anyways, your tutorial really helped me and I thought I'd pay it forward.

Вам также может понравиться