I am trying to develop an Arduino sketch for ADS8320 ADC IC, which is 3-wire SPI. I need a 2.4MHz serial clock for the SPI interface. I tried the code below, but I don't know how to generate clock pulses at 2.4MHz clock speed.
#define CLK 22
#define DBIT 24 // so
#define CS 26
#include <SoftwareSerial.h>
int v = 0;
float Ctemp, Ftemp;
void setup() {
Serial.begin(9600);
pinMode(CLK, OUTPUT);
pinMode(DBIT, INPUT);
pinMode(CS, OUTPUT);
digitalWrite(CS, HIGH);
digitalWrite(CLK, LOW);
}
void loop() {
v = spiRead();
if (v == -1) {
Serial.print("No sensor \n");
} else {
Serial.println(Ftemp);
}
delay(100);
}
int spiRead() {
int value = 0;
digitalWrite(CS, LOW);
delay(2);
digitalWrite(CS, HIGH);
delay(220);
/* Bring CS pin low to allow us to read the data from
the conversion process */
digitalWrite(CS, LOW);
/* 1st 5 clock cycles */
digitalWrite(CLK, HIGH);
delay(1);
digitalWrite(CLK, LOW);
digitalWrite(CLK, HIGH);
delay(1);
digitalWrite(CLK, LOW);
digitalWrite(CLK, HIGH);
delay(1);
digitalWrite(CLK, LOW);
digitalWrite(CLK, HIGH);
delay(1);
digitalWrite(CLK, LOW);
digitalWrite(CLK, HIGH);
delay(1);
/*
Read bits 16-0 from ads8320 for the adc. Loop for each bit reading
the value and storing the final value in 'adc'
*/
for (int i = 16; i >= 0; i--) {
digitalWrite(CLK, HIGH);
value += digitalRead(DBIT) << i;
digitalWrite(CLK, LOW);
}
// check bit D2 if HIGH no sensor
if ((value & 0x04) == 0x04) return -1;
// shift right three places
return value >> 3;
}
' i have added the code i tried with arduino spi library still i don't get any result
// inslude the SPI library:
#include <SPI.h>
// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;
void setup() {
// set the slaveSelectPin as an output:
pinMode(slaveSelectPin, OUTPUT);
// initialize SPI:
SPI.beginTransaction(SPISettings(2400000, MSBFIRST, SPI_MODE0))
void loop() {
while(1){
spiread(slaveSelectPin);
}
}
void spiread(int slaveSelectPin) {
// take the SS pin low to select the chip:
digitalWrite(slaveSelectPin, LOW);
receivedVal = SPI.transfer(val);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin, HIGH);
}