summaryrefslogtreecommitdiff
path: root/tube-rotator/stepper.c
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2014-05-20 02:43:59 +0200
committerChristian Pointner <equinox@spreadspace.org>2014-05-20 02:43:59 +0200
commitfbc7b65a6ef8e43378bb03fe64bfe1b5b2735434 (patch)
treec197142db5c58a0f5c8fa81a1de8edfed734a564 /tube-rotator/stepper.c
parentspeed is actually reversed (diff)
added new speeds
make access to target_speed atomic
Diffstat (limited to 'tube-rotator/stepper.c')
-rw-r--r--tube-rotator/stepper.c28
1 files changed, 19 insertions, 9 deletions
diff --git a/tube-rotator/stepper.c b/tube-rotator/stepper.c
index 675be26..064db5a 100644
--- a/tube-rotator/stepper.c
+++ b/tube-rotator/stepper.c
@@ -22,6 +22,7 @@
#include <avr/sfr_defs.h>
#include <avr/interrupt.h>
+#include <util/atomic.h>
#include "stepper.h"
@@ -45,7 +46,7 @@ uint8_t step_table [] =
#define LENGTH_STEP_TABLE (sizeof(step_table)/sizeof(uint8_t))
#define STEPPER_OUTPUT_BITMASK (~(0xF << STEPPER_FIRST_BIT ))
-uint16_t target_speed;
+volatile uint16_t target_speed;
uint16_t current_speed;
void stepper_init(void)
@@ -84,10 +85,12 @@ static inline void stepper_handle(void)
step_idx++;
step_idx %= LENGTH_STEP_TABLE;
- if(current_speed > target_speed)
- OCR1A = --current_speed;
- else if(current_speed < target_speed)
- OCR1A = ++current_speed;
+ if(step_idx != 0 && step_idx % 8) {
+ if(current_speed > target_speed)
+ OCR1A = --current_speed;
+ else if(current_speed < target_speed)
+ OCR1A = ++current_speed;
+ }
}
ISR(TIMER1_COMPA_vect)
@@ -97,18 +100,25 @@ ISR(TIMER1_COMPA_vect)
void stepper_set_speed(uint16_t new_speed)
{
- if(new_speed <= STEPPER_SPEED_MIN && new_speed >= STEPPER_SPEED_MAX)
- target_speed = new_speed;
+ if(new_speed <= STEPPER_SPEED_MIN && new_speed >= STEPPER_SPEED_MAX) {
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ target_speed = new_speed;
+ }
+ }
}
void stepper_inc_speed(void)
{
- target_speed = (target_speed <= STEPPER_SPEED_MAX) ? STEPPER_SPEED_MAX : target_speed - 1;
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ target_speed = (target_speed <= STEPPER_SPEED_MAX) ? STEPPER_SPEED_MAX : target_speed - 1;
+ }
}
void stepper_dec_speed(void)
{
- target_speed = (target_speed >= STEPPER_SPEED_MIN) ? STEPPER_SPEED_MIN : target_speed + 1;
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ target_speed = (target_speed >= STEPPER_SPEED_MIN) ? STEPPER_SPEED_MIN : target_speed + 1;
+ }
}
uint16_t stepper_get_speed(void)