-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathltc2440.cpp
68 lines (59 loc) · 1.84 KB
/
ltc2440.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* ltc2440.c
*
* Created on: 7 Jul 2017
* Author: Lukas Martisiunas
*
*/
#include "ltc2440.h"
#include "Arduino.h"
#include "SPI.h"
long unsigned int SpiRead(void) {
long result = 0;
long b;
char slaveSelectPin = slave_pin_ADC;
// long result2 = 0;// MOSI/SDI pin 7 HIGH => 7 Hz, best resolution
digitalWrite(slaveSelectPin, LOW); // take the SS pin low to select the chip
delayMicroseconds(10); // probably not needed, only need 25 nsec delay
b = SPI.transfer(0xff); // B3
if (b>>4 != 0b0010){
digitalWrite(slaveSelectPin,HIGH);
Serial.println(b,BIN);
return(0);
}
result = b << 8;
// Serial.println(b,BIN);
b = SPI.transfer(0xff); // B2
result |= b;
result = result << 8;
// Serial.println(b,BIN);
b = SPI.transfer(0xff); // B1
result |= b;
result = result << 8;
// Serial.println(b,BIN);
b = SPI.transfer(0xff); // B0
result |= b;
// Serial.println(b,BIN);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin, HIGH);
return (result);
}
long unsigned int read_ADC() {
long unsigned int read_adc_val;
read_adc_val = SpiRead();
delay(10);
read_adc_val &= 0x1FFFFFFF; // force high four bits to zero
read_adc_val = read_adc_val >> 5; // truncate lowest 5 bits
return read_adc_val;
}
void init_ADC(){
SPI.begin(); // initialize SPI, covering MOSI,MISO,SCK signals
SPI.setBitOrder(MSBFIRST); // data is clocked in MSB first
SPI.setDataMode(SPI_MODE0); // SCLK idle low (CPOL=0), MOSI read on rising edge (CPHI=0)
SPI.setClockDivider(SPI_CLOCK_DIV32); // system clock = 16 MHz, chip max = 1 MHz
byte temp = slave_pin_ADC;
pinMode (temp, OUTPUT);
digitalWrite(temp, HIGH); // chip select is active low
char temp_2 = SpiRead();
temp_2 = SpiRead();
}