Timer0 for generating 1 micro second

Status
Not open for further replies.

isannnn

New Member
Hi,
I used the bellow code for generating 1 microsecond in codevision:




Code:
include <mega32a.h>
#include <delay.h>
#include <stdbool.h>
#include <io.h>
#include <stdio.h>

static void handleInterrupt();
int i=0;
int c=0;
int nReceiverInterrupt;
unsigned long nReceivedValue = NULL;
unsigned int nReceivedDelay = 0;
unsigned int nReceivedProtocol = 0;

// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
   PORTC.0=!(PORTC.0);
intpt= PIND.2;
   PORTB.3=0;
  nReceiverInterrupt = intpt;
    nReceivedValue = NULL;
    nReceivedBitlength = NULL;
   handleInterrupt();

}

// Timer0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer0 value
i++;
PORTA.2=1;
if(i==256){
TCNT0=0xFF;
i=0;
c=c+1;
}
TCNT0=0xFF;
}

void main(void)
{
// Declare your local variables here
DDRC.0=1;
DDRB = 0xFF; //PORTC as Output
DDRA.2=1;
PORTC.0=0;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 1000/000 kHz
// Mode: Normal top=0xFF
// OC0 output: Disconnected
// Timer Period: 1 us
TCCR0=(0<<WGM00) | (0<<COM01) | (0<<COM00) | (0<<WGM01) | (0<<CS02) | (1<<CS01) | (0<<CS00);
TCNT0=0xFF;
OCR0=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (0<<OCIE0) | (0<<TOIE0);

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Any change
// INT1: Off
// INT2: Off
GICR|=(0<<INT1) | (1<<INT0) | (0<<INT2);
MCUCR=(0<<ISC11) | (0<<ISC10) | (0<<ISC01) | (1<<ISC00);
MCUCSR=(0<<ISC2);
GIFR=(0<<INTF1) | (1<<INTF0) | (0<<INTF2);

// Global enable interrupts
#asm("sei")
while(1){
//do something
}
void handleInterrupt() {
static unsigned int duration;
  static unsigned long lastTime=0;
  duration = c - lastTime;

      //do something
lastTime=c;
}

I used function interrupt [TIM0_OVF] void timer0_ovf_isr(void) to generate 1 microsecond for some future comparisons in my code but when I check it, this function doesn't work and c value in this function is always zero, also portA.2 has zero value and hasn't changed to 1. why this function doesn't work? do I miss anything?
 
Last edited:
I expect that 1 microSecond is to small of a time interval to expect from microcontroller running on a 1000 KHz clock, since the period of one clock cycle is 1 microsecond.

You either need to increase your clock frequency, or settle for a longer time period.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…