Open Source Software in Ontario Secondary Schools | |
---|---|
Overview | Notebook | Downloads | Documentation | |
Next Previous
Contents
We'll be writing a program to get a temperature reading.By the time you get to the end you will probably start wondering whether a thermometer is cheaper :)If you don't have a temperature probe, use a ph probe. I think any simple analogue probe will work. 3.1 Return CodesAll the functions in libsw500-l1 will return a value greater then (not equal to) zero if they are successfull and zero or negative otherwise. Do yourself a favour and check the return value of every function you call. 3.2 Serial Port SetupThere are only two functions dealing with the serial port: initSerial() and unInitSerial(). initSerial() will return an int, openPort. This variable will be used by all other functions, it's a reference (of sorts) to the socket used for communicating with the lbb. No function except unInitSerial() will change that variable. unInitSerial() is called at the end of your program. It's probably not necessary but you should get into the habit of using it just in case your application gets advanced enough.
3.3 ROM->RAM Mode
When the lbb is turned on, it is in a state called ROM mode.
It has executed the little code burned into a chip inside.
At this point, it is only capable of doing two things: download and execute some code that will enable all it's usefull functions.
if( identify(openPort) == 1 ) Note that you will always use initialize() and execute() together like that. Actually this entire block of code could be one function, feel free to make one. The initialize() function takes several seconds to complete, that is normal and has nothing to do with the speed of your serial port or the cable. That's just the way it works. In case turning the lbb off and back on is unacceptable, use the reset(int openPort) function. It will take the lbb from whatever state it's in back to the virgin ROM mode. An advanced note: stuff may still be in your serial port buffer even though the lbb has been reset, you have to make sure you clear that. 3.4 Preparing for data collectionBefore you can start collecting information from your temperaure probe you must set up the lbb. For example, it must be told which port the probe is plugged into.Input SelectThis function tells the lbb which channel(s) to give you readings from. Analog channels A,B,C and digital channels 1 and 2 are available. Keep in mind that analog channels A and B have an optional 'with gain' mode which I guess is good for some probes. Besides the openPort, inputSelect() takes an array of 10 bytes (char channels[10]). The first five bytes are for analog channels. We want to select channel A without gain, that's the first byte.
Rate SelectThis function tells the lbb how many times per second to do/send a reading. The parameters to this function are somewhat complicated, refer to Appendix A for an explanation.
3.5 Start Sending RecordsLet's tell the lbb to start sending records at the rate we specified.
3.6 Reading RecordsNow that the lbb is sending records we have to read them in a hurry before some buffer overflows and messes everything up: When reading a record from the lbb (in one of several ways) you must first read the record type, use readRecordType() for that. In our case you will find that we get a clocked sample record. A clocked sample record is followed by readings for each selected channel (remember inputSelect()?). To get that/those readings use readRecordCS(). It is very important that you remember which channels you selected and tell readRecordCS() about them properly. If you select one channel but try to get readings from two, it might work but you will get your reading so messed up you'll have to reset (or at least restart sampling) to get out of the mess. Here's an illustration of what a record looks like:
Can you see the potential problem yet? This is what two records look like:
They are not delimited in any way so if you try to read a 'clocked sample' record with 3 channel readings, you will get 'TAABBTA' which has an unpredictable value and you will not be able to restore order until you restart sampling. To ease dealing with this, readRecordCS() takes as a parameter the same array that inputSelect() used. This may cause problems if you're not carefull, so just give it some thought. We only have channel A selected so our code would look like this:
Now that reading is not very obvious. One can hardly imagine the temperature in his room is in between -12.5 and +12.5 degrees.
It's not but converting raw signals such as those we just read into comprehensible values (degrees, meters) is a different science for each kind of probe and I have no documentation for that. Sorry.
3.7 Sample conversionUnfortunately you will not get exactly what you want from the lbb and what exactly it sends you is different for every probe. Readings are also slightly different when made with or without gain and from different ports. Look in the Pasco docs for a couple of confusing tips. For temperature, port A without gain we have to do this:
3.8 Full code
Make sure the library is installed, then compile like this (change path to the library if needed): cc main.c /usr/lib/libsw500-l1.a I tested this code, it works. Next Previous Contents |
|