Buzz
Buzz

Reputation: 1422

Simulating T0 external clock for Timer0 on Microchip Studio with ATmega328p

I've setup Timer0 to work with an external clock on pin T0:

TCCR0A=0x02 // CTC Mode
TCCR0B=0x07 // Start timer on external T0 clock (rising edge)
OCR0A=90 // Compare after 90 clock ticks
TIMSK0=0x03 // Interrupt enable on OCR0A compare match or timer overflow (irrelevant this latter)

I'm using Microchip Studio Simulator, and a stimuli file to simulate the clock on T0 pin. Since T0 is not an internal register of the chip, I've thought of setting the PORTD register, since the pin T0 is (physically) the same pad as PD4. I've no other pin connected to portd, so for easiness:

$repeat 1000
   PORTD=0xFF // Set PORTD high
   #800000 // wait 50ms
   PORTD=0x00 // Set PORTD low
   #800000 // wait 50ms
$endrep

The stimuli file is loaded correctly, and it does not throw any error. But the timer is not starting (TCNT0 remains always 0x00) and I'm pretty sure the problem is T0 is not varying. Any clues how to overcome the problem?

Upvotes: 0

Views: 27

Answers (1)

Buzz
Buzz

Reputation: 1422

The idea was correct, just the port was wrong. PIND is to be used instead of PORTD, and it makes sense: it's the input port register, from which the T0 value is taken.

$repeat 1000
   PIND=0xFF // Set PORTD high
   #800000 // wait 50ms
   PIND=0x00 // Set PORTD low
   #800000 // wait 50ms
$endrep

Upvotes: 0

Related Questions