PWM with attiny85. help.

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

why is PWM not working?

#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
/*fPWM = fclk_I/O / N. 256*/
/*N = prescaler*/
void PWM_config()
{
	DDRB =(1<<PB1)|(1<<PB0); // set PB1 and PB0 as output to enable PWM generation
	TCCR0A= 0x00;
	TCCR0A|= (1<<WGM00) | (1<<WGM01) | (1<<COM0A1) | (1<<COM0B1) | (1<<COM0A0) | (1<<COM0B0);
	TCCR0B= 0x00;
	TCCR0B = (0<<WGM02) | (1<<CS00); //no prescaling
	TCNT0=0;      
	OCR0A = 0;
	OCR0B = 0;     
}

int main()
{
	PWM_config();
	while(1)
	{
		OCR0A=50;
		OCR0B=50;
	}
}

 

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

what is not working? and how have you determined that it is not working?

and why have you not added comments to your code such that we know what you intend to do, but perhaps are not doing.

Also not sure if continuously writing to the OCR registers is something you want to do.

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

Do you have a scope to check how it is working?

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

meslomp wrote:
Also not sure if continuously writing to the OCR registers is something you want to do

 

Incorrect of course, but no harm expected, and btw. some threads does have ADC run in a parallel, providing OCR value, so that it can be visible by a LED.

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

I would write so that humans can understand it e.g.

void PWM_config()
{
	DDRB = (1<<PB1)|(1<<PB0); // set OC0B, OC0A as output to enable PWM generation
	TCCR0A = (3<<WGM00) | (3<<COM0A0) | (3<<COM0B0); //WGM=3 Fast PWM, inverting mode
	TCCR0B = (0<<WGM02) | (1<<CS00); //div 1
	TCNT0 = 0; //these default to 0 but does no harm
	OCR0A = 0;
	OCR0B = 0;
}

Untested.  But everything looks fine to me.   A bit pointless to assign OCR0A, OCR0B continuously.   You could have put this in the config function.

 

What is your problem?

 

David.

Last Edited: Thu. Nov 25, 2021 - 12:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Context: this code is PWM code from a previous project, from a few months back. I assumed at first that the problem is with my setup, connections, and whatnot, but after testing and testing this seems to work perfectly so the only problem can be with the code. maybe I changed it, I am not sure. I don't have a scope to check if it is working or not. no matter how much I adjust the PWM, the brightness of the LED used to test it is full. what could the issue be? also, I'm open up to suggestions of what to check for the setup itself in case it's not a code issue.

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

Sorry, confused:

anonymous69 wrote:
after testing and testing this seems to work perfectly ... I don't have a scope to check if it is working or not

So how did you test it to determine that it "seems to work perfectly" ?

 

the code. maybe I changed it, I am not sure

time to start using a revision tracking system, eg Git

Then you would not only know whether it had been changed, but also exactly what was changed, and when.

 

And you could easily go back to the last working version ...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would not expect the average user to own oscilloscope, Logic Analyser, ... or even a DMM.  (Digital MultiMeter)

 

If you write your code neatly,  that is all that you need.   PWM_config() is only 3 (useful) lines.

And you can test with a single LED + resistor.    Both inverted and non-inverted modes.

 

Life is more complicated with the newer AVRs or an ARM chip.

 

If you are interested in the hobby,  buy a $10 Saleae clone Logic Analyser,   a pack of Dupont cables,   a Uno clone.  (or an XMINI / Curiosity board).

 

David.

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

no matter how much I adjust the PWM, the brightness of the LED used to test it is full. what could the issue be?

Are you on the right pin?

What values did you try...a sensitive led could look rather bright at any level, depending on the resistor.  Did you try 1,2, 3, etc?  How about the high end, 250 (or 65530) if the PWM is upside down?

Better to use a 10uf cap and 1k resistor and measure the voltage of the filtered RC, will be much more accurate reading than an LED 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:

Better to use a 10uf cap and 1k resistor and measure the voltage of the filtered RC, will be much more accurate reading than an LED 

An LED should work.   e.g. OCR0A = 0 or OCR0A = 255 will give different brightness.   Obviously you need a resistor.

A filtered RC should work with a DMM.

A simple R will work with an old-fashioned moving coil meter.

 

Not every beginner will have a DMM (or a 10uF capacitor).   But they are likely to own an LED + resistor.

You can do sophisticated stuff with $20.   Wait until you are rich before buying a scope.

 

David.

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

Not every beginner will have a DMM

If you are building microcontroller circuits you'd should certainly have at least the basic parts and tools.  Get a kit of resistor and capacitors when you set up shop.  If your are serious about electronics, you should def get a scope and cheapo logic analyzer---they will be invaluable.  Maybe you can scrape by without any parts or tools, if you are just messing around with off the shelf boards, but go for the whole kaboodle & be ready for anything.  

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

I got it working. I don't know how. I was going to take different readings by putting my multimeter leads in a screw terminal, upon doing so the readings showed the PWM to be working perfectly fine. the only thing I changed in the code was setting PB0 and PB1 high but even when I removed that it still worked... ill take the miracle. yes