First experiments with AVR (AtTiny2313)
authorHarvie <tomas@mudrunka.cz>
Sat, 14 Jan 2012 03:09:20 +0000 (04:09 +0100)
committerHarvie <tomas@mudrunka.cz>
Sat, 14 Jan 2012 03:09:20 +0000 (04:09 +0100)
avr/arduino-isp.txt [new file with mode: 0644]
avr/blink/Makefile [new file with mode: 0644]
avr/blink/helloworld.c [new file with mode: 0644]
avr/blink/helloworld2.c [new file with mode: 0644]
c/dreammachine/strobe.c

diff --git a/avr/arduino-isp.txt b/avr/arduino-isp.txt
new file mode 100644 (file)
index 0000000..07daab9
--- /dev/null
@@ -0,0 +1,9 @@
+arduino        MCU port        tiny45  tiny2313
+GND    GND             4       10
+5V     VCC             8       20
+10     RESET           1       1
+11     MOSI/SDA        5       17
+12     MISO/DO         6       18
+13     SCK/USCK/SCL    7       19
+
+avrdude -cstk500v1 -P/dev/ttyUSB0 -b19200 -p attiny2313
diff --git a/avr/blink/Makefile b/avr/blink/Makefile
new file mode 100644 (file)
index 0000000..9d4a517
--- /dev/null
@@ -0,0 +1,27 @@
+#MCU=at90s2313
+MCU=attiny2313
+CC=avr-gcc
+OBJCOPY=avr-objcopy
+PROJECT=helloworld
+# optimize for size:
+CFLAGS=-g -mmcu=$(MCU) -Wall -Wstrict-prototypes -Os -mcall-prologues
+#-------------------
+all: $(PROJECT).hex
+#-------------------
+$(PROJECT).hex : $(PROJECT).out 
+       $(OBJCOPY) -R .eeprom -O ihex $(PROJECT).out    $(PROJECT).hex 
+$(PROJECT).out : $(PROJECT).o 
+       $(CC) $(CFLAGS) -o $(PROJECT).out -Wl,-Map,$(PROJECT).map $(PROJECT).o 
+$(PROJECT).o : $(PROJECT).c 
+       $(CC) $(CFLAGS) -Os -c $(PROJECT).c
+asm : $(PROJECT).c 
+       $(CC) $(CFLAGS) -O -S $(PROJECT).c
+# you need to erase first before loading the program.
+# load (program) the software into the eeprom:
+load: $(PROJECT).hex
+       avrdude -cstk500v1 -P/dev/ttyUSB0 -b19200 -p $(MCU) -Uflash:w:$(PROJECT).hex:i
+       # uisp -dlpt=/dev/parport0 --erase --upload --verify if=$(PROJECT).hex -dprog=dapa  -v=3 --hash=12
+#-------------------
+clean:
+       rm -f *.o *.map *.out *.hex
+#-------------------
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
+  }
+}
diff --git a/avr/blink/helloworld2.c b/avr/blink/helloworld2.c
new file mode 100644 (file)
index 0000000..1b9520f
--- /dev/null
@@ -0,0 +1,33 @@
+#include <avr/io.h>
+#include <util/delay.h>
+
+//#include <compat/deprecated.h>
+/*
+variable = PINx;  // Read a byte from a port, replace x with the port number
+PORTx = variable; //Write a byte to a port
+PORTx |= 1<<pin_number;  // Set a pin in a port
+PORTx &= ~(1<<pin_number); // Clear a pin in a port
+*/
+
+void delay(unsigned int ms) {
+  unsigned int i,j;
+  for ( j=0; j<1000; j++)
+    for ( i=0; i<ms; i++);
+}
+
+int main (void){
+
+  //sbi(DDRD,PD5); /*enable port 5 for output*/
+       DDRD |= 1<<5;
+
+  while (1) {
+               //cbi (PORTD, PD5);
+               PORTD &= ~(1<<5);
+               delay (500);
+               //sbi (PORTD, PD5);
+               PORTD |= 1<<5;
+               delay (500);
+  }
+
+  return 0;
+}
index 1c2c797f6abec6f40559d8985a05424a7b06e40c..172130960e1a47789c5238d182684f42719ef1b4 100644 (file)
@@ -3,10 +3,16 @@
  * usage ./strobe [frequency]
  */
 
+//#define __WIN32__
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <signal.h>
+#ifndef __WIN32__
+  #include <fcntl.h>
+#else
+  #include <windows.h>
+#endif
 
 void signal_handler(int signo) {
        if(signo == SIGTERM || signo == SIGINT || signo == SIGQUIT) {
@@ -48,6 +54,14 @@ int main(int argc, char *argv[]) {
                fputs(state_a, stdout);
                        fflush(stdout);
                        usleep(half_interval);
+               #ifndef __WIN32__
+               int fflags = fcntl(fileno(stdin), F_GETFL, 0);
+               fcntl(fileno(stdin), F_SETFL, fflags | O_NONBLOCK);
+                               while(getchar()) puts(".");
+               fcntl(fileno(stdin), F_SETFL, fflags);
+               #else
+               //WARNING! Somehow implement FILE_FLAG_OVERLAPPED & FILE_FLAG_NO_BUFFERING support on windows
+                       #endif
                        if(argc > 2) getchar(); //interactive strobe
                fputs(state_b, stdout);
                        fflush(stdout);
This page took 0.162791 seconds and 4 git commands to generate.