a5768549

Untitled

Aug 7th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.82 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/wdt.h>
  3. #include <avr/interrupt.h>  /* for sei() */
  4. #include <util/delay.h>     /* for _delay_ms() */
  5.  
  6.  
  7. #include <avr/pgmspace.h>   /* required by usbdrv.h */
  8. #include "usbdrv.h"
  9. #include "oddebug.h"        /* This is also an example for using debug macros */
  10.  
  11. //ADC輸入
  12. #define VARinput PIN5   //PC5
  13. volatile uint8_t FunNo;
  14. unsigned int timer[3];
  15. int tmp = 0;
  16. void Delay_ms(unsigned int S);
  17. void InitPort(void);
  18. /* ----------------------------- USB interface ----------------------------- */
  19. PROGMEM const char usbHidReportDescriptor[22] =
  20. {/* USB report descriptor */
  21.     0x06, 0x00, 0xff,              // USAGE_PAGE (Generic Desktop)
  22.     0x09, 0x01,                    // USAGE (Vendor Usage 1)
  23.     0xa1, 0x01,                    // COLLECTION (Application)
  24.     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
  25.     0x26, 0xff, 0x00,              //   LOGICAL_MAXIMUM (255)
  26.     0x75, 0x08,                    //   REPORT_SIZE (8)
  27.     0x95, 0x80,                    //   REPORT_COUNT (128)
  28.     0x09, 0x00,                    //   USAGE (Undefined)
  29.     0xb2, 0x02, 0x01,              //   FEATURE (Data,Var,Abs,Buf)
  30.     0xc0                           // END_COLLECTION
  31. };
  32.  
  33. static uchar    currentAddress;
  34. static uchar    bytesRemaining;
  35.  
  36. uchar   usbFunctionRead(uchar *data, uchar len)
  37. {
  38.     if(len > bytesRemaining)
  39.     {
  40.         len = bytesRemaining;
  41.     }
  42.     if(currentAddress == 0)
  43.     {
  44.         *(data)   = 1;      // Reserve (zero)
  45.         *(data+1) = tmp;        // Reserve (zero)
  46.         *(data+2) = 0;      // Reserve (zero)
  47.         *(data+3) = 0;      // Reserve (zero)
  48.         *(data+4) = 0;      // Reserve (zero)
  49.         *(data+5) = 0;      // Reserve (zero)
  50.         *(data+6) = 0;      // Reserve (zero)
  51.         *(data+7) = 0;      // Reserve (zero)
  52.     }
  53.     currentAddress += len;
  54.     bytesRemaining -= len;
  55.     return len;
  56. }
  57.  
  58. uchar   usbFunctionWrite(uchar *data, uchar len)
  59. {
  60.     if(bytesRemaining == 0)
  61.     {
  62.         return 1;
  63.     }
  64.     if(len > bytesRemaining)
  65.     {
  66.         len = bytesRemaining;
  67.     }
  68.    
  69.     if(currentAddress == 0)
  70.     {
  71.         if(*(data) ==  1){ 
  72.             PORTC ^= _BV(0);
  73.         }
  74.     }
  75.     currentAddress += len;
  76.     bytesRemaining -= len;
  77.     return bytesRemaining == 0; /* return 1 if this was the last chunk */
  78. }
  79.  
  80. usbMsgLen_t usbFunctionSetup(uchar data[8])
  81. {
  82.     usbRequest_t    *rq = (void *)data;
  83.     if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS)
  84.     {
  85.         if(rq->bRequest == USBRQ_HID_GET_REPORT)
  86.         {
  87.             bytesRemaining = 8;
  88.             currentAddress = 0;
  89.             return USB_NO_MSG;
  90.         }else if(rq->bRequest == USBRQ_HID_SET_REPORT)
  91.         {
  92.             currentAddress = 0;
  93.             return USB_NO_MSG;
  94.         }
  95.         }else{
  96.         /* ignore vendor type requests, we don't use any */
  97.     }
  98.     return 0;
  99. }
  100.  
  101. /*********************************/
  102. /*                               */
  103. /*        主    程    式         */
  104. /*                               */
  105. /*********************************/
  106. int main(void)
  107. {
  108.  
  109.     InitPort();
  110.     for(;;)
  111.     {
  112.         Delay_ms(100);
  113.         if(tmp == 1)
  114.         {
  115.             PORTC ^= _BV(0);
  116.            
  117.         }
  118.     }
  119.     return 0;
  120. }
  121.  
  122.  
  123. /*********************************/
  124. /*                               */
  125. /*        副    程    式         */
  126. /*                               */
  127. /*********************************/
  128. void Delay_ms(unsigned int S)
  129. {
  130.     while(S--)
  131.     {
  132.         usbPoll();
  133.         wdt_reset();
  134.         _delay_ms(1);
  135.     }
  136. }
  137. //腳位、中斷初始化
  138. void InitPort(void)
  139. {
  140.     uchar i;
  141.     wdt_enable(WDTO_1S);
  142.     /* IO SETUP */
  143.    
  144.     DDRB = 0x00;
  145.     DDRC = 0x59;
  146.     DDRD = 0x2a;     //PD2 PD4 為USB資料腳,不能改變
  147.  
  148.     usbInit();
  149.     usbDeviceDisconnect();
  150.     i = 0;
  151.     while(--i)
  152.     {
  153.         wdt_reset();
  154.         _delay_ms(1);
  155.     }
  156.     usbDeviceConnect();
  157.     sei();
  158. }
  159. /*********************************/
  160. /*                               */
  161. /*     中  斷  副  程  式        */
  162. /*                               */
  163. /*********************************/
  164. ISR (TIMER0_OVF_vect)
  165. {
  166.     TCNT0 = 22;
  167.     if (timer[1]-- == 0){
  168.         timer[1]=50;
  169.         ADCSRA |= (1 << ADSC);
  170.         wdt_reset();
  171.     }
  172. }
Add Comment
Please, Sign In to add comment