Cannot Use I2C and Timer Simultaneously
Posted: Tue Apr 13, 2021 7:08 am
Dear All,
I am trying to use timers and I2C simultaneously, for which I have initialized timer and I2c in void setup(), but after initializing only the timer is working and I2C is working, however, if I initialize I2C before the timer in the setup, I2C is working but the timer is not. So, I created a new task where I am initializing I2C. Since I have two I2C sensors with the same address, and whose address cannot be changed, I have two initialize two I2C buses. Now after initializing both the I2C buses in the same task, I cannot access data from the second I2C sensor(initialized on a new I2C bus). I feel that there are some issues in using the timer and I2C simultaneously, I am not able to solve them. Kindly help me with this issue.
I am trying to use timers and I2C simultaneously, for which I have initialized timer and I2c in void setup(), but after initializing only the timer is working and I2C is working, however, if I initialize I2C before the timer in the setup, I2C is working but the timer is not. So, I created a new task where I am initializing I2C. Since I have two I2C sensors with the same address, and whose address cannot be changed, I have two initialize two I2C buses. Now after initializing both the I2C buses in the same task, I cannot access data from the second I2C sensor(initialized on a new I2C bus). I feel that there are some issues in using the timer and I2C simultaneously, I am not able to solve them. Kindly help me with this issue.
Code: Select all
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Test Code for SFM3300");
Initialize_Timer();
//I2C_Default.begin(SDA1,SCL1);
//I2C_New.begin(SDA2,SCL2);
//FlowSensor_Ins.begin(&I2C_Default);
//FlowSensor_Exp.begin(&I2C_New);
//FlowSensor_Ins.Start_Measurement();
//FlowSensor_Exp.Start_Measurement(); /* pin task to core 0 */
xTaskCreatePinnedToCore(
DisplayFlowTask, /* Task function. */
"Task2", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task2, /* Task handle to keep track of created task */
0);
delay(1000);
//FlowSensor_Ins.Start_Measurement();
}
uint16_t RawFlowVal_Exp=0;
uint16_t RawFlowVal_Ins=0;
float Flow_Exp,Flow_Ins=0;
void loop()
{
// put your main code here, to run repeatedly:
// Serial.println("================================================================");
// Serial.print("Flow Expiratory:");
delay(100);
}
void Initialize_Timer(void) {
// Use 1st timer of 4
timer2 = timerBegin(2, 80, true); /* 1 tick take 1/(80MHZ/80) = 1us so we set divider 80 and count up */
// timer1 = timerBegin(1, 80, true);
timerAttachInterrupt(timer2, &onTimer2, true); /* Attach onTimer function to our timer */
// timerAttachInterrupt(timer1, &onTimer1, true);
// Repeat the alarm (third parameter) /
timerAlarmWrite(timer2, 1000000, true); /* Set alarm to call onTimer function every second 1 tick is 1us => 1 second is 1000000us */
// timerAlarmWrite(timer1, GI16u_http_post_delay * 1000, true);
timerAlarmEnable(timer2); /* Start an alarm */
// timerAlarmDisable(timer1);
}
void IRAM_ATTR onTimer2(void)
{
Count++;
}
void DisplayFlowTask( void * pvParameters )
{
I2C_Default.begin(SDA1,SCL1);
FlowSensor_Ins.begin(&I2C_Default);
FlowSensor_Ins.Start_Measurement();
I2C_New.begin(SDA2,SCL2);
FlowSensor_Exp.begin(&I2C_New);
FlowSensor_Exp.Start_Measurement();
delay(1000);
for (;;)
{
// Serial.println("From Task2");
Serial.println("***********************");
Serial.println("Flow Inspiratory:");
RawFlowVal_Ins=FlowSensor_Ins.ReadFlowVal();
Flow_Ins = (((float)RawFlowVal_Ins - 32768) / 120);
Serial.println(Flow_Ins);
Serial.print("Timer Count:");Serial.println(Count);
RawFlowVal_Exp=FlowSensor_Exp.ReadFlowVal();
Flow_Exp = (((float)RawFlowVal_Exp - 32768) / 120);
Serial.print("Final Exp:");
Serial.println(Flow_Exp);
Serial.println("--------------------");
delay(2000);
}
}