First experiments with AVR (AtTiny2313)
[mirrors/Programs.git] / avr / blink / helloworld.c
diff --git a/avr/blink/helloworld.c b/avr/blink/helloworld.c
new file mode 100644 (file)
index 0000000..f0ffecb
--- /dev/null
@@ -0,0 +1,40 @@
+#define F_CPU 100000UL // Sets up the default speed for delay.h
+// this is the header file that tells the compiler what pins and ports, etc.
+// are available on this chip.
+#include <avr/io.h>
+
+// define what pins the LEDs are connected to.
+// in reality, PD6 is really just '6'
+#define LED PD5
+
+// Some macros that make the code more readable
+#define output_low(port,pin) port &= ~(1<<pin)
+#define output_high(port,pin) port |= (1<<pin)
+#define set_input(portdir,pin) portdir &= ~(1<<pin)
+#define set_output(portdir,pin) portdir |= (1<<pin)
+
+// this is just a program that 'kills time' in a calibrated method
+void delay_ms(uint8_t ms) {
+  uint16_t delay_count = F_CPU / 17500;
+  volatile uint16_t i;
+
+  while (ms != 0) {
+    for (i=0; i != delay_count; i++);
+    ms--;
+  }
+}
+
+int main(void) {
+  // initialize the direction of PORTD #6 to be an output
+  set_output(DDRD, LED);
+
+  while (1) {
+    // turn on the LED for 200ms
+    output_high(PORTD, LED);
+    delay_ms(200);
+    // now turn off the LED for another 200ms
+    output_low(PORTD, LED);
+    delay_ms(200);
+    // now start over
+  }
+}
This page took 0.10239 seconds and 4 git commands to generate.