Microchip USB: Part 1
Here's the simple test program:
#include <p18f2550.h>
#pragma config FOSC = HSPLL_HS
#pragma config PLLDIV = 5
#pragma config USBDIV = 2
#pragma config CPUDIV = OSC1_PLL2
#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config DEBUG = ON
#include <delays.h>
void InitApp(void)
{
/* TODO Initialize User Ports/Peripherals/Project here */
TRISA=0xFB; //LED output
ADCON1=0xD; // two analog channels
CMCON=0xF;
ADCON2=0x86; // 64 cycles for acq time, lright justify
ADCON0=0; // start with channel 0
}
// use the built-in 100 cycle delay to get 1mS delays
void dly(unsigned int duration)
{
// the way the clock is set up the frequency should be 48MHz
// and 4 clock cycles per instruction
// so 12Mhz effective or about 83nS per tick
// therefore 12000 ticks should be 1ms
while (duration--) Delay100TCYx(120);
}
int read_analog()
{
int result;
ADCON0|=1; // turn on A/D
ADCON0|=2;
while (ADCON0 & (1<<1));
result=ADRESH;
result<<=8;
result|=ADRESL;
return result;
}
void main(void)
{
int v;
/* Initialize I/O and Peripherals for application */
InitApp();
while (1)
{
LATA=0xff;
dly(500);
while ((PORTA & 8)==0);
LATA=0;
v=read_analog();
dly(500);
}
}
If you get this far, you should see a blinking LED; it will stop blinking when you push the attached push button. Not exciting, I agree, but it does show you have the build environment and the hardware ready.
That catches you up with me. The next step (maybe next week or maybe the week after) is to load the USB libraries and see how far I can get towards making a working HID device.
Actually, you don't actually need support for a USB stack from the vendor, but it sure helps. If you want to get an idea of what it takes to roll your own, have a look at V-USB, an open source USB port for the Atmel AVR.
Another controller I thought looked promising was the NXP LPC11U1X family. I even bought the LPCXpresso board, and will probably eventually experiment with it and share what I find here. The attractive part is it has USB firmware built into the chip. The documentation, however, is a little daunting, so I need to set aside a block of time to play with it properly.

