ATTINY441 PWM with Timer Over Flow

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

 

Hello dear friends.
My knowledge in AVR is not perfect and I ran into a problem.

 

I want to use FAST PWM on timer 0 and timer overflow.

I found a similar discussion on this forum. Link. The author used a slightly different mode of operation, but he succeeded.

I was hoping it was possible. However, when I started the tests, it turned out that it didn't work.

 

In the datasheet, section "11.7.3 Fast PWM Mode" contains the following text:

 

 

If the interrupt can be used to update the compare value, then it can be used for other tasks as well.

Here is what I tried to do:

 

#define F_CPU 8000000UL

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>

#define LED_test PB0

void port_ini(void)
{
	DDRA  |=   (1<<PA2); // OUTPUT
	PORTA &=  ~(1<<PA2);  // off

	DDRB  |=   (1<<LED_test); //OUTPUT
	PORTB &=  ~(1<<LED_test);  // off
}

void init_PWM_FAN_timer(void)
{
	TOCPMSA0 |= ((1<<TOCC1S0) | (1<<TOCC1S1));  // Connect the PA2 (TOCC1) port to the OCR0A compare register
	TOCPMCOE |= (1<<TOCC1OE); // Enable output compare channel on port PA2 (TOCC1)
	TCCR0A |= ((1<<WGM01) | (1<<WGM00) | (1<<COM0A1)); // FAST PWM  (TOV FLAG Set on MAX (0xFF))

	TCCR0B  |= (1<<CS01) | (1<<CS00); // clkI/O/64 (From prescaler)
	TCNT0 = 0x00; //  to start counting from zero.
	OCR0A = 0; // PWM 0%
	TIMSK0 |= (1<<TOIE0); // overflow interrupt enable
}

ISR(TIMER0_OVF_vect) // Timer 0 Overflow Interrupt
{
	PORTB ^=  (1<<LED_test);
}

int main(void)
{
	port_ini();
	init_PWM_FAN_timer();
	sei(); // Global interrupt enable

	while (1)
	{

	}
}

 

Unfortunately this code doesn't work. The LED is off. (The interrupt should be triggered at ~ 490 Hertz and the LED should always appear to be on). (8 MHz/64 divider) and / 255 (0xFF) ~ 490 Hz Overflow.

 

 

If someone faced a similar task or problem, I would be very grateful for your comment.

I did not use (OCR0B) in this example because it is already taken in my project. Timer 1 and Timer 2 are also busy.

 

 

 

Last Edited: Tue. Nov 23, 2021 - 04:33 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

radiotehnik777 wrote:

ISR(TIMER0_COMPA_vect) // Timer 0 Overflow Interrupt

That is the COMPA interrupt vector, not the overflow vector, you want the Overflow interrupt vector, so you have the wrong name.....

Most likely TIMER0_OVF_vect or TIM0_OVF_vect  or similar for your tool chain...

 

Jim

 

 

Keys to wealth:

Invest for cash flow, not capital gains!

Wealth is attracted, not chased! 

Income is proportional to how many you serve!

Lets go Brandon!

Last Edited: Tue. Nov 23, 2021 - 04:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Just to say that the Studio 7 simulator is a great pace to try out code like this - see what actually happens - where does the PC go? (I predict it ends in the infinite loop at _exit after _bad_interrupt).

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

Hi, thanks for the comment.
But this is just a misprint .... I corrected the post so as not to mislead people anymore.

This code still doesn't work, unfortunately.

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

Dear friends.
I am sorry for disturbing you.
When I described my problem and revised the code again, I found a mistake.
Incorrectly set the bit in register TIMSK0 .
(I edited the post. Now everything works)

I hope that if someone will work with Attiny441 in the future, they will see all my failures and will not repeat my mistakes.

Thank you all for your quick response!

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

radiotehnik777 wrote:
corrected the post so as not to mislead people anymore.
Please don't do that or anyone reading this entire thread won't be able to make sense of chronology. In future if you have updated/corrected code post the new version at the end of the thread.

 

Moderator.

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

clawson wrote:

Please don't do that or anyone reading this entire thread won't be able to make sense of chronology. In future if you have updated/corrected code post the new version at the end of the thread.

 

Ok, I understand you.
I won't do that anymore. Thanks.