avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] AVR Timer problem


From: Matte
Subject: [avr-gcc-list] AVR Timer problem
Date: Sat, 05 Jun 2004 17:49:44 +0200

Hi 

I could need some help getting the 16 bit timer/counter working with the
ATMega16 device. I have used the 8 bit timers and they works like a
charm. However, now I need the 16-bit timer so I created a small testapp
(that really does not demand a 16 bit timer an 8-bit should be more
suitable-- please don't commet that). The testapp is just for learning
howto use the timer. 

I want the timer to generate an interrupt after some time by using the
compare intterrupt, SIG_OUTPUT_COMPARE1A. The interrupt handler does
just switch the value from 0x00 to 0xFF when called every 10:th time. 

I've tried to setup the registers to enable the interrupt according to
tutorials and datasheets, however no success yet. I will be very
greateful if someone could look at my code (below) and tell me whats
wrong. I just want to get it working.... .-)

Betst Regarda 
Mattias

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


// Interupt function delclaration
void Timer1Int( void );

// Set up the interrupt handlers
INTERRUPT( SIG_OUTPUT_COMPARE1A ) {
  Timer1Int();
}


void Timer1Int( void ) {
  volatile uint8_t *PortA; PortA = &PORTA;
  static uint8_t counter = 0;
  static uint8_t lastValue = 0;
 
  if( counter == 10 ) {
    counter = 0;
    lastValue = ( lastValue == 0 ) ? 0xFF : 0;
    *PortA = lastValue;
  }
  else
    counter++;

  return;
}


void setupTimers( void ) {
  
  volatile uint8_t *timsk; timsk = &TIMSK;
  volatile uint8_t *tifr;  tifr  = &TIFR;
  volatile uint16_t *ocr1A; ocr1A = &OCR1A;
  volatile uint8_t *tccr1A; tccr1A = &TCCR1A;
  volatile uint8_t *tccr1B; tccr1B = &TCCR1B;
  volatile uint16_t *tcnt1; tcnt1 = &TCNT1;
 
  cli();

  // Set the timeout value
  *tcnt1 = 5;
  *ocr1A = 128;
  
  // set timer-clock fclk/1024
  *tccr1A  = 0x00;
  *tccr1B  = 0x05;
  

  // Enable the compare interrupt
  *timsk = 0x10;
  *tifr= 0x10;
  
  sei();
}
 


int main( void ) {

  // Set the port input/output mode
  // Define port A as output
  volatile uint8_t *PortA; PortA = &PORTA;
  volatile uint8_t *DDR_A; DDR_A = &DDRA;
  
  *DDR_A = 0xFF;
  *PortA = 0x00;
  
  // Now set up the 16-bit timer;
  setupTimers();
}




-- 
Mattias

address@hidden

reply via email to

[Prev in Thread] Current Thread [Next in Thread]