Subversion Repositories Code-Repo

Rev

Rev 316 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
312 Kevin 1
#include "defines.h"
2
#include "STEPPER.h"
315 Kevin 3
#include "OLED_SSD1306.h"
316 Kevin 4
#include "ADC.h"
5
#include "TIMER.h"
312 Kevin 6
 
315 Kevin 7
static STEPPER_MICROSTEP currStep = STEP_1_1;
8
static uint8_t autoOn = 0;
9
 
312 Kevin 10
void STEPPER_Set_Microstep(STEPPER_MICROSTEP step) {
11
#ifdef DVR8825
12
    if (step == STEP_1_1) {
13
        M0_LAT = 0;
14
        M1_LAT = 0;
15
        M2_LAT = 0;
16
    } else if (step == STEP_1_2) {
17
        M0_LAT = 1;
18
        M1_LAT = 0;
19
        M2_LAT = 0;
20
    } else if (step == STEP_1_4) {
21
        M0_LAT = 0;
22
        M1_LAT = 1;
23
        M2_LAT = 0;
24
    } else if (step == STEP_1_8) {
25
        M0_LAT = 1;
26
        M1_LAT = 1;
27
        M2_LAT = 0;
28
    } else if (step == STEP_1_16) {
29
        M0_LAT = 0;
30
        M1_LAT = 0;
31
        M2_LAT = 1;
32
    } else if (step == STEP_1_32) {
33
        M0_LAT = 1;
34
        M1_LAT = 0;
35
        M2_LAT = 1;
36
    }
37
#endif
38
}
39
 
315 Kevin 40
void STEPPER_Set_Next_Step() {
41
    switch (currStep) {
42
        case STEP_1_1:
43
            currStep = STEP_1_2;
44
            break;
45
        case STEP_1_2:
46
            currStep = STEP_1_4;
47
            break;
48
        case STEP_1_4:
49
            currStep = STEP_1_8;
50
            break;
51
        case STEP_1_8:
52
            currStep = STEP_1_16;
53
            break;
54
        case STEP_1_16:
55
            currStep = STEP_1_32;
56
            break;
57
        case STEP_1_32:
58
        default:
59
            currStep = STEP_1_1;
60
            break;
61
    }
62
    STEPPER_Set_Microstep(currStep);
63
}
64
 
65
STEPPER_MICROSTEP STEPPER_Get_Cur_Step(void) {
66
    return currStep;
67
}
68
 
69
void STEPPER_Toggle_Auto() {
70
    if (autoOn == 0) {
316 Kevin 71
        // Turn on automatic stepping
72
        TIMER_2_Init(STEPPER_Step);
73
        TIMER_2_Set_Delay((ADC_Read(POT_ADC_CHANNEL) >> 4) + 1);
74
        TIMER_2_Start();
315 Kevin 75
        autoOn = 1;
312 Kevin 76
    } else {
316 Kevin 77
        // Turn off automatic stepping
78
        TIMER_2_Stop();
315 Kevin 79
        autoOn = 0;
312 Kevin 80
    }
315 Kevin 81
}
82
 
83
void STEPPER_Step() {
316 Kevin 84
    // Toggle step pin
315 Kevin 85
    STEP_LAT = 1;
86
    __delay_us(2);
87
    STEP_LAT = 0;
316 Kevin 88
    // Set the timer delay for the next step
89
    TIMER_2_Set_Delay((ADC_Read(POT_ADC_CHANNEL) >> 4) + 1);
315 Kevin 90
}