timer compare interupt

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
jakazugelj
Posts: 4
Joined: Fri Feb 22, 2008 3:53 am
Contact:

timer compare interupt

Post by jakazugelj »

Hi,

i'm trying to get a timer0 output compare interupt
to work with 32,768KHz clock, and 1024 prescaler on atmega128.

I'm stuck and can't find the problem.
Since i'm new to AVR's and c for microcontrollers i could really use someone's help.

Here is the c code:

Code: Select all

#define MX_AVR

//Defines for microcontroller
#define MX_AVR
#define MX_EE
#define MX_EE_SIZE 4096
#define MX_SPI
#define MX_SPI_B
#define MX_SPI_SDI 3
#define MX_SPI_SDO 2
#define MX_SPI_SCK 1
#define MX_UART
#define MX_UART_E
#define MX_UART_TX 1
#define MX_UART_RX 0
#define MX_PWM
#define MX_PWM_PORT PORTB
#define MX_PWM_CNT 2
#define MX_PWM_TRIS1 DDRB
#define MX_PWM_1 5
#define MX_PWM_TRIS2 DDRB
#define MX_PWM_2 6

//Functions
#define F_CPU 16000000UL
#include <avr\io.h>
#include <avr\interrupt.h>
#include <avr\eeprom.h>
#include <MX_util\delay.h>
#include <MX_util\bit_cmds.h>

//Configuration data
#pragma DATA 0x0, 0xdf
#pragma DATA 0x1, 0xff
#pragma DATA 0x2, 0xff

//Internal functions
#include "C:\Program Files\Matrix Multimedia\Flowcode_AVR\FCD\internals.h"

//Macro function declarations
short FCM_stej();


//Variable declarations
volatile short FCV_OVER;
volatile short FCV_COUNT;



//Supplementary defines


//Macro implementations

short FCM_stej()
{
	
	//Local variable definitions
	short FCL_COUNT;

	short FCR_RETVAL;

	//STEJ
	//Calculation:
	//  over = over + 1
	FCV_OVER =  FCV_OVER + 1 ;
	

	return (FCR_RETVAL);
}

//Supplementary implementations


int main()
{
	
	//Initialisation
	MCUCR=0x00;
	MCUCSR=0x00;
	WDTCR=0x10;


	//Interrupt initialisation code
	EICRA=0x00;
EICRB=0x00;


	//Interrupt
	//Interrupt: Enable OCR0
	sei();
	TIMSK &= ~(1<<OCIE0);
	ASSR |= (1<<AS0);
	TCNT0=0x00;
	OCR0=32;
	TCCR0=0x0F;
	WHILE((TCN0UB!=0)&(OCR0UB!=0)&(TCR0UB!=0))
	{
	}
	TIFR &= ~(1<<OCF0);
	OCIE0 |= (1<<OCIE0);



	//Loop
	//Loop: While 1
	while( 1 )
	{
		//Calculation
		//Calculation:
		//  count = count + 1
		FCV_COUNT =  FCV_COUNT + 1 ;
		

	}


	mainendloop: goto mainendloop;
}

	ISR($001E)
	{
	FCM_stej(); // call selected macro
	}

If i understand correctly, i need to set TCCR0 to 0x0F, AS0 bit in ASSR to 1, OCIE0 bit in TIMSK to 1, TCNT0 to 0x00, OCR0 to 0x20, wait for TCN0UB,OCR0UB and TCR0UB to clear and set I bit in SREG to 1?

Does it matter in which order they are?

Your help would be most appreciated.

jaka

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: timer compare interupt

Post by Sean »

I have checked your program against the data sheet and everything looks o.k.

Do you have a 32768Hz xtal connected between PG3 and PG4?

Post Reply