NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » High voltage signal timer

December 22, 2009
by lcruz007
lcruz007's Avatar


I am working in a project in which I need to read a digital signal, and precisely count the time that a pin of my MCU was high. I have done a code for it already, but it is not working as precisely was I want. So I wanted to hear your opinions and get an idea from you :)

Thanks in advance.

December 23, 2009
by Rick_S
Rick_S's Avatar

I think the best way to do that is to use a timer and a pin change interrupt. Where you would connect the line to check to INT0 and have it trigger an event when it goes high. Then using code similar to the real time clock, set the start time at the beginning of the triggered event and end time when the event ends.

Pretty vague, I know, I'd have to do a bit more research but I think that is the basic idea.

Hopefully someone with a bit more experience in this will chime in! :)

In the mean time that may give you some ideas...


December 23, 2009
by lcruz007
lcruz007's Avatar

Hello, Thanks for the reply. I am trying with a pin change interrupt, but I don't know how to do it. I wrote this code, but it is not working. It compiles and all, but the LCD does not shows anything, and I doubt it is working as I want.

void setup()


EICRA = (1<<ISC11);

PCMSK0 |= (1<<PC5);

// turn on interrupts!

EIMSK |= (1<<INT0);


volatile int32_t the_time;





int main()







fprintf_P(&lcd_stream, PSTR("%16.2f sec"), (double) the_time / 100.0);


return 0;


Post a Reply

Please log in to post a reply.

Did you know that talking to the microcontroller over the USB/Serial link is easy under Windows, Linux, and OS X? Learn more...