+++ /dev/null
-
-void init_leds(void) {
- DDRLED0 |= 1<<LED0;
- DDRLED1 |= 1<<LED1;
- DDRLED2 |= 1<<LED2;
-}
-
-
-void set_led0(uint8_t value){
- PORTLED0 &= ~(1<<LED0);
- PORTLED0 |= (!(value) & 0x01)<<LED0;
-}
-
-void set_led1(uint8_t value){
- PORTLED1 &= ~(1<<LED1);
- PORTLED1 |= (!(value) & 0x01)<<LED1;
-}
-
-void set_led2(uint8_t value){
- PORTLED2 &= ~(1<<LED2);
- PORTLED2 |= (!(value) & 0x01)<<LED2;
-}
-
-
-void init_sw(void) {
- DDRSW0 &= ~(1<<SW0);
- DDRSW1 &= ~(1<<SW1);
- DDRSW2 &= ~(1<<SW2);
- PORTSW0 |= (1<<SW0);
- PORTSW1 |= (1<<SW1);
- PORTSW2 |= (1<<SW2);
-}
-
-
-uint8_t sw0_state(void){
- return (PINSW0 & (1<<SW0))>>SW0;
-}
-
-uint8_t sw1_state(void){
- return (PINSW1 & (1<<SW1))>>SW1;
-}
-
-uint8_t sw2_state(void){
- return (PINSW2 & (1<<SW2))>>SW2;
-}
\ No newline at end of file
#include "motors.h"
#include "misc.h"
#include "pins.h"
-#include "leds.c"
-
int16_t plate_pos_x = 0,plate_pos_y = 0;
-
-
-
-
void print_steps_in_mm(int16_t steps) {
int16_t predot,postdot;
print_steps_in_mm(plate_pos_x);
uart_puts(" y_pos: ");
print_steps_in_mm(plate_pos_y);
- uart_puts("\r");
+ uart_puts(" end_sw: ");
+ uart_print_number_wlzeros(XEND2_state(),1);
+ uart_print_number_wlzeros(XEND1_state(),1);
+ uart_print_number_wlzeros(YEND2_state(),1);
+ uart_print_number_wlzeros(YEND1_state(),1);
+ uart_puts("\r\n");
}
}
}
-int main(void)
-{
+
+
+int main(void){
init_motors();
- char dummy;
- uint8_t field_val = 0;
SetupHardware();
touchpad_init(); // you need to call this to setup the I/O pin!
sei();
touchpad_set_rel_mode_100dpi();// use touchpad in relative mode
- int16_t x, y = 0;
int8_t dx, dy = 0;
uint8_t busy = 0, last_busy = 0;
Usb2SerialTask();
parse_command(); // read data from virtual comport
touchpad_read(); // read data from touchpad
- dx = -4*delta_x();// returns the amount your finger has moved in x direction since last readout
+ dx = 4*delta_x();// returns the amount your finger has moved in x direction since last readout
dy = -4*delta_y();// returns the amount your finger has moved in y direction since last readout
plate_pos_x += dx;
if (last_busy && !(busy)){
pos_report();
+
}
}
F_USB = $(F_CPU)\r
OPTIMIZATION = s\r
TARGET = main\r
-SRC = $(TARGET).c Descriptors.c $(LUFA_SRC_USB) $(LUFA_SRC_USBCLASS) TM1001A.c motors.c misc.c USBtoSerial.c\r
+SRC = $(TARGET).c Descriptors.c $(LUFA_SRC_USB) $(LUFA_SRC_USBCLASS) pins.c TM1001A.c motors.c misc.c USBtoSerial.c\r
LUFA_PATH = ../LUFA/LUFA-130303/LUFA\r
CC_FLAGS = -DUSE_LUFA_CONFIG_HEADER -IConfig/\r
LD_FLAGS =\r
#include <util/delay.h>
#include "motors.h"
#include "misc.h"
+#include "pins.h"
void set_x(uint8_t byte) {
- PORTX0 &= ~(1<<X0);
- PORTX1 &= ~(1<<X1);
- PORTX2 &= ~(1<<X2);
- PORTX3 &= ~(1<<X3);
-
- PORTX0 |= ((byte & (1<<0))>>0)<<X0;
- PORTX1 |= ((byte & (1<<1))>>1)<<X1;
- PORTX2 |= ((byte & (1<<2))>>2)<<X2;
- PORTX3 |= ((byte & (1<<3))>>3)<<X3;
+ X0_PORT &= ~(1<<X0);
+ X1_PORT &= ~(1<<X1);
+ X2_PORT &= ~(1<<X2);
+ X3_PORT &= ~(1<<X3);
+
+ X0_PORT |= ((byte & (1<<0))>>0)<<X0;
+ X1_PORT |= ((byte & (1<<1))>>1)<<X1;
+ X2_PORT |= ((byte & (1<<2))>>2)<<X2;
+ X3_PORT |= ((byte & (1<<3))>>3)<<X3;
}
void set_y(uint8_t byte) {
- PORTY0 &= ~(1<<Y0);
- PORTY1 &= ~(1<<Y1);
- PORTY2 &= ~(1<<Y2);
- PORTY3 &= ~(1<<Y3);
-
- PORTY0 |= ((byte & (1<<0))>>0)<<Y0;
- PORTY1 |= ((byte & (1<<1))>>1)<<Y1;
- PORTY2 |= ((byte & (1<<2))>>2)<<Y2;
- PORTY3 |= ((byte & (1<<3))>>3)<<Y3;
+ Y0_PORT &= ~(1<<Y0);
+ Y1_PORT &= ~(1<<Y1);
+ Y2_PORT &= ~(1<<Y2);
+ Y3_PORT &= ~(1<<Y3);
+
+ Y0_PORT |= ((byte & (1<<0))>>0)<<Y0;
+ Y1_PORT |= ((byte & (1<<1))>>1)<<Y1;
+ Y2_PORT |= ((byte & (1<<2))>>2)<<Y2;
+ Y3_PORT |= ((byte & (1<<3))>>3)<<Y3;
}
void init_motors(void){
set_x(0);
set_y(0);
- DDRX0 |= (1<<X0);
- DDRX1 |= (1<<X1);
- DDRX2 |= (1<<X2);
- DDRX3 |= (1<<X3);
- DDRY0 |= (1<<Y0);
- DDRY1 |= (1<<Y1);
- DDRY2 |= (1<<Y2);
- DDRY3 |= (1<<Y3);
+ X0_DDR |= (1<<X0);
+ X1_DDR |= (1<<X1);
+ X2_DDR |= (1<<X2);
+ X3_DDR |= (1<<X3);
+ Y0_DDR |= (1<<Y0);
+ Y1_DDR |= (1<<Y1);
+ Y2_DDR |= (1<<Y2);
+ Y3_DDR |= (1<<Y3);
+
+
+ XEND1_as_pullup();
+ XEND2_as_pullup();
+ YEND1_as_pullup();
+ YEND2_as_pullup();
+
}
uint8_t move_plate(int16_t dx, int16_t dy){
static int16_t todo_x,todo_y = 0;
int8_t signum;
- uint8_t returnval = 0;
+ uint8_t busy = 0;
todo_x += dx;
todo_y += dy;
+ //if end switch closed, stop moving against the stop!
+ if(XEND1_state() && (sign(todo_x) == 1) ){
+ todo_x = 0;
+ }
+ if(XEND2_state() && (sign(todo_x) == -1) ){
+ todo_x = 0;
+ }
+ if(YEND1_state() && (sign(todo_y) == 1) ){
+ todo_y = 0;
+ }
+ if(YEND2_state() && (sign(todo_y) == -1) ){
+ todo_y = 0;
+ }
+
+
signum = sign(todo_x);
if(signum != 0) {
- returnval++;
+ busy = 1;
}
motor_step(X,signum);
todo_x -= signum;
+
+
signum = sign(todo_y);
if(signum != 0) {
- returnval++;
+ busy = 1;
}
motor_step(Y,signum);
todo_y -= signum;
+
+
+
_delay_us(PHASE_DELAY_US);
- return returnval; // busy
+
+
+ return busy; // busy
}
#define X0 4
-#define DDRX0 DDRC
-#define PORTX0 PORTC
-#define PINX0 PINC
+#define X0_DDR DDRC
+#define X0_PORT PORTC
+#define X0_PIN PINC
#define X1 5
-#define DDRX1 DDRC
-#define PORTX1 PORTC
-#define PINX1 PINC
+#define X1_DDR DDRC
+#define X1_PORT PORTC
+#define X1_PIN PINC
#define X2 4
-#define DDRX2 DDRD
-#define PORTX2 PORTD
-#define PINX2 PIND
+#define X2_DDR DDRD
+#define X2_PORT PORTD
+#define X2_PIN PIND
#define X3 6
-#define DDRX3 DDRC
-#define PORTX3 PORTC
-#define PINX3 PINC
+#define X3_DDR DDRC
+#define X3_PORT PORTC
+#define X3_PIN PINC
#define Y0 7
-#define DDRY0 DDRC
-#define PORTY0 PORTC
-#define PINY0 PINC
+#define Y0_DDR DDRC
+#define Y0_PORT PORTC
+#define Y0_PIN PINC
#define Y1 6
-#define DDRY1 DDRB
-#define PORTY1 PORTB
-#define PINY1 PINB
+#define Y1_DDR DDRB
+#define Y1_PORT PORTB
+#define Y1_PIN PINB
#define Y2 7
-#define DDRY2 DDRB
-#define PORTY2 PORTB
-#define PINY2 PINB
+#define Y2_DDR DDRB
+#define Y2_PORT PORTB
+#define Y2_PIN PINB
#define Y3 5
-#define DDRY3 DDRB
-#define PORTY3 PORTB
-#define PINY3 PINB
+#define Y3_DDR DDRB
+#define Y3_PORT PORTB
+#define Y3_PIN PINB
void set_x(uint8_t byte);
--- /dev/null
+#include <avr/io.h>
+#include <stdlib.h>
+#include "pins.h"
+
+
+// functions for pin XEND1
+
+void XEND1_set(uint8_t value){
+ XEND1_PORT &= ~(1<<XEND1);
+ XEND1_PORT |= (value & 0x01)<<XEND1;
+}
+
+void XEND1_as_output(void){
+ XEND1_DDR |= 1<<XEND1;
+}
+
+void XEND1_as_input(void){
+ XEND1_DDR &= ~(1<<XEND1);
+}
+
+void XEND1_as_pullup(void){
+ XEND1_DDR &= ~(1<<XEND1);
+ XEND1_PORT |= 1<<XEND1;
+}
+
+uint8_t XEND1_state(void){
+ return (XEND1_PIN & (1<<XEND1))>>XEND1;
+}
+
+
+// functions for pin XEND2
+
+void XEND2_set(uint8_t value){
+ XEND2_PORT &= ~(1<<XEND2);
+ XEND2_PORT |= (value & 0x01)<<XEND2;
+}
+
+void XEND2_as_output(void){
+ XEND2_DDR |= 1<<XEND2;
+}
+
+void XEND2_as_input(void){
+ XEND2_DDR &= ~(1<<XEND2);
+}
+
+void XEND2_as_pullup(void){
+ XEND2_DDR &= ~(1<<XEND2);
+ XEND2_PORT |= 1<<XEND2;
+}
+
+uint8_t XEND2_state(void){
+ return (XEND2_PIN & (1<<XEND2))>>XEND2;
+}
+
+
+// functions for pin YEND1
+
+void YEND1_set(uint8_t value){
+ YEND1_PORT &= ~(1<<YEND1);
+ YEND1_PORT |= (value & 0x01)<<YEND1;
+}
+
+void YEND1_as_output(void){
+ YEND1_DDR |= 1<<YEND1;
+}
+
+void YEND1_as_input(void){
+ YEND1_DDR &= ~(1<<YEND1);
+}
+
+void YEND1_as_pullup(void){
+ YEND1_DDR &= ~(1<<YEND1);
+ YEND1_PORT |= 1<<YEND1;
+}
+
+uint8_t YEND1_state(void){
+ return (YEND1_PIN & (1<<YEND1))>>YEND1;
+}
+
+
+// functions for pin YEND2
+
+void YEND2_set(uint8_t value){
+ YEND2_PORT &= ~(1<<YEND2);
+ YEND2_PORT |= (value & 0x01)<<YEND2;
+}
+
+void YEND2_as_output(void){
+ YEND2_DDR |= 1<<YEND2;
+}
+
+void YEND2_as_input(void){
+ YEND2_DDR &= ~(1<<YEND2);
+}
+
+void YEND2_as_pullup(void){
+ YEND2_DDR &= ~(1<<YEND2);
+ YEND2_PORT |= 1<<YEND2;
+}
+
+uint8_t YEND2_state(void){
+ return (YEND2_PIN & (1<<YEND2))>>YEND2;
+}
+
+
--- /dev/null
+#!/bin/bash
+
+cat <<EOF
+#include <avr/io.h>
+#include <stdlib.h>
+#include "pins.h"
+
+
+EOF
+
+for i in $@
+do
+cat <<EOF
+// functions for pin $i
+
+void ${i}_set(uint8_t value){
+ ${i}_PORT &= ~(1<<$i);
+ ${i}_PORT |= (value & 0x01)<<$i;
+}
+
+void ${i}_as_output(void){
+ ${i}_DDR |= 1<<$i;
+}
+
+void ${i}_as_input(void){
+ ${i}_DDR &= ~(1<<$i);
+}
+
+void ${i}_as_pullup(void){
+ ${i}_DDR &= ~(1<<$i);
+ ${i}_PORT |= 1<<$i;
+}
+
+uint8_t ${i}_state(void){
+ return (${i}_PIN & (1<<$i))>>$i;
+}
+
+
+EOF
+done
-
-
-
#define XEND1 7
-#define DDRXEND1 DDRD
-#define PORTXEND1 PORTD
-#define PINXEND1 PIND
+#define XEND1_DDR DDRD
+#define XEND1_PORT PORTD
+#define XEND1_PIN PIND
#define XEND2 0
-#define DDRXEND2 DDRB
-#define PORTXEND2 PORTB
-#define PINXEND2 PINB
-
+#define XEND2_DDR DDRB
+#define XEND2_PORT PORTB
+#define XEND2_PIN PINB
#define YEND1 4
-#define DDRYEND1 DDRB
-#define PORTYEND1 PORTB
-#define PINYEND1 PINB
+#define YEND1_DDR DDRB
+#define YEND1_PORT PORTB
+#define YEND1_PIN PINB
#define YEND2 3
-#define DDRYEND2 DDRB
-#define PORTYEND2 PORTB
-#define PINYEND2 PINB
-
-
-
-
-
-#define SW0 4
-#define DDRSW0 DDRD
-#define PORTSW0 PORTD
-#define PINSW0 PIND
-
-#define SW1 5
-#define DDRSW1 DDRC
-#define PORTSW1 PORTC
-#define PINSW1 PINC
-
-#define SW2 4
-#define DDRSW2 DDRC
-#define PORTSW2 PORTC
-#define PINSW2 PINC
-
-
-
-
-#define LED0 1
-#define DDRLED0 DDRB
-#define PORTLED0 PORTB
-#define PINLED0 PINB
+#define YEND2_DDR DDRB
+#define YEND2_PORT PORTB
+#define YEND2_PIN PINB
-#define LED1 0
-#define DDRLED1 DDRB
-#define PORTLED1 PORTB
-#define PINLED1 PINB
-#define LED2 6
-#define DDRLED2 DDRD
-#define PORTLED2 PORTD
-#define PINLED2 PIND
+void XEND1_set(uint8_t value);
+void XEND1_as_output(void);
+void XEND1_as_input(void);
+void XEND1_as_pullup(void);
+uint8_t XEND1_state(void);
+void XEND2_set(uint8_t value);
+void XEND2_as_output(void);
+void XEND2_as_input(void);
+void XEND2_as_pullup(void);
+uint8_t XEND2_state(void);
+void YEND1_set(uint8_t value);
+void YEND1_as_output(void);
+void YEND1_as_input(void);
+void YEND1_as_pullup(void);
+uint8_t YEND1_state(void);
+void YEND2_set(uint8_t value);
+void YEND2_as_output(void);
+void YEND2_as_input(void);
+void YEND2_as_pullup(void);
+uint8_t YEND2_state(void);
\ No newline at end of file
--- /dev/null
+#!/bin/bash
+
+echo "# skeleton for pin definitions"
+for i in $@
+do
+cat <<EOF
+
+#define $i
+#define ${i}_DDR DDR
+#define ${i}_PORT PORT
+#define ${i}_PIN PIN
+
+EOF
+done
+
+for i in $@
+do
+cat <<EOF
+
+void ${i}_set(uint8_t value);
+void ${i}_as_output(void);
+void ${i}_as_input(void);
+void ${i}_as_pullup(void);
+uint8_t ${i}_state(void);
+
+EOF
+done
\ No newline at end of file