summaryrefslogtreecommitdiff
path: root/example.c
diff options
context:
space:
mode:
authorAstrid Smith2011-09-11 18:17:01 -0700
committerAstrid Smith2011-09-11 18:17:15 -0700
commite94b760d621b41e77e358d9c24877162099c47d9 (patch)
treeee0263a4c74e633895659d837037f5efc14c2fcd /example.c
parent1963e89a7907b56d4914aa7855d5bdfd12ddd1f6 (diff)
Works: measures time and client displays a primitive stripchart.
Diffstat (limited to 'example.c')
-rw-r--r--example.c72
1 files changed, 53 insertions, 19 deletions
diff --git a/example.c b/example.c
index 129b6eb..7409fdf 100644
--- a/example.c
+++ b/example.c
@@ -36,38 +36,55 @@ void send_str(const char *s);
uint8_t recv_str(char *buf, uint8_t size);
void parse_and_execute_command(const char *buf, uint8_t num);
-// Very simple character echo test
int main(void)
{
- int changed = 0;
+ int changed = 0, val;
int port = 'B' - 'A';
- int pin = '0' - '0';
- int val;
- CPU_PRESCALE(0);
+ int pin = 1 << ('0' - '0');
+ int latency = 0;
+
+ CPU_PRESCALE(0x00); // 16 MHz
usb_init();
LED_CONFIG;
while (!usb_configured()) /* wait */ ;
_delay_ms(1000);
-
- LED_ON;
-
+ CPU_PRESCALE(0x04); // 1 MHz
uint8_t *portload = 0x20 + port * 3;
+ *portload &= ~pin;
- *(uint8_t *)(0x21 + port * 3) &= ~(1 << pin);
-
+ TCCR1B |= (1 << CS10); // Set timer1 to 1 microsecond steps
while (1) {
- val = *portload & (1 << pin);
-// usb_serial_putchar(val + '0');
- if ((val == 1) && (changed == 0)) {
- changed = 1;
- usb_serial_putchar(val ? '1' : '0');
- LED_ON;
- } else if ((val == 0) && (changed == 1)) {
+ val = *portload & pin;
+ if ((val == 0) && (changed == 1)) {
+ /* trailing edge of pulse.
+ *
+ * I track the 1->0 transition rather than
+ * 0->1 because it's sharper.
+ */
+ int count;
+ count = TCNT1; // read timer
+ if (count < 15000)
+ /* ignore spurious pulses. If I don't
+ * do this, for some reason I'll see a
+ * bunch of half-width pulses.
+ */
+ continue;
+ TCNT1 = 0; // reset timer
changed = 0;
- usb_serial_putchar(val ? '1' : '0');
- LED_OFF;
+
+// usb_serial_putchar(count > 16667 ? '>' : '<');
+ send_int(count);
+ usb_serial_putchar(' ');
+ send_int(latency);
+ usb_serial_putchar('\n');
+ latency = TCNT1;
+ } else if ((val == 1) && (changed == 0)) {
+ /* leading edge of pulse.
+ */
+ changed = 1;
+// usb_serial_putchar('0');
}
}
}
@@ -85,6 +102,23 @@ void send_str(const char *s)
}
}
+/* Write a decimal integer to the serial port
+ */
+void send_int(int val)
+{
+ char c_out[10];
+ int ptr;
+ c_out[ptr++] = '\0';
+ while (val > 0) {
+ c_out[ptr++] = (val % 10) + '0';
+ val /= 10;
+ }
+ ptr--;
+ while (ptr) {
+ usb_serial_putchar(c_out[ptr--]);
+ }
+}
+
// Receive a string from the USB serial port. The string is stored
// in the buffer and this function will not exceed the buffer size.
// A carriage return or newline completes the string, and is not