dinsdag 9 december 2008

Keeping time and date



Keeping time and date is relatively simple if one uses an external component. It is in principle possible to use the PIC microcontroller itself, moving it into sleep mode, giving it a 32.768 kHz crystal, et cetera. However, the head aches are not worth it if you have the space for a 8 pin DIP, like the DS1302. The DS1307 is another option if you wish to use I2C. This circuit was meant for my calculator, mostly to keep the date so it would tell me of birthdays that I might otherwise miss.

Connecting a DS1302 is pretty simple: just connect the 3 wires to the PIC microcontroller, connect the X1 and X2 to an appropriate crystal (these crystals have very small pins, so if a socket is used for the DS1302, something I would recommend, you can add the crystal to the same socket, and not solder it) and connect the power. Do not forget the 0.1uF decoupling capacitor, although in this case it might be less important, because the DS1302 is supposed to work using a backup battery. The DS1302 can charge this backup battery, so the ideal choice is a super capacitor. The value in the circuit is no joke, I used a 1F capacitor for this purpose. They are slightly bigger than a watch battery and can NOT handle voltages larger than 5V. However, most of the time it will not charge over 2V, which is still sufficient for the DS1302 to work. It has never lost time in my application, even with the power being off for weeks. The hardest part of the DS1302 is the software, but fortunately you can just copy the pieces of code that I wrote.

maandag 8 december 2008

Calculator switchpad



Today I got my first comment on this blog, and I appreciate it a lot. Hopefully more comments follow.

The switchpad of the calculator came from Jameco, and was basically 16 switches in a grid, as displayed in this schematic. To connect them to the PIC16F877A was simple: on the read side the voltage is pulled up to a known voltage (5V makes perfect sense) using a resistor network. The values of these resistors are arbitrary. I like 100k Ohm because this reduces power consumption. Speed does not really matter as they are switches anyway, and their fastest response time is 10ms. On the scan side you just connect the pins to the PIC16F877A. To read it is a matter of setting a particular line to 0V and see if any of the receiving ports detect it. If this is the case, that particular switch was pressed.

The software is slightly more complicated, because switches tend to "bounce" a bit, and it needs to be debounced, preferably over a 20ms range. So if there are changes faster than 20ms, these should be ignored, and the system should wait until the signal is stable for over 20ms. This is relatively easy to code as well, though.

Version 2 GPS software

Version 2 of the GPS had a new program, although some of the old software is still recognizable.

It first initializes the variables, and the LCD display. It also sets the serial port for 4800 bps communication, which is what is needed for the GPS module. This means output to the PC will also be in this speed. The LCD activation sequence is always the same, as the R/W line is pulled down, we need to have maximum delays (160uS and 5ms respectively). Finally the software displays a . on the display to indicate that something is happening.

During the start loop it reads from the GPS until a valid string is read. If the button is pressed during this time, it is remembered so that the location can be recorded.

During the main loop it continues to read from the GPS and makes sure that a valid sentence is stored separately for later recording. The SendString routine sends the GPS string from the sentence that is at location Number. This is used to display relevant information for each screen.

There are a number of states, that can be cycled through using the button. In one state it works normally, displaying the location and recording locations and pictures in case the camera triggers the flash port. In various other states different information is displayed. In the sending state it displays "Sending..." and sends the complete memory to the PC over the serial port.

Other routines handle the storing in external memory, the reading from external memory, the writing and reading from the serial port, and the recognizing of a valid sentence and skipping all the other information from the GPS module.

The detecting of the switches is done with a separate routine, which sets a flag when it happens. This routine is called as often as possible, to make sure no button presses are missed.

Serial port for the new GPS



Version 2 of the GPS has a more complicated serial port interface, as it has two functions. The original only had to read from the GPS, and store the results in memory, the new version also has to be able to send the result to a PC. Unlike version 1, which used an external GPS and therefore used a regular serial input, version 2 uses an actual GPS module. The second version also has to be able to output to a PC, something that was not covered at all in the first version.

The connection to the GPS module has some intricacies, because the GPS module, despite having a 4-6V input, had only a 3.3V output. Glancing through the data sheets of the PIC16F628A this might seem to be no problem, until one notices that the serial input port of this microcontroller uses Schmitt Trigger inputs, which means the minimum voltage for a '1' is 4V. To do the conversion I used a 74HCT125, which accepts a large range of input voltages, and converts them to whatever voltage is set on Vcc. Of course it is a bit of a waste of the other 3 buffers inside this chip, but it works and makes it very unlikely to damage the GPS module. An alternative would be to run the PIC16F628A on 3.3V, but this would cause other difficulties, for example with the interface to the LCD display, which needs 5V. Apart from the buffer, the connection to the GPS is very simple, as there is no other conversion necessary.

To connect to the PC we would officially have to output +12V and -12V signals. Clearly this is not easy with 5V power, and even if we used the 9V battery power we would still run into trouble with negative voltages. Fortunately there is the DS275. This small component leeches voltage from the RS232 interface of the PC and uses that voltage to send the messages to the PC. Of course, a PC might not have the right voltage (in fact, often they do not) but you would expect a PC to be able to read its own voltage levels back. The DS275 covers the -12V, for the +12V it uses Vdrv, which in this case is still connected to +5V. As the RS232 specification states that anything above +3V should be considered legal, +5V is fine, though barely. Hooking up the DS275 is very staightforward. As you can see it handles reads as well, but in this case it is not used. However, on the PC side the read should still be connected to allow access to the voltage levels. Note that if you decide to use the DS275 for sending AND receiving you should realise the component is NOT full duplex. You can either send or receive, but not both at the same time. If you need to do this, you might want to consider the Maxim 232 range of devices, which will be covered at another time.

Of course, you can see the decoupling capacitors of 0.1uF everywhere in the schematic. The GPS, the DS275 and the 74HCT125 all need them. Also, the plug used for the serial port is female, in this case, and this is why pins 2 and 3 were swapped compared to the previous schematic.

zondag 7 december 2008

More power (on voltage doubling)



The calculator uses 2 AAA batteries. However, so far all my circuits use 5V, and even the HD44780 LCD display requires 5V. Initially I decided to use 4 rechargeable AAA batteries (1.25V each, 5V total) but this was too cumbersome and heavy.

I had already used the ICL7660A to invert a voltage. This time I decided to use it to double a voltage. The circuit above comes straight from the documentation, and it works. However, as you can see, there is a diode involved, which causes the voltage to drop by 0.7V. So the 3V becomes 5.3V. Clearly there is a risk involved here: if the batteries are exceedingly fresh, they will deliver over 1.5V each and the 5.5V threshold of the PIC microcontroller could be reached. However, this has not happened yet. Also, rechargeable batteries don't work well in this configuration, because they have too little voltage to properly power the LCD display.

In the schematic the +5V is not 5V that is applied, but actually the voltage that comes out of the circuit. The battery is hooked up to the 3V input. Despite the odd configuration of the capacitors, they are correct.

A more ideal way to create the 5V is to use a bucket converter, but that will not be covered in this post.

Software licensing

In this blog there are many references to software I published using google documents. As you can tell from the source files, this software is copyrighted. This post is to clarify the nature of this copyright.

First of all, you can always contact me regarding the use of a specific file. In fact, I appreciate people contacting me. You can contact me for other things as well. So if there is any doubt, just send me an Email.

PERSONAL USE: Next is the use of the files. Obviously, if you want to try the code, that is fine. There is no warranty: if this file blows something up, it's not my fault. They are on the blog merely to explain how I did certain things for myself.

OTHER USE: If you use the source code (or a modified version) to create something and it stays in a quantity less than 10, there will be no objection from me (but again, telling me would be nice). The moment things become more commercial (even if you plan to give them away) you need to contact me first.

MODIFICATIONS: If you want to modify the code, this is usually fine too, though I would appreciate it if you told me, and sent me an updated version.

DISTRIBUTING: Unless I give permission (which I most likely will do), you are not allowed to distribute the source code, or a modified version of it. Obviously you can create a patch file, and publish it together with a link to my code. Even in that case, it would be best to let me know. In general, I just would like to be aware where my code is going.

Hopefully this explains things. This is version 1.0, if you need a reference. If there is a later version, it will void this one, except for those cases where this one was used before the other one appeared.

My calculator



On the piclist I recently posted that I would publish my calculator here. So here is the first installment, with picture.

As you can see, the calculator uses a weird keyboard that I found in the catalog of Jameco. It used an even smaller one first (with only 12 keys) and later I replaced it with the current one. It uses a PIC16F877A microcontroller to do basic arithmic operations, has an HD44780 display and works using 2 AAA batteries (and has worked on the same batteries since 2006). There is a LED to indicate it is on, as well as an on/off switch. I've started making a cover out of it (using a cat litter container) but this is not finished yet. The software for the calculator is available.

Features of the calculator are:
* Keeps time and date
* Alerts of birthdays
* Stores addresses
* Basic arithmetic (multiply, divide, addition, subtraction, random number)
* 0.00000001 to 99999999