Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
312 Kevin 1
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
2
// PIC16F1825 Configuration Bit Settings
3
 
4
// CONFIG1
5
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
6
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT enabled)
7
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
8
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
9
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
10
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
11
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
12
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
13
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
14
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
15
 
16
// CONFIG2
17
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
18
#pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
19
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
20
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
21
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
22
// </editor-fold>
23
 
24
#include "defines.h"
25
#include "INTERRUPTS.h"
26
#include "STEPPER.h"
27
#include "IOC.h"
315 Kevin 28
#include "SPI.h"
29
#include "OLED_SSD1306.h"
312 Kevin 30
 
31
void Pins_Init(void) {
32
    // RA0 and RA1 pins as analog input
33
    ANSELA = 0x3;
34
    ANSELC = 0x0;
35
 
36
//    // Enable weak pull-up if WPU bit is set
37
//    OPTION_REGbits.nWPUEN = 0;
38
 
39
    // SDO1 on RC2
40
    APFCON0bits.SDOSEL = 0;
41
 
42
    STEP_TRIS = 0;
43
    STEP_LAT = 0;
44
 
45
    M0_TRIS = 0;
46
    M0_LAT = 0;
47
 
48
    M1_TRIS = 0;
49
    M1_LAT = 0;
50
 
51
    M2_TRIS = 0;
52
    M2_LAT = 0;
53
 
54
    SW_1_TRIS = 1;
315 Kevin 55
    SW_1_INLVL = 1;
56
 
312 Kevin 57
    SW_2_TRIS = 1;
315 Kevin 58
    SW_2_INLVL = 1;
59
 
312 Kevin 60
    STEP_CURRENT_TRIS = 1;
61
    POT_CURRENT_TRIS = 1;
62
 
63
    SPI_MOSI_TRIS = 0;
64
    SPI_CLK_TRIS = 0;
315 Kevin 65
    SPI_DC_SELECT_TRIS = 0;
66
    SPI_DC_SELECT_LAT = 0;
67
    SPI_RESET_TRIS = 0;
68
    SPI_RESET_LAT = 0;
312 Kevin 69
}
70
 
71
OPERATING_MODE currMode;
72
 
73
int main(void) {
74
    // Set internal oscillator speed to 32MHz
75
    OSCCONbits.SPLLEN = 1;  // 4x PLL enable (overwritten by config bits)
76
    OSCCONbits.IRCF = 0xE;  // Base frequency @ 8MHz
77
    OSCCONbits.SCS = 0b00;  // System clock determined by config bits
78
 
79
    // Initialize I/O
80
    Pins_Init();
81
 
82
    IOC_Init();
315 Kevin 83
 
84
    SPI_DATA spi_data;
85
    SPI_Init(&spi_data, SPI2_FOSC_16);
86
 
87
    SSD1306_DATA ssd1306_data;
88
    SSD1306_Init(&ssd1306_data);
89
 
312 Kevin 90
    Interrupt_Init();
91
    Interrupt_Enable();
92
 
93
    currMode = SINGLE_STEP;
315 Kevin 94
 
95
    SSD1306_Begin(SSD1306_SWITCHCAPVCC);
96
 
312 Kevin 97
    while(1) {
315 Kevin 98
 
99
        Update_OLED();
312 Kevin 100
 
315 Kevin 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);
141
                break;
142
            case DELAY_STOPPED:
143
                break;
144
        }
312 Kevin 145
    }
146
}
147
 
148
void Set_Next_Mode() {
149
    switch (currMode) {
150
        case SINGLE_STEP:
151
            currMode = AUTO_STEP;
152
            break;
153
        case AUTO_STEP:
315 Kevin 154
            currMode = SET_DELAY;
155
            break;
156
        case SET_DELAY:
312 Kevin 157
            currMode = SET_MICROSTEP;
158
            break;
159
        case SET_MICROSTEP:
160
        default:
161
            currMode = SINGLE_STEP;
162
            break;
163
    }
164
}
165
 
166
OPERATING_MODE Get_Cur_Mode(void) {
167
    return currMode;
168
}
169
 
315 Kevin 170
void Update_OLED(void) {
171
    SSD1306_Clear_Display();
172
    SSD1306_Set_Text_Size(2);
173
    SSD1306_Set_Text_Wrap(0);
174
    switch (currMode) {
175
        case SINGLE_STEP:
176
            Draw_Manual_Text(1);
177
            Draw_Auto_Text(0);
178
            Draw_Step_Text(STEPPER_Get_Cur_Step(), 0);
179
            Draw_Speed_Text(STEPPER_Get_Cur_Speed(), 0);
312 Kevin 180
            break;
315 Kevin 181
        case AUTO_STEP:
182
            Draw_Manual_Text(0);
183
            Draw_Auto_Text(1);
184
            Draw_Step_Text(STEPPER_Get_Cur_Step(), 0);
185
            Draw_Speed_Text(STEPPER_Get_Cur_Speed(), 0);
312 Kevin 186
            break;
315 Kevin 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);
312 Kevin 192
            break;
315 Kevin 193
        case SET_MICROSTEP:
194
            Draw_Manual_Text(0);
195
            Draw_Auto_Text(0);
196
            Draw_Step_Text(STEPPER_Get_Cur_Step(), 0);
197
            Draw_Speed_Text(STEPPER_Get_Cur_Speed(), 1);
312 Kevin 198
            break;
199
    }
315 Kevin 200
    SSD1306_Display();
201
}
202
 
203
void Draw_Manual_Text(uint8_t selected) {
204
    uint8_t stringManual[] = "MANUAL";
205
    if (selected) {
206
        SSD1306_Fill_Rect(0, 0, 75, 16, 1);
207
        SSD1306_Set_Text_Color(0);
208
    } else {
209
        SSD1306_Set_Text_Color(1);
210
    }
211
    SSD1306_Set_Cursor(3, 1);
212
    SSD1306_Write_String(stringManual, 6);
213
}
214
 
215
void Draw_Auto_Text(uint8_t selected) {
216
    uint8_t stringAuto[] = "AUTO";
217
    if (selected) {
218
        SSD1306_Fill_Rect(76, 0, 53, 16, 1);
219
        SSD1306_Set_Text_Color(0);
220
    } else {
221
        SSD1306_Set_Text_Color(1);
222
    }
223
    SSD1306_Set_Cursor(79, 1);
224
    SSD1306_Write_String(stringAuto, 4);
225
}
226
 
227
void Draw_Speed_Text(STEPPER_SPEED speed, uint8_t selected) {
228
    uint8_t stringSpeed[] = "1000Hz";
229
    if (selected) {
230
        SSD1306_Fill_Rect(53, 16, 76, 16, 1);
231
        SSD1306_Set_Text_Color(0);
232
    } else {
233
        SSD1306_Set_Text_Color(1);
234
    }
235
    SSD1306_Set_Cursor(55, 17);
236
    SSD1306_Write_String(stringSpeed, 6);
237
}
238
 
239
void Draw_Step_Text(STEPPER_MICROSTEP step, uint8_t selected) {
240
    uint8_t stringStepping[] = "1/32";
241
    if (selected) {
242
        SSD1306_Fill_Rect(0, 16, 52, 16, 1);
243
        SSD1306_Set_Text_Color(0);
244
    } else {
245
        SSD1306_Set_Text_Color(1);
246
    }
247
    SSD1306_Set_Cursor(2, 17);
248
    SSD1306_Write_String(stringStepping, 4);
312 Kevin 249
}