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.

Edit: 2022-May-5. Rather late, I know, especially as the clock kit seems to be no longer available, but I’ve been playing with newer versions of the STC microcontrollers (notably the STC8A8K64S4A12, which is cheap, but a blindingly fast powerhouse) and installed the latest version of SDCC under Linux Mint, so I have an open-source way of compiling 8051 code without hitting the Keil 2K limit. I edited the source code for the POVclock (with the Dickens scrolling text on the bottom line) so that it compiles with SDCC, and uploads using the stcgal uploader. If the file is named POVclockSDCC.c then compile with:

sdcc –mmcs51 –std-c99 POVclockSDCC.c

Upload the hex (.ihx) file with:

stcgal POVclockSDCC.ihx

The source code that compiles with a standard install of sdcc is here: https://ceptimus.co.uk/POVclockSDCC.c

Main differences were to change the include to <8052.h>, add some double-underscores in front of the keywords code, interrupt, using, bit (sbit), add the definitions for P4, INT2, and T2MOD to the c file (instead of modifying the standard header file), adding a few initializers = 0 to keep the compiler happy about not using variables before they were initialized, and putting some static qualifiers on the interrupt edge detection ‘snapshot’ bits. That got it to compile to a hex file without errors or warnings (and actually work). I’m using the latest version of sdcc at the time of writing sdcc -v

gives 4.2.0 but I think it should work with whatever older versions of sdcc come with your distro: just use whatever package manager your distro has and install sdcc and stcgal.


Posted

in

,

by

Tags:

Comments

  1. Ewald Burger avatar
    Ewald Burger

    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…;-)

    Regards,

    Ewald

Leave a Reply

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