summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2013-09-23 00:32:20 +0000
committerChristian Pointner <equinox@spreadspace.org>2013-09-23 00:32:20 +0000
commite2cfa9a1399d81b83c7d00a9304df46df807320c (patch)
tree53061ea985731aad7bbe84ef8d642bfc4ba7b5ad
parentsome timing fixed for stepper table (diff)
limits work now with ADC value
moved ajar sensor dor PORTC7 git-svn-id: https://svn.spreadspace.org/avr/trunk@226 aa12f405-d877-488e-9caf-2d797e2a1cc7
-rw-r--r--tuer-rfid/ajar.c8
-rw-r--r--tuer-rfid/limits.c78
2 files changed, 29 insertions, 57 deletions
diff --git a/tuer-rfid/ajar.c b/tuer-rfid/ajar.c
index 18bf25b..06ca436 100644
--- a/tuer-rfid/ajar.c
+++ b/tuer-rfid/ajar.c
@@ -25,10 +25,10 @@
#include <stdio.h>
#include "ajar.h"
-#define AJAR_PIN PIND
-#define AJAR_PORT PORTD
-#define AJAR_DDR DDRD
-#define AJAR_BIT 4
+#define AJAR_PIN PINC
+#define AJAR_PORT PORTC
+#define AJAR_DDR DDRC
+#define AJAR_BIT 7
#define AJAR_LP_MAX 255
diff --git a/tuer-rfid/limits.c b/tuer-rfid/limits.c
index 027a3b5..6f567af 100644
--- a/tuer-rfid/limits.c
+++ b/tuer-rfid/limits.c
@@ -23,72 +23,44 @@
#include <avr/io.h>
#include "limits.h"
+#include <LUFA/Drivers/Peripheral/ADC.h>
-#define LIMITS_PIN PINC
-#define LIMITS_PORT PORTC
-#define LIMITS_DDR DDRC
-#define LIMITS_OPEN 6
-#define LIMITS_CLOSE 7
+#define LIMITS_ADC_CHAN_NUM 8
+#define LIMITS_ADC_CHAN ADC_CHANNEL8
-#define LIMITS_LP_MAX 255
+#define LIMITS_RINGBUF_SIZE 5
+/* HINT: this is compared to a sliding sum not an average! */
+#define LIMITS_TH_CLOSE 250
+#define LIMITS_TH_OPEN 1000
void limits_init(void)
{
- LIMITS_DDR = LIMITS_DDR & ~(1<<LIMITS_OPEN | 1<<LIMITS_CLOSE);
- LIMITS_PORT |= (1<<LIMITS_OPEN | 1<<LIMITS_CLOSE);
+ ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128);
+ ADC_SetupChannel(LIMITS_ADC_CHAN_NUM);
+ ADC_StartReading(ADC_REFERENCE_INT2560MV | ADC_LEFT_ADJUSTED | LIMITS_ADC_CHAN);
}
-uint8_t limits_get_close(uint8_t pin)
+limits_t limits_get(void)
{
- static uint8_t last_state = 1<<LIMITS_CLOSE;
- static uint8_t lp_cnt = 0;
-
- uint8_t state = pin & (1<<LIMITS_CLOSE);
- if(state != last_state)
- lp_cnt++;
- else
- lp_cnt += lp_cnt ? -1 : 0;
+ static uint8_t s = 0;
+ static uint8_t r[LIMITS_RINGBUF_SIZE] = { 0 };
+ static uint8_t idx = 0;
- if(lp_cnt >= LIMITS_LP_MAX) {
- last_state = state;
- lp_cnt = 0;
+ if(ADC_IsReadingComplete()) {
+ r[idx] = (ADC_GetResult()>>8);
+ idx = (idx + 1) % LIMITS_RINGBUF_SIZE;
+ s = 0;
+ uint8_t i;
+ for(i=0; i<LIMITS_RINGBUF_SIZE; ++i) s += r[i];
}
- return last_state;
-}
-
-uint8_t limits_get_open(uint8_t pin)
-{
- static uint8_t last_state = 1<<LIMITS_OPEN;
- static uint8_t lp_cnt = 0;
-
- uint8_t state = pin & (1<<LIMITS_OPEN);
- if(state != last_state)
- lp_cnt++;
- else
- lp_cnt += lp_cnt ? -1 : 0;
-
- if(lp_cnt >= LIMITS_LP_MAX) {
- last_state = state;
- lp_cnt = 0;
- }
-
- return last_state;
-}
-
-limits_t limits_get(void)
-{
- uint8_t tmp = LIMITS_PIN & (1<<LIMITS_OPEN | 1<<LIMITS_CLOSE);
- if(!limits_get_open(tmp)) {
- if(limits_get_close(tmp))
- return open;
- else
- return both;
- }
- else if(!limits_get_close(tmp))
+ if(s < LIMITS_TH_CLOSE)
return close;
- return moving;
+ if(s < LIMITS_TH_OPEN)
+ return moving;
+
+ return open;
}
const char* limits_to_string(limits_t limits)