Channels ▼

Al Williams

Dr. Dobb's Bloggers

Microchip USB: Part 1

March 19, 2012

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
    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
    while (ADCON0 & (1<<1));
    return result;

void  main(void)
    int v;

    /* Initialize I/O and Peripherals for application */

    while (1)
        while ((PORTA & 8)==0);


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.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.