1

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.

3
  • Since the Arduino ecosystem is open-sourced, you could look into their sources to find out about timer usage. -- However, as the Arduino IDE comes with a complete GCC for AVR, you can use any editor or IDE you like and write software without using the Arduino IDE / library. Sep 18 '20 at 6:30
  • Your code (the second one) does not use any Arduino functions, and thus the IDE is not relevant. It would help to indent your code in the usual way, to make it more readable.
    – Nick Gammon
    Sep 18 '20 at 6:44
  • Without going through your code line-by-line, I would say your issue is the registers you have used. The IDE is not relevant to your problem. Or possibly your use of i=i++ as I mentioned in my reply.
    – Nick Gammon
    Sep 18 '20 at 6:48
6
 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.

2
  • i = ++i; then? q-;
    – Gerben
    Sep 18 '20 at 13:58
  • Very amusing. No.
    – Nick Gammon
    Sep 19 '20 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.