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

📄 main-usb.c

📁 AVRusb开发的一个电脑遥控器
💻 C
字号:
/*  * Project: IR-Mouse * File desc.: This file contains USB uC functions * Author: Krzysiek Szczuka * Creation Date: 2007-02-14 * Tabsize: 4 * Copyright: (c) 2007 by Krzysiek Szczuka * License: Proprietary, free under certain conditions. See Documentation. * This Revision: $Id: main-usb.c,v 1.13 2007/02/27 19:02:57 ksz Exp $ *//* * USB HID mouse controlled by IR remote. * * I used only byttons and X/Y reports, new mouses as fourth byte reports * wheel move, I didn't need this.. * * Here's how mouse report looks like (I've used SnoopyPro to record it): * * 0xXX 0xXX 0xXX 0xXX *   ^    ^    ^    ^ *   |    |    |    | *   |    |    |    +-- Fourth byte, for mouse's wheel(not used in this project) *   |    |    |        0x01 - wheel UP *   |    |    |        0xFF - wheel DOWN *   |    |    |         *   |    |    +------- Y axis (values depends on mouse's speed) *   |    |             0x81 .. 0x00 : -127 ..   0 : UP *   |    |             0x00 .. 0x7f :    0 .. 127 : DOWN *   |    |              *   |    +------------ X axis (values depends on mouse's speed) *   |                  0x81 .. 0x00 : -127 ..   0 : LEFT *   |                  0x00 .. 0x7f :    0 .. 127 : RIGHT *   |                   *   +----------------- mouse buttons states *                      r r r r r W R L, where: *                      r - reserved (depends on device type?) *                      W - wheel pressed (my mouse's wheel is a button too) *                      R - right button *                      L - left button * * * Sorry for my english in any file of this, feel free to correnct it....  (-; *  regards, Krzysiek Szczuka */#define F_CPU   12000000L    /* evaluation board runs on 12MHz */#include <avr/io.h>#include <avr/interrupt.h>#include <avr/pgmspace.h>#include <avr/wdt.h>#include <inttypes.h>#include "usbdrv.h"static	unsigned char volatile	flags = 0;static	uchar    				reportBuffer[3];    #include "defines.h"ISR(USART_RX_vect){	static unsigned char bc = 0;	// byte counter		reportBuffer[bc++] = UDR;		if( (bc%3) == 0 ) {		flags |= FLG_IR_RECEIVED;		bc = 0;		UCSRB &= ~_BV(RXCIE);	//disable this interrupt	}}static void hardwareInit(void){uchar	i, j;        PORTD = 0xe3;   /* 1110 0011 bin: activate pull-ups except on USB lines */    DDRD = 0x1c;    /* 0001 1100 bin: all pins input except USB (-> USB reset) */	j = 0;	while(--j){     /* USB Reset by device only required on Watchdog Reset */		i = 0;		while(--i); /* delay >10ms for USB reset */	}    DDRD = 0x10;    /* 0001 0000 bin: remove USB reset condition */        /*** UART init ***/	// baud rate	UBRRH = (unsigned char)(((F_CPU)/((BAUD_RATE)*16l)-1)>>8);
    UBRRL = (unsigned char) ((F_CPU)/((BAUD_RATE)*16l)-1);	// in this uC we only need a receiver	UCSRB = _BV(RXEN) | _BV(RXCIE);	// asynchronous mode, 8N1	UCSRC = _BV(UCSZ1) | _BV(UCSZ0);}/* ------------------------------------------------------------------------- *//* ----------------------------- USB interface ----------------------------- *//* ------------------------------------------------------------------------- */// generated by usb.org's HIDTool for standard mouse// probably you want (must?!) to change it if you need mouse's wheel supportchar usbHidReportDescriptor[] = {    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)    0x09, 0x02,                    // USAGE (Mouse)    0xa1, 0x01,                    // COLLECTION (Application)    0x09, 0x01,                    //   USAGE (Pointer)    0xa1, 0x00,                    //   COLLECTION (Physical)    0x05, 0x09,                    //     USAGE_PAGE (Button)    0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)    0x29, 0x03,                    //     USAGE_MAXIMUM (Button 3)    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)    0x95, 0x03,                    //     REPORT_COUNT (3)    0x75, 0x01,                    //     REPORT_SIZE (1)    0x81, 0x02,                    //     INPUT (Data,Var,Abs)    0x95, 0x01,                    //     REPORT_COUNT (1)    0x75, 0x05,                    //     REPORT_SIZE (5)    0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)    0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)    0x09, 0x30,                    //     USAGE (X)    0x09, 0x31,                    //     USAGE (Y)    0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)    0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)    0x75, 0x08,                    //     REPORT_SIZE (8)    0x95, 0x02,                    //     REPORT_COUNT (2)    0x81, 0x06,                    //     INPUT (Data,Var,Rel)    0xc0,                          //   END_COLLECTION    0xc0                           // END_COLLECTION};uchar	usbFunctionSetup(uchar data[8]){usbRequest_t    *rq = (void *)data;    usbMsgPtr = reportBuffer;    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){    /* class request type */        if(rq->bRequest == USBRQ_HID_GET_REPORT){  /* wValue: ReportType (highbyte), ReportID (lowbyte) */            /* we only have one report type, so don't look at wValue */            //makeReport();            return sizeof(reportBuffer);        }    }else{        /* no vendor specific requests implemented */    }	return 0;}/* ------------------------------------------------------------------------- */int	main(void){	wdt_enable(WDTO_2S);	hardwareInit();	usbInit();	usbDeviceConnect();	sei();	for(;;){	/* main event loop */		wdt_reset();		usbPoll();		if (flags & FLG_IR_RECEIVED) {			if (usbInterruptIsReady()){            	usbSetInterrupt(reportBuffer, sizeof(reportBuffer));            }           	flags &= ~FLG_IR_RECEIVED;            UCSRB |= _BV(RXCIE);	// reenable usart rx interrupt		}	}	return 0;}/* ------------------------------------------------------------------------- */

⌨️ 快捷键说明

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