I have bought a Digispark ATtiny85 board to learn about that microcontroller. So I tried using internal register names in the code rather than using Arduino in built functions. I can get the board to work when comes just input and output operations. For example the below code works and I can get the on board LED to turn ON and OFF
#include<avr/io.h>
void setup()
{
DDRB |=(1 << PB1); //set PB1 as output
}
void loop() {
PORTB |= (1 << PB1); // Turning ON LED connected to PB1
delay(1000); //Using Arduino IDE inbuilt delay function to generate delay of 1 second
PORTB &= ~(1 << PB1); //Turning the LED off
delay(1000);
}
I have tried using Timer0 of ATtiny85 module to generate time delay of one second. But unfortunately I couldn't get it to work.
#include<avr/io.h>
#define F_CPU 16500000UL
#include<util/delay.h>
void timer_config()
{
DDRB =0b00000010; // set PB1 as output
GTCCR|= (1<<TSM); ///Halt the timer for configuration
TCCR0A=0x00; //Normal mode
TCCR0B=0x00;
TCCR0B |= (1<<CS00)|(1<<CS02); //prescaling with 1024
TCNT0=0;
GTCCR&=~(1<<TSM); //Start the timer
}
void tmdel()
{
int i=0;
while(i<=6)
{
while((TIFR & (1 << TOV0) )==0); //Waiting for 0-255 and flag to raise
TIFR|=(1<<TOV0); //Clear the flag
i=i++; //increment by one
}
}
int main()
{
timer_config();
while(1)
{
PORTB|=(1<<PB1); //PortB1 high
tmdel(); //Delay of 1 second
PORTB&=~(1<<PB1); //PORTB1 low
tmdel();
}
}
I have used the overflow flag to identify TCNT0 register status. I have uploaded the above code but the LED isn't responding as anticipated.
When searching the internet I have got few suggestions that this might be because Arduino IDE uses timer registers for delay function. When user attempts to access timer registers it might not work as intended.
But am not sure about the validity of above opinion. Can anyone help me with it? Since am learning this controller, practicing core programming is quite important. If I cannot program ATtiny85 using Arduino IDE this way, can someone suggest a way or alternate hardware setup to program ATtiny85 and access its registers without a problem.
1 Answer 1
i=i++; //increment by one
The result of the above statement is not defined. That is, it is not necessarily "i + 1".
See:
Instead use:
i++;
Or:
i = i + 1;
A note about "undefined behavior":
http://en.wikipedia.org/wiki/Undefined_behavior
When an instance of undefined behavior occurs, so far as the language specification is concerned anything could happen, maybe nothing at all.
-
-
Very amusing. No.2020年09月19日 06:48:02 +00:00Commented Sep 19, 2020 at 6:48
Explore related questions
See similar questions with these tags.
i=i++
as I mentioned in my reply.