Is timer 0 from ATtiny2313 compatible with ATMEGA128 ?

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

Guys,

I tried to do timer tutorial from :

https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=76406

Is it compatible with ATMEGA128 ?

I used this function and I didn't see any responses from the board, only blinking one time and off..

/*
***************************************************************************
**  Example 0: 
** - CT1 in Phase & Frequency Correct PWM Mode
** - Functioning like CTC Mode:
** -- Just toggle OC1A with OCR1A as TOP
** Change frequency every 10 cycles.
***************************************************************************
*/

void Examp_0( void )
{
	uint8_t count = 0;
	
	// Set OCR1A (TOP)
	OCR1A = 49;		// toggles every one tenth second
	
	// Configure Timer/Counter 1 
	// Select Phase & Frequency Correct PWM Mode, and OCR1A as TOP. Use WGM Bits.
	// WGM Bits are in TCCR1A and TCCR1B. Any previous settings are cleared.
	TCCR1A = _BV(WGM10);
	TCCR1B = _BV(WGM13); 
	// Configure OC1A to Toggle at TOP.
	TCCR1A |= _BV(COM1A0);
	// Select Internal Clock Divided by 1024. This starts the Timer/Counter.
	TCCR1B |= _BV(CS12) | _BV(CS10);

	while (TRUE)	// Do this forever
	{
		// monitor the overflow flag and change the time every 10 cycles
		while ((TIFR & _BV(TOV1))==0); // wait for timer overflow
		TIFR |= _BV(TOV1);	// Clear the flag by writing 1
		
		if (count >= 10) 
		{
			if (OCR1A > 100)
			{
				OCR1A = 49;		// flash at one tenth second rate
			}
			else
			{
				OCR1A = 490;		// flash at one second rate
			}
			count = 0;			// reset count
		}
		else
		{
			count++;
		}	
	}	
}

Complete code :

/*
 * Timer_PWM_example0.c
 *
 * Created: 29/05/2013 10:58:47 AM
 *  Author: Antonius
 */ 

/*
 * Timer_PWM_v1_0.c
 *
 * Created: 29/05/2013 8:14:08 AM
 *  Author: Antonius
 */ 




/* Include useful pre-defined functions */
#include 
#include 
#include     // Defines pins, ports, etc to make programs easier to read
#define F_CPU 8000000UL	      // Sets up the default speed for delay.h
#include 

/* General Defines */
#define	TRUE	1
#define	FALSE	0

/*//
**  Outputs will be on pins that Timer/Counter 1 can manipulate.
*/
#define PORT_CT	PORTB
#define DDR_CT	DDRB
#define	CT1A	PB3		// OC1A
#define	CT1B	PB4		// OC1B

// LED and Button Ports - specific to ATtiny2313
#define PORT_LED	PORTC
//#define PORT_BUTTON	PORTD		// used for jumpers
#define DDR_LED   	DDRC
#define P_LED_1		PC1			// used for init & data signal (for Debug)


// Examples
#define EXAMP_0	00
//Function Declarations

void Examp_0( void );
void blinkEm( uint8_t count, uint8_t led);

int main( void ){
	uint8_t examp;
/*
** Initialization
**  Configure ports
**	Blink a led at start
**  Read Selection Bits
** Jump to selected Example
**
*/
	// Could configure clock to run at 8MHz here. Default is 1MHz
	// If changed, must fix calls to _delay_ms
/*	CLKPR = _BV(CLKPCE) ;		// Initiate write cycle for clock setting
	CLKPR = 0x00;		// Prescaler divider 1 => 8 MHz clock out
*/	

	DDR_CT |= _BV(CT1A) | _BV(CT1B) ;   /* Enable CT1 output pins */
	PORT_CT &= ~(_BV(CT1A) | _BV(CT1B));     /* Set them to lo - LEDs off */

	/* Initialize Selection Bits - Turn on pull-ups*/
	//PORT_BUTTON |= _BV(EXAMP_SEL_IN_1) | _BV(EXAMP_SEL_IN_2) | _BV(EXAMP_SEL_IN_3);
	
	/* Initialize LED blinker  */
	DDR_LED |= _BV(P_LED_1);   // enable output
	
// Read the Example Selects.
// Have to reset to select a different one.
	//examp = (PIN_BUTTON >> 4) & 0x07;
	while(1)
	{
	   blinkEm(1,P_LED_1);		// Blink once as init signal
	   //Examp_0();
	}
			
	return(0);
}


/*
***************************************************************************
**  Example 0: 
** - CT1 in Phase & Frequency Correct PWM Mode
** - Functioning like CTC Mode:
** -- Just toggle OC1A with OCR1A as TOP
** Change frequency every 10 cycles.
***************************************************************************
*/

void Examp_0( void )
{
	uint8_t count = 0;
	
	// Set OCR1A (TOP)
	OCR1A = 49;		// toggles every one tenth second
	
	// Configure Timer/Counter 1 
	// Select Phase & Frequency Correct PWM Mode, and OCR1A as TOP. Use WGM Bits.
	// WGM Bits are in TCCR1A and TCCR1B. Any previous settings are cleared.
	TCCR1A = _BV(WGM10);
	TCCR1B = _BV(WGM13); 
	// Configure OC1A to Toggle at TOP.
	TCCR1A |= _BV(COM1A0);
	// Select Internal Clock Divided by 1024. This starts the Timer/Counter.
	TCCR1B |= _BV(CS12) | _BV(CS10);

	while (TRUE)	// Do this forever
	{
		// monitor the overflow flag and change the time every 10 cycles
		while ((TIFR & _BV(TOV1))==0); // wait for timer overflow
		TIFR |= _BV(TOV1);	// Clear the flag by writing 1
		
		if (count >= 10) 
		{
			if (OCR1A > 100)
			{
				OCR1A = 49;		// flash at one tenth second rate
			}
			else
			{
				OCR1A = 490;		// flash at one second rate
			}
			count = 0;			// reset count
		}
		else
		{
			count++;
		}	
	}	
}

/*------------------------------------------------------------------------
**  blinkEm - function to blink LED for count passed in
**		Assumes that leds are all on the same port. 1MHz Clock rate.
** ---------------------------------------------------------------------*/
void blinkEm( uint8_t count, uint8_t led){
	uint8_t i;
	while (count > 0){
		PORT_LED |= _BV(led);
		for (i=0;i<5;i++)
		{
			_delay_ms(200);
		}
		PORT_LED &= ~_BV(led);
		for (i=0;i<5;i++)
		{
			_delay_ms(200);
		}
		count--;
	}
}

any helps or ideas will be appreciated,
Thank you

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

According to the code you posted, it should only blink once.is the timer different? It is most likely very similar so you would need to compare the data sheets to note the differences.

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

Kartman wrote:
According to the code you posted, it should only blink once.is the timer different? It is most likely very similar so you would need to compare the data sheets to note the differences.

yes it's only blinking one time, (blinkEm) function...
how can I know the timer is working ??

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

Your post is confusing. Your title says timer 0, but the code uses timer 1. I'm also not sure which AVR the code is meant for. Also the comments in the code do not match what the code is doing. The comments say phase and frequency correct with OCR1A as TOP, but the code is setting fast PWM with a fixed TOP and OCR1A as the duty cycle. But then you set OC1A to toggle which defeats the PWM. So I'm not really sure what the code is supposed to do.

Regards,
Steve A.

The Board helps those that help themselves.

Last Edited: Wed. May 29, 2013 - 04:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I commented out Examp_0(); but I didn't see any response,
it should be a loop, shouldn't it ?

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

Koshchi wrote:
Your post is confusing. Your title says timer 0, but the code uses timer 1. I'm also not sure which AVR the code is meant for. Also the comments in the code do not match what the code is doing. The comments say phase and frequency correct with OCR1A as TOP, but the code is setting fast PWM with a fixed TOP and OCR1A as the duty cycle. So I'm not really sure what the code is supposed to do.

Sorry, I don't know if it's timer0 or timer1, I just read from the tutorial and try to understand it...

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

if I commented out //Examp_0();
LED is blinking continously....

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

may be I can find something easier and understandable from :
http://www.atmel.com/Images/doc2505.pdf

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

The corresponding C code for the IAR Compiler looks like this:

void interrupt [TIMER0_OVF0_vect] ISR_TOV0 (void)
{
PORTB = ~PORTB; // Toggle pins on Port B
}

How can I rewrite it on AVR Studio ?
thanks

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

Am I writing it correctly ? or do I miss something here ?
How to blink it with timer ?

/*
 * Timer_simple_v1.0
 *
 * Created: 29/05/2013 11:52:36 AM
 *  Author: Antonius
 */ 
 #include 



void init_Ex1(void)
{
	TCCR0 = (1<<CS02)|(1<<CS00); //Timer clock =
	//system clock /
	//1024
	TIFR = 1<<TOV0; //Clear TOV0 / clear
	//pending interrupts
	TIMSK = 1<<TOIE0; //Enable Timer0
	//Overflow Interrupt
	DDRC = 0xFF; //Set Port B as
	//output
}

ISR(TIM0_OVF_vect)
{
		// Add code here to derive other timebases
		PORTC = ~PORTC;
	
}

 
int main (void)
{
       DDRC = 0xFF;
	   PORTC = 0xFF;
	   init_Ex1();
	   ISR();
	  
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include 
#include 

void init_Ex1(void)
{
   TCCR0 = (5<<CS00);   //Timer clock = div1024
   TIFR = (1<<TOV0);    //Clear pending TOV0 interrupt
   TIMSK = (1<<TOIE0);  //Enable Timer0 overflow interrupt
   DDRC = 0xFF;         //Set Port C as output
}

ISR(TIM0_OVF_vect)
{
      // Add code here to derive other timebases
      PORTC = ~PORTC;
}

int main (void)
{
      init_Ex1();   //interrupt every F_CPU/1024/256
      sei();        //enable global interrupts
      while (1) {   //must always have a loop
          // add your loop code here
      }
}

I have altered comments to 'my' style. Note that the only significant changes are the sei() and the while() loop. I also made your DDRC comment honest.

Your Timer interrupt will occur every 0.26 seconds if your F_CPU is 1MHz. This would toggle LEDs at 1.9Hz. (you need two interrupts for each cycle)

Personally, I think it is important to have short pieces of commented code. Your first posts were too complex.

It is possible to remove the TIFR line. You won't have a pending interrupt because the Timer has never started running !
However, it is wise practice to 'start with a clean slate'.

David.

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

I took this code from
[url]
https://www.avrfreaks.net/index.p...

#include 

int main (void)
{
   DDRB |= (1 << 0); // Set LED as output

   // TODO: Set up timer at Fcpu/64

   for (;;)
   {
      // Check timer value in if statement, true when count matches 1 second
      if (TCNT1 >= 15624)
      {
         PORTB ^= (1 << 0); // Toggle the LED

         TCNT1 = 0; // Reset timer value
      }
}
   

}
but when I change the counter to 124599, it doesn't work,
I don't know why ?

I changed it because I have 8Mhz clock, and the code is for 1Mhz clock...

Any ideas ?

thanks

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

What is the maximum size of a 16 bits unsigned integer?

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

Did you try what david.prentice suggested?
When reading your posts it seems to me that you are madly jumping from one code to another, finally leaving them all unresolved.

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

Visovian wrote:
Did you try what david.prentice suggested?
When reading your posts it seems to me that you are madly jumping from one code to another, finally leaving them all unresolved.

I just tried to find something easy for me to understand....part of learning...

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

dbrion0606 wrote:
What is the maximum size of a 16 bits unsigned integer?

Got it, and thank you,

1 second delay :

int main (void)
{
	DDRC |= (1 << 0); // Set LED as output

	
    TCCR1B |= ((0 << CS10) | (0 << CS11) | (1 << CS12)); // Set up timer at Fcpu/256
	for (;;)
	{
		// Check timer value in if statement, true when count matches 1 second
		if (TCNT1 >= 31250)
		{
			PORTC ^= (1 << 0); // Toggle the LED

			TCNT1 = 0; // Reset timer value
		}
	}
}