Thursday, September 29, 2011

UBW32 Generic USB demo

I ported and cleaned up the generic USB demo from the Microchip application libraries. The project is designed to work with the UBW32.

  • UBW32 or any other pic32 microcontroller board.
  • PICKIT/ICD/REALICE to program your board, or a linker script to load with the USB bootloader.

Download the project and compile/flash the firmware onto your chip. Open the USB/Device - LibUSB - Generic Driver Demo host program for your operating system. Attach your device to the host and connect.

PIC32 Timer1 example code

The following code snippet opens and uses Timer1 as an interrupt. In your initialization sequence:
//Open Timer1 with 1:8 prescaler (80MHz -> 10MHz), with period of 10, therefore tick = 1MHz.
OpenTimer1(T1_ON | T1_PS_1_8 | T1_SOURCE_INT, 10);
ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
And the interrupt handler:
void __ISR(_TIMER_1_VECTOR, ipl2) _Timer1Handler(void) {
	//Your code

PIC32 pin change interrupt

The following is a quick snippet for interrupt on change pin
void __ISR(_CHANGE_NOTICE_VECTOR, ipl2) ChangeNotice_Handler(void) {            
	mPORTDRead(); //Need to read the port (see PIC32 datasheet)
	//Your code...

And, in your initializing code:

mCNOpen(CN_ON | CN_IDLE_CON, CN13_ENABLE | CN14_ENABLE | CN15_ENABLE, CN_PULLUP_DISABLE_ALL); //See plib documentation and PIC32 datasheet

Monday, September 26, 2011

Crimping a modular connector without a cimp tool

So it turns out that the guy at the local electronics store near the university is too damn cheap to lend me a crimp tool for literally 10 seconds, and would rather charge me $5 to merely crimp a header onto the end of a cable. So I decided to crimp the modular header (for ICD) onto a the cable myself. I decided to instead use a flat ribbon cable (like the computer IDE cable) since they're thinner and much more flexible than the 6 wire telephone cable I bought. The wire-to-wire pitch is slightly larger than for a normal telephone type (6p6c) cable so you can't insert it directly, but if you pull the individual wires apart to about 5mm from the end, and then put them in, it works out well. Crimping without a tool was a snap - took only about a minute with nothing more than a flat head screwdriver to push in the tab as well as each of the contacts.

Friday, September 23, 2011

Simple USB Hello World for PIC32 (UBW32)

Here's a simple USB Hello World program that I wrote/modified/set up for the PIC32 (specifically UBW32 board, but any PIC32 will run it). It's a very quick, minimal, bare bones project, with minimal comments and clutter, formatted nicely.

Download project



1. Open the project in MPLAB (I'm using 8.76 with C32 v2.01).
2. Go to Build Options (green button on toolbar)
3. Go to Directories -> Include Search Path and edit the MAL line to your Microchip Applications Library Include folder. Go to Library Search Path and edit the line to your C32 libraries folder.
4. If you are NOT using the UBW32 board, edit HardwareProfile.h for your board (LEDs, etc). Also make sure to select your specific chip from Configure -> Select device...
5. Compile and program your board with a PICKIT/ICD/REAL ICE. This package does NOT support the bootloader because I use a REAL ICE to program my board, although you could probably very easily load it from the UBW32 bootloader by compiling it with the procdefs.ld file from the HelloWorldUSB package from the UBW32 website.
6. When the board is running the program, open a terminal program on the host computer (for example, Termite on Windows, or the "screen" command line tool on Linux/Mac OS X). Type something. You should see text sent back on the terminal.

Friday, September 16, 2011

Constant current source/sink

A very accurate MOSFET current source/sink I designed.

Number of MOSFETs is variable. I designed this for load testing, so the circuit is using 4 MOSFETs to be able to dissipate more power. As can be seen on the bottom graph (green), it works well down to very low voltages due to the use of MOSFETs instead of BJTs.