POV source code – part 3

If you’re trying to compile the source code with the Keil compiler, you’re probably getting error messages about ‘undefined identifier’ or similar.  This is because the standard Keil reg52.h header file doesn’t define all the necessary identifiers for the STC89C5x chips.

We need to define the special function register (sfr)

P4 = 0xE8;

so the code can access bits of the fourth GPIO port and then define the special bit (sbit)

INT2 = P4^3;

so that the code can react to the infra red photodiode that is connected to that pin.  It seems that the best way to include these extra hardware definitions is to edit and save the standard reg52.h file.  Here’s my modified version: reg52.h

Timer 2 interrupt

The bottom line of the display is handled in a slightly different way.  The idea is that the current rotation rate (measured inside the timer 1 interrupt) is used to calculate the settings for timer 2 so that the timer2 interrupt occurs 256 times per revolution.  Then from inside the timer2 interrupt code we just have to output the next set of 8 pixels to each of the ports that control the lower set of 8 LEDS on the arms.

Timer 2 has a 16-bit counter and an interrupt is generated when it counts up to FFFF.  To get 256 interrupts per turn, we count the number of ticks per turn from interrupt 1 and work out 3/4 of that value.  This is subtracted from FFFF to get the ‘start’ count.

Inside the timer 2 interrupt we count how many interrupts actually occur and use this to tweak the timer 2 start count slightly so that the 256 pulses eventually synchronize and stay in register with the infra red photodiode pulse.  If 256 or more pulses occurred in the last revolution the start value is tweaked slightly lower so that the interrupts during the next revolution happen slightly slower – and vice versa if 255 or fewer pulses occurred.

With the comments in the code that should enable you to modify the code for your own applications.  Be aware that the free version of the Keil compiler limits the compiled code size (to 2K, I think) so the full program here won’t produce a hex file.  But if you trim the program down to only work the clock on the upper row or only scroll a limited amount of text on the lower row, then you can get it to fit within 2K.

For an individual just writing code for fun, the Keil licence costs way too much: if you want to write larger programs you have to use one of the free alternatives to the Keil compiler – which are a little more tricky to set up and get working.

One comment

  1. Hi Martin,

    Although there was no messages from me, i have been trying to compile your code using the free SDCC C-compiler, I don’t know if you know it, but it seems to be a nice alternative for the Keil. I did create a header file for the STC82C52RC and made some slight modifications to your code as well as SDCC prefixes several keywords with an undescore, like “_sbit” instead of “bit” and “_uses” instead of “uses”, for the rest it seems to be quite similar.

    The code seems to compiles just fine, but the clock/messages do not appear on the LED-board.
    The procedure to set the clock however seems to work,

    After setting a time and starting the motor, the pattern I see however is a horizontal line repeatedly moving from top to bottom but no clock.

    If I then shift a piece of paper over the infra red LED, all LEDs turn of and when i remove the paper they remain off until the next start.
    I have tried some simple code to turn several leds on and off and that seems to work just fine, so the compile proces seems to work.

    I was only tonight when I saw your update. I will compare your version of reg52.h to the cersion I created to see if you followed a similar approach as I did.

    By the way I started assembling the upgraded POV I ordered from banggood. I think you ordered that as well. Much easier to assemble than the other one…;-)



Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.