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"
316 Kevin 29
#include "ADC.h"
315 Kevin 30
#include "OLED_SSD1306.h"
316 Kevin 31
#include "TIMER.h"
32
#include <stdio.h>
312 Kevin 33
 
34
void Pins_Init(void) {
35
    // RA0 and RA1 pins as analog input
36
    ANSELA = 0x3;
37
    ANSELC = 0x0;
38
 
39
//    // Enable weak pull-up if WPU bit is set
40
//    OPTION_REGbits.nWPUEN = 0;
41
 
42
    // SDO1 on RC2
43
    APFCON0bits.SDOSEL = 0;
44
 
316 Kevin 45
    // Stepper Driver
312 Kevin 46
    STEP_TRIS = 0;
47
    STEP_LAT = 0;
48
    M0_TRIS = 0;
49
    M0_LAT = 0;
50
    M1_TRIS = 0;
51
    M1_LAT = 0;
52
    M2_TRIS = 0;
53
    M2_LAT = 0;
54
 
316 Kevin 55
    // I/O
312 Kevin 56
    SW_1_TRIS = 1;
315 Kevin 57
    SW_1_INLVL = 1;
312 Kevin 58
    SW_2_TRIS = 1;
315 Kevin 59
    SW_2_INLVL = 1;
60
 
312 Kevin 61
    STEP_CURRENT_TRIS = 1;
62
    POT_CURRENT_TRIS = 1;
63
 
316 Kevin 64
    // SPI
312 Kevin 65
    SPI_MOSI_TRIS = 0;
66
    SPI_CLK_TRIS = 0;
315 Kevin 67
    SPI_DC_SELECT_TRIS = 0;
68
    SPI_DC_SELECT_LAT = 0;
69
    SPI_RESET_TRIS = 0;
70
    SPI_RESET_LAT = 0;
312 Kevin 71
}
72
 
73
OPERATING_MODE currMode;
74
 
75
int main(void) {
76
    // Set internal oscillator speed to 32MHz
77
    OSCCONbits.SPLLEN = 1;  // 4x PLL enable (overwritten by config bits)
78
    OSCCONbits.IRCF = 0xE;  // Base frequency @ 8MHz
79
    OSCCONbits.SCS = 0b00;  // System clock determined by config bits
80
 
81
    // Initialize I/O
82
    Pins_Init();
83
 
84
    IOC_Init();
315 Kevin 85
 
316 Kevin 86
    ADC_Init();
87
 
88
    TIMER_DATA timer_data;
89
    TIMER_Init(&timer_data);
90
 
315 Kevin 91
    SPI_DATA spi_data;
92
    SPI_Init(&spi_data, SPI2_FOSC_16);
93
 
94
    SSD1306_DATA ssd1306_data;
95
    SSD1306_Init(&ssd1306_data);
96
 
312 Kevin 97
    Interrupt_Init();
98
    Interrupt_Enable();
99
 
100
    currMode = SINGLE_STEP;
315 Kevin 101
 
102
    SSD1306_Begin(SSD1306_SWITCHCAPVCC);
103
 
312 Kevin 104
    while(1) {
315 Kevin 105
        Update_OLED();
316 Kevin 106
//        __delay_ms(1);
312 Kevin 107
    }
108
}
109
 
110
void Set_Next_Mode() {
111
    switch (currMode) {
112
        case SINGLE_STEP:
113
            currMode = AUTO_STEP;
114
            break;
115
        case AUTO_STEP:
116
            currMode = SET_MICROSTEP;
117
            break;
118
        case SET_MICROSTEP:
119
        default:
120
            currMode = SINGLE_STEP;
121
            break;
122
    }
123
}
124
 
125
OPERATING_MODE Get_Cur_Mode(void) {
126
    return currMode;
127
}
128
 
315 Kevin 129
void Update_OLED(void) {
130
    SSD1306_Clear_Display();
131
    SSD1306_Set_Text_Size(2);
132
    SSD1306_Set_Text_Wrap(0);
133
    switch (currMode) {
134
        case SINGLE_STEP:
135
            Draw_Manual_Text(1);
136
            Draw_Auto_Text(0);
137
            Draw_Step_Text(STEPPER_Get_Cur_Step(), 0);
312 Kevin 138
            break;
315 Kevin 139
        case AUTO_STEP:
140
            Draw_Manual_Text(0);
141
            Draw_Auto_Text(1);
142
            Draw_Step_Text(STEPPER_Get_Cur_Step(), 0);
312 Kevin 143
            break;
316 Kevin 144
        case SET_MICROSTEP:
315 Kevin 145
            Draw_Manual_Text(0);
146
            Draw_Auto_Text(0);
147
            Draw_Step_Text(STEPPER_Get_Cur_Step(), 1);
312 Kevin 148
            break;
149
    }
316 Kevin 150
    Draw_Stepper_Current();
151
    Draw_Pot_Value();
315 Kevin 152
    SSD1306_Display();
153
}
154
 
155
void Draw_Manual_Text(uint8_t selected) {
316 Kevin 156
    // Draw and/or highlight the stepping mode in the top left corner
315 Kevin 157
    uint8_t stringManual[] = "MANUAL";
158
    if (selected) {
159
        SSD1306_Fill_Rect(0, 0, 75, 16, 1);
160
        SSD1306_Set_Text_Color(0);
161
    } else {
162
        SSD1306_Set_Text_Color(1);
163
    }
164
    SSD1306_Set_Cursor(3, 1);
165
    SSD1306_Write_String(stringManual, 6);
166
}
167
 
168
void Draw_Auto_Text(uint8_t selected) {
316 Kevin 169
    // Draw and/or highlight the stepping mode in the top right corner
315 Kevin 170
    uint8_t stringAuto[] = "AUTO";
171
    if (selected) {
172
        SSD1306_Fill_Rect(76, 0, 53, 16, 1);
173
        SSD1306_Set_Text_Color(0);
174
    } else {
175
        SSD1306_Set_Text_Color(1);
176
    }
177
    SSD1306_Set_Cursor(79, 1);
178
    SSD1306_Write_String(stringAuto, 4);
179
}
180
 
316 Kevin 181
void Draw_Stepper_Current() {
182
    // Draw the stepper motor current draw in the bottom right corner
183
    uint8_t buffer[6] = {0};
184
    uint16_t potVoltage = ADC_Read(STEP_ADC_CHANNEL);
185
    // Compute current from voltage reading (2x)
186
    uint16_t current = potVoltage * 0.3222 * 2;
187
    uint8_t preDecimal = current / 100;
188
    uint8_t postDecimal = current % 100;
189
    sprintf(buffer, "%1d.%02dA", preDecimal, postDecimal);
190
    SSD1306_Set_Text_Color(1);
191
    SSD1306_Set_Cursor(67, 17);
192
    SSD1306_Write_String(buffer, 5);
315 Kevin 193
}
194
 
316 Kevin 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);
199
}
200
 
315 Kevin 201
void Draw_Step_Text(STEPPER_MICROSTEP step, uint8_t selected) {
316 Kevin 202
    // Draw and/or highlight the stepping in the bottom left corner
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";
315 Kevin 209
    if (selected) {
210
        SSD1306_Fill_Rect(0, 16, 52, 16, 1);
211
        SSD1306_Set_Text_Color(0);
212
    } else {
213
        SSD1306_Set_Text_Color(1);
214
    }
316 Kevin 215
    if (step == STEP_1_32) {
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);
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
    }
312 Kevin 234
}