[add] init check

This commit is contained in:
stijn
2021-03-31 13:10:25 +02:00
parent 83863ec5f5
commit 03f4d72f16
24 changed files with 3237 additions and 84 deletions

View File

@@ -0,0 +1,113 @@
/*
* stepper_driver.c
* PORTB
* Created: 10-Mar-21 12:21:47 PM
* Author: lemms
*/
#define F_CPU 10e6
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#include <stdbool.h>
#include <stdio.h>
#include "stepper_driver.h"
#define BIT(x) (1 << (x))
uint8_t CCW[8] = {0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};
uint8_t CW[8] = {0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};
/*
Een timer met interupt routine.
in de interump routine een state machine voor de 8 nibbles
elke keer dat de interupt routine wordt geroepen wordt de state
incremented. hier houd je ook een var bij om de relatieve
positie te weten van de stappenmotor.
Nog een timer voor microstepping.
Timers:
timer voor de speed.
stappen van 100ms naar 10ms
prescaler = 255
*/
enum rotation_wise rotation;
void stepper_rotate_full_rotation_CW();
void stepper_rotate_full_rotation_CCW();
void set_stepper_state(uint8_t count){
if(rotation == ClockWise){
PORTE = CW[count];
} else {
PORTE = CCW[count];
}
}
void (*snap_event)(uint8_t);
void set_snap_event(void (*snap_event_p)(uint8_t)){
snap_event = snap_event_p;
}
uint16_t steps_each_turn = 0;
uint16_t steps_to_do = 0;
uint8_t stepper_state = 0;
ISR( TIMER2_COMP_vect ){
TCNT2 = 0;
set_stepper_state(stepper_state);
if(stepper_state < 7){
stepper_state++;
} else {
//OCR2 = ADCH;
stepper_state = 0;
if(steps_to_do == 0){
if(rotation == ClockWise){
rotation = CounterClockWise;
} else {
rotation = ClockWise;
}
steps_to_do = steps_each_turn;
} else {
if(steps_to_do % 32 == 0){
if(snap_event != NULL)
snap_event(steps_to_do);
}
steps_to_do--;
}
}
}
void stepper_rotate_angle(uint16_t steps, enum rotation_wise rot){
steps_to_do = steps;
steps_each_turn = steps;
rotation = rot;
TCCR2 = 0b00001100;
}
void stepper_rotate_stop(){
TCCR2 = 0b00000000;
}
void init_stepper_driver(){
DDRE = 0xff;
DDRG = 0xff;
PORTG = 0x01;
PORTE = 0x00;
OCR2 = 150;
TIMSK = BIT(7);
sei();
}