Teensy 4.0 i2c Port1

Status
Not open for further replies.

P Dobson

Active member
The following code runs correctly if I use i2c port 0 changing (wire1 to wire). But this would occupy two useful PWM channels, so I want to use i2c port1.

If I modify the code to "wire1" this redirects the port pins that are used correctly. I can see the data on an oscilloscope. But the readings available in the print statements are all overflows. What must I do to pick up the correct data from the wire library. I tries using i2c_wire library but got confused.
BR PeteD.
 

Attachments

  • Bare_Wire_test.ino
    1.4 KB · Views: 45
WireData.h is hardwired to use Wire.

Since WireData.h is so small why not just use it directly within your sketch.

Code:
/*
|| @usage
|| | e.g.
|| | float myNumber = 3.14159;
|| | uint16_t numberOfBytesSent = wireWriteData(myNumber);
|| #
*/
template <typename T> uint16_t wireWriteData(const T &value)
{
  const uint8_t *p = (const uint8_t *) &value;

  Wire.write(p, sizeof value);

  return sizeof value;
}


/*
|| @usage
|| | e.g.
|| | float myNumber;
|| | uint16_t numberOfBytesReceived = wireReadData(myNumber);
|| | Serial.println(myNumber);
|| #
*/
template <typename T> uint16_t wireReadData(T &value)
{
  uint8_t *p = (uint8_t *) &value;
  uint16_t i;

  for (i = 0; i < sizeof value; i++)
    *p++ = Wire.read();

  return i;
}

Code:
// Receives 7 blocks of data via i2c from a Teensy LC that is measuring phase angles on 6 off 400Hz Synchros.
// received data structure should be: "2500  0  0  0  0  0  0"

#include <Wire.h>
//#include <WireData.h>
template <typename T> uint16_t wireReadData(T &value)
{
  uint8_t *p = (uint8_t *) &value;
  uint16_t i;

  for (i = 0; i < sizeof value; i++)
    *p++ = Wire1.read();			//CHANGED FROM Wire.read()

  return i;
}

struct data_block_struct
{
  unsigned long t1; // frame time referance i.e. frequency of Synchro 400Hz (2500us).
  unsigned long t2;
  unsigned long t3;
  unsigned long t4;
  unsigned long t5;
  unsigned long t6;
  unsigned long t7;
};

struct data_block_struct data_block;

//define I/O pins
byte led = 13;


void setup()
{

  pinMode (led, OUTPUT);

  Serial.begin (9600);

  Wire1.begin(); //Wire1 uses Port 1.
  Wire1.setClock(400000);

} //END OF SETUP.

void loop()
{
  digitalWrite (led, ! digitalRead(led)); // toggle the internal LED to check program execution speed
  Wire1.requestFrom(8, sizeof(data_block));
  wireReadData(data_block); // I think this line is to blame but don't know how to make it work with Port1.

  Serial.print (data_block.t1);
  Serial.print ("  ");
  Serial.print (data_block.t2);
  Serial.print ("  ");
  Serial.print (data_block.t3);
  Serial.print ("  ");
  Serial.print (data_block.t4);
  Serial.print ("  ");
  Serial.print (data_block.t5);
  Serial.print ("  ");
  Serial.print (data_block.t6);
  Serial.print ("  ");
  Serial.println (data_block.t7);


} // END OF LOOP.
 
Hi BriComp,

Works a treat, thank you so much for your help. I think I understand why it works but now I have got this going I can finish what I was doing and then spend more time understanding how the code is working. Appreciate the library "WireData.h" is exchanges for direct code instead.
I am actually a hardware engineer so trying to writing software is a fairly new activity for me!!

Thanks Again. BR Pete D.
 
Yea, I was trained as a Mechanical Engineer, first picked up electronics by reading the magazine Electronics Today International (ETI), unfortunately it's not with us any more, then bought a Nascom 2 in kit form. Assembled it, which started my software and further electronics electronics education.
Went through:- Basic, Z80 Assembler, Pascal, Modula2 and now C/C++.

Modula2 beats them all hands down.
You can be dirty if you like but you must explicitly do your dirty stuff.
 
I just wish I had started software earlier in my career, always seemed to me to be a black art. I ran an electronics company for ten years and now retired stared dabbling in my other hobby, flying. As a result I now use Teensys for high calibre hobby flight simulators driving simulated flight instruments. The most common these days being from Frasca. The Frasca electronics is very outdated and bulky. With a Teesny 4 I can drive up to 24 instruments through PWM channels from a card 4" x 2". It keeps me out of mischief, especially on a rainy day.

Thanks again.
 
Status
Not open for further replies.
Back
Top