need help with my code ,thx

hey guys,

I am practicing on the millis() function. I wrote my code like this:

#include<Wire.h>
int ini = 0;
unsigned long pre_timer = 0;
unsigned long cur_timer;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void i2c_read()
{
Wire.beginTransmission(0x5A);
Wire.write(0x80);
Wire.endTransmission(false);
Wire.requestFrom(0x5A, 32);
while(Wire.available()) // slave may send less than requested
{
int c = Wire.read(); // receive a byte as character
Serial.print(c);
Serial.println();
}
Wire.endTransmission();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup()
{
pinMode(11, OUTPUT);
pinMode(6, OUTPUT);
TCCR2A = ((1 << WGM21) | (1 << COM2A0));
TCCR0A = ((1 << WGM01) | (1 << COM0A0));
TCCR2B = (1 << CS20); //8MHz
TCCR0B = ((1 << CS01) | (1 << CS00));
TIMSK2 = 0;
TIMSK0 = 0;
OCR2A = 0; //change to 3 for 2KHz
OCR0A = 11;
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop()
{
cur_timer = millis();
if(ini == 0)
{
Serial.print(cur_timer);
Serial.println();
if((cur_timer - pre_timer) >1000)
{
ini = 1;
Serial.print(“hello”);
Serial.println();
}
else
{
Serial.print(“hello_2”);
Serial.println();
}
}
else
{
Serial.print(“not_hello”);
Serial.println();
}
Serial.print(“delay”);
Serial.println();
delay(1000);
}

I am doing some I2C practice too, so there are some extra clk generation and I2C read function in the code,
I thought the extra code won’t do anything harm but I am wrong, with those code, I can only see the serial printed :

0
hello2
delay

It seems the loop ends after the first try. but if I remove the extra code, it just works fine.
Can anyone help me find out the conflict please? Thx

It seems the loop ends after the first try. but if I remove the extra code, it just works fine.

What code do you consider "the extra code"?

void i2c_read()
{
Wire.beginTransmission(0x5A);
Wire.write(0x80);
Wire.endTransmission(false);
Wire.requestFrom(0x5A, 32);
while(Wire.available()) // slave may send less than requested
{
int c = Wire.read(); // receive a byte as character
Serial.print(c);
Serial.println();
}
Wire.endTransmission();
}

and

pinMode(11, OUTPUT);
pinMode(6, OUTPUT);
TCCR2A = ((1 << WGM21) | (1 << COM2A0));
TCCR0A = ((1 << WGM01) | (1 << COM0A0));
TCCR2B = (1 << CS20); //8MHz
TCCR0B = ((1 << CS01) | (1 << CS00));
TIMSK2 = 0;
TIMSK0 = 0;
OCR2A = 0; //change to 3 for 2KHz
OCR0A = 11;
Wire.begin(); // join i2c bus (address optional for master)

If I remove these two part, it just worked.

Since you never call i2c_read(), that is not the problem code. Messing with the timers, without implementing a timer callback routine probably is. Why are you doing that?

PaulS:
Since you never call i2c_read(), that is not the problem code. Messing with the timers, without implementing a timer callback routine probably is. Why are you doing that?

I am trying to read from a I2C chip, and I need to generate 2 clk for the chip. that’s why I used Timer 0 and 2.

and I need to generate a delay before I start to read from that chip, that is why I wrote this code.

can you explain more about the timer callback routine please?

gzanta:
It seems the loop ends after the first try. but if I remove the extra code, it just works fine.
Can anyone help me find out the conflict please? Thx

What does

TIMSK0 = 0;

do?

If it switches off Timer0 interrupts it probably stops millis() from working.

Please use the code button </>

so your code looks like this

and is easy to copy to a text editor

…R

Robin2:
What does

TIMSK0 = 0;

do?

If it switches off Timer0 interrupts it probably stops millis() from working.

Please use the code button </>

so your code looks like this

and is easy to copy to a text editor

…R

Thanks for the reply,

TIMSK0 is the Timer/Counter Interrupt Mask Register, I deleted that line, and it still not working .

IT seems the timer parts caused the problem,since I removed the I2C part and it didn’t help.

IT seems the timer parts caused the problem

Yes, you messed up timer0 and the millis() function is not working. There is more wrong than TIMSK0=0.

You have changed the mode from the default mode (Fast PWM to 255 with TCCR0A = ((1 << WGM01) | (1 << WGM00)) and you need to have the overflow interrupt enabled TIMSK0 = (1<<TOIE0)

You did get the prescaler = 64 correct. TCCR0B = ((1 << CS01) | (1 << CS00))

cattledog:
Yes, you messed up timer0 and the millis() function is not working. There is more wrong than TIMSK0=0.

You have changed the mode from the default mode (Fast PWM to 255 with TCCR0A = ((1 << WGM01) | (1 << WGM00)) and you need to have the overflow interrupt enabled TIMSK0 = (1<<TOIE0)

You did get the prescaler = 64 correct. TCCR0B = ((1 << CS01) | (1 << CS00))

THX, seems that’s the reason, if I remove the timer0 related code, it just worked fine.