POV source code – part 2

Clock interrupt

The STC89C52 has three timer/counters (most of the 8051 chips have the first two, and the third one is pretty common).  One of the modes for timer 1 is to count clock pulses in an 8-bit register and when FF is reached, reload the count register from a separate 8-bit register.  Each time this overflow is reached an interrupt is generated, and we can use this to provide a regular clock ‘tick’.

The crystal frequency is divided by 6 when the chip is set to fast mode and by 12 when it’s set to standard (slow) mode.  The crystal oscillates at 11.0592 MHz so assuming we’re using fast mode the slowest rate we can generate interrupts this way would be to set the timer reload value to zero and then we’d get:

(11059200 / 6) / 256 interrupts per second = 7200 Hz.

I wanted the interrupts to happen a little faster than that as I was also intending to piggy-back the LED switching from the interrupt routine, so I chose a reload value of 0x40 (64) which results in 9600 interrupts per second.

The set-up code to initialize the timer is as follows:

// set up timer 1 interrupt for 9.6 kHz (assuming 11.0592 MHz xtal)
TMOD = 0x20; // timer 1 in 8-bit auto-reload mode
TH1 = 0x40; // reload value to give 9.6 kHz in ‘fast’ (6T) mode
ET1 = 1; // enable timer 1 interrupt
EA = 1; // enable interrupts
TR1 = 1; // run timer 1

The first part of the interrupt handler just counts the interrupts and uses them to advance the clock: (seconds, minutes, hours)

unsigned int fracSecs; // fractions of second in ticks

void T1INT(void) interrupt 3 using 1 { // configured to fire @ 9.6 kHz
    if (++fracSecs == 9600) { // one second elapsed
        fracSecs = 0;
        if (++topRow[7] == 26) { // units seconds overflow
            topRow[7] = 16;
            if (++topRow[6] == 22) { // tens seconds overflow
                topRow[6] = 16;
                if (++topRow[4] == 26) { // units minutes overflow
                    topRow[4] = 16;
                    if (++topRow[3] == 22) { // tens minutes overflow 
                        topRow[3] = 16;
                        // check for midnight
                        if (++topRow[1] == 20 && topRow[0] == 18) {
                            topRow[0] = topRow[1] = 16;
                        } else if (topRow[1] == 26) { // 10am or 8pm
                            topRow[1] = 16;
                            ++topRow[0];
                        }
                    }
                }
            }
        }
    }
}

It’s a little confusing as the routine directly manipulates ‘topRow’ which is a 16-character buffer holding the data to display on the top row of the display in the format: “HH:MM:SS        “ except that the character values are not ASCII but (ASCII – 32) to suit the way the font is defined.

So the first check (topRow[7] == 26) checks if the units seconds character has reached ASCII 58 (a colon – which is the character that comes after ‘9’) and if so it resets it back to 16 (ASCII 48 to display a ‘0’) and increments topRow[6] which is the tens of seconds character, and so on…

So why did I choose 9.6kHz?  Well it was a compromise to do with the rate that the LEDs spin round at, and wanting to get a reasonably well-defined display only using a single interrupt source and without overloading the microcontroller with too many interrupts.  Actually when I moved onto the bottom row display, I used a second interrupt source auto-adjusted to give exactly 256 interrupts per rotor revolution – but more on that later…

When powered with a nominal five volts, the board spins about 900 rpm (revolutions per minute).  The lowest voltage my kit would operate at without the microcontroller ‘browning out’ and resetting was about 3.3V which gave 700 rpm.  It’s unreliable at this low speed – if you try to switch too many LEDs on at the same time the extra current drags down the voltage on the board to the point where the microcontroller resets itself.  The ‘regulator’ on the board is a little sketchy – just a zener diode to bleed away current when the voltage begins to exceed 5.1V – so I’ve not risked going above 6V on the power supply at which voltage the motor still only spins about 950 rpm.

My program displays 32 characters around the circumference of the cylinder drawn out by the LEDs and with 8 pixel width characters at 900 rpm that gives a pixel rate of 900/60 x 32 x 8 = 3840 pixels per second.  As the voltage varies or the rotor is affected by draughts or similar, the rate can vary in the range roughly 3000 to 4000 pixels per second.

So at an interrupt rate of 9.6kHz we need to output a fresh set of pixels roughly every two-and-a-half interrupts (9600 / 3840).  We can’t do half-interrupts, of course, so the program needs to use a mixture of two, three, maybe even four, interrupts between each update – and spread them out in such a way as to provide a fairly regular and stable display.

As the rotor passes a reference position each revolution, it picks up an ‘index pulse’ from a photodiode illuminated by a stationary infra red LED – so it can use that information to recalculate the correct ratio of two interrupt/three interrupt cycles to keep the display fairly steady.

It sounds like a lot of calculation, maybe involving division (which is very slow on this chip as it doesn’t have multiply or divide instructions so it has to ‘long division’ by repeated subtraction/addition and bit-shifts).  Luckily there is a faster way just involving addition and bit shifts, although it’s a little confusing until you’ve studied it for a while.

The basic idea is to have a 32-bit counter which we try to get to count up to 0x01000000 (16777216) for each rev of the rotor.  We do this by adding an amount to the counter each time there is an interrupt.  If the rotor speed were exactly 900 rpm (fifteen revs per second) then we’re going to get 9600 / 15 interrupts  (640) for each revolution of the rotor so to count up to our target number we add 0x01000000 / 640 (which is about 26214)  every interrupt.

Now when the index pulse comes around we can see if we’ve counted past our target number or not reached it – and the error between the actual count and the target can be used to adjust our 26214 value up or down for the next revolution.  If you just divide the error by 1024 and add that value to the ‘26214’ then the counter quickly locks into counting to the correct target value after a few revolutions – and then it automatically adjusts the rate to compensate for any changes in the rotor speed.  Dividing by 1024 is something the microcontroller can do pretty quickly – just by bit shifts.  In fact because the 32-bit count is held in four 8-bit registers, the correction calculation can be done with just  a 2-bit shift on a 16-bit quantity and a 16-bit subtraction.

Looking at the 32-bit count as four bytes, we see that the most significant byte is targeted to count up to exactly ‘1’ per revolution, which means that the next most significant byte counts from 0 to 255 per revolution with the 0 aligned with the index pulse – so we can just look at this byte to give us the current position around the circle in “brads” (one brad = 1/256 of a circle or 1.40625 degrees).

That should be enough explanation to allow you to understand the source code which you can see/download: here.

I’ve not covered how the second interrupt source for the lower row of text works yet though – that’s for another post…

 

 

POV source code – part 1

These posts relate to the previous few concerning the Banggood kit: Cross LED Dot Matrix Display Circuit Board Rotating Electronic Kit

It uses an STC89C52RC microcontroller which is a (fairly) modern Chinese version in the 8051 family.

You can program it in assembler, but I chose to use the C language for this project.

The font

We’re going to be displaying text so we need some kind of font.  The kit provides 16 LEDs arranged vertically on each of the two arms, but I thought that if we use characters the full height of the arms then we wouldn’t be able to fit many of these large characters around the cylinder that the arms sweep out (unless the characters were ridiculously skinny for their height) so I decided to use an 8×8 font which would allow for two rows of characters.

For simplicity I wanted the characters defined in the font to include the spacing to the left and/or right of normal characters so most characters in the font are only 7 pixels wide – or even less for skinny characters such as i – and only a few characters like q and y have descenders, so the spacing between the two rows of characters is included in the font too.

The kit sensibly arranges the pins driving the LEDs so that the chip’s four 8-bit I/O ports, P0, P1, P2, P3 each drive either the top 8 LEDS or the bottom 8 LEDs on an arm:  one arm has P2 on the top and P0 on the bottom, the other arm has P1 at the top and P3 at the bottom (at least that’s the way mine turned out).  I chose to fit all blue LEDs to the P0/P2 arm and all red LEDs to the other one – obviously you can fit them in other ways – it might look pretty to get two extra colours – then you might have (say) yellow and green on the top row with red and blue on the bottom.

Anyway, each of the four ports has the least significant bit at the top, and then the bits going in order down to the most significant bit at the bottom.  If you wire up the motor with the red wire as positive, the board spins clockwise (viewed from above) so the LEDs scan the characters out in right-to-left order.  The wiring is such that the program has to write a ‘0’ to an I/O pin to illuminate the corresponding pin, or write a ‘1’ to switch it off.  It’s convenient to write 8-bit quantities in hexadecimal and in the C language we write 0xFF to indicate all 8 bits high or 0x00 for all eight bits low.

I searched on line and found this 8×8 font which I thought would be suitable:

toncFont The font is on this page and includes the data in C-friendly form http://www.coranac.com/tonc/text/text.htm

I had to manipulate the font data for right-to-left scan order with the least significant bit at the top and a zero-bit indicating ‘LED on’.  I wrote a C# program to do that.

Note that the font starts with the space character (ASCII 32) and is in ASCII order.  The last character (ASCII 127 [DEL]) renders a space too, but as that’s a duplicate you could tweak it to get a £ or € or some other symbol you might want to display.

Say our program wants to display the number 2  This is the ASCII character 50.  Our font doesn’t have characters defined for the first 32 ASCII values so we subtract 32 to get 18 and then multiply by 8 (eight bytes per character in the table) to get 144.  The data we need to display a 2 therefore starts 144 bytes into the font table and consists of the eight bytes:

0xFF,0xB9,0xB0,0xA6,0x8E,0x9C,0xBD,0xFF

So the program outputs those eight bytes in that order and (remembering that the arms scan out the characters from right to left) we get:

two

Cheap POV (persistence of vision) clock kit

POVclockThese kits are available on Banggood.com for about $11 shipping included.  Tricky soldering, and they don’t come with any instructions.  There are instructions available on the website – but not unless you read Chinese.  The pictures on the website help though.

Anyway, I recommend the kit.  Even if you can’t get it working properly, it’s worth the money for the parts alone – you get everything you need apart from a bit of strip board to solder the power supply components to, and a hunk of aluminium to act as a heat sink on one power transistor.

It runs on about 4 volts up to 5 or so.  I wouldn’t recommend going much above 5.5 volts as the ‘regulator’ on the main board is just a small zener diode that leaks current away once the voltage gets up to 5.1  It draws about a third of an amp – I run mine off a single LiPo cell.

Gary asked about the inside of the box.  Please see VoltLog’s build video – the only thing I did differently was to build up the circuit on some stripboard instead of in mid-air, and I added a bit of aluminium plate to the main power transistor to act as a heatsink.

There are more (and bigger) photos of the top and bottom of my stripboard layout here:  http://ceptimus.co.uk/pov/

Look at this video on the Voltlog YouTube channel to get an idea of how tricky it is to build.

I certainly got good value from it as it also comes without a program – so I wrote one, and no tool for uploading the program to the microcontroller.  See my previous post for the upload tool.  Here’s the link to the hex file for my clock program. (Right click and save as… if it opens up as a text file when you click on the link).

http://ceptimus.co.uk/POVclock.hex

I made a quick video showing how to set the time.  The camera settings were bad though – auto focus and strobing problems. 🙁

ISP programmer for the STC89C51 and STC89C52

I couldn’t find an ISP (programmer) for the STC89C52RC that worked with modern versions of Windows. I found several programmers that work with other chips in the STC range.

I did find one that worked on Windows XP, and with some hacking got it to work with Windows 7; however I had no luck with Windows 10. In any case, it was a Chinese program, and as I don’t read that language, I just had to remember which controls to use.

I also found one that worked under Linux, from the command line.  Using that source code as a guide I figured out how to write my own Windows version.

Image1It’s pretty basic but all you really need.  You select the COM port and the hex file you want to upload.  Connect up to your chip with a 5-volt serial port such as an FTDI (or you can use an Arduino UNO or similar that has a 5V FTDI built-in).

Here’s the video showing how to use the Arduino UNO as a programmer.  Here it’s being used to program a Banggood POV kit which uses an STC89C52RC chip – but you could obviously adapt the Arduino sketch and/or make a wiring harness to work with any STC89 board:

 

Here’s the Arduino sketch when you’re using the UNO as a programmer as shown in the video: Arduino sketch

Edit: the programmer program is now at V0.4 which fixes a few bugs and lets you switch the microcontroller to high speed (6T per machine cycle) or normal speed (12T per machine cycle).

Further Edit (August 2017): the programmer program is now at V0.5, and supports some different STC-chip bootloader firmware versions that previous versions did not.

If you’re not using the Arduino (which powers up the STC chip by itself when it detects activity on the serial port) then press the button to start the upload and then apply power to the target chip.  There’s a progress bar indicator – it takes maybe ten seconds to program the chip.

I’ve tried it on Windows 10 and Windows 7.  I’ve no reason to suspect that it won’t work with other versions of Windows.

Beware the fake Prolific serial chips that come in some USB-serial adapters and some cheap STC89C5x development boards.  I have one of those boards. I found an old version of the driver that let me get the Prolific chip operating as a loop back device – it even worked under Windows 10.  But it’s flaky and doesn’t work for programming the chip.  I found the only way I could get the programmer to work reliably was to cut the tracks leading from the STC chip’s serial port to the Prolific chip and connect an FTDI interface to the STC chip instead.

You may also need to fit a pull-up resistor to the STC’s TxD pin – only try that if you think you’ve connected everything up properly but it’s still not working – something like 10K or 100K should do the trick.  It depends on how much pull-up effect is already present on the RxD line of your serial interface and on the amount of stray capacitance present on your boards and cables.

Here is the link to download the install zip file for the programmer

Update: Ewald Burger (see comments below) encountered problems with the installation that he eventually traced to his AVAST virus scanner.  If you encounter problems with the installation you may want to try temporarily disabling your virus scanner while installing.

Here’s the source code – unzip it to your ‘projects’ folder. It was done using the free version of Visual Studio 2015 source code

Fast, small, and simple Arduino SPI RAM chip routines.

The 8-pin chip, 23LC1024, is a good candidate for adding extra RAM to simple Arduinos such as the Uno, Nano, Mini.  It provides 128K of RAM (the ATmega328 chip that these Arduinos use only has 2K of internal RAM).

The chip works at 5V (anything from 2.5V to 5.5V) and uses SPI so it only uses 4 pins of the Arduino to talk to it.  You can buy the chips on eBay for less than £5 each.

I wanted some fast simple routines to copy any arbitrary number of bytes between ‘normal’ RAM and the SPI RAM.  I didn’t want to link in any huge libraries so I wrote these small routines that hit the 328 chip’s SPI registers direct.

There are only three functions:

spiRam::start(); // sets up the SPI interface – call once from your setup() routine

spiRam::writeSpi(*ptr, address, length); // copies to SPI RAM

spiRam::readSpi(*ptr, address, length); // copies from SPI RAM

*ptr is an unsigned byte (uint8_t) pointer – but you can cast any other data type address to that easily – see example.

address is an unsigned long (uint32_t) as addresses in the SPI RAM range from 0 to 131071 (2^17 – 1).

length is an unsigned int (uint16_t) – but will never be bigger than 2048 as the 328 chip only has 2K of RAM.

Standard connections (you can use a different pin than D10 for the chip select – but there’s not much point unless you want to use more than one RAM chip to give more than 128K RAM – see notes in the .h file).

Chip pin Arduino pin
1 D10
2 D12
3 5V
4 Gnd
5 D11
6 D13
7 5V
8 5V

Here’s the sketch: SpiRam23LC1024.zip Unzip it inside your Arduino folder. You want to end up with the SpiRam23LC1024 folder in your Arduino folder with the SpiRam23LC1024.ino and the two other files inside.  Then it should compile and upload okay.  To use it with your own code just move the .h and .cpp files inside your folder where your .ino file is and add the line

#include “spiRam.h”

up at the top of your sketch.

Because the routines are interrupt driven your code could be doing something else while the transfers are taking place – but be careful if you’re not sure – it’s easy to start destroying or changing contents before they’ve finished being transferred if you’re careless. The routines are very fast anyhow (9 milliseconds to transfer 1024 bytes in either direction), so as standard there is a ‘busy wait loop’ until the transfers are complete so you don’t need to worry about checking from inside your code.