#define F_CPU 16500000UL #include "src/io.h" #include "src/deprecated.h" #include "src/usbdrv.h" #include "src/usbportability.h" #include "src/oddebug.h" #include "src/eeprom.h" #include "src/delay.h" unsigned char SlotID = 0; static unsigned char idleRate = 0; /* Funktion zur Berechnung des Slots */ short Slot(short sRES) { sRES=sRES*32; unsigned char i = 23; unsigned char Slot = 25; short x = 0; short y = 0; short ssRES[25]; ssRES[0]=17723; ssRES[1]=17471; ssRES[2]=17204; ssRES[3]=16932; ssRES[4]=16648; ssRES[5]=16354; ssRES[6]=16041; ssRES[7]=15722; ssRES[8]=15388; ssRES[9]=15041; ssRES[10]=14672; ssRES[11]=14293; ssRES[12]=13896; ssRES[13]=13480; ssRES[14]=13036; ssRES[15]=12578; ssRES[16]=12094; ssRES[17]=11588; ssRES[18]=11045; ssRES[19]=10482; ssRES[20]=9885; ssRES[21]=9255; ssRES[22]=8573; ssRES[23]=7863; ssRES[24]=7103; while(Slot == 25) { if(sRES >= ssRES[i]) i--; else { x=ssRES[i]-sRES; y=sRES-ssRES[i+1]; if(x>=y) Slot=i+2; else Slot=i+1; } } return Slot; } /* ---------------------------------usbFunctionSetup---------------------------------- */ unsigned char usbFunctionSetup(unsigned char data[8]) { usbRequest_t *rq = (void *)data; if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { if(rq->bRequest == USBRQ_HID_GET_REPORT) { return sizeof(SlotID); } else if(rq->bRequest == USBRQ_HID_GET_IDLE) { usbMsgPtr = &idleRate; return 1; } else if(rq->bRequest == USBRQ_HID_SET_IDLE) { idleRate = rq->wValue.bytes[1]; } } return 0; } /* ---------------------------------calibrateOscillator---------------------------------- */ static void calibrateOscillator(void) { unsigned char step = 128; unsigned char trialValue = 0, optimumValue; int x, optimumDev, targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5); do{ OSCCAL = trialValue + step; x = usbMeasureFrameLength(); if(x < targetValue) trialValue += step; step >>= 1; }while(step > 0); optimumValue = trialValue; optimumDev = x; for(OSCCAL = trialValue - 1; OSCCAL <= trialValue + 1; OSCCAL++){ x = usbMeasureFrameLength() - targetValue; if(x < 0) x = -x; if(x < optimumDev){ optimumDev = x; optimumValue = OSCCAL; } } OSCCAL = optimumValue; } void usbEventResetReady(void) { calibrateOscillator(); eeprom_write_byte(0, OSCCAL); } /* ----------------------------------main--------------------------------- */ int main(void) { outp(0x22, DDRB); /* ----------------------------------ADC conversion--------------------------------- */ /* ADC conversion */ unsigned short sRES = 0; unsigned char SlotID = 0; sbi(PORTB, 5); MCUCR = (1<