- // Wait an extra second for the PC's operating system to load drivers
- // and do whatever it does to actually be ready for input
- _delay_ms(1000);
-
- // Configure timer 0 to generate a timer overflow interrupt every
- // 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
- TCCR0A = 0x00;
- TCCR0B = 0x05;
- TIMSK0 = (1<<TOIE0);
-
- while (1) {
- // if received data, do something with it
- r = usb_rawhid_recv(buffer, 0);
- if (r > 0) {
- // output 4 bits to D0, D1, D2, D3 pins
- DDRD = 0x0F;
- PORTD = (PORTD & 0xF0) | (buffer[0] & 0x0F);
- // ignore the other 63.5 bytes....
- }
- // if time to send output, transmit something interesting
- if (do_output) {
- do_output = 0;
- // send a packet, first 2 bytes 0xABCD
- buffer[0] = 0xAB;
- buffer[1] = 0xCD;
- // put A/D measurements into next 24 bytes
- for (i=0; i<12; i++) {
- val = analogRead(i);
- buffer[i * 2 + 2] = val >> 8;
- buffer[i * 2 + 3] = val & 255;
- }
- // most of the packet filled with zero
- for (i=26; i<62; i++) {
- buffer[i] = 0;
- }
- // put a count in the last 2 bytes
- buffer[62] = count >> 8;
- buffer[63] = count & 255;
- // send the packet
- usb_rawhid_send(buffer, 50);
- count++;
- }
- }