Atiny85 Programming with Arduino odd behavior

Discussion in 'Embedded Systems and Microcontrollers' started by durable126, Mar 20, 2016.

  1. durable126

    Thread Starter Member

    Feb 20, 2016
    56
    1
    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 (Text):
    1. /*
    2.   Blink
    3.   Turns on an LED on for one second, then off for one second, repeatedly.
    4.  
    5.   Most Arduinos have an on-board LED you can control. On the Uno and
    6.   Leonardo, it is attached to digital pin 13. If you're unsure what
    7.   pin the on-board LED is connected to on your Arduino model, check
    8.   the documentation at http://arduino.cc
    9.  
    10.   This example code is in the public domain.
    11.  
    12.   modified 8 May 2014
    13.   by Scott Fitzgerald
    14. */
    15.  
    16.  
    17. // the setup function runs once when you press reset or power the board
    18. void setup() {
    19.   // initialize digital pin 13 as an output.
    20.   pinMode(0, OUTPUT);
    21. }
    22.  
    23. // the loop function runs over and over again forever
    24. void loop() {
    25.   digitalWrite(0, HIGH);   // turn the LED on (HIGH is the voltage level)
    26.   delayMicroseconds(13);              // wait for a second
    27.   digitalWrite(0, LOW);    // turn the LED off by making the voltage LOW
    28.   delayMicroseconds(13);              // wait for a second
    29. }
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,789
    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.
     
    ErnieM likes this.
  3. durable126

    Thread Starter Member

    Feb 20, 2016
    56
    1
    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
     
  4. dannyf

    Well-Known Member

    Sep 13, 2015
    1,787
    360
    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.
     
  5. durable126

    Thread Starter Member

    Feb 20, 2016
    56
    1
    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
     
  6. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,789
    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.
     
  7. durable126

    Thread Starter Member

    Feb 20, 2016
    56
    1
    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!
     
  8. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,789
    You're definitely too smart for me. Hope you can solve your problem with all those resources at your disposal.
     
    nsaspook likes this.
  9. dannyf

    Well-Known Member

    Sep 13, 2015
    1,787
    360
    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.
     
  10. dannyf

    Well-Known Member

    Sep 13, 2015
    1,787
    360
    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 (Text):
    1.  
    2.     pwm0b_init();                            //reset the pwm module. phase correct pwm, top at OCR0A
    3.     OCR0A = F_CPU / F_PWM / 2;                //set period
    4.     pwm0b_setdc(OCR0A/2);                    //50% dc
    5.  
    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:
     
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    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.
     
Loading...