Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/wdt.h>
- #include <avr/interrupt.h> /* for sei() */
- #include <util/delay.h> /* for _delay_ms() */
- #include <avr/pgmspace.h> /* required by usbdrv.h */
- #include "usbdrv.h"
- #include "oddebug.h" /* This is also an example for using debug macros */
- //ADC輸入
- #define VARinput PIN5 //PC5
- volatile uint8_t FunNo;
- unsigned int timer[3];
- int tmp = 0;
- void Delay_ms(unsigned int S);
- void InitPort(void);
- /* ----------------------------- USB interface ----------------------------- */
- PROGMEM const char usbHidReportDescriptor[22] =
- {/* USB report descriptor */
- 0x06, 0x00, 0xff, // USAGE_PAGE (Generic Desktop)
- 0x09, 0x01, // USAGE (Vendor Usage 1)
- 0xa1, 0x01, // COLLECTION (Application)
- 0x15, 0x00, // LOGICAL_MINIMUM (0)
- 0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
- 0x75, 0x08, // REPORT_SIZE (8)
- 0x95, 0x80, // REPORT_COUNT (128)
- 0x09, 0x00, // USAGE (Undefined)
- 0xb2, 0x02, 0x01, // FEATURE (Data,Var,Abs,Buf)
- 0xc0 // END_COLLECTION
- };
- static uchar currentAddress;
- static uchar bytesRemaining;
- uchar usbFunctionRead(uchar *data, uchar len)
- {
- if(len > bytesRemaining)
- {
- len = bytesRemaining;
- }
- if(currentAddress == 0)
- {
- *(data) = 1; // Reserve (zero)
- *(data+1) = tmp; // Reserve (zero)
- *(data+2) = 0; // Reserve (zero)
- *(data+3) = 0; // Reserve (zero)
- *(data+4) = 0; // Reserve (zero)
- *(data+5) = 0; // Reserve (zero)
- *(data+6) = 0; // Reserve (zero)
- *(data+7) = 0; // Reserve (zero)
- }
- currentAddress += len;
- bytesRemaining -= len;
- return len;
- }
- uchar usbFunctionWrite(uchar *data, uchar len)
- {
- if(bytesRemaining == 0)
- {
- return 1;
- }
- if(len > bytesRemaining)
- {
- len = bytesRemaining;
- }
- if(currentAddress == 0)
- {
- if(*(data) == 1){
- PORTC ^= _BV(0);
- }
- }
- currentAddress += len;
- bytesRemaining -= len;
- return bytesRemaining == 0; /* return 1 if this was the last chunk */
- }
- usbMsgLen_t usbFunctionSetup(uchar data[8])
- {
- usbRequest_t *rq = (void *)data;
- if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS)
- {
- if(rq->bRequest == USBRQ_HID_GET_REPORT)
- {
- bytesRemaining = 8;
- currentAddress = 0;
- return USB_NO_MSG;
- }else if(rq->bRequest == USBRQ_HID_SET_REPORT)
- {
- currentAddress = 0;
- return USB_NO_MSG;
- }
- }else{
- /* ignore vendor type requests, we don't use any */
- }
- return 0;
- }
- /*********************************/
- /* */
- /* 主 程 式 */
- /* */
- /*********************************/
- int main(void)
- {
- InitPort();
- for(;;)
- {
- Delay_ms(100);
- if(tmp == 1)
- {
- PORTC ^= _BV(0);
- }
- }
- return 0;
- }
- /*********************************/
- /* */
- /* 副 程 式 */
- /* */
- /*********************************/
- void Delay_ms(unsigned int S)
- {
- while(S--)
- {
- usbPoll();
- wdt_reset();
- _delay_ms(1);
- }
- }
- //腳位、中斷初始化
- void InitPort(void)
- {
- uchar i;
- wdt_enable(WDTO_1S);
- /* IO SETUP */
- DDRB = 0x00;
- DDRC = 0x59;
- DDRD = 0x2a; //PD2 PD4 為USB資料腳,不能改變
- usbInit();
- usbDeviceDisconnect();
- i = 0;
- while(--i)
- {
- wdt_reset();
- _delay_ms(1);
- }
- usbDeviceConnect();
- sei();
- }
- /*********************************/
- /* */
- /* 中 斷 副 程 式 */
- /* */
- /*********************************/
- ISR (TIMER0_OVF_vect)
- {
- TCNT0 = 22;
- if (timer[1]-- == 0){
- timer[1]=50;
- ADCSRA |= (1 << ADSC);
- wdt_reset();
- }
- }
Add Comment
Please, Sign In to add comment