Method for measuring the amount of time that a pin is high

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, I am a programmer/hacker that has been working on the Raspberry-Pi for some time now using python to program a project I am working on. At this point in that project, I would like to utilize an ATtiny to trigger physical and software shutdowns of the raspberry pi. The main inspiration for this is the typical PC-Laptop, where you have a button to press for a duration of time(I assume there is some sort of micro-controller behind this, which is independent from the OS-system) which forces the Laptop to power down. This physical shutdown option is good, as I personal know from experience that commanding a system to shutdown through and OS-GUI or command-line interface environment doesn't always work, especially in cases where the OS has frozen or something.

 

To replicate this, I have started developing a prototype which uses an ATtiny85(In my final draft of all this, I will most likely use a smaller chip, with the primary factor being that I doubt this project needs more than 0.5kb, so I am eyeing the ATtiny4 right now.) with one input, PB4, for the MC to listen for the software shutdown("...I told the Pi to shutdown from within Raspbian."), and another input, PB2, to listen for the physical shutdown("...I push down on a button for about to seconds, just as you would a laptop."). 

 

In the previous version of this prototype, things were simpler and more isolated. First off, the ATtiny85 wasn't always powered on because I hadn't included the software shutdown option, and all I did was hold a button for about two seconds and used _delay_ms to delay the output I needed.

 

Now though I want to use a small button battery to keep the chip running, so with this, the steps of my previous algorithm wont work, a simple delay at start of script won't work. As I stated in my opening, I am more familiar with hardware such as the Pi and programming language of choice is Python. What I need help with is figuring out a C code statement which represents a logical if-then statement such as:

 

p= PB2 has been high for two seconds or more.

q= PB5 is high

p-->q

"If time high of PB2 is equal to or greater than 2 seconds then PB5 is high."

 

With C code and my ATtiny85, how could I implement this? How could I initiate and measure the amount of time that an input pin is high or low?

Why Mr. Anderson? Why?...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Tiny85 has Arduino support, so that might be worth investigating. It implements timing via the millis() function. Otherwise you have to set up a timer to give you a tick and then you count ticks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes I am going to start doing research on timers right now. Are their any good online lectures regarding the subject matter that you know about?

Why Mr. Anderson? Why?...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Greetings -

 

There are two basic techniques to do the task of measuring how long something lasts. They are optimized, respectively, for very short times and much longer times.

 

1) Using Input Capture on a standard timer. With this method, at the start of the interval, you record the timer value. Then, at the end, you save the timer value and subtract the start to find the difference. This method works well from intervals lasting maybe a minimum of 10us to a maximum of 100's of milliseconds. However, this really does not represent the "dynamic range" because you need to know, somewhat, how long the interval will be and optimize the counter for that based on its clock rate.

 

2) Count some standard time interval. For example, if you have some software that delays for 100ms, and turn it on at the start of the interval, and count how many of these have occurred at the end of the interval, you can tell how long the interval is.

 

Input capture relies on internal hardware and can run with (almost) no intervention. The second method relies totally on software and relies on full participation of the software. 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net