⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 picusbnut.c

📁 Pic USB interfnace
💻 C
字号:
#include <18F4550.h>
#fuses HSPLL,USBDIV,PLL5,CPUDIV1,VREGEN,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock=48000000)

/////////////////////////////////////////////////////////////////////////////
//
// 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
//
/////////////////////////////////////////////////////////////////////////////
#define USB_HID_DEVICE     FALSE             //deshabilitamos el uso de las directivas 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    33                //size to allocate for the rx endpoint 1 buffer

// Include the CCS USB Libraries.  See the comments at the top of these
// files for more information
//
/////////////////////////////////////////////////////////////////////////////
#include <pic18_usb.h>     //Microchip PIC18Fxx5x Hardware layer for CCS's PIC USB driver
#include <picusbnut.h>     //USB configuration and descriptors
#include <usb.c>        	//handles usb setup tokens and get descriptor reports

#include "include\types.h"
#include "include\lcd.c"

/////////////////////////////////////////////////////////////////////////////
#define  LED1     PIN_B2
#define  LED2     PIN_B3
#define  LED3     PIN_B4
#define  LED4     PIN_B5
#define  LED5     PIN_C6

#define  SW1      PIN_E0         // button 1
#define  SW2      PIN_E1         // button 2
#define  BUZZER   PIN_E2         // buzzer

#define  LED_ON   output_low
#define  LED_OFF  output_high

#define  mode     datain[0]
#define  data     datain[1]
#define  param1   datain[1]
#define  param2   datain[2]
#define  modeo    dataout[0]
#define  conv     dataout[1]
#define  result   dataout[1]
#define  swst     dataout[1]

void main(void)
{
   int8  datain[33];
   int8  dataout[2];
   char  rcvstr[16];
   int8  i;

   lcd_init ();

   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);

   lcd_putc("\f");
   lcd_gotoxy(4,1);
   printf(lcd_putc,"PicUSBNut");
   lcd_gotoxy(1,2);
   printf(lcd_putc,"www.hobbypic.com");

   LED_OFF(LED1);
   LED_OFF(LED2);                    //ALL Led off
   LED_OFF(LED3);
   LED_OFF(LED4);
   LED_ON(LED5);

   output_low (BUZZER);
   delay_ms(50);
   output_high (BUZZER);

   usb_init();                      //init USB

   usb_task();                      //usb  interrupt
   usb_wait_for_enumeration();      //wait from host PC
   LED_OFF(LED5);                    //conection established

   while (TRUE)
   {
      if(usb_enumerated())          //usb config
      {
         if (usb_kbhit(1))          //
         {
            usb_get_packet(1, datain, 64); //

            switch(mode)
            {
               case 0:        // leds game!
               {
                  switch(data)
                  {
                     case 0: {LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3); LED_OFF(LED4); break;}
                     case 1: {output_toggle(LED1); break;}
                     case 2: {output_toggle(LED2); break;}
                     case 3: {output_toggle(LED3); break;}
                     case 4: {output_toggle(LED4); break;}
                  }
                  break;
               }
               case 1:        // text string game!
               {
                  for(i=0;i<16;i++)
                  {
                     rcvstr[i] = datain[i+1];
                  }
                  lcd_putc("\f");
                  lcd_gotoxy(1,1);
                  printf(lcd_putc,"String recived:");                  
                  lcd_gotoxy(1,2);
                  printf(lcd_putc,"%s",rcvstr);
                  break;
               }
               case 2:        // A/D game!
               {
                  modeo = 0;
                  conv = read_adc();
                  usb_put_packet(1, dataout, 2, USB_DTS_TOGGLE); //enviamos el paquete de tama駉 1byte del EP1 al PC
                  break;
               }
            }
         }
         if (!input(SW1))
         {
            modeo = 1;
            swst = 1;
            usb_put_packet(1, dataout, 2, USB_DTS_TOGGLE);
         }
         if (!input(SW2))
         {
            modeo = 1;
            swst = 2;
            usb_put_packet(1, dataout, 2, USB_DTS_TOGGLE);
         }         
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -