AtTiny85 8mhz

Go To Last Post
20 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


    #include <avr/wdt.h>
     
    short int potPos(0);
     
    void setup()
    {  
        wdt_disable();//Disable Watchdog
        sei(); //Enable Interrupts
        PLLCSR &= ~_BV(PCKE); //Set Synchronous mode (disable PLL)
        // This is meant to make PB0 & PB1 produce an inverted square wave at 32khz
        TCCR0A |= (1<<WGM01)|(1<<WGM00)|(1<<COM0A1)|(1<<COM0B0);
        // Time stuff
        TCCR0B |= (0<<CS02)|(0<<CS01)|(1<<CS00);
       
        // Duty Cycle or it is meant to meant
        OCR0A = 50;
        OCR0B = 50;
       
        // Pin modes
        DDRB |= (1<<DDB0)|(1<<DDB1);
     
    }
     
    void loop()
    {
        /*
        // How can I get this to change the duty cycle
        potPos = map(analogRead(A3),0,1023,0,255);
        */
    }

 

I had a go at getting an AtTiny85 to actually produce a respectable square wave. Suffice to say it did not end well even when doing my homework on Youtube. Could this be fixed or should I try bit banging it if so how would that work?

 

 

Last Edited: Mon. Oct 2, 2017 - 02:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How about a repost without the editing sequences?

 

"Did not end well..."  Perhaps elaborate--what DID you get?

 

Now, when doing setup, whether in the Arduino environment or not, I'll set pin directions first, set up the peripheral subsystem, [if necessary] clear any false hits, and then SEI.

 

This appears to be Arduino.  If so, what is with the |= sequences when setting up the timer?  Does the Arduino environment already have a default setup for timer(s)? [probably yes]

 

I forget whether for an AVR8 timer, whether the pin direction needs to be set to output before attaching to timer with COM bits.  I seem to recall that this is so.

 

[edit]  Looking more closely, you will, indeed, need to tell what you want to do.  If you want to use CTC mode with timer0 of that AVR model, then OCR0A is TOP and only OC0B can be an output.  But that model also has complementary output provisions -- but on timer1.

 

What do you mean by "inverted square wave"?  Two waves out of phase with each other?

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Mon. Oct 2, 2017 - 01:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

How about a repost without the editing sequences?

 

"Did not end well..."  Perhaps elaborate--what DID you get?

 

Now, when doing setup, whether in the Arduino environment or not, I'll set pin directions first, set up the peripheral subsystem, [if necessary] clear any false hits, and then SEI.

 

This appears to be Arduino.  If so, what is with the |= sequences when setting up the timer?  Does the Arduino environment already have a default setup for timer(s)? [probably yes]

 

I forget whether for an AVR8 timer, whether the pin direction needs to be set to output before attaching to timer with COM bits.  I seem to recall that this is so.

 

What do you mean by "inverted square wave"?  Two waves out of phase with each other?

 

 

I'm unable to adjust the duty cycle and PB1 is live while PB0 is dead. It is also under the ardunio ide.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would suggest you post your complete arduino sketch.

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

I would suggest you post your complete arduino sketch.

 

Jim

 

I've changed the post to include a link to pastebin with the code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The code in the OP was:

 

#include <avr/wdt.h>

short int potPos(0);

void setup()
{    
    wdt_disable();//Disable Watchdog
    sei(); //Enable Interrupts
    PLLCSR &= ~_BV(PCKE); //Set Synchronous mode (disable PLL)
    // This is meant to make PB0 & PB1 produce an inverted square wave at 32khz
    TCCR0A |= (1<<WGM01)|(1<<WGM00)|(1<<COM0A1)|(1<<COM0B0);
    // Time stuff
    TCCR0B |= (0<<CS02)|(0<<CS01)|(1<<CS00);
    
    // Duty Cycle or it is meant to meant
    OCR0A = 50;
    OCR0B = 50;
    
    // Pin modes
    DDRB |= (1<<DDB0)|(1<<DDB1);

}

void loop()
{
    /*
    // How can I get this to change the duty cycle
    potPos = map(analogRead(A3),0,1023,0,255);
    */
}

The OP had used the old style [ code ] and [ /code ] tags instead of using the modern method for posting code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Atton wrote:
I've changed the post to include a link to pastebin with the code.
Don't do that - no one here wants to go to a virus fest ! Just learn to post code properly. It starts here:

Click that button then paste your code into the pop-up window that appears.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Atton wrote:
I'm unable to adjust the duty cycle and PB1 is live while PB0 is dead. It is also under the ardunio ide.

So,

1)  Arduino may well have a pre-existing setup for the timer.  So get rid of the |=

2)  In my edit, I mentioned about OCR0A being TOP.

 

So, tell what waveform you really want and we'll see what can be done.  Can timer1 be used?

 

How do you adjust the duty cycle of a square wave?  I guess I'm thinking "square wave" is 50% duty.  Do you really want some kind of PWM mode?  Or adjustable frequency, 50% duty, square waves?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

Atton wrote:
I'm unable to adjust the duty cycle and PB1 is live while PB0 is dead. It is also under the ardunio ide.

So,

1)  Arduino may well have a pre-existing setup for the timer.  So get rid of the |=

2)  In my edit, I mentioned about OCR0A being TOP.

 

So, tell what waveform you really want and we'll see what can be done.  Can timer1 be used?

 

How do you adjust the duty cycle of a square wave?  I guess I'm thinking "square wave" is 50% duty.  Do you really want some kind of PWM mode?  Or adjustable frequency, 50% duty, square waves?

I think on timer 0 the ardunio back end eats up the timer 0, I don't really know how to actually fix that one. But the desired output is like the 4047 albeit at a fix frequency with a variable duty cycle output. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Atton wrote:
I don't really know how to actually fix that one.

I told you how.  Get rid of the |=.  But first:

Atton wrote:
So, tell what waveform you really want and we'll see what can be done. Can timer1 be used?

Your answer didn't tell me...

Atton wrote:
But the desired output is like the 4047 albeit at a fix frequency with a variable duty cycle output.

What is a "4047"?  Show a desired waveform, even if "ASCII art".  Do you want complementary outputs, or not?  Or two identical outputs, as your OP hinted?  It sounds like you really want "PWM".  For the desired 32kHz frequency and two outputs, that might be difficult with timer0.  Can timer1 be used or not?  How close do you need to be with 32kHz period of the PWM? 

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If indeed we use a fixed TOP of 0xff on timer0 with a nominal 8MHz AVR frequency, then at /1 the period is pretty close.  So use Fast PWM.  Invert the second channel or not, as is your wish.  For non-inverted, make PB0 and PB1 outputs and:

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 8000.000 kHz
// Mode: Fast PWM top=0xFF
// OC0A output: Non-Inverted PWM
// OC0B output: Non-Inverted PWM
// Timer Period: 0.032 ms
// Output Pulse(s):
// OC0A Period: 0.032 ms Width: 8.0314 us
// OC0B Period: 0.032 ms Width: 8.0314 us
TCCR0A=(1<<COM0A1) | (0<<COM0A0) | (1<<COM0B1) | (0<<COM0B0) | (1<<WGM01) | (1<<WGM00);
TCCR0B=(0<<WGM02) | (0<<CS02) | (0<<CS01) | (1<<CS00);
TCNT0=0x00;
OCR0A=0x40;
OCR0B=0x40;

Arbitrary duty cycle of 25%.  Not an |= in sight.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
What is a "4047"?
Googling suggest an Astable multivibrator. As far as I can see it has 3 outputs that are all 50:50 square waves (no PWM). One is OSC_OUT and the Q/_Q outputs are then the same thing divided by 2 either in phase or inverted.

 

http://www.learningaboutelectron...

 

Output waveform signals of a 4047 multivibrator

Last Edited: Mon. Oct 2, 2017 - 02:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is being used to drive a h bridge that then drives a transfomer. If that clears up the kind of signal that I am looking to target.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Atton wrote:
This is being used to drive a h bridge that then drives a transfomer. If that clears up the kind of signal that I am looking to target.

Not really.  Is this the case where you need dead time?

 

The sparkies will need to comment beyond.  High-speed PWM?  Perhaps, to avoid audible whine.  PWM?  What would the purpose of that be when driving a transformer?

 

I asked several times about timer1.  Do you realize that your chosen AVR model has a built-in dead-time generator as well as complementary outputs on timer1?  And in addition, timer1 can be driven from the fast PCK?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sounds like he's trying to make some kind of inverter, as Theusch has pointed out, T1 is the timer to use, as it has dead time generators needed to prevent shoot through in your half bridge driver(assuming the HB does not have dead time built in).

 

 The more info you provide us, the better answers we can provide!!! 

Please tell us what it is your trying to do, first an overall project description, then ask how to product the output you want.

 

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

TCCR0A is timer 1

TCCR0B is timer 0 (The Ardunio software might be eating this, based on something said during a Youtube video I saw while back.)

From what I understand in this project I am using both timers. I am rather new to using registers in general if that wasn't already obvious.

The application requires a PWM signal to adjust for changes in voltage via a pot. To drive the H bridge the signal must be inverted in order to push and pull current through the windings. 

The transformer in question has an extremely low inductance, thus requiring the use of a high frequency square wave signal. Think of it like any brushed motor driver where you can't hear the PWM signal where you can adjust the motor direction.

Brb pulling out hair.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Atton wrote:
TCCR0A is timer 1 TCCR0B is timer 0

???

 

Say what?!?

 

TCCR0A is one of the I/O registers related to timer0.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Atton wrote:
TCCR0A is timer 1

TCCR0B is timer 0

 

Wrong!  the zero in the register name is the Timer number,

 

TCCR1a = timer 1  Timer Counter Control Register (1)a

 

For this app, do not use two timers, you need to use one timer with complimentary outputs, that will let you change TOP and PWM duty cycle.

 

You still have not told us what the project is?

 

Jim

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From my understanding, the OP wants 2 complementary square waves? So #11 already has the answer, you just need to adjust the duty cycle to 50% and invert one of the channels.

If the problem is the Arduino using timer 0, it doesn't matter, just use main() instead of the setup() and loop() stuff, this will prevent the init code from running.
 

About the dead time, if timer 1 has a dead time generator, great; in any case, phase correct PWM can be used to create dead time using any timer, similar to what I did here: http://www.avrfreaks.net/forum/u...

Last Edited: Mon. Oct 2, 2017 - 08:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

El Tangas wrote:

From my understanding, the OP wants 2 complementary square waves? So #11 already has the answer, you just need to adjust the duty cycle to 50% and invert one of the channels.

If the problem is the Arduino using timer 0, it doesn't matter, just use main() instead of the setup() and loop() stuff, this will prevent the init code from running.
 

About the dead time, if timer 1 has a dead time generator, great; in any case, phase correct PWM can be used to create dead time using any timer, similar to what I did here: http://www.avrfreaks.net/forum/u...

Dead on