Page 1 of 1
[SOLVED] BluePill I2C Wire.setClock(400000) Stops USB working
Posted: Wed Apr 01, 2020 8:38 am
by Jimbo13
On my BluePill programme, using I2C , when I include the line "Wire.setClock(400000);" the USB port, which was working before, Stops working.
The I2C was working before as well.
I have "wire version=1.0" installed.
Any suggestions please.
Re: BluePill I2C Wire.setClock(400000) Stops USB working
Posted: Wed Apr 01, 2020 10:04 am
by fpiSTM
Seems obvious as there is no link btw them.
Could you share you sketch?
And also provide all options sets in the Arduino menu, please.
Re: BluePill I2C Wire.setClock(400000) Stops USB working
Posted: Wed Apr 01, 2020 10:32 am
by Jimbo13
I am using Visual Micro.

- Annotation 2020-04-01 112459.jpg (43.42 KiB) Viewed 7946 times
Even setting "Wire.setClock(100000);" stops USB and I get a windows message "USB Device not recognised etc", and the LED does not blink.
The programme seems to compiles and uploads OK.
As you can see I have remmed out most things.
Code: Select all
//thanks to JoopBrokking http://www.brokking.net/imu.html
#include <Wire.h>
#include <LiquidCrystal_I2C.h> //johnrickman/LiquidCrystal_I2C
//Declaring global variables
int gyro_x, gyro_y, gyro_z;
long acc_x, acc_y, acc_z, acc_total_vector;
int temperature;
long gyro_x_cal, gyro_y_cal, gyro_z_cal;
long loop_timer;
int lcd_loop_counter;
float angle_pitch, angle_roll, angle_yaw;
int angle_pitch_buffer, angle_roll_buffer, angle_yaw_buffer;
//boolean set_gyro_angles;
float angle_roll_acc, angle_pitch_acc, angle_yaw_acc;
float angle_pitch_output, angle_roll_output, angle_yaw_output;
LiquidCrystal_I2C lcd(0x27, 16, 4);//Initialize the LCD library
void setup() {
pinMode(PC13, OUTPUT);// Set up the built-in LED pin as an output:
// Serial.begin(9600);//not needed
// Wire.setClock(100000);
Wire.begin();
delay(250);
//setup_mpu_6050_registers();
}
// the loop function runs over and over again until power down or reset
void loop() {
digitalWrite(PC13, !digitalRead(PC13));
delay(500); //change each build as programme check
Serial.println("Hello Jimmy");
/*
read_mpu_6050_data();
Serial.print("X = ");
Serial.println(gyro_x);
Serial.print("Y = ");
Serial.println(gyro_y);
Serial.print("Z = ");
Serial.println(gyro_z);
delay(250);
*/
}
void read_mpu_6050_data() { //Subroutine for reading the raw gyro and accelerometer data
Wire.beginTransmission(0x68); //Start communicating with the MPU-6050
Wire.write(0x3B); //Send the requested starting register
Wire.endTransmission(); //End the transmission
Wire.requestFrom(0x68, 14); //Request 14 bytes from the MPU-6050
while (Wire.available() < 14); //Wait until all the bytes are received
acc_x = Wire.read() << 8 | Wire.read(); //Add the low and high byte to the acc_x variable
acc_y = Wire.read() << 8 | Wire.read(); //Add the low and high byte to the acc_y variable
acc_z = Wire.read() << 8 | Wire.read(); //Add the low and high byte to the acc_z variable
temperature = Wire.read() << 8 | Wire.read(); //Add the low and high byte to the temperature variable
gyro_x = Wire.read() << 8 | Wire.read(); //Add the low and high byte to the gyro_x variable
gyro_y = Wire.read() << 8 | Wire.read(); //Add the low and high byte to the gyro_y variable
gyro_z = Wire.read() << 8 | Wire.read(); //Add the low and high byte to the gyro_z variable
}
void setup_mpu_6050_registers() {
//Activate the MPU-6050
Wire.beginTransmission(0x68); //Start communicating with the MPU-6050
Wire.write(0x6B); //Send the requested starting register
Wire.write(0x00); //Set the requested starting register
Wire.endTransmission(); //End the transmission
//Configure the accelerometer (+/-8g)
Wire.beginTransmission(0x68); //Start communicating with the MPU-6050
Wire.write(0x1C); //Send the requested starting register
Wire.write(0x10); //Set the requested starting register
Wire.endTransmission(); //End the transmission
//Configure the gyro (500dps full scale)
Wire.beginTransmission(0x68); //Start communicating with the MPU-6050
Wire.write(0x1B); //Send the requested starting register
Wire.write(0x08); //Set the requested starting register
Wire.endTransmission(); //End the transmission
}
Re: BluePill I2C Wire.setClock(400000) Stops USB working
Posted: Wed Apr 01, 2020 11:42 am
by fpiSTM
Try the setClock after the WireBegin
Re: BluePill I2C Wire.setClock(400000) Stops USB working
Posted: Wed Apr 01, 2020 12:43 pm
by Jimbo13
OK , thanks that worked.
I followed this video which states that the Setclock() should be called before the wire.begin otherwise the I2c will run at the default speed.
Look at 4 minutes 30.
https://www.youtube.com/watch?v=ImctYI8hgq4
I also did do a lot of searching to find an example which included the Setclock but could not find anything. I aslo tried to see what was happening in the library but it got too complicated.
Jim
Re: [SOLVED] BluePill I2C Wire.setClock(400000) Stops USB working
Posted: Wed Apr 01, 2020 2:16 pm
by fpiSTM
In our implementation setClock assume the Wire is initialized.
https://www.arduino.cc/en/Reference/WireSetClock
Even the API is not clear. It only specify "modifies".
Re: [SOLVED}BluePill I2C Wire.setClock(400000) Stops USB working
Posted: Wed Apr 01, 2020 2:19 pm
by fpiSTM
About the video, it uses Roger's core not the STM32 core.
Re: [SOLVED}BluePill I2C Wire.setClock(400000) Stops USB working
Posted: Wed Apr 01, 2020 2:52 pm
by Jimbo13
In our implementation setClock assume the Wire is initialized.
And in the Arduino link:
https://www.arduino.cc/en/Reference/WireSetClock
There is no mention that Wire should be initialized before setClock.
I just noticed that a long way down in the comments for the video somebody does mention that with the new STM32 the setClock must go after Wire.begin(), where did they get that information from or was it just by experimenting?
Its an uphill struggle to do anything in STM32 as there seems not to be much information for the beginner.