Atiny85 Programming with Arduino odd behavior

Thread Starter

durable126

Joined Feb 20, 2016
56
So I'm trying to run a simply sketch on an atiny85

I am able to program the board correctly

the function I'm having difficulty with is delayMiroseconds
Im running the 85 in 8MHZ mode on 1.6.7

Im trying to get a certain frequency of digital oscillation.

38khz

The period in seconds obviously being 0.0000263
divide that by 2 to get the appropriate delay roughly 13 microseconds should get me close


When i put the chip on the scope with nothing connected to output pin 0 except the scope probe the square wave looks terrible with very large rising edge peaks. its also showing about 3khz not 38khz like it should me

Can anyone help me figure out why the delayMicrosecond function is not working properly? I was told it works just fine on a tiny85 and accurately when running the chip at 8mhz internal clock

It should be noted that i have also tried running the atiny85 in both 1meg and 8 meg mode. Seems to just split the output frequency in half

There are many threads about this working down to about 3 microseconds so i should be ok at 13

all I'm using is the blink example with the delayMicro for testing


Code:
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at http://arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
*/


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(0, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(0, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds(13);              // wait for a second
  digitalWrite(0, LOW);    // turn the LED off by making the voltage LOW
  delayMicroseconds(13);              // wait for a second
}
 

Papabravo

Joined Feb 24, 2006
21,159
In order for those library delay functions to work properly, there needs to be a definition that they can use to determine the system frequency. This may default to some value that makes it unlikely to function correctly. Reread the documentation carefully. It may be that the part is not fast enough to do what you want it to. Again, verify each and every one of your assumptions.
 

Thread Starter

durable126

Joined Feb 20, 2016
56
In order for those library delay functions to work properly, there needs to be a definition that they can use to determine the system frequency. This may default to some value that makes it unlikely to function correctly. Reread the documentation carefully. It may be that the part is not fast enough to do what you want it to. Again, verify each and every one of your assumptions.
believe me i have verified over and over again

the system frequency is defined in the sketch and setup by the boot loader


And from everything i have read, it is possible to get this work accurately down to 3 microseconds
Do you have anything useful to add except "go back and read the data sheets"

If not I'm not sure why you bothered commenting. Clearly i have read the data sheets about 40 times before i posted this
 

dannyf

Joined Sep 13, 2015
2,197
delay roughly 13 microseconds should get me close
I think you are likely seeing the ugly head of Arduino's IO functions: they take a long time to execute.

For what you are trying to do, use the timer's pwm function.
 

Thread Starter

durable126

Joined Feb 20, 2016
56
I think you are likely seeing the ugly head of Arduino's IO functions: they take a long time to execute.

For what you are trying to do, use the timer's pwm function.
Thanks for the reply

Can you tell me what the name of the function is so i can explore it as an option?

are you talking about an arduino function or an assembly level AVR instruction

Thanks
 

Papabravo

Joined Feb 24, 2006
21,159
believe me i have verified over and over again

the system frequency is defined in the sketch and setup by the boot loader


And from everything i have read, it is possible to get this work accurately down to 3 microseconds
Do you have anything useful to add except "go back and read the data sheets"

If not I'm not sure why you bothered commenting. Clearly i have read the data sheets about 40 times before i posted this
You're the one that asked for help and there is a great deal of information that you have not provided. I'm doing the best I can with minimal information. But hey, if you don't need my help then so be it.
 

Thread Starter

durable126

Joined Feb 20, 2016
56
You're the one that asked for help and there is a great deal of information that you have not provided. I'm doing the best I can with minimal information. But hey, if you don't need my help then so be it.
Minimal information?

I gave you example code, i gave you version of software and component numbers.

Your reply was. "read the data sheet"
Yea you were sooooooo helpful!
 

dannyf

Joined Sep 13, 2015
2,197
an you tell me what the name of the function is so i can explore it as an option?
Check the datasheet, under the timer / pwm section, for either output compare or pwm mode. Essentially, you set up the timer to generate a fixed time base (for 38Khz), and then set the output compare to 50% duty cycle.
 

dannyf

Joined Sep 13, 2015
2,197
To give you a sense of how it can be done (fairly easily as well), here is an example of using the timer0 pwm phase correct function to generate a 19Khz 50% duty-cycle square wave.

Code:
    pwm0b_init();                            //reset the pwm module. phase correct pwm, top at OCR0A
    OCR0A = F_CPU / F_PWM / 2;                //set period
    pwm0b_setdc(OCR0A/2);                    //50% dc
F_PWM here is set to 38000ul, and F_CPU is set to 8Mhz.

Obviously, you will need to write your own pwm0b_init() and pwm0b_setdc() functions, but it is fairly simple once you go through the datasheet.

The resulting signal is 19Khz, due to the use of phase correct pwm, as confirmed by our simulation:
 

Attachments

ErnieM

Joined Apr 24, 2011
8,377
the system frequency is defined in the sketch and setup by the boot loader
One may wonder how the code compiler has any clue what the bootloader is doing, or even how it finds this mysterious "sketch."

Perhaps you should go back to whomever told you this should work and ask them for clarification.
 
Top