Re:how to find count for 1sec and 59mins and 59 sec's for fast PWM for attiny45

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

Hi,

 

I am new to this contorller ,can any one suggest me how to find the exact count for 1sec and 59mins and 59 sec's for  fast PWM for attiny45.i would like to use 1MHZ clock,kindly guide me on this

 

 

thanks&Regards

Samathasan

This topic has a solution.

Last Edited: Mon. Mar 23, 2015 - 04:57 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why fast pwm? Can you not use ctc mode and set the timer to say, 10ms. You can then count in terms of 10ms for whatever time period you require.
1second is 100, 10ms ticks
59 seconds is 5900 ticks
59 minutes is 59 times 60 times 100 ticks.

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

Hi Kartman 

 

i would like to thank you for the response,

 

 

#include <avr/io.h>
#include <avr/interrupt.h>

#define FLAG_NONE 0
#define FLAG_TOGGLE_LED 1
//#define TIMSK   _SFR_IO8(0x39)

void setFlag(int);
int getFlag();
volatile int flag=FLAG_NONE;

ISR (TIM1_OVF_vect)
{
	TCNT1=12;
	setFlag(FLAG_TOGGLE_LED);
}


int main(void)
{

	DDRB|=(1<<DDB3);


	//TURNOFF LED
	PORTB&=~(1<<PB3);
	//Comparator A mode select

	TCCR1&=~((1<<COM1A1)|(1<<COM1A0));    //COM1A1=0,COMA10=0
	//COMPRATOR B output mode
	TCCR1&=~((1<<COM1B1)|(1<<COM1B0));
	//Timer /countr Prescalar
	TCCR1|=(1<<CS13)|(1<<CS12);
	TCCR1&=~((1<<CS11)|(1<<CS10));
	//Enable Interrupt
	TIMSK |= (1<<TOIE1);
	sei();
	TCNT1=0;


	while(1)
	
	{
		if(getFlag()==FLAG_TOGGLE_LED)
		{
			PORTB^=(1<<PB3);
			setFlag(FLAG_NONE);
			

		}
	}

}


void setFlag(int f)
{
	flag=f;
}

int getFlag()
{
	return flag;
}

i tried of blinking LED  for evey 1sec (on/off).kindly guide how can i add 10ms count to this code and to copmare kindly guide me.Kindly find the attachment

Attachment(s): 

Last Edited: Mon. Mar 23, 2015 - 08:56 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Make your flag a char - an int is 16 bits so it's a bit of a waste using 16 bits to represent 1 bit.
Set the timer to ctc mode.
Have a variable to keep count of your timer ticks. If it is 0'set the port bit, if it is 100, clear the port bit. If it is 59 times 60 times 100 plus 5900 then the count =0

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

Hi Kartman

 

Thank you for your response   i am bit confused can you please explain me with what value i need to compare for eg:for 1sec the count is Timer1 Prescaler = 4096, Preload = 244;

how i should i add the count and to  compare with variable  kindly guide me.

 

 

Thanks&Regards

samathasan

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

You appear to be crying out to be a C++ programmer!

 

Accessor functions only really work if you can keep "flag" as something private. When it's global what's the point of accessors? The code (anywhere) can just read/write "flag" anyway because that's what global means.

 

If you want "semi privacy" (in C) then put the definition of flag and the two accessors in a separate C file and make the variable itself "static" so nothing else can "see" it.

 

But, as I say, this is what C++ is all about. In C++ you could:
 

class flag {
public:
  int getFlag() { return flag; }
  void setFlag(int f) { flag = f; }

private:
  int flag;

};

Now nothing outside this class can "get to" the flag variable itself. It is "ring fenced" by the class.

 

BTW just sit back for a minute and ponder whether "int" in this is really the correct type to use on an AVR.

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

You can count seconds with a hardware interrupt and an interrupt handler, or just use the simple delay_ms(1000) function. You increment the variable secs and when it increments from 59 to 60, you reset it back to 0 and increment mins. When that increments from 59 to 60 you set it to 0 and increment hours. See the pattern? I think this isn't specific to the avr microcontroller. You said you were new to the avr. I bet this hr min and sec algorithm would apply to the other microcontrollers that you already know how to program.

 

 

Imagecraft compiler user

Last Edited: Mon. Mar 23, 2015 - 04:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you so much Kartman smiley for guiding me. got the output.

 

 

 

Thnks&Regards

Samathasan