A A A

Electronicfr.com

Home arrow Microcontrollers and USB arrow Part 1 : How to build a USB device with a PIC 18F4550 or 18F 24.07.2008

Main Menu

Home
Robotic
Microcontrollers and Ethernet
Microcontrollers and USB
Embedded computing
Submit a circuit

Search

Google
Web
electronicfr


Part 1 : How to build a USB device with a PIC 18F4550 or 18F

Written by Administrator, on 27-10-2007 13:30
Views 58259    

PIC 18F4550 and 18F2550 are powerful microcontrollers including a full-speed USB V2.0 compliant interface. With these MCU it's very easy for the hobbyist to design USB devices with very few components.

In these pages, I'm describing how to use the CDC firmware from Microchip. It permits to emulate a serial port with a PC running Windows or Linux. It's also very easy to build HID devices.


The 18F4550 experimentation board

Here is a picture of the experimentation board:

The following schematic is very simple, only few components are needed. It's a self powered USB device.
Please note I'm using the Olimex tiny-ICSD programmer, if you want to use another PIC ICSP programmer, you should remove D1 and D2 and change the connector

usb_schema

(You can click on the picture to get an high resolution picture)

This device is compatible with the microchip USB bootloader

Please note C5 is 470nF (the datasheet recommend 220nF but more is better) and should be low ESR (for example a multi-layer ceramic). Don't forget the 100nF decoupling caps.

Here is a possible implementation

pcb

Click on the picture to get a 300dpi PCB

You can look at the wire's colour of your USB cable: Black=GND, Red=+5V, Green=Data+, White=Data-

4/1/2007 : IMPORTANT ERRATA - There's a little mistake in this PCB. R5 must be connected to +5V, not to the ground. Do not connect R5 to GND, but to +5V or it will not works at all. (thanks to Direk who pointed to this problem)

The software

You can download MCHPFSUSB_Setup  on the Microchip website. It's a windows .exe :(. The firmware we will use is written in C (for the C18 Microchip compiler). This code is (royaltyes) free, but it's not a free software, so it's not possible to redistribute modified source code

 

    In this archive, you will find:
  • The USB bootloader and its source code in C18
  • A windows utility for the bootloader
  • The CDC firmware source code (serial emulation) in C18. It can be used with the USB bootloader without modification
  • Another firmware for the HID device
  • A windows driver for CDC (under linux CDC-ACM is natively supported)

 

Some tips for your prototypes:

 

The source code is in the fw/cdc/MCHPUSB.mcw directory

You should comment the following lines in usbcfg.h for a self powered usb device:

#define USE_SELF_POWER_SENSE_IO
#define USE_USB_BUS_SENSE_IO

 

On my schematic, LED's are connected on port B. So you should modify io_cfg.h

 

#define mLED_1              LATBbits.LATB0
#define mLED_2              LATBbits.LATB1
#define mLED_3              LATBbits.LATB2
#define mLED_4              LATBbits.LATB3

You can put your own code on user.c. For example, look at Exercise_Example() in user.c.

Useful functions are getsUSBUSART() (get data) and putrsUSBUSART() (put data other the serial port)

Don't forget to check mUSBUSARTIsTxTrfReady() before to send data.

They are some examples in the exercise directory. For example, the following program (to put in user.c) toggle the LED number 4 when you press the key "1" in the terminal

They are some examples in the exercice directory. For example, the following program (to put in user.c) toggle the LED number 4 when you press the key "1" in the terminal

void Exercise_03(void){
if(getsUSBUSART(input_buffer,1))
{
if(input_buffer[0] == '1')
mLED_4_Toggle(); 
}

Under windows, you must install the provided driver and you can use the hyperterminal utility (19200bd with a 20MHz crystal). Under linux, check the cdc-acm module, a new device /dev/ttyACM0 should appear.


If you don't want to use the bootloader, you must remove the vector remapping section in main.c and change the linker script (look at important.readme.txt in the sources directory)

Next part -> Application: How to build a USB thermometer

Quote this article in website Print Related articles Save this to del.icio.us

Users' Comments (9) RSS feed comment
Comment language: English (9), French (3)
Posted by Vijay, on 22-12-2007 16:00, IP 91.189.0.206
1. Inquiry about PIC 18F4550 programmer
i am doing a project on USB interfacing. i want to send the the data to USB port to control various electronic device interfaced with PIC 18F4550.PC(Host) will have some coding in C/C++ and will send the data to USB port.i want USB driver for The IC18F4550.i have downloaded the MCHPFSUSB.exe.but i don't know how to use it to write the program in turboC/C++. 
I want programmer circuit and software for PIC18F4550.in your project you have used the Olimex tiny-ICSD programmer..can you tell me from where will it be available?
 
» Report this comment to administrator
» Reply to this comment...
» See all 2 replie(s)

Posted by direk, on 30-12-2007 12:21, IP 83.13.192.34
2. Error on PCB?
On schematic R5 is connected between +5V and RB4. Why on PCB it's connected to GND instead of +5V? Am I wrong, or there's a little mistake? :-)
 
» Report this comment to administrator
» Reply to this comment...
» See all 1 replie(s)

Posted by m.ashrafpur, on 09-02-2008 16:19, IP 82.99.254.4
3. question
hi.thanks for your useful web page. 
2 question: 
1-in which compiler do u compile your code? 
2-how I can program the 18f2550/4555... usp version pic's 
request: 
ur labview program is very simple and very useful.may u create a labview programing training part for usb communication in ur site? 
in datasheet of pic18f2455/.. family in first page note that "...programing (ICSP)via two point". is it same as 16f877A in circuit program able or is it support bootloader programer that use serial port? 
 
thanks a lot for ur reply
 
» Report this comment to administrator
» Reply to this comment...
» See all 1 replie(s)

Posted by stefan, on 26-02-2008 15:02, IP 194.138.39.56
4. access to transmitted data
Hi, 
I would like to use the PIC18F4550 USB Demo Board to read out an SPI-Interface and send these data via USB to the PC. The compiler I use is the C18 compiler. I belief, that I am able to implement the SPI interface at the PIC. But I don't know which .h and .c files from the firmware are required. Do you have some examples? 
The second question I have is how can I visualize the transmitted data on the PC? The software I am going to use is Visual Basic 2008 (Freeware). Have you some basic instructions?
 
» Report this comment to administrator
» Reply to this comment...
» See all 1 replie(s)

Posted by Amilion, on 14-04-2008 03:33, IP 70.53.195.22
5. CDC program and Timer on 18F4550
I have built a demo board using a 18F4550. I modified the cdc example in order to send data to the PC using the USB interface. I want to time stamp the messages, to do this i use Timer0 as a real time clock. The timer works in the simulation mode, but does not work on the real board. Does anyone have a clue to solve this problem ?
 
» Report this comment to administrator
» Reply to this comment...

More comments...

Add your comment



mXcomment 1.0.5 © 2007-2008 - visualclinic.fr
License Creative Commons - Some rights reserved
 
Next >
 
TOP