Real time microcontroller applications, such as digital power supplies, motor controllers, and others that utilize one or several real time digital control loops generally can’t be efficiently debugged using LEDs, single-stepping, setting breakpoints, and other traditional programming techniques. In the process of developing a real time system it is often necessary to change a variable on the fly while observing changes in some other variable, also in real time. Microchip MPLAB X and MPLAB IDE have a tool called DMCI (Data Monitor and Control Interface) allowing to do what the name suggests when used in combination with dsPIC and PIC24 micros. I am a big fan of new “enhanced mid range” PIC16 micros and I always wanted to have similar tool to use with these chips. Some time ago I started porting dsPIC RTDM (Real Time Data Monitor, used to communicate with DMCI) protocol implementation to PIC16 and this short article is the announcement of the first working implementation of the protocol.
The title video (expand it to full-screen and switch the resolution to 1080 to see numbers better) demonstrates PIC16F1509 MCU reading its internal fixed voltage reference (FVR). A plot on the right shows, in near real time, last 128 ADC conversions collected into a buffer. A slider on the left allows me to modify sampling delay of the ADC and you can see the difference in conversion when I change this delay from 50 to 20 microseconds. A FVR is high-impedance source and the effect of sampling too fast can be clearly seen. The output is free running, similar to free running oscilloscope timebase; with little extra effort, reads and writes can be synchronized, if desired – see Microchip code examples CE155, CE455 for details.
It should be noted that PIC16 is a very small micro and no amount of clever coding will change that. The RTDM overhead will likely constrain the use of RTDM to test cases rather than real projects. Still, ability to change and read MCU memory on the fly is very useful while testing implementation of digital filters or PID loops.
The C code used to produce the demo along with DMCI config file is available on GitHub. It will run on PIC16F1509 with external 16MHz crystal, other necessary connections being a decoupling capacitor on VCC and a serial connection to a PC running MPLAB X. All software versions are current at the time of writing.
The detailed documentation of RTDM implementation will be posted in several weeks (or so I hope). To increase speed and reduce code size I will be replacing chunks of C code with assembly so if you don’t like assembly, use github commits made earlier than the timestamp of this article. In the mean time, much can be learned from the code itself and developers already familiar with DMCI for dsPIC will have no difficulties using the code as-is.
Please try this code and let me know how it worked for you. Comments can be left in a my Google Plus community or in the “Issues” section of the GitHub repo. You can also use the comment section of the article or by contacting me using one of the e-mails from the “About” section of the site.
G+ Community Screenshot
The comment system on this site leaves much to be desired. Formatting is non-existent, attachments are not supported, and most importantly, spam-reducing features are very basic leaving the bulk of spam processing to yours truly. At the same time, about 75% of human commenters have @gmail.com address and therefore already have access to Google Plus. Given all that, creating a Google Plus community
seems to be the right way to go.
At present, the community have no structure and there are almost no rules. All G+ functionality is present, including links and attachments. Self-promotion is allowed but spam is not. Pictures of your own (in)complete projects are much welcome.
Even though the built-in comments will stay in place for some time I encourage everybody to migrate to G+. This will allow better support, discussion, as well as idea exchange. I’m also hoping the spam will go down giving me more time to develop and share useful stuff.
Thank you for your attention!
Bitmine A1 chip soldered in place
Several weeks ago, a friend e-mailed me asking for help building a bitcoin miner based on Bitmine A1 ASIC – a mighty chip capable of 40GH/sec and also very DIY friendly. Last Sunday my friend showed up in the morning carrying a box of parts and in the evening we had semi-functioning board and zero casualties. In this article I’m writing my notes hoping that other builders following the same path may find them useful. As soon as we get working board I’ll build another one and post the real build log.
Note 1: The board we were building is a reference design by Bitmine.ch, a company that designed A1 ASIC. The reference board documentation is inconsistent; the rev.1.0.A schematic is different from rev.1.0.B Gerbers. Several part designators won’t match the PCB silkscreen, and the 500 ohm R12 resistor, likely added to improve stability of 2-phase buck converter, was not present on the schematic and/or BOM; we finally managed to figure out what it is by studying the board’s Pick-and-Place job file.
Note 2: Gerber file of a paste layer (the one used to cut a stencil) has openings for the thermal pads and power rails that are too large, using them as-is caused too much paste to be dispensed. Ohararp, the stencil shop, suggested shrinking some of the openings in half. This has helped, especially for pads with many thermal vias but the amount of solder on A1′s power bars was still excessive – take a look at the title picture, left side of the A1. On the subsequent builds about 3/4 of the paste from the power bars would have to be removed manually.
Continue reading Bitmine A1 reference board build notes.
The PS4 controller is now also supported via Bluetooth. It uses the same API as the other libraries I have written, so if you have used them before, then you should be quite familiar with it.
The example code is available at Github: https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/Bluetooth/PS4BT/PS4BT.ino.
For more information take a look at my blog post: http://blog.tkjelectronics.dk/2014/01/ps4-controller-now-supported-by-the-usb-host-library/. This explains how to pair with the controller and have some background information as well.
You should also check out the readme which will always have the newest information available.
That is all for now. Hopefully this will be useful to anybody out there that wants to use the PS4 controller with the library.
Update 18. January 2014
A USB version of the library is now also available.
I am glad to announce that Bluetooth HID devices are now supported by the USB Host library. The library already supports PS3 and Wiimote controllers, but now you are also able to use Bluetooth mice and keyboards with the library.
An example is available at the following link: https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/Bluetooth/BTHID/BTHID.ino.
I have personally tested it with a PS3 keyboard (see image), an Apple Wireless Keyboard and an old Bluetooh mouse from Microsoft and all of them works fine.
For more specific instructions on how to use the library I recommend taking a look at the blog post at my own blog: http://blog.tkjelectronics.dk/2013/12/bluetooth-hid-devices-now-supported-by-the-usb-host-library/.
Feel free to post a comment below if you got questions or got problem with a specific device and I will answer as fast as possible.
E strobe train
Traditionally, Toshiba HD44780-compatible alphanumeric LCD displays are driven by bit-banging bus signals combined with long delays between sending commands and data. In many cases this method is good enough. There are other cases as well where extra CPU cycles are not available and more economical method of driving a display is needed. I’m currently working on a design involving very fast USB exchanges combined with occasional LCD output and developed a solution which works very well for me. I’m posting it with hope that my fellow developers will find it useful.
HD44780 displays have been around for a long time. The internet provides plenty of posts about them, code samples and even a Wikipedia article. My favorite introductory text on the topic is Dincer Aydin’s LCD Info page.
PIC24 16-bit microcontrollers from Microchip have been around for some time as well. They are cheap and powerful and the Microchip C30 compiler (free version available) is quite good. They are not as popular as their 8-bit counterparts from Microchip and Atmel therefore good PIC24 resources are scarce. One nice introductory text on the topic can be found at Engscope.
Since I’m trying to minimize CPU time spent driving the LCD let’s first talk about timing in general. When developing for HD44780 we need to deal with 3 different times. First is the timing of the display part – the screen we see. LCD glass is very slow. When we attempt to update the screen faster than say twice a second the symbols become blurry and pale. The fastest display in my collection still looks OK when updated at 4Hz rate (250ms), while most others are twice as slow.
On the other hand, display data bus timing is many times faster. In order to write to the display we first need to set RS, RW and data lines, wait a little, then assert E line, wait some more and then de-assert it. If we are reading from the display we will also need to wait a little more after de-asserting E before we can read the data on the bus. Total bus cycle length is ~2.5us, which is 200 000 times less than the update rate of typical LCD glass. This time is pretty short but the MCU is still faster – a PIC24F clocked at 32MHz has an instruction cycle of 62.5ns and in 2.5us it will be able to execute 40 instructions. Therefore, no matter how simple it looks, it is preferable not to bit-bang the bus.
The third timing we need to deal with is command execution time. All but two LCD commands have stated execution time of 40us. Two slow commands – Clear and Home require 1.64ms to finish. Those are datasheet numbers, in reality the fast command on a modern display may finish in as low as 10us and slow commands on an old display can take as much as 3.5ms, depending on the age and the particular “HD44780-compatible” controller used. It is about 100 times faster than the glass.
Continue reading Driving a character LCD using PIC24 Enhanced Parallel Master Port
Arduino reading digital scale
I am the proud owner of Stamps.com Model 510 5lb digital scale. It is a nice little scale which works very well (much better than Stamps.com service itself) while attached to my workstation. The scale doesn’t have a display making any kind of standalone use difficult. However, since the scale is a USB HID device reading data from it should be as easy as from a joystick and Arduino board should be adequate to provide a display function for it. To test this theory I made a simple setup consisting of Arduino UNO, USB Host shield and HD44780-compatible LCD display. I also wrote a small sketch which polls the scale and outputs the weight. The secondary objective of this project was to demonstrate LCD support in USB Host shield library.
For this project I used the following:
- An Arduino board. Standard size board, such as UNO, Duemilanove or Leonardo, will work
- USB Host Shield
- Toshiba HD44780-compatible LCD display, in 16×1 or 16×2 configuration. If you’re planning to use this sketch for something else, like data logging, the display is optional – all output from the scale is repeated to the serial port
- Stamps.com 5lb digital scale. Scales are standard HID devices with usage table 0x8d, therefore, scales from other brands may work as well with no or minimal modifications to the code
- USB Host library
The example code is also hosted at github, as well as in ‘examples’ section of the library under ‘HID’. It has been tested with Arduino IDE version 1.0.5.
In this project, the LCD is connected to the shield’s GPOUT pins, as documented in max_LCD.h header file. In addition to data lines, 5V and ground must also be connected to the shield’s 5V and GND terminals; the RW pin must be grounded – I do it on the LCD itself. In order to see the characters, the display must be biased – a 5K-10K pot with wiper on Vo and other two pins on 5V and ground will provide contrast adjustment.
Continue reading Adding a display to a digital scale using Arduino and USB Host shield
Power switch populated
About a month ago I started shipping USB host shields built on PCB bearing revision number 2.0.1. On this PCB I added a new feature, suggested by Andrew Kroll – a VBUS power switch. The board comes with power switch unpopulated and if you don’t care about this feature it can simply be ignored. However, if you do care about power control, read on.
The ability to turn VBUS on and off at will can be very beneficial. In battery-powered projects the run time can be significantly increased by powering on USB device only when needed. Some other devices can’t even be initialized reliably without a powercycle. Also, many power switches incorporate current limiting circuitry allowing VBUS overload detection and prevention.
An example of populated power switch is shown in the title picture (click on it to make it larger). A is a power switch IC (in this case, Micrel MIC2004). B is 0.1uF ceramic capacitor in 0603 package. C is a wire from MAX3421E GPX pin to the ENABLE pin of the power switch. Finally, D is VBUS Power jumper which needs to be opened, as pictured. Current revision of USB Host 2.0 library is needed to support power control.
Next picture will be used to explain the details of the power control circuitry.
Arrow A points to the jumper which needs to be cut open
Arrow B shows the position where 0603 0.1uF ceramic capacitor needs to be placed
C and D show the places for the power switches (only one switch is needed). Many switches packaged in SOT23-5 and SOT23-6 use this footprint, use On Semiconductor NCP380 as a reference. Also, some other 5 pin switches, such as Maxim MAX4793 and Micrel MIC20xx, will work while placed on SOT23-6 footprint, as shown on the title picture.
Certain switches, such as 6 pin NCP380, allow for adjustable current limit. The position for current setting 0603 resistor is marked ILIM – for the value of this resistor consult the datasheet for the part you’re planning on using
Many switches provide FAULT pin to signal various fault conditions, like output overload, reverse votage, or over-temperature. The pin is typically active low open drain type. It is broken out to a pad labelled VBUS OVL. The signal can be used in several different ways. A LED with a series resistor can be connected across VBUS OVL and a power rail. Also, it can be connected to a MCU input. In this case, a position labelled 10K should be populated with 0603 resistor, typical value is 10K. The other (upper) end of the resistor is connected to 5V rail with a trace which is placed under the letter K; if 3.3V level signal is desired, cut the trace and solder the upper end of the resistor to the 3.3V rail.
The power control signal is labelled VBUS EN. The library uses GPX pin for
Init() functions. There is also a variant of
Init() function which will hold the VBUS off for the number of milliseconds passed to it as a parameter. See usbhost.h file for details. Also, testusbhostFAT.ino demonstrates usage of powercycling
Continue reading VBUS power control on USB Host shield