Silicon ChipThe Micromite: An Easily Programmed Microcontroller, Pt.1 - May 2014 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Publisher's Letter: Planning for future disposal of your assets
  4. Feature: Android Apps For Tech-Savvy Users by Stan Swan
  5. Project: RGB LED Strip Controller/Driver by Nicholas Vinen
  6. Project: The Micromite: An Easily Programmed Microcontroller, Pt.1 by Geoff Graham
  7. Product Showcase
  8. Project: 40V Switchmode/Linear Bench Power Supply, Pt.2 by Nicholas Vinen
  9. Project: Deluxe 230VAC Fan Speed Controller by John Clarke
  10. Salvage It: What can you do with a dead UPS... or two? by Bruce Pierson
  11. Review: Tektronix MDO3054 Mixed-Domain Oscilloscope by Nicholas Vinen
  12. Vintage Radio: The AWA B30: a transistor radio just like grandma's by John Carr
  13. Subscriptions
  14. Order Form
  15. Market Centre
  16. Advertising Index
  17. Notes & Errata
  18. Outer Back Cover

This is only a preview of the May 2014 issue of Silicon Chip.

You can view 27 of the 104 pages in the full issue, including the advertisments.

For full access, purchase the issue for $10.00 or subscribe for access to the latest issues.

Items relevant to "RGB LED Strip Controller/Driver":
  • RGB LED Strip Driver PCB [16105141] (AUD $10.00)
  • ATmega48-20AU programmed for the RGB LED Strip Driver/Controller [1610514B.HEX] (Programmed Microcontroller, AUD $15.00)
  • SMD parts for the RGB LED Strip Driver (Component, AUD $20.00)
  • Firmware (C and HEX) files for the RGB LED Strip Driver [1610514B.HEX] (Software, Free)
  • RGB LED Strip Driver PCB pattern (PDF download) [16105141] (Free)
Items relevant to "The Micromite: An Easily Programmed Microcontroller, Pt.1":
  • PIC32MX170F256B-50I/SP programmed for the Micromite Mk2 plus capacitor (Programmed Microcontroller, AUD $15.00)
  • PIC32MX170F256D-50I/PT programmed for the Micromite Mk2 (44-pin) (Programmed Microcontroller, AUD $15.00)
  • CP2102-based USB/TTL serial converter with 5-pin header and 30cm jumper cable (Component, AUD $5.00)
  • Firmware (HEX) file and user manual for the Micromite (Software, Free)
  • Firmware (HEX) file and user manual for the 44-pin Micromite (Software, Free)
  • 44-pin Micromite PCB pattern (PDF download) [24108141] (Free)
  • 44-pin Micromite PCB [24108141] (AUD $5.00)
Articles in this series:
  • The Micromite: An Easily Programmed Microcontroller, Pt.1 (May 2014)
  • The Micromite: An Easily Programmed Microcontroller, Pt.1 (May 2014)
  • The Micromite: An Easily Programmed Microcontroller, Pt.2 (June 2014)
  • The Micromite: An Easily Programmed Microcontroller, Pt.2 (June 2014)
  • Micromite, Pt.3: Build An ASCII Video Display Terminal (July 2014)
  • Micromite, Pt.3: Build An ASCII Video Display Terminal (July 2014)
  • The 44-pin Micromite Module (August 2014)
  • The 44-pin Micromite Module (August 2014)
Items relevant to "40V Switchmode/Linear Bench Power Supply, Pt.2":
  • 40V/5A Hybrid Switchmode/Linear Bench Supply PCB [18104141] (AUD $20.00)
  • SMD parts for the 40V/5A Hybrid Switchmode/Linear Bench Supply (Component, AUD $50.00)
  • 40V/5A Hybrid Switchmode/Linear Bench Supply PCB pattern (PDF download) [18104141] (Free)
  • 40V/5A Hybrid Switchmode/Linear Bench Supply panel artwork (PDF download) (Free)
Articles in this series:
  • 40V Switchmode Bench Power Supply, Pt.1 (April 2014)
  • 40V Switchmode Bench Power Supply, Pt.1 (April 2014)
  • 40V Switchmode/Linear Bench Power Supply, Pt.2 (May 2014)
  • 40V Switchmode/Linear Bench Power Supply, Pt.2 (May 2014)
  • 40V Switchmode/Linear Bench Power Supply, Pt.3 (June 2014)
  • 40V Switchmode/Linear Bench Power Supply, Pt.3 (June 2014)
Items relevant to "Deluxe 230VAC Fan Speed Controller":
  • Deluxe 230VAC Fan Speed Controller PCB [10104141] (AUD $10.00)
  • AOT10N60 High-voltage Mosfet for the 230VAC Fan Speed Controllers (Component, AUD $5.00)
  • Deluxe 230VAC Fan Speed Controller PCB pattern (PDF download) [10104141] (Free)
  • Deluxe 230VAC Fan Speed Controller panel artwork (PDF download) (Free)

Purchase a printed copy of this issue for $10.00.

Introducing the By GEOFF GRAHAM Micromite, Pt.1 . . . an easily programmed powerful microcontroller Want a powerful microcontroller in your next custom project but you are concerned about how to program it? Behold the Micromite! It’s a low-cost 28-pin PIC32 microcontroller which comes loaded with a Microsoft-compatible BASIC interpreter with all the features you need. And programming with MMBasic is dead easy. This month we describe its features, show how to drive it and how to use it to build a GPS-Controlled Digital Clock. B ELIEVE US! Even if you’ve never programmed a micro before, now you can do it! If you thought PICAXE was good, just check out the Micromite! MMBasic has all the power that you need including a huge amount of memory, floating point numbers, string handling, arrays, 19 I/O pins, two serial ports, I2C, SPI, 1-Wire and PWM. You can write, test and save your program on the chip (it even includes a full-screen editor) and it is very easy This low-cost 28-pin PIC32 chip includes a full-featured BASIC interpreter with the capability of driving 19 I/O pins, two serial ports, I2C, SPI, 1-Wire and PWM. It can also handle up to five servos, infrared remote control, distance sensors, temperature sensors and much more. 28  Silicon Chip to get something up and running. When you have finished, you can lock down the chip and it will automatically run your program at start up. For people who remember our Maximite series of computers, we are using the same Microsoft-compatible BASIC programming language but it’s now running inside a cheap 28-pin IC. The result is not quite a Maximite . . . but it’s close. Instead of using a keyboard, video & USB, it uses serial I/O for the console and instead of using an SD card, it stores its program in internal flash memory. Other than that, the Micromite runs the same full MMBasic with all its high level features including easy control of its I/O pins, powerful mathematics and a full range of communications protocols. It has extra features such as being able to: (1) change the processor’s clock speed (to reduce power consumption), (2) put the chip to sleep (80µA sleep current) and (3) set a password to prevent someone from listing/changing the program, etc. As far as performance is concerned, it’s no slouch. Our benchmark clocks it at 21,748 lines/second (the Colour Maximite does 27,340) and it has a total of 42KB memory for the program plus variables (the Colour Maximite has 31KB). Everthing happens inside the chip; the only extra component needed is a 47µF capacitor. The power supply requirements are tiny. The Micromite is powered from a 2.3V-3.6V rail and consumes between 4mA and 25mA, depending on the clock speed selected. This can be provided by a couple of 1.5V AA cells or a simple power supply. Where’s the PCB? Readers familiar with the Maximite might ask “where’s the PCB with a display, I/O connectors, etc?” There is none! What? How can that be? The answer is to not think of the Micromite as a computer but as a programmable microcontroller which you build into a circuit and program in place. If you want to experiment with the chip, you can plug it into a solderless breadboard as shown on the opposite page. Once you have the hang of how it works, you then design a circuit around it and develop your program while it is in the circuit. This “in circuit development” is siliconchip.com.au very productive as it allows you to develop and test small parts of the program as you go. For example, if your project was a home-brew controller, you could develop and test the temperature sensor first, then the power control and so on. The final program would just string these modules together. Quick demonstration For a lot of people, microcontrollers are a mysterious technology. If you buy one and simply connect it into a circuit, it will do nothing. That’s because you must first write and install a software program for it. That usually involves installing the required compiler, linker and other software on a desktop computer, then learning a complex programming language such as C or Assembler. As a result, most people’s experience with microcontrollers simply consists of buying a pre-programmed chip as part of a kit. But then you cannot change what it does to suit your preferences, since all the instructions are encoded in a cryptic hex file; unless, that is, you have the original source code, a compiler and a programmer to reflash the firmware. You also need to understand the language used for the source code to make any modifications. The Micromite is completely different and we will now show you how it’s programmed by quickly demonstrating how to flash a LED on and off. To begin, the Micromite is programmed via a serial terminal. You have many choices here and we will go into these later. Once it’s connected, you will be presented with the Micro­ mite’s command prompt (a greater than symbol, ie ‘>’). At this point you can enter the command EDIT and the Micromite’s full screen editor will start up. Fig.1 shows the editor in action with the LED flashing program already entered. The first line of the program configures an output pin (pin 15 in this case) as a digital output (DOUT). The program then enters a continuous loop where the output is repeatedly set high and then set low again, with a pause of 250ms between each state. This is as simple as it gets and is all that that is needed to make a LED cycle on and off. When you exit the editor the program will be automatically saved to the Micromite’s internal flash memory siliconchip.com.au Fig.1: the inbuilt editor is very useful as it allows you to edit, save and run your programs directly on the Micromite. You don’t need a host computer, compiler or other special software (other than a terminal emulator). If you want to experiment with the chip you can plug it into a solderless breadboard. Then, once you have the hang of how the chip works, you can design a circuit around it and develop your program while it is in circuit. This test set-up is running the flashing LED program shown in Fig.1. which is non-volatile. This means that you will never lose your work, even if you remove the power. If you now type RUN at the command prompt, the program will run, flashing the LED on and off. It’s a very simple program but it does illustrate how the Micromite can interface to external circuitry. You now have the Micromite doing something useful (if you can call flashing a LED useful). If that’s all you want it to do, you can then instruct MMBasic to always run this program whenever power is applied. That’s done by entering the command OPTION AUTORUN ON at the command prompt. To test this, simply remove the power and then reapply it again. The Micromite should immediately begin flashing the LED. If you now disconnect the serial console, it will sit there flashing the LED forever (well, for as long as the battery lasts). And if you ever need to change something (for example, to flash a second LED), it’s just a matter of re-attaching your serial terminal to the Micromite while it’s still in circuit and editing the program as required. That’s the great benefit of the Micro­ mite – it’s very easy to write and change a program. Microcontroller The Micromite is essentially a MicroMay 2014  29 (WIRED TO +V DIRECTLY OR VIA 10kΩ RESISTOR) RESET 1 DIGITAL / INT / ANALOG 2 SPI OUT / DIGITAL / INT / ANALOG 3 PWM1A / DIGITAL / INT / ANALOG 4 PWM1B / DIGITAL / INT / ANALOG 5 PWM1C / DIGITAL / INT / ANALOG 6 COM1: ENABLE / DIGITAL / INT / ANALOG 7 GROUND 8 COM2: TRANSMIT / INT / DIGITAL 9 COM2: RECEIVE / INT / DIGITAL 10 CONSOLE Tx (DATA OUT) 11 CONSOLE Rx (DATA IN) 12 POWER (+2.3 TO +3.6V) 13 SPI IN / 5V / DIGITAL 14 28 27 26 25 24 23 22 21 20 19 18 17 16 15 ANALOG POWER (+2.3 TO +3.6V) ANALOG GROUND ANALOG / DIGITAL / PWM2A ANALOG / DIGITAL / SPI CLOCK ANALOG / DIGITAL / PWM2B ANALOG / DIGITAL DIGITAL / 5V / COM1: RECEIVE DIGITAL / 5V / COM1: TRANSMIT 47 µF TANT CAPACITOR (+) TO GROUND GROUND 2 DIGITAL / 5V / COUNT / I C DATA 2 DIGITAL / 5V / COUNT / I C CLOCK DIGITAL / 5V / COUNT / WAKEUP/ IR DIGITAL / 5V / COUNT Fig.2: these are the pin connections for the Micromite while below are the functions that each pin can be used for. The pins marked with colour labels are used for power and serial data communications, etc and cannot be used for general I/O. The other pins can be used for the following functions: • ANALOG: these pins can be used to measure voltage (AIN). • DIGITAL: can be used for digital I/O such as digital input (DIN), digital output (DOUT) and open collector output (OOUT). INT: can be used to generate an interrupt (INTH, INTL and INTB). • COUNT: can be used to measure frequency (FIN), period (PIN) or counting (CIN). • • 5V: these pins can be connected to 5V circuits. All other I/O pins are strictly 3.3V maximum. • COM xxx: used for serial communications. • I2C xxx: used for I2C communications. • SPI xxx: if SPI is enabled, these pins can be used for SPI I/O. • PWM xxx: PWM or SERVO output (see the PWM and SERVO commands). • IR: can be used to receive signals from an infrared remote control (see the IR command). • WAKEUP: can be used to wake the CPU from a sleep (see the CPU SLEEP command). Note: the mnemonics in brackets are the modes used in the SETPIN command. chip PIC32MX150F128 microcontroller programmed with the MMBasic firmware (available on the SILICON CHIP website, along with the Micromite User Manual). Blank chips can be purchased for $3-$4 from ele­ment14, RS Components, Digi-Key etc or direct from Microchip. You will also need a programmer such as the PICkit3 to install the MMBasic firmware. An easier option is to purchase a pre-programmed chip from the SILICON CHIP Online Shop for $15 (plus postage) and we will even throw in the 47µF capacitor that you need. A panel later in this article lists the chips that you can use. Essentially, they are available with two frequency ratings (40MHz and 50MHz) and in a variety of package styles. When the Micromite starts up, its clock frequency will be set at 40MHz but this can be changed to 48MHz under program control. We have tested a number of 40MHz chips at 48MHz and they worked fine, so the decision as to exactly which chip you want to use 30  Silicon Chip is up to you. The chip that’s supplied pre-programmed from the SILICON CHIP Online Shop is rated at 50MHz and has 19 I/O pins (see below). You can also use the PIC32MX25­ 0F128 series of chips from Microchip. These have two pins dedicated to a USB controller (which is not used in the Micromite) so these chips only have 17 I/O pins available to your BASIC program. MMBasic will also run on the 44-pin surface-mount chips in the PIC32MX150F128 range and these give you a 33 I/O pins to play with. We’ll have more to say about this chip in a follow-up article. I/O pins Fig.2 shows the pin-outs of the Micromite and the capabilities of each I/O pin. It would be worth copying and laminating this diagram as you will find yourself referring to it often when designing with this microcontroller. In MMBasic, the pin numbers are the same as the physical pin numbers on the chip. So, for example, PIN(15) = 1 will set the physical pin 15 on the chip high. Nine of the 28 pins are dedicated to functions such as power and ground so that leaves 19 pins that you can use in your program. All the I/O pins can be set as digital inputs or outputs. An input uses TTL voltage levels, has a high input impedance (typically 1MΩ) and a Schmitt trigger buffer which ensures a clean transition from high to low. The pins that are marked ‘5V’ can be connected to a 0-5V source, while the remainder are limited to 3.3V maximum. When used as digital outputs each I/O pin can source or sink about 10mA. This can be used to directly drive a LED via a resistor (typically 82Ω) or a transistor which can in turn control a relay and some other high-powered device. Ten of the pins can also be set to analog inputs. In this mode, the input is returned as a floating point number representing the voltage on the pin. For example, when measuring an alkaline siliconchip.com.au cell, the reading might be 1.246. The four pins marked COUNT can be used for measuring frequency (up to 400kHz), measuring the period between input pulses or simply as a counter counting the number of pulses on the input over time. If by now you are fretting about remembering and using all these functions, the Micromite firmware download on the SILICON CHIP website includes a comprehensive user’s manual (over 63 pages). We will also explore the above features and more in detail next month. Specialised functions Many of the pins on the Micromite are also used for more specialised functions. For example, the pins marked COM1 and COM2 can be used for serial communications. COM1 is especially versatile; it can run at up to 230,400 baud and will support RS-232 signals without a transceiver, as well as 9-bit transfers with RS-485 support. Pins 3, 14 & 25 can be used for SPI (Serial Peripheral Interface) communications which can run at up to 10MHz, while pins 17 & 18 support the I2C protocol at speeds up to 400kHz as either a master or a slave. In addition, any I/O pin can be used for 1-Wire communications and MMBasic even includes a special function to conveniently get the reading from a DS18B29 temperature sensor chip. In practice, multiple temperature sensors can be used and this makes the Micromite ideal for temperature control and monitoring. There are five pins marked PWM and these can generate a pulse width modulated (PWM) signal of between 20Hz and 500kHz. This feature lets you control devices that require an analog input voltage, such as motor speed controllers or test equipment. This function is also useful for dimming the backlight of LCD modules under program control. Each PWM output can also control a servo using the SERVO command, so you can control up to five such devices. The IR function pin (pin 16) can be used to receive signals from an infrared remote control. This will work with any Sony-compatible remote control and MMBasic will do all the work for you in decoding the signal and handling features such as automatic repeat. This allows your Micromitesiliconchip.com.au Micromite Specifications Power Supply Supply Voltage: Current at 48MHz: Current at 5MHz: Sleep current: 2.3-3.6V (3.3V nominal) <at> 5-25mA; 4V absolute maximum 26mA typical (plus current from the I/O pins). 5mA typical (plus current from the I/O pins). 80µA typical (plus current from the I/O pins). Digital Inputs Logic Low: Logic High: Input Impedance: Freq. Response: 0-0.65V 2.5-3.3V on normal pins; 2.5-5.5V on pins rated at 5V >1MΩ. All digital inputs are Schmitt trigger buffered up to 200kHz (pulse width 20ns or more) on the counting inputs (pins 15-18). Analog Inputs Voltage Range: Accuracy: Input Impedance: 0-3.3V analog measurements are referenced to the supply voltage on pin 28 and the ground on pin 27. If the supply voltage is precisely 3.3V, the typical accuracy will be ±1%. >1MΩ (for accurate readings, the source impedance should be <10kΩ) Digital Outputs Current draw or sink: 10mA (absolute maximum 15mA per pin or 200mA for the whole chip) Maximum Open Collector Voltage: 5.5V PWM Output Frequency range: Duty cycle: 20Hz to 500kHz 0-100% with 0.1% resolution below 25kHz Communications Speeds Console Serial: COM1 Serial: COM2 Serial: SPI: I2C: 1-Wire: default = 38,400 baud; range = 100bps to 230,400bps (at 40MHz) default = 9600 baud; range = 10bps to 230,400bps (at 40MHz) default = 9600 baud; range = 10bps to 19,200bps (at 40MHz) 10Hz to 10MHz (<at> 40MHz clock speed); limited to one quarter of the clock speed. 10-400kHz fixed at 15kHz Timing Accuracy All timing functions (the timer, tick interrupts, PWM frequency, baud rate, etc) dependent on the internal fast RC oscillator which has a specified tolerance of ±0.9% but typically is within ±0.1% at 24°C. Flash Endurance Over 20,000 erase/write cycles. Every program save incurs one erase/write cycle. In a normal program development it is highly unlikely that more than a few hundred program saves would be required. based project to be remotely controlled with just a few lines of BASIC. You can also generate IR commands from within MMBasic, so that one Micromite could control another via infrared using an appropriate IR LED. Driving an LCD module Another handy feature built into MMBasic is the ability to directly control low-cost LCD modules with one, two or four display lines. By using just one program instruction, you can display whatever data you would like on these modules. An associated feature is the ability to connect a 4 x 3 or 4 x 4 keypad and easily read the key presses using your BASIC program. This, along with the LCD driver, makes it easy to build things like burglar alarms, reticulation controllers, home-brew controllers and more, all based on the Micromite. We will describe these features in greater detail next month and give some example circuits. Loading the firmware As mentioned earlier, you can purchase a Micromite chip preprogrammed with MMBasic from the SILICON CHIP shop. That’s the easy May 2014  31 +2.3 TO +3.6V (25mA) PICKIT 3 CONNECTOR MCLR Vcc GND PGD PCC (NC) 10k 1 1 28 27 2 3 4 4 5 5 6 MICROMITE 20 8 47 µF 6V CERAMIC OR TANTALUM 19 13 LOADING FIRMWARE Fig.3: here’s how to connect a blank PIC32 chip to a PICkit3 programmer to load the MMBasic firmware. Once connected, you use MPLAB IPE (free from Microchip) to program the device. BASIC CONNECTIONS 1 28 +2.3 TO +3.6V (25mA) (CAN BE 2 x AA CELLS OR A NOMINAL 3.3V POWER SUPPLY) 27 SERIAL CONSOLE: VT100 TERMINAL OR USB TO TTL CONVERTER (38,400 BAUD, 8 BITS, NO PARITY, 1 STOP BIT, TTL VOLTAGE LEVELS) 8 MICROMITE 20 47 µF 6V Rx SERIAL TERMINAL Tx DATA FROM MICROMITE DATA TO MICROMITE GND 11 12 13 19 CERAMIC OR TANTALUM Fig.4: follow this diagram to connect the Micromite to a serial terminal. The easiest option is to use a USB-to-serial converter (eg, Jaycar Cat. XC4241) or the USB-To-RS232C Serial Interface described last month. option but you could also purchase a virgin chip (ie, blank) and load the firmware yourself. That’s done using a programmer such as the Microchip PICkit3. These are reasonably cheap and there are clones on eBay that are even cheaper. If you install the free Microchip MPLAB X development environment on your computer, you will find that it includes the MPLAB IPE which is an independent programmer that knows how to drive the PICkit3. You connect the PICkit3 to the blank PIC32 chip as shown in Fig.3. It’s then just a matter of instructing MPLAB IPE to program the device after which you can disconnect the programmer as it will not be needed again (unless you wish to later re-program the chip with an updated version of the firmware). 32  Silicon Chip Note that unlike the Maximite, the Micromite doesn’t have the ability to update its firmware itself. So you will need the PICkit3 if an MMBasic update is released and you wish to upgrade. That said, we made a considerable effort to remove any bugs and the firmware has been checked by a team of over 40 beta testers for a couple of months, so we believe that the need for this will be unlikely. Connecting it Once you have the chip running MMBasic, you can connect it into a circuit. A solderless breadboard makes it easy to experiment. With this set-up, you can program and test the Micromite chip with external devices such as LEDs, button switches and sensors. Fig.4 shows the basic terminal con- nection diagram. As previously stated, the power supply voltage can range from 2.3-3.6V (3.3V recommended) and can come from a couple of 1.5V AA cells or a conventional supply. The Micromite’s current drain is modest (25mA maximum plus the current drawn by any external LEDs, etc), so a simple power supply would be fine. Generally, it’s good design practice to install a 100nF bypass capacitor close to each of the power supply pins but this is not critical and they are not shown in this diagram. The capacitor connected to pin 20 is essential and is used to stabilise the internal 1.8V regulator that powers the chip’s CPU. It must be a highquality capacitor (not an electrolytic) and should have a minimum value of 10µF with an ESR (equivalent series resistance) of less than 1Ω. The recommended type is either a 47µF tantalum or a 10µF multilayer ceramic. Console connection You also need to connect a serial terminal to the pins reserved for the console. The console defaults to 38,400 baud, eight bits, no parity and one stop bit. The voltage levels are TTL which means that idle is voltage high (3.3V), the start bit is voltage low, data logic 1 is voltage high and the stop bit is voltage high. This is standard in the microcontroller world so you will not have any trouble finding something to connect to it. Probably the easiest option is to use one of the many USB-to-serial converters available such as Cat.XC4241 from Jaycar (its output switch should be set for 3.3V operation). If you look on the internet, you will find thousands more with prices as low as $5. The USB-To-RS232C Serial Interface described in the April 2014 issue of SILICON CHIP could also be used. However, since its output swing is 5V and the Micromite’s serial interface can only handle 3.3V, you need to install a 1kΩ resistor in series with pin 12. In fact, this is a good idea regardless, as it will protect the chip in case you hook up a RS-232 port or other serial adaptor that works above 3,3V. These converters plug into a spare USB port on your computer and at the other end provide a TTL level serial output which can be directly connected to the console input/output pins of the Micromite. Another option is the SILICON CHIP siliconchip.com.au ASCII Video Terminal which we will describe in a following article. As well as containing a USB-to-serial converter, this cheap gadget will also take an input from a standard PS/2 keyboard and output either composite or VGA video on a suitable monitor. This means that you can develop and run your Micromite programs without a host computer. You may be tempted to directly connect an RS-232 serial device, such as a PC’s serial port, directly to the Micromite’s console pins. Don’t do this, as RS-232 uses ±12V for signalling and you could easily damage your Micromite. If you do want to connect an RS-232 device to the console pins, you must use a converter. ASCII serial terminal Now that you are connected you need a terminal emulator running on your computer. This is a piece of software that emulates an ASCII serial terminal. Anything typed on your computer’s keyboard will be sent to the Micromite via the USB-to-serial converter and any output from the Micromite will be displayed on your computer’s screen. It’s important that your terminal program emulates a VT100 terminal as the program editor built into the Micromite uses that scheme to control the cursor and to display things like reverse video. For Windows, Tera Term is the best choice and the Micromite Fig.5: MMEDIT was written by Jim Hiley and can be installed on a Windows or Linux PC. It allows you to edit your program on the PC and then, with a single mouse click, transfer it to the Micromite for testing. has been extensively tested with it. When installed with the correct drivers, the USB-to-serial converter appears as a serial COM device on your PC, eg, as COM17. In the Tera Term set-up menu, you should select the COM number and set the other items to 38,400 baud, eight bits, no parity and one stop bit. If you don’t know the COM number for your USB-to-serial converter, you can check this in Device Manager (under Serial Ports). Alternatively, you can select each port found by Tera Term in turn and test it by pressing return (ie, a trial and error process). When you find the Micromite (ie, the correct port is selected), you will be greeted by the command prompt (ie, >). If you are using the ASCII Video Terminal (described in a future issue), it’s even easier – just connect a keyboard and a monitor and set the jumpers to a baud rate of 38,400. You don’t need a terminal emulator as the ASCII Video Terminal emulates a VT100 terminal, so you can edit your programs on the Micromite using only this device. In fact, the Micromite and the ASCII Video Terminal together provide most of the facilities of the original Maximite in two 28-pin DIP ICs. Program editor This is a typical USB-to-TTL serial converter that you can use with the Micromite. The serial interface is connected to the Micromite’s console pins, while the USB interface goes to a standard PC. You can use a terminal emulator such as Tera Term to connect to the Micromite and edit/ run your programs. A converter like this can also be used with MMEDIT which gives you much better editing facilities. siliconchip.com.au As stated, the built in editor is very useful as it allows you to edit, save and run your programs on the Micromite. You don’t need a host computer, compiler or other special software (other than a terminal emulator). Fig.1 shows what the editor looks like. The editor is invoked with the EDIT command. The cursor will be automatically positioned where you left off editing or, if your program has just been stopped by an error, will be positioned at the line that caused the error. If you are used to an editor like Notepad, you will find that the operation of this editor is familiar. The arrow keys will move your cursor around in the text, while home and end will take you to the beginning or end of the line. Page up and page down will do what their titles suggest, the delete key will delete the character at the cursor, and backspace will delete the character before the cursor. The insert key toggles between insert and over-type modes. About the only unusual key combination is that two home key presses will take you to the start of the program and two end key presses will take you to the end. At the bottom of the screen, the status line will list the various function keys used by the editor and their action. These include save (F1), save and run (F2), find (F3), etc. The editor also includes the facility for marking text which can be copied or cut to the clipboard and inserted elsewhere. By using this editor, you can write your program and then save and run it directly on the Micromite by pressing the F2 key. If the program stops with an error, pressing function key F4 will run the editor again with the cursor positioned at the line that caused the error. This edit/run/edit cycle is very fast. MMEDIT Another convenient method of creating and testing your programs is to use “MMEDIT” (see Fig.5). This program was written by SILICON CHIP reader Jim Hiley from Tasmania. It can be installed on a Windows or Linux computer and it allows you to edit your program on your PC then, with a single button May 2014  33 REG1 LP2950-3.3 +5V GND IN 10 µF GND +5V +3.3V OUT 1 10 µF 13 LCD CONTRAST VR1 10k 28 20 47 µF 6V TANTALUM 2 Vdd (CERAMIC PATCH ANTENNA) Vcc GLOBALSAT EM-408 GPS RECEIVER MODULE EN RxD TxD GND 5 1 3 21 4 22 17 4 18 6 IC1 MICROMITE RS EN 16 x 2 LCD MODULE CONTRAST D7 D6 D5 D4 D3 D2 D1 D0 GND 1 14 13 12 11 10 9 8 7 26 3 R/W 5 25 2 24 23 TO SERIAL CONSOLE Rx Tx DATA IN 12 DATA OUT 11 GND LP2950-3.3 GND IN 8 19 OUT 27 GPS-CONTROLLED CLOCK CIRCUIT Fig.6: the circuit details for our GPS-Controlled Digital Clock. The Micromite (IC1) decodes the output from the GPS module, calculates the time zone offset and daylight saving adjustment and drives a 16 x 2-line LCD module. Power comes from a 5V USB supply, as used to charge tablets and mobile phones. click, transfer it to the Micromite for testing. Because it runs on a PC, MMEDIT is very easy to use, with colour-coded text, mouse-based cut and paste and many more useful features such as bookmarks and automatic indenting. And because the program is running on your PC, you can save and load your programs to and from the computer’s hard disk. Fig.5 shows MMEDIT in action. The most important feature is the righthand button on the tool bar (the icon of a running man). When you click on this button, the program will be immediately transferred to your Micromite using the XModem protocol. Following the transfer, a window will automatically open and connect to the Maximite’s console where you can run and test your program. If there’s an error or it needs tweaking, it’s very easy to go back to the editor, make the changes and transfer it to the Micromite again. MMEDIT can be downloaded from Jim’s website at: http://www.c-com. com.au/MMedit.htm. It’s free although Jim would appreciate a small donation. GPS-controlled digital clock We needed a small project to demonstrate the potential of the Micromite and we decided that a digital clock which used a GPS module for accurate timekeeping was just the ticket. This Recommended Micromite Microcontrollers The following microcontrollers can be used for the Micromite: • • • • PIC32MX150F128B-50I/SP: maximum clock speed = 50MHz; 28-pin DIL package. PIC32MX150F128B-50I/SO: maximum clock speed = 50MHz; 28-pin surface mount SOIC package. PIC32MX150F128B-I/SP: maximum clock speed = 40MHz; 28-pin DIL package. PIC32MX150F128B-I/SO: maximum clock speed = 40MHz; 28-pin surface mount SOIC package. The following microcontrollers will also run the firmware but only 17 I/O pins will be available in MMBasic: • • PIC32MX250F128B-50I/SP: maximum clock speed = 50MHz; 28-pin DIL package. PIC32MX250F128B-I/SP: maximum clock speed = 40MHz; 28-pin DIL package. 34  Silicon Chip GPS-Controlled Digital Clock uses just 10 components (including the connectors) so there’s not much to it. Fig.6 shows the circuit details. Most of the work is done in the Micromite which decodes the output from the GPS module, calculates the time zone offset and daylight saving and then drives an LCD which displays the date and time. Considering just how few components are used, the result is impressive. The clock runs from a cheap 5V USB power supply and displays the time accurate to within a second. It never needs setting and it automatically compensates for daylight saving – just the thing for your office desk! The important point to remember is that the program is not encrypted in a hex file that you cannot change. Instead, it’s an easy to read BASIC program that you can modify to suit your requirements. For example, you might want to change how the time is displayed and this can be done with just a few keystrokes. Alternatively, you might want to display the speed and heading from the GPS module instead of the time. Again, that’s easily done with a few keystrokes. You could also extend the program to measure the room temperature and display that along with the time, all using the BASIC programming language. siliconchip.com.au This is what our demonstration GPS-Controlled Digital Clock looks like. Because the program is written in BASIC you can easily modify how the time is displayed. The rear view at right shows just how simple it is. The Micromite and a GPS module do most of the work and there are just seven other parts plus the LCD module, all mounted on a small piece of stripboard. Parts List: GPS Digital Clock 1 Micromite microcontroller (available from the SILICON CHIP Online Shop – see text) 1 2-line x 16-character LCD module (eg, Altronics Cat. Z7001 or Jaycar Cat. QP5512) 1 GPS module (eg, EM408) 1 3.3V fixed voltage regulator (MCP1700-330, LP2950CZ-3.3, etc) 1 47µF 6V tantalum capacitor. 2 10µF 6.3V electrolytic or tantalum capacitors. 1 10kΩ trimpot Miscellaneous USB cable, stripboard, spacers, machine screws & nuts There are many different LCD mod­ ules that you can use (with different pin-outs). For this reason, we didn’t design a PCB but instead built the prototype on a piece of perforated strip board which we piggybacked on the back of the LCD module (see above photo). The 5V DC power supply uses a USB charger/supply of the type used with mobile phones, book readers, etc. These are so cheap and plentiful these days that it’s not worth designing a dedicated unit. The supply is connected to the clock using a surplus USB cable. It’s just a matter of cutting off the unwanted connector and soldering the wires direct to siliconchip.com.au This photo shows just some of the devices that the Micromite has inbuilt support for and so are easy to add to your Micromite-based project. Shown is an infrared remote control, ultrasonic distance measuring sensors, a 2-line LCD display, a battery-backed real time clock (RTC) and a servo motor. Other devices not shown include temperature sensors, infrared transmitters and 4x4 keypads. The full details will be in part 2 next month. the board. The red wire is +5V and the ground is black, although you should check this first with a multimeter just to make sure. The other two wires are the signal leads and they can be cut short since they aren’t needed. The 5V supply is used to directly power the LCD module but the GPS May 2014  35 $GPGSV,3,1,12,11,75,324,36,01,59,146,27,32,58,161,34,20,56,209,30*75 $GPGSV,3,2,12,23,52,301,40,25,42,101,,13,23,311,23,17,19,237,23*72 $GPGSV,3,3,12,31,12,136,,19,08,358,13,14,06,136,,27,05,350,*72 $GPRMC,043359.000,A,3158.7597,S,11451.8693,E,0.29,58.06,101008,,*2B $GPGGA,043400.000,3158.7598,S,11451.8693,E,1,05,3.4,25.0,M,-29.3,M,,0000*58 $GPGSA,A,3,23,20,13,11,32,,,,,,,,4.7,3.4,3.4*31 $GPRMC,043400.000,A,3158.7598,S,11451.8693,E,0.20,72.85,101008,,*25 Fig.7: this is a typical data stream from an EM-408 GPS module captured over one second. Each message is on a separate line and consists of the message type at the start followed by a number of data fields separated by commas. We are interested in the line beginning with $GPRMC as this contains the current date and time. SUB GetGPSRecord DO DO WHILE INPUT$(1, #1) <> "$" : LOOP FOR i = 0 TO 20 arg$(i) = "" DO x$ = INPUT$(1, #1) IF x$ = "," THEN EXIT DO IF x$ = "*" THEN EXIT SUB arg$(i) = arg$(i) + x$ LOOP NEXT i LOOP END SUB ' wait for the start ' clear ready for the data ' loop until a specific exit ' get the character ' new data item, new field ' end of record, so return with it ' add to the data ' keep going ' increment the field Fig.8: getting data from the GPS involves loops within loops. The result is that the global array arg$() is loaded with the contents of the GPS message – one field to each element of the array. module and Micromite require 3.3V so we obtained this from a simple 3-terminal regulator. The LCD is wired with its read/ write (R/W) line held low so that the module is always ready to receive data (we never read data from it). It’s also connected in 4-bit mode so all data must be sent as 4-bit “nibbles”. This detail is handled by MMBasic which has an inbuilt command to directly drive this type of display. GPS output Before we describe how the program works, we should quickly explain how a GPS module outputs its data. Normally, the data is transmitted as a serial stream of characters at 4800 bits per second (bps). Some modules use 9600bps or even 19,200bps but they can be easily accommodated by editing the BASIC program. The format conforms to the NMEA 0183 standard and an example data stream is shown in Fig.7. Basically, the data is formatted into a series of 1-line messages. Each message starts with a dollar symbol ($) and is terminated by an asterisk (*) followed by two hex digits that are a checksum. 36  Silicon Chip The individual fields within a message are separated by commas. The first field is the type of message and for our clock project we need the RMC message which starts with the code word GPRMC. The RMC message is standard for GPS modules and they all generate it. Other messages produced by a GPS module provide a variety of useful information including latitude, longitude, altitude and the number of satellites that the GPS module is listening to. Within the RMC message we are particularly interested in the date (second field) and the time (tenth field). The third field is also important; it indicates if the module has a lock on the satellites and has an accurate time. Capturing GPS data Interfacing to the GPS module is done using the serial interface. The command OPEN “COM1:4800” AS #1 will set that up for us. We then need to capture each message and split it into the individual fields. This is done by the function GetGPSRecord() as shown in Fig.8. Before this function is called, a string array has to be created with 20 elements in it. This array is called arg$() and the GetGPSRecord() function will fill it with the data fields of the message (one field to each element of the array). We won’t go through the detailed operation of this function but instead leave it as an exercise for the reader. However, the important section is the seventh line which gets a character from the GPS. Subsequent lines in the program examine this character to determine if it is the end of a field (a comma) or the end of the message (an asterisk), or if it is just part of a field. Once this function has captured a complete message it returns to the caller (ie, the part of the software that triggered this function) which then checks the first field to determine if it is the required message type (more on that later). Adjusting the time/date The GPS unit provides the date and time as individual numbers (month, day, hours, etc). Once we have these numbers, we need to adjust them to take into account the local time zone and daylight saving (GPS data is always transmitted as UTC time). Your first instinct might be to do this by adding the time offset to the hours field, then checking if it has overflowed and then adjusting the day of the month accordingly. However, this quickly gets complicated because you might have to adjust the month or even the year while taking into account leap years. And then there’s the possibility of having to account for a negative time zone (ie, west of Greenwich). Because the Micromite runs a powerful BASIC interpreter which can handle large numbers we simply convert the date/time into minutes since midnight on the 1st January 2014. As the years go by, this can become a very large number but MMBasic can cope with large numbers. In fact, it will be able handle the time in this format up to the year 2045 when it will be over 15 billion minutes. With this conversion, it’s then easy to add or subtract the time zone and make comparisons to see if the resulting time is subject to daylight saving adjustment. The conversion itself is done by the function GetMins() which is shown in Fig.9. This function is supplied with siliconchip.com.au the year, month, day and time and returns with the number of minutes since the start of 2014. We also need to know when daylight saving starts and ends and this is done by the function GetDST(), as shown in Fig.10. It is fed the current year, along with the month and hour that DST starts and ends. It then figures out what day the first Sunday of the month falls on. It then returns the number of minutes from the start of 2014 that daylight will start or stop. For example, this year, daylight sav­ ing in Australia will start at exactly 400,440 minutes since the start of 2014. All we need then do is compare the current number of minutes with this number and if it is greater we need to add one hour for daylight saving (unless, of course, daylight saving has already ended). Note that you’ll need to set a flag (UseDST) in the software to indicate whether daylight saving is relevant to your area. Note also that the daylight saving calculations are correct for Australia and overseas readers will need to adjust the calculations to suit their local daylight saving rules. Converting to time/date Once we have the number of minutes (ie, representing the current time), we need to convert it back to the date and time format that we are familiar with (eg, 5th May 2014). This is done with the GetDate$() and GetTime$() functions. Fig.11 shows the GetTime$() function which extracts the individual elements (hours, minutes and seconds) and then converts them to strings. By using the powerful string handling features of MMBasic, these are then joined together to make one complete string. The GetDate$() function is similar in operation to GetTime$() and so is not shown here. Putting it together Now all we need to do is put all these functions together to make our main program which is shown in Fig.12. This starts by calling our function to get the next message from the GPS module. We want the RMC message which contains the time so we keep looping until we have that. Inside the RMC message, we look at the third field which will contain the letter “A” if the module has locked onto sufficient satellites to get an accusiliconchip.com.au FUNCTION GetMins(yr, mth, day, hr, min) GetMins = (yr - 14) * (365 * 24 * 60) + ((yr - 13) \ 4) * (24 * 60) GetMins = GetMins + (md(mth) * (24 * 60)) GetMins = GetMins + ((day - 1) * (24 * 60)) GetMins = GetMins + (hr * 60) GetMins = GetMins + min IF (yr - 16) MOD 4 = 0 AND mth > 2 THEN GetMins = GetMins + (24 * 60) END FUNCTION Fig.9: this is how we convert date/time into a simple number, ie, the number of minutes since 1st January 2014. With this single number, it’s much easier to change the time zone and detect when daylight saving begins and ends. FUNCTION GetDST(yr, mth, hr) LOCAL d, m m = GetMins(yr, mth, 1, hr, 0) ' minutes to the 1st day of the month d = ((m \ (24 * 60)) + 3) MOD 7 ' day of the week that this falls on GetDST = m + (((7 – d) MOD 7) * 24 * 60) ' minutes to the first Sunday END FUNCTION Fig.10: this function calculates when daylight saving (DST) will start or end. It takes the current year and the month and hour when DST starts/ends and returns the number of minutes to the first Sunday in that month. It’s then easy to compare this number to the current date/time (in minutes) to determine if daylight saving has started or ended. FUNCTION GetTime$(minutes) LOCAL hr, min, am$ am$ = "AM" hr = (minutes \ 60) MOD 24 IF hr > 12 THEN am$ = "PM" : hr = hr - 12 IF hr = 0 THEN hr = 12 min = minutes MOD 60 GetTime$ = STR$(hr) + ":" GetTime$ = GetTime$ + RIGHT$("0" + STR$(min), 2) + ":" GetTime$ = GetTime$ + RIGHT$("0" + STR$(sec), 2) + " " + am$ END FUNCTION Fig.11: this is how we convert the minutes value back into time, displayed as hours, minutes and seconds in 12-hour format. The function returns the result as a string of text characters which can be sent to the LCD for display. The GetDate$() function (not shown) does the same thing for the date. rate time. If not, we display a message on the LCD and jump back to find the next message from the GPS. Once we have an RMC message with an accurate time we can extract the year, month, etc as individual numbers. The GPS module provides this as a string (ie, a series of ASCII characters), so we convert them to numbers using the MMBasic VAL() function. It’s then quite simple to convert the data/time to minutes, add/subtract the time zone and adjust for daylight saving. Finally, this time is converted back to a string of characters and displayed on the LCD. The LCD command is one of a series of powerful commands built into MMBasic for communicating with special hardware devices. In this case, the LCD command only needs to know the line on the LCD module to display the data, the length of the line (the C16 symbol means 16 characters) and the text to display. The LCD command then does all the work required to transfer the text, centre it on the specified line and display it on the LCD module. It cannot get much easier than that. This section of the program is contained within a loop which repeats forever. Every second, it will get an updated time from the GPS, convert the time and display it – forever looping. Note that these code fragments don’t show you the whole program but they do show how easy it is to write a program in MMBasic. If you want to build the GPS clock or examine the May 2014  37 Main Features Of The Micromite (1) The Micromite is a fast 32-bit CPU with 128K of flash memory and 32K RAM running a powerful BASIC interpreter. 20KB of non-volatile flash memory is reserved for the program, while 22KB of RAM is available for BASIC variables, arrays, buffers, etc. This is sufficient for quite large BASIC programs up to 1000 lines or more. (2) A full-featured BASIC interpreter with floating point and string variables, long variable names, arrays of floats or strings with multiple dimensions, extensive string handling and user defined subroutines and functions. Typically it will execute a program at 21,000 lines per second. (3) Nineteen input/output pins are available on a 28-pin chip. These can be independently configured as digital inputs or outputs, as analog inputs or configured for frequency or period measurement and counting. Ten of the pins can be used to measure voltages and another seven can be used to interface with DO KeepSearching: DO GetGPSRecord LOOP UNTIL arg$(0) = "GPRMC" IF arg$(2) <> "A" THEN LCD 1, C16, "Searching" LCD 2, C16, "For Satellites" GOTO KeepSearching ENDIF 5V systems. MMBasic can also be installed on a 44-pin version of the chip, providing 33 input/output pins. (4) Programming and control via a serial console (TTL voltage levels) at 38,400 baud (configurable). Once the program has been written and debugged, the Micromite can be instructed to automatically run the program on power up with no user intervention. Special software is not needed to develop programs. (5) Inbuilt full-screen program editor. This only requires a VT100 terminal emulator and can edit a full 20KB program in one session. It includes advanced features such as search and copy, as well as cut and paste to and from a clipboard. (6) Easy transfer of programs from another computer (Windows, Mac or Linux) using the XModem protocol or by streaming the program over the serial console input. (7) Input/output functions in MMBasic will generate pulses (both positive and negative ' get a GPS record ' we only want the RMC record ' "A" means valid record ' go back and keep looking ' extract the elements of the date/time from the GPS record year = VAL(RIGHT$(arg$(9), 2)) ' extract the date month = VAL(MID$(arg$(9), 3, 2)) day = VAL(LEFT$(arg$(9), 2)) hour = VAL(LEFT$(arg$(1), 2)) ' extract the time min = VAL(MID$(arg$(1), 3, 2)) sec = VAL(MID$(arg$(1), 5, 2)) ' convert the time to minutes and add/subtract the time zone and daylight saving mins = GetMins(year, month, day, hour, min) mins = mins + TimeZone * 60 ' adjust for the timezone IF UseDST THEN ' if we observe daylight saving IF mins < GetDST(year, 4, 2) OR mins > GetDST(year, 10, 2) THEN mins = mins + 60 ' adjust for AWST DST ENDIF ENDIF ' convert the minutes back into the current date/time and display it LCD 1, C16, GetDate$(mins) LCD 2, C16, GetTime$(mins) LOOP Fig.12: putting it all together. First, the correct GPS message is found, then the date/time is extracted as numbers representing the year, month, etc. These are converted to a minutes number which is then adjusted for the time zone and daylight saving. Finally, this time is converted back to text and displayed on the LCD. This loop repeats every second and never stops. 38  Silicon Chip going) that will run in the background while the program is running. Other functions include timing (with 1ms resolution), BASIC interrupts generated on any change on an input pin and an internal real time clock. (8) Comprehensive range of communications protocols implemented including I2C, asynchronous serial, RS-232, IEEE 485, SPI and 1-Wire. These can be used to communicate with various sensors (temperature, humidity, acceleration, etc) as well as for sending data to test equipment. (10) Built in commands to directly interface with infrared remote controls, the DS18B20 temperature sensor, LCD display modules, battery-backed clocks, ultrasonic distance sensors and numeric keypads. (11) Up to five PWM or SERVO outputs can be used to create various sounds, control servos or generate computer controlled voltages for driving equipment that uses an analog input (eg, motor controllers). (12) Special embedded controller features in MMBasic allow the clock speed to be varied to balance power consumption and speed. The CPU can also be put to sleep with a standby current of just 80µA. During sleep, the program state and all variables are preserved. (13) A watchdog feature monitors the running program and can be used to restart the processor if the program fails with an error or is stuck in a loop. (14) The running program can be protected by a PIN number. This will prevent an intruder from listing or modifying the program or changing any features of MMBasic. complete program, you can download it from the SILICON CHIP website (free for subscribers). At the risk of labouring a point that we made earlier, it’s easy to change these functions to display different data or change the format. All you need to do is connect an ASCII terminal and edit the program – then give it a run to see if it worked. That’s the strength of the Micromite; it’s incredibly easy to program. Next Month Next month, we will go into more detail on programming the Micromite. We’ll also show you how to control it via an infrared remote control, how to measure temperature and much, much more. Finally, for helpful tips and support check out the author’s web page at http://geoffg.net/micromite.html SC siliconchip.com.au