|
|
View previous topic :: View next topic |
Author |
Message |
de zhao Guest
|
use USB module of PIC 18F4550 |
Posted: Fri Oct 17, 2008 5:23 am |
|
|
I have written code that follows and it not working, can anybody help me? Thanks
Code: | /////////////////////////////////////////////////////////////////////////
//set to 1 to use a PIC's internal USB Peripheral
//set to 0 to use a National USBN960x peripheral
#define __USB_PIC_PERIF__ 1
#if __USB_PIC_PERIF__
#include <18F4550.h>
#fuses HSPLL,PLL2,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,CPUDIV1,VREGEN
#use delay(clock=48000000)
#endif //endif check to see which peripheral to use
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
/////////////////////////////////////////////////////////////////////////////
//
// CCS Library dynamic defines. For dynamic configuration of the CCS Library
// for your application several defines need to be made. See the comments
// at usb.h for more information
//
/////////////////////////////////////////////////////////////////////////////
//Tells the CCS PIC USB firmware to include HID handling code.
#DEFINE USB_HID_DEVICE TRUE
//the following defines needed for the CCS USB PIC driver to enable the TX endpoint 1
// and allocate buffer space on the peripheral
#define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 for IN bulk/interrupt transfers
#define USB_EP1_TX_SIZE 8 //allocate 8 bytes in the hardware for transmission
//the following defines needed for the CCS USB PIC driver to enable the RX endpoint 1
// and allocate buffer space on the peripheral
#define USB_EP1_RX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 for OUT bulk/interrupt transfers
#define USB_EP1_RX_SIZE 8 //allocate 8 bytes in the hardware for reception
/////////////////////////////////////////////////////////////////////////////
//
// If you are using a USB connection sense pin, define it here. If you are
// not using connection sense, comment out this line. Without connection
// sense you will not know if the device gets disconnected.
// (connection sense should look like this:
// 100k
// VBUS-----+----/\/\/\/\/\----- (I/O PIN ON PIC)
// |
// +----/\/\/\/\/\-----GND
// 100k
// (where VBUS is pin1 of the USB connector)
//
/////////////////////////////////////////////////////////////////////////////
///only the 18F4550 development kit has this pin
#define USB_CON_SENSE_PIN PIN_B2
/////////////////////////////////////////////////////////////////////////////
//
// Include the CCS USB Libraries. See the comments at the top of these
// files for more information
//
/////////////////////////////////////////////////////////////////////////////
#include <pic18_usb.h> //Microchip 18Fxx5x hardware layer for usb.c
#include "usb_desc_hid.h" //USB Configuration and Device descriptors for this UBS device
#include <usb.c> //handles usb setup tokens and get descriptor reports
/////////////////////////////////////////////////////////////////////////////
//
// usb_debug_task()
//
// When called periodically, displays debugging information over serial
// to display enumeration and connection states. Also lights LED1 based upon
// enumeration and status.
//
/////////////////////////////////////////////////////////////////////////////
void usb_debug_task(void) {
static int8 last_connected;
static int8 last_enumerated;
int8 new_connected;
int8 new_enumerated;
new_connected=usb_attached();
new_enumerated=usb_enumerated();
if (new_connected && !last_connected)
printf("\r\n\nUSB connected, waiting for enumaration...");
if (!new_connected && last_connected)
printf("\r\n\nUSB disconnected, waiting for connection...");
if (new_enumerated && !last_enumerated)
printf("\r\n\nUSB enumerated by PC/HOST");
if (!new_enumerated && last_enumerated)
printf("\r\n\nUSB unenumerated by PC/HOST, waiting for enumeration...");
last_connected=new_connected;
last_enumerated=new_enumerated;
}
void main() {
int8 out_data[20];
int8 in_data[2];
out_data[0]=1;
out_data[1]=2;
usb_init_cs();
while (TRUE) {
usb_task();
usb_debug_task();
if (usb_enumerated()) {
if (usb_kbhit(1)) {
usb_get_packet(1, in_data, 2);
printf("\r\n--> Received data: 0x%X 0x%X",in_data[0],in_data[1]);
if (usb_put_packet(1, out_data, 2, USB_DTS_TOGGLE))
printf("\r\n<-- Sending 2 bytes: 0x%X 0x%X", out_data[0], out_data[1]);
}
delay_ms(1);
}
}
} |
|
|
|
Ttelmah Guest
|
|
Posted: Fri Oct 17, 2008 3:04 pm |
|
|
Start with the simple things. Is your crystal 8MHz?. Have you got the external connection detection wiring present?.
Is Windows seeing the device?.
Best Wishes |
|
|
Guest
|
|
Posted: Fri Oct 17, 2008 9:17 pm |
|
|
Ttelmah wrote: | Start with the simple things. Is your crystal 8MHz?. Have you got the external connection detection wiring present?.
Is Windows seeing the device?.
Best Wishes |
Thanks for your help. I don't use VBUS connection sense, use 8MHz crystal, and comment out // #define USB_CON_SENSE_PIN PIN_B2, so Window don't remains to detect it. Could you help me?
Best Regards |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|