#include <avr/interrupt.h>
#include <util/delay.h>
#include "usb_rawhid.h"
-#include "analog.h"
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
volatile uint8_t do_output=0;
-uint8_t buffer[64];
+uint8_t read_buffer[64];
+uint8_t write_buffer[64];
int main(void)
{
- int8_t r;
- uint8_t i;
- uint16_t val, count=0;
+ int8_t r;
+ uint8_t i;
+ uint16_t val, count=0;
- // set for 16 MHz clock
- CPU_PRESCALE(0);
+ // set for 16 MHz clock
+ CPU_PRESCALE(0);
+ DDRF|=3;
+ PORTF&=~1;
+ // Configure timer 0 to generate a timer overflow interrupt every
+ // 200*8 clock cycles, 100us
+ TCCR0A = 1<<WGM01;
+ TCCR0B = 1<<CS01;
+ OCR0A = 199;
+ TCNT0 = 0;
+ TIMSK0 = (1<<OCIE0A);
- // Initialize the USB, and then wait for the host to set configuration.
- // If the Teensy is powered without a PC connected to the USB port,
- // this will wait forever.
- usb_init();
- while (!usb_configured()) /* wait */ ;
+ // Initialize the USB, and then wait for the host to set configuration.
+ // If the Teensy is powered without a PC connected to the USB port,
+ // this will wait forever.
+ usb_init();
+ while (!usb_configured()) /* wait */ ;
+ // 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);
+ while (1) {
+ // if received data, do something with it
+ r = usb_rawhid_recv(read_buffer, 0);
+ if (r>0)
+ usb_rawhid_send(read_buffer, 50);
- // 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++;
- }
- }
+ }
}
// This interrupt routine is run approx 61 times per second.
-ISR(TIMER0_OVF_vect)
+ISR(TIMER0_COMPA_vect)
{
- static uint8_t count=0;
+ PORTF^=2;
- // set the do_output variable every 2 seconds
- if (++count > 122) {
- count = 0;
- do_output = 1;
- }
}