I have the following code that both reads and parses through can bus messages, while also checking temperatures and humidity levels with a SHT 4XI sensor. The board turns on, reads and writes can bus messages fine but the sensor (which communicates via I2C) does not give any readout. When I attach an Arduino Uno with verified code it does read out on my custom board so the hardware should be fine. The main code is as follows:
#include "CAN.h"
#include "mbed.h"
#include "SHT40 Class.h"
int oldmilis = 200;
int milis = 0;
int i2caddress = 0x44;
I2C i2c(PC_7, PC_6);
SHT40 sht40;
namespace mbed
{
FileHandle *mbed_override_console(int fd)
{
static BufferedSerial console(USBTX, USBRX, MBED_CONF_PLATFORM_STDIO_BAUD_RATE);
return &console;
}
}
int main()
{
// printf("CAN Nucleo-F767ZI\n");
printf("CAN Datahub\n");
i2c.frequency(100000);
CAN can(PB_8, PB_9,500000);
CAN can2(PB_5, PB_6,500000);
DigitalOut led1(PC_8);
CANMessage msg1;
CANMessage msg2;
if(can.filter(0x03FE,0x03FF,CANStandard)){
printf("Can 1 Filter applied\n");
}
if(can2.filter(0x03FF,0x03FF,CANStandard)){
printf("Can 2 Filter applied\n");
}
// trigger first read of opposite board
if (can.write(msg1));
// if (can2.write(CANMessage(1333, &counter, 1))) counter++;
while (1)
{
sht40.sampleHigh();
if (can.read(msg1)) {
printf("Received CAN 1: %d\n", msg1.data[0]);
ThisThread::sleep_for(500ms);
if (can2.write(msg1)){
printf("Sent to CAN 2: %d\n",msg1.data[0]);
led1 = !led1;
}
}
if (can2.read(msg2)) {
printf("Received CAN 2: %d\n", msg2.data[0]);
ThisThread::sleep_for(500ms);
if (can.write(msg2)){
printf("Sent to CAN 1: %d\n",msg2.data[0]);
led1 = !led1;
}
if(oldmilis > milis){
led1 = !led1;
float Temp = sht40.getTemperature();
float Hum = sht40.getHumidity();
printf("Temperature: %d\n", Temp);
printf("Humidity : %d\n",Hum);
milis = 0;
}
milis++;
}
}
}
// CLASS SHT40 /////////////////////////////////////////////////////////////////
void SHT40::sampleLow(){
i2c.start();
ThisThread::sleep_for(10ms);
if(i2c.write(0xE0)==1){ //Set: Read low precision (0xE0)
i2c.read(i2caddress,SHT_DLC, 6); // request 6 bytes from slave device #8
}
i2c.stop();
}
void SHT40::sampleHigh(){
i2c.start();
ThisThread::sleep_for(10ms);
if(i2c.write(SHT_address[0],"0xFD",1)==0){
printf("write succesful\n");
}
if(i2c.write(0xFD)!=0){
printf("Faulty write\n");
} //Set: Read low precision (0xE0)
i2c.stop();
i2c.read(i2caddress,SHT_DLC,6); // request 6 bytes from slave device #8
}
float SHT40::getTemperature(){
uint16_t Temp = SHT_DLC[0] * 256 + SHT_DLC[1];
return Temp;
}
float SHT40::getHumidity(){
uint16_t Temp = SHT_DLC[3] * 256 + SHT_DLC[4];
return Temp;
}
I have used the following code to check if the known address is even accessible; but I get all NACK’s back. If I run the same address checking code with Arduino it does pick up the address I’m expecting to find.
This is the code;
#include "mbed.h"
BufferedSerial pc(USBTX, USBRX,9600);
#define D_SDA PB_9
#define D_SCL PB_8
// sda=PB7, scl=PB_6 Pins specific to Nucleo-F303K8
// must change pins to match your board.
I2C i2c(D_SDA, D_SCL);
DigitalOut myled(PC_8);
int ack;
int address;
void scanI2C() {
for(address=1;address<127;address++) {
ack = i2c.write(address, "0x94", 1);
if (ack == 0) {
printf("\tFound at %3d -- %3x\r\n", address,address);
}
if(ack != 0){
printf("I2C Error: %d\n", ack);
}
ThisThread::sleep_for(5ms);
}
}
int main() {
i2c.frequency(100000);
printf("I2C scanner \r\n");
scanI2C();
printf("Finished Scan\r\n");
// just blink to let us know the CPU is alive
while(1) {
ThisThread::sleep_for(1s);
myled = !myled;
}
}
I have no Idea what to do know, it seems to me this must be a software Issue as when I connect the Arduino to a header connected to the SDA & SCK traces it works perfectly fine. But I have no clue what to do from here. Hopefully any of you can help out…
Kind regards,
Ties