PIC32 to RTC Communication via MMBASIC (3-wire Protocol)

Set Micromite MMBASIC's DATE and TIME Variables using External RTC Module (DS1302 RTC as part of a Velleman VMA301 Module)

main project image

A "Micromite" is a PIC32 microcontroller with MMBASIC firmware loaded onto it, allowing the user to create BASIC programs that have access to the PIC32's peripherals and capabilities using a beginner-friendly, high level language.

The MMBASIC environment has some system variables which keep track of the PIC32's internal timer (which can be used to derive a time and date). These variables get reset to midnight of January 1, 2000 on each power cycle (because the timer these values are based on is reset with each power cycle). Here's what those variables hold soon after power on:

> PRINT DATE$

01-01-2000

> PRINT TIME$

00:00:10

In order to have the actual (current, real-world) date and time, an external RTC module can be used. This project uses a DS1302 chip that is hooked up to a backup battery source (a CR2032 3V coin battery) to maintain the time when the power to the PIC32 is off. Specifically, a module made by Velleman, called the VMA301, is used. This module has the DS1302 chip, an external clock crystal, the backup battery, and external pins (4 of which are used in this project: GROUND, DATA, CLOCK, RESET).

This project physically hooks up the RTC module to a minimal Micromite setup (which is on a breadboard), and two BASIC functions are written which can be used to interact with the VMA301 module:

main project image

The user interacts with the Micromite using a terminal emulator such as Tera Term, running on a computer connected to the Micromite via a USB cable. Here's what the input/output looks like for each of the BASIC programs that were built for this project: main project image main project image

Project Setup

Micromite on breadboard minimal setup

1. First make sure all the connections are in place on the Micromite breadboard (so without the VMA301 connected) and that a stable connection can be made to Tera Term. Set the terminal emulator to 80 x 30 characters, 38400 baud.

2. Note the connections from PINs 11 and 12 to the TX and RX pins on the FTDI module. PIN 11, labeled "Console Tx" in the micromite manual, goes to the pin labeled "RXD" on the FTDI board PIN 12, labeled "Console Rx" in the micromite manual, goes to TXD.

3. The FTDI board converts a UART signal from the PIC32 to a USB transmission, where it is then converted back to a UART signal on the host computer. The host must have an FTDI driver which presents an available USB port as a COM port (for serial communication).

4. A diagram of the PIC32MX pinout will be needed to decide which pins will be used for digital input/output. This can be found in the Micromite manual.

5. Connect the VMA301 to the Micromite using the following connections:

VMA301 wiring

6. Connect these wires to the PIC32 breadboard:

PIC32 wiring

Project Solution, Part 1: Research DS1302

Starting with the DS1302 datasheet, I gathered information about how the DS1302 chip worked. (note: MMBASIC supports other RTC chips directly in it's BASIC software library, utilizing standard I2C communication protocols, but the chip used in this project, the DS1302, isn't on the supported list, as it uses a proprietary 3-wire communication, requiring a careful reading of the datasheet and a "bit banging" software solution).

Project Solution, Part 2: Implement MMBASIC Code for SETTIME

Run this program to gather current date/time from the user (from command line), and the program will communicate with the DS1302 to update it's memory/registers. The following is pseudocode for how the program functions. You can see the actual source code here.

Project Solution, Part 3: Implement MMBASIC Code for GETTIME

"GETTIME" in this sense refers to "getting" the date/time information that is stored on the DS1302 and using it to set MMBASIC's TIME$ and DATE$ variables. The following is pseudocode for how the program functions. You can see the actual source code here.