📄 picwinusb.c
字号:
/////////////////////////////////////////////////////////////////////////
//// PicWinUSB.c ////
//// ////
//// Este ejemplo muestra como desarrollar un sencillo dispositivo ////
//// USB con un microcontrolador PIC de la serie 18Fxx5x. ////
//// El dispositivo hace toggle de un led cuando se le envia la ////
//// orden desde el PC, y lee el conversor A/D enviando el valor ////
//// obtenido al PC. Esto lo realiza haciendo uso del Driver ////
//// WinUSB incluido en Vista y compatible con XP. A diferencia ////
//// del ejemplo PicUSB, de esta misma p醙ina, que lo realizaba con ////
//// el driver de Microchip, mchpusbapi.dll incompatible con Vista. ////
//// ////
//// Realizado con el compilador CCS PCWH 4.057 ////
//// ////
//// Por: Jaime Fern醤dez-Caro Belmonte hobbypic@hotmail.com ////
//// ////
//// http://www.hobbypic.com ////
/////////////////////////////////////////////////////////////////////////
#include <18F2550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#define USB_HID_DEVICE FALSE //disabled HID
#define USB_EP1_TX_ENABLE USB_ENABLE_BULK //turn on EP1(EndPoint1) for IN bulk/interrupt transfers
#define USB_EP1_RX_ENABLE USB_ENABLE_BULK //turn on EP1(EndPoint1) for OUT bulk/interrupt transfers
#define USB_EP1_TX_SIZE 2 //size to allocate for the tx endpoint 1 buffer
#define USB_EP1_RX_SIZE 2 //size to allocate for the rx endpoint 1 buffer
#include <pic18_usb.h> //Microchip PIC18Fxx5x Hardware layer for CCS's PIC USB driver
#include <PicWinUSB.h> //Descriptors and USB configuration
#include <usb.c> //handles usb setup tokens and get descriptor reports
#define LED PIN_B0
#define LED_ON output_low
#define LED_OFF output_high
void main(void) {
int8 iBuff[2]; //buffers
int8 oBuff[2];
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
LED_OFF(LED);
usb_init();
usb_task();
usb_wait_for_enumeration();
while (TRUE)
{
if(usb_enumerated()) // PicWinUSB is enumerated?
{
if (usb_kbhit(1)) // EP has data?
{
usb_get_packet(1, iBuff, 2); // Read 2 byte packet from EP 1
if (iBuff[0] == 0) // Led_Mode
{
if (iBuff[1] == 1) output_toggle(LED); // Toggle LED
}
if (iBuff[0] == 1) // ADC_Mode
{
oBuff[0] = 1;
oBuff[1] = read_adc();
usb_put_packet(1, oBuff, 2, USB_DTS_TOGGLE); // Send 2 byte packet to EP 1
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -