Hi. ![]()
I have several of these cheap PCF8574 modules and I was curious as to it being able to act as a I2C to SPI bridge like the SC18IS602 I2C to SPI Bridge Module for Arduino with Arduino Library.
Since the PCF8574 is being advertised as a IIC/I2C/TWI/SPI Serial interface Board Module, I thought why bother buying the more expensive one if I can get away with doing the same thing for cheaper?
I have an SPI based sensor that I want to directly interface to a I2C DAC. The data rate is I'm working with is not that fast and I only want to work with 12 bits every 250 milliseconds anyhow.
My sensor is a temperature sensor that goes from 0 - 1024 C and the voltage range of the DAC is 0 - Vcc.
Buffering the array and addressing the interface is where I got a bit lost.
Is there any advice on how a PCF8574 module (normally used for LCDs) can be used to function as I tried to describe above?
I found some excellent code from: Simple 3-Wire MAX6675 Thermocouple ADC Arduino Interface
/* 3-wire connection between the MAX6675 Cold-Junction-Compensated K-Thermocouple- to-Digital Converter and Arduino.
This is a read only device with a 12-bit output (16 total bits) with bit 15 (MSB) output first.
15 is a dummy sign bit and is discarded. Bits 14-3 are the actual temperature reading,
bit 2 is normally 0 but if the sensor is not attached is a 1. Useful for detecting if sensor is attached.
Bits 0 and 1 are discarded.
There are several "libraries" on the web for Arduino to read the MAX6675 but to me are a mess - I wrote my own more
portable code anyone can use.
The code works as follows in subroutine spiRead():
We declare variable "value" as an integer and set to zero. An integer variable in Arduino is 16-bits.
CS is taken LOW for 2 mSec. then HIGH to start the conversion process. We delay 200 mSec. for the process to complete.
The CS is taken LOW again to read the data. One CLK cycle from LOW to HIGH to LOW discards bit 15.
Next we use a "for" loop to retrieve the next 15 bits (14-0) that are stored in variable "value".
Next we test bit 2 to see if the sensor is connected. A 1 means no sensor and a value of -1 is returned to the main program.
If bit 2 is 0 everything is OK, "value" is shifted right three places and the Centigrade integer value is returned to the main program.
Back in "loop" if a -1 is returned to variable v a message "No sensor" is generated. Otherwise v is multiplied by 0.25 to get the
temperature in Celsius which is converted to Fahrenheit then displayed on the serial monitor or on a LCD display.
*/
#define Gnd 22
#define Vcc 24
#define CLK 26
#define CS 28
#define DBIT 30 // so
//#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);
 pinMode(Gnd, OUTPUT);
 pinMode(Vcc, OUTPUT);
 digitalWrite(Gnd, LOW);
 digitalWrite(Vcc, HIGH);
Â
}
void loop()Â {
 v = spiRead();
 if (v == -1) {
  Serial.print("No sensor \n");
 }
 else {
  Ctemp = v * 0.25;
  Ftemp = (Ctemp * 9 / 5) + 32;
  Serial.println(Ftemp);
 // Serial.print("\n");
 }
 delay(100);
}
int spiRead()Â {
 int value = 0; //We declare variable "value" as an integer and set to zero
 digitalWrite(CS,LOW); //CS is taken LOW for 2 mSec. then HIGH to start the conversion process.
 delay(2);
 digitalWrite(CS,HIGH);
 delay(220);
 /* Read the chip and return the raw temperature value */
 /* Bring CS pin low to allow us to read the data from
 the conversion process */
 digitalWrite(CS,LOW); //The CS is taken LOW again to read the data. One CLK cycle from LOW to HIGH to LOW discards bit 15.
 /* Cycle the clock for dummy bit 15 */
 digitalWrite(CLK,HIGH);
 delay(1);
 digitalWrite(CLK,LOW);
 /*
 Read bits 14-3 from MAX6675 for the Temp. Loop for each bit reading
 the value and storing the final value in 'temp'
 */
 for (int i=14; i>=0; i--) { //Next we use a "for" loop to retrieve the next 15 bits (14-0) that are stored in variable "value".
  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 wish more open-source coders could lay it out line-by-line like this in order to describe the whats/whens/wheres/whys/hows as comprehensively as this one seems to.
(It would make many lives easier.) ![]()
Anyhow, I now must figure out how to send the bits from this code into an I2C device and have the I2C device reflect what the SPI device is doing.
Wish me luck.
![]()