Radio Controlled Timer, Step D

  • Setting and reading date/time in the RTC (Real Time Clock) DS1302.
  • Using a super cap for DS1302 backup

No need to read individual bytes in the RTC, only burst mode needed.

Reading the DS1302 datasheet, the minimum clock low or high time is 1 µs. As the 1829 runs at 4MHz, the minimum instruction execution time is 1 µs. No need for delays in program, except for the CE signal requiring a 4µs delay before and after clocking data.

As functions in other source files will read/write the RTC registers a common definition of the RTC clock registers are needed. Created “” file.

“RTC_1829_RadioControlledTimer.asm” (and “.inc”) implements the burst read/write functions.


Previously parts where simple to test.

A. If MPLAB X signalled “Running”
B. LED light
C. Character received in Realterm

But this part needs some sort of unit tests and these tests should not be included in the final application.

(OK, integration test may be more adequate term to use, but I’ll stick to the term unit test for all automatic tests.)

Used the MPLAB X possibility to use different configurations

Renamed the default configuration to “Final” and created “Unit_testing” by duplicating “Final” configuration.

Final : no change, the same as default

Unit_testing : defined the Preprocessor macro “UNIT_TEST” for mpasm.

Now there are 2 configurations to select between.

And in the source code an “ifdef UNIT_TEST” or “ifndef UNIT_TEST” directive can be used to select different code blocks.

To start the unit tests another main program is needed. Ok it is possible to use the “main_1829_RadioControlledTimer.asm” file with ifdef’s but the unit test will clobber up this file.

Better to use a separate main file for unit tests.

Created a new .asm file “main_unittest_1829_RadioControlledTimer.asm”.

In MPLAB X there is a possible to exclude some files for a configuration. So the Final configuration excludes “main_unittest_1829_RadioControlledTimer.asm” and the Unit_testing configuration excludes the “main_1829_RadioControlledTimer.asm


Assuming there will be a need for more unit tests later on, created an .asm  file containing only the unit test for the RTC module, “RTC_unitest_1829_RadioControlledTimer.asm” and a corresponding .inc file.

Some other files also added to make the structure easier to follow.

  • main_unittest_1829_RadioControlledTimer.asm” contains a routine that unittests jumps to if any error detected. An “.inc“file added.
  • Unit testing uses some common routines/macros, added “UnitTest_common_1829_RadioControlledTimer.asm” and its “.inc” file.

This is what the project file structure looks like now when Final configuration selected. The excluded files showN as “grey”.

To be able to see the result from the unit tests, output to UART where include and the LED’s was also used.

When starting RTC unit tests “RTC” is written to the UART. As every (ok only 1 yet) unit test is assigned a number this number is printed to the UART after success.

If all unit tests pass, the green LED is lit.

If any test fails the red LED is lit. And with the help of the UART printout the failing test can be pinpointed.

When you run the tests for a number of times the RealTerm screen becomes a bit messy to read. But RealTerm can emulate an ANSI terminal and it is possible to “clear screen” by sending a specific character sequence, beginning with an Escape character (ESC). Every time the unit testing starts a “clear screen” sequence is sent. (Sorry, if you use the Arduino serial monitor you will get a number of junk characters, as this monitor don’t understand ANSI character sequences.)

The supercap charging can be observed, after power has been applied some time, by disconnecting programmer and any battery/power. Wait some minute and measure the supercap voltage. It should be above 2V.


Code on Github

EDIT: 2018-10-15
  • Of course, 1829 ICSPDAT and ICSPCLK = pin 19 and pin 18
  • Green LED still connected to RC3, pin 7
  • 1302 CE = pin 5, SCLK = pin 7

Schema changed!

Leave a Reply

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