Jump to content

Timers, how do they work?

Recommended Posts

I picked up some cheap DHT11 humidity and temperate sensors, they use some sort of 1 wire protocol (not the dallas one wire). From the testing I did on my Arduino, they seem fairly accurate for a $3 sensor.


From reading the datasheet I've gathered that you start temp conversion by pulling the data line low for 18ms then high for 40us, then it will acknowledge with a 80us low and then an 80us high signal and then start sending the 40bit temperature/humidity data. It seems that the length of time that the data line is high determines if its sending a 1 or a 0.


Here's the datasheet if anyone wants to take a look:


I paid 2.59$ for mine here:

http://www.satistronics.com/humidity-te ... p2860.html


What would be the best way to go about reading this? It seems like the proper way would be using a timer that is triggerd when the data line goes high and counts how long its high, is this possible using TimerA?


Are there any reading materials that explains in laymens terms how timers work? I was goggling around but a lot of stuff I found was way over my head.


Would love to hear how you guys would go about this!

Link to post
Share on other sites

Timers require interrupts. If you don't use timers then you have to use a delay loop ie: _delay_cycles(x)


What I suggest is to create your interface routines using delay loops to start with. This is what we did with the OneWire stuff.


Once you get these drivers running reliably, you can then analyze their timing to see where they could be optimized with a timer. They can be optimized if you are using huge _delay_cycle(x) calls.


With the OneWire stuff, I decided that it didn't make sense to use a timer so I stuck with the delay loops. I couldn't make it any more efficient.


The way I learn about timers is to experiment then read sample code then tweak it some and then see what happens. I make sure to take notes of what works and what doesn't work. Then I can make a recipe for myself the next time I want to do the same thing again.


Each MSP430 comes with a zip file full of example programs that illustrate how to use a particular feature of that specific processor. You'll find them on the TI page for each micro. Grab that zip file and read through the examples for the timer peripheral. Try compiling them and loading them onto the LP to see if you can make it happen. Then modify the code to suit your needs.


Here's an example of where a timer makes sense.


Recently, I did a project where I needed to run a state machine every second. I also needed to check on some digital inputs every 0.5 seconds. So I set up the timer to give an interrupt every 0.5 seconds. In the ISR, I called InputChecking() every time it interrupted and I called RunStateMachine() every other time. The timer was perfect for that.


I'm sure others could come up with things way more complex to use the timer for (ie PWM output) but this is my example.


Does that help?

Link to post
Share on other sites

I realize that I didn't mention something important.


I've learned a great deal from the book MSP430 Microcontroller Basics by John Davies.


Chapter 8 goes into great detail on how the timers work.


This is probably the best resource I know of on the topic of timers.


There's a link to that book somewhere on this website but I can never find it easily. Maybe Mr. B.Hash can make it exceedingly obvious to me where it is?

Link to post
Share on other sites

After reading the 9 page datasheet on that part, I would strongly recommend that you use polled timers and bit bang it.


It will be very similar to how we bit banged the OneWire protocol.


You will just have to modify the read and write functions to accommodate the new protocol. Not easy but straight forward.

Link to post
Share on other sites

Thanks, both the one-wire timer and what you posted zeke is a great deal of help. I found the book you referenced on amazon and picked that up, it looks like it would be a lot of help not only with the MSP430 but also in general how these embedded micro controllers work.


I was giving some more thought to how I could do it without timers. Since we know a 1 is 70ms and a 0 is 28us, I suppose I could just use a delay for 28us and if its still high after that, I'd know a 1 was being sent, then use a blocking while() loop until the next bit start signal is sent.


How hard is doing something like this without a logical analyzer or scope? I've realized how nice it would be able to compare a working signal vs the one I've generated. I noticed that there's a cheap logic analyzer out now, the Logic Shrimp, for $35 (http://dangerousprototypes.com/docs/Log ... c_analyzer), seems like it may be useful for something like this.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...