Subversion Repositories Code-Repo

Rev

Rev 315 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 315 Rev 316
Line 24... Line 24...
24
#include "defines.h"
24
#include "defines.h"
25
#include "INTERRUPTS.h"
25
#include "INTERRUPTS.h"
26
#include "STEPPER.h"
26
#include "STEPPER.h"
27
#include "IOC.h"
27
#include "IOC.h"
28
#include "SPI.h"
28
#include "SPI.h"
-
 
29
#include "ADC.h"
29
#include "OLED_SSD1306.h"
30
#include "OLED_SSD1306.h"
-
 
31
#include "TIMER.h"
-
 
32
#include <stdio.h>
30
 
33
 
31
void Pins_Init(void) {
34
void Pins_Init(void) {
32
    // RA0 and RA1 pins as analog input
35
    // RA0 and RA1 pins as analog input
33
    ANSELA = 0x3;
36
    ANSELA = 0x3;
34
    ANSELC = 0x0;
37
    ANSELC = 0x0;
Line 37... Line 40...
37
//    OPTION_REGbits.nWPUEN = 0;
40
//    OPTION_REGbits.nWPUEN = 0;
38
 
41
 
39
    // SDO1 on RC2
42
    // SDO1 on RC2
40
    APFCON0bits.SDOSEL = 0;
43
    APFCON0bits.SDOSEL = 0;
41
 
44
 
-
 
45
    // Stepper Driver
42
    STEP_TRIS = 0;
46
    STEP_TRIS = 0;
43
    STEP_LAT = 0;
47
    STEP_LAT = 0;
44
 
-
 
45
    M0_TRIS = 0;
48
    M0_TRIS = 0;
46
    M0_LAT = 0;
49
    M0_LAT = 0;
47
 
-
 
48
    M1_TRIS = 0;
50
    M1_TRIS = 0;
49
    M1_LAT = 0;
51
    M1_LAT = 0;
50
 
-
 
51
    M2_TRIS = 0;
52
    M2_TRIS = 0;
52
    M2_LAT = 0;
53
    M2_LAT = 0;
53
 
54
 
-
 
55
    // I/O
54
    SW_1_TRIS = 1;
56
    SW_1_TRIS = 1;
55
    SW_1_INLVL = 1;
57
    SW_1_INLVL = 1;
56
 
-
 
57
    SW_2_TRIS = 1;
58
    SW_2_TRIS = 1;
58
    SW_2_INLVL = 1;
59
    SW_2_INLVL = 1;
59
 
60
 
60
    STEP_CURRENT_TRIS = 1;
61
    STEP_CURRENT_TRIS = 1;
61
    POT_CURRENT_TRIS = 1;
62
    POT_CURRENT_TRIS = 1;
62
 
63
 
-
 
64
    // SPI
63
    SPI_MOSI_TRIS = 0;
65
    SPI_MOSI_TRIS = 0;
64
    SPI_CLK_TRIS = 0;
66
    SPI_CLK_TRIS = 0;
65
    SPI_DC_SELECT_TRIS = 0;
67
    SPI_DC_SELECT_TRIS = 0;
66
    SPI_DC_SELECT_LAT = 0;
68
    SPI_DC_SELECT_LAT = 0;
67
    SPI_RESET_TRIS = 0;
69
    SPI_RESET_TRIS = 0;
Line 79... Line 81...
79
    // Initialize I/O
81
    // Initialize I/O
80
    Pins_Init();
82
    Pins_Init();
81
 
83
 
82
    IOC_Init();
84
    IOC_Init();
83
 
85
 
-
 
86
    ADC_Init();
-
 
87
 
-
 
88
    TIMER_DATA timer_data;
-
 
89
    TIMER_Init(&timer_data);
-
 
90
    
84
    SPI_DATA spi_data;
91
    SPI_DATA spi_data;
85
    SPI_Init(&spi_data, SPI2_FOSC_16);
92
    SPI_Init(&spi_data, SPI2_FOSC_16);
86
 
93
 
87
    SSD1306_DATA ssd1306_data;
94
    SSD1306_DATA ssd1306_data;
88
    SSD1306_Init(&ssd1306_data);
95
    SSD1306_Init(&ssd1306_data);
Line 93... Line 100...
93
    currMode = SINGLE_STEP;
100
    currMode = SINGLE_STEP;
94
 
101
 
95
    SSD1306_Begin(SSD1306_SWITCHCAPVCC);
102
    SSD1306_Begin(SSD1306_SWITCHCAPVCC);
96
 
103
 
97
    while(1) {
104
    while(1) {
98
 
-
 
99
        Update_OLED();
105
        Update_OLED();
100
        
-
 
101
        switch (STEPPER_Get_Auto()) {
-
 
102
            case DELAY_1000MS:
-
 
103
                STEPPER_Step();
-
 
104
                __delay_ms(1000);
-
 
105
                break;
-
 
106
            case DELAY_500MS:
-
 
107
                STEPPER_Step();
-
 
108
                __delay_ms(500);
-
 
109
                break;
-
 
110
            case DELAY_333MS:
-
 
111
                STEPPER_Step();
-
 
112
                __delay_ms(333);
-
 
113
                break;
-
 
114
            case DELAY_250MS:
-
 
115
                STEPPER_Step();
-
 
116
                __delay_ms(250);
-
 
117
                break;
-
 
118
            case DELAY_100MS:
-
 
119
                STEPPER_Step();
-
 
120
                __delay_ms(100);
-
 
121
                break;
-
 
122
            case DELAY_50MS:
-
 
123
                STEPPER_Step();
-
 
124
                __delay_ms(50);
-
 
125
                break;
-
 
126
            case DELAY_25MS:
-
 
127
                STEPPER_Step();
-
 
128
                __delay_ms(25);
-
 
129
                break;
-
 
130
            case DELAY_10MS:
-
 
131
                STEPPER_Step();
-
 
132
                __delay_ms(10);
-
 
133
                break;
-
 
134
            case DELAY_5MS:
-
 
135
                STEPPER_Step();
-
 
136
                __delay_ms(5);
-
 
137
                break;
-
 
138
            case DELAY_1MS:
-
 
139
                STEPPER_Step();
-
 
140
                __delay_ms(1);
106
//        __delay_ms(1);
141
                break;
-
 
142
            case DELAY_STOPPED:
-
 
143
                break;
-
 
144
        }
-
 
145
    }
107
    }
146
}
108
}
147
 
109
 
148
void Set_Next_Mode() {
110
void Set_Next_Mode() {
149
    switch (currMode) {
111
    switch (currMode) {
150
        case SINGLE_STEP:
112
        case SINGLE_STEP:
151
            currMode = AUTO_STEP;
113
            currMode = AUTO_STEP;
152
            break;
114
            break;
153
        case AUTO_STEP:
115
        case AUTO_STEP:
154
            currMode = SET_DELAY;
-
 
155
            break;
-
 
156
        case SET_DELAY:
-
 
157
            currMode = SET_MICROSTEP;
116
            currMode = SET_MICROSTEP;
158
            break;
117
            break;
159
        case SET_MICROSTEP:
118
        case SET_MICROSTEP:
160
        default:
119
        default:
161
            currMode = SINGLE_STEP;
120
            currMode = SINGLE_STEP;
Line 174... Line 133...
174
    switch (currMode) {
133
    switch (currMode) {
175
        case SINGLE_STEP:
134
        case SINGLE_STEP:
176
            Draw_Manual_Text(1);
135
            Draw_Manual_Text(1);
177
            Draw_Auto_Text(0);
136
            Draw_Auto_Text(0);
178
            Draw_Step_Text(STEPPER_Get_Cur_Step(), 0);
137
            Draw_Step_Text(STEPPER_Get_Cur_Step(), 0);
179
            Draw_Speed_Text(STEPPER_Get_Cur_Speed(), 0);
-
 
180
            break;
138
            break;
181
        case AUTO_STEP:
139
        case AUTO_STEP:
182
            Draw_Manual_Text(0);
140
            Draw_Manual_Text(0);
183
            Draw_Auto_Text(1);
141
            Draw_Auto_Text(1);
184
            Draw_Step_Text(STEPPER_Get_Cur_Step(), 0);
142
            Draw_Step_Text(STEPPER_Get_Cur_Step(), 0);
185
            Draw_Speed_Text(STEPPER_Get_Cur_Speed(), 0);
-
 
186
            break;
-
 
187
        case SET_DELAY:
-
 
188
            Draw_Manual_Text(0);
-
 
189
            Draw_Auto_Text(0);
-
 
190
            Draw_Step_Text(STEPPER_Get_Cur_Step(), 1);
-
 
191
            Draw_Speed_Text(STEPPER_Get_Cur_Speed(), 0);
-
 
192
            break;
143
            break;
193
        case SET_MICROSTEP:
144
        case SET_MICROSTEP:
194
            Draw_Manual_Text(0);
145
            Draw_Manual_Text(0);
195
            Draw_Auto_Text(0);
146
            Draw_Auto_Text(0);
196
            Draw_Step_Text(STEPPER_Get_Cur_Step(), 0);
147
            Draw_Step_Text(STEPPER_Get_Cur_Step(), 1);
197
            Draw_Speed_Text(STEPPER_Get_Cur_Speed(), 1);
-
 
198
            break;
148
            break;
199
    }
149
    }
-
 
150
    Draw_Stepper_Current();
-
 
151
    Draw_Pot_Value();
200
    SSD1306_Display();
152
    SSD1306_Display();
201
}
153
}
202
 
154
 
203
void Draw_Manual_Text(uint8_t selected) {
155
void Draw_Manual_Text(uint8_t selected) {
-
 
156
    // Draw and/or highlight the stepping mode in the top left corner
204
    uint8_t stringManual[] = "MANUAL";
157
    uint8_t stringManual[] = "MANUAL";
205
    if (selected) {
158
    if (selected) {
206
        SSD1306_Fill_Rect(0, 0, 75, 16, 1);
159
        SSD1306_Fill_Rect(0, 0, 75, 16, 1);
207
        SSD1306_Set_Text_Color(0);
160
        SSD1306_Set_Text_Color(0);
208
    } else {
161
    } else {
Line 211... Line 164...
211
    SSD1306_Set_Cursor(3, 1);
164
    SSD1306_Set_Cursor(3, 1);
212
    SSD1306_Write_String(stringManual, 6);
165
    SSD1306_Write_String(stringManual, 6);
213
}
166
}
214
 
167
 
215
void Draw_Auto_Text(uint8_t selected) {
168
void Draw_Auto_Text(uint8_t selected) {
-
 
169
    // Draw and/or highlight the stepping mode in the top right corner
216
    uint8_t stringAuto[] = "AUTO";
170
    uint8_t stringAuto[] = "AUTO";
217
    if (selected) {
171
    if (selected) {
218
        SSD1306_Fill_Rect(76, 0, 53, 16, 1);
172
        SSD1306_Fill_Rect(76, 0, 53, 16, 1);
219
        SSD1306_Set_Text_Color(0);
173
        SSD1306_Set_Text_Color(0);
220
    } else {
174
    } else {
Line 222... Line 176...
222
    }
176
    }
223
    SSD1306_Set_Cursor(79, 1);
177
    SSD1306_Set_Cursor(79, 1);
224
    SSD1306_Write_String(stringAuto, 4);
178
    SSD1306_Write_String(stringAuto, 4);
225
}
179
}
226
 
180
 
-
 
181
void Draw_Stepper_Current() {
227
void Draw_Speed_Text(STEPPER_SPEED speed, uint8_t selected) {
182
    // Draw the stepper motor current draw in the bottom right corner
228
    uint8_t stringSpeed[] = "1000Hz";
183
    uint8_t buffer[6] = {0};
-
 
184
    uint16_t potVoltage = ADC_Read(STEP_ADC_CHANNEL);
229
    if (selected) {
185
    // Compute current from voltage reading (2x)
230
        SSD1306_Fill_Rect(53, 16, 76, 16, 1);
186
    uint16_t current = potVoltage * 0.3222 * 2;
231
        SSD1306_Set_Text_Color(0);
187
    uint8_t preDecimal = current / 100;
232
    } else {
188
    uint8_t postDecimal = current % 100;
-
 
189
    sprintf(buffer, "%1d.%02dA", preDecimal, postDecimal);
233
        SSD1306_Set_Text_Color(1);
190
    SSD1306_Set_Text_Color(1);
234
    }
-
 
235
    SSD1306_Set_Cursor(55, 17);
191
    SSD1306_Set_Cursor(67, 17);
236
    SSD1306_Write_String(stringSpeed, 6);
192
    SSD1306_Write_String(buffer, 5);
-
 
193
}
-
 
194
 
-
 
195
void Draw_Pot_Value() {
-
 
196
    // Draw a line indicating auto rotation delay
-
 
197
    uint8_t potVoltage = ADC_Read(POT_ADC_CHANNEL) >> 4;
-
 
198
    SSD1306_Draw_Line(127, 31, 127 - potVoltage, 31, 1);
237
}
199
}
238
 
200
 
239
void Draw_Step_Text(STEPPER_MICROSTEP step, uint8_t selected) {
201
void Draw_Step_Text(STEPPER_MICROSTEP step, uint8_t selected) {
-
 
202
    // Draw and/or highlight the stepping in the bottom left corner
240
    uint8_t stringStepping[] = "1/32";
203
    uint8_t stringStepping32[] = "1/32";
-
 
204
    uint8_t stringStepping16[] = "1/16";
-
 
205
    uint8_t stringStepping8[] = "1/8";
-
 
206
    uint8_t stringStepping4[] = "1/4";
-
 
207
    uint8_t stringStepping2[] = "1/2";
-
 
208
    uint8_t stringStepping1[] = "1/1";
241
    if (selected) {
209
    if (selected) {
242
        SSD1306_Fill_Rect(0, 16, 52, 16, 1);
210
        SSD1306_Fill_Rect(0, 16, 52, 16, 1);
243
        SSD1306_Set_Text_Color(0);
211
        SSD1306_Set_Text_Color(0);
244
    } else {
212
    } else {
245
        SSD1306_Set_Text_Color(1);
213
        SSD1306_Set_Text_Color(1);
246
    }
214
    }
-
 
215
    if (step == STEP_1_32) {
247
    SSD1306_Set_Cursor(2, 17);
216
        SSD1306_Set_Cursor(2, 17);
-
 
217
        SSD1306_Write_String(stringStepping32, 4);
-
 
218
    } else if (step == STEP_1_16) {
-
 
219
        SSD1306_Set_Cursor(2, 17);
248
    SSD1306_Write_String(stringStepping, 4);
220
        SSD1306_Write_String(stringStepping16, 4);
-
 
221
    } else if (step == STEP_1_8) {
-
 
222
        SSD1306_Set_Cursor(8, 17);
-
 
223
        SSD1306_Write_String(stringStepping8, 3);
-
 
224
    } else if (step == STEP_1_4) {
-
 
225
        SSD1306_Set_Cursor(8, 17);
-
 
226
        SSD1306_Write_String(stringStepping4, 3);
-
 
227
    } else if (step == STEP_1_2) {
-
 
228
        SSD1306_Set_Cursor(8, 17);
-
 
229
        SSD1306_Write_String(stringStepping2, 3);
-
 
230
    } else if (step == STEP_1_1) {
-
 
231
        SSD1306_Set_Cursor(8, 17);
-
 
232
        SSD1306_Write_String(stringStepping1, 3);
-
 
233
    }
249
}
234
}
250
235