25,6 → 25,8 |
#include "INTERRUPTS.h" |
#include "STEPPER.h" |
#include "IOC.h" |
#include "SPI.h" |
#include "OLED_SSD1306.h" |
|
void Pins_Init(void) { |
// RA0 and RA1 pins as analog input |
40,9 → 42,6 |
STEP_TRIS = 0; |
STEP_LAT = 0; |
|
DIR_TRIS = 0; |
DIR_LAT = 0; |
|
M0_TRIS = 0; |
M0_LAT = 0; |
|
53,17 → 52,23 |
M2_LAT = 0; |
|
SW_1_TRIS = 1; |
SW_1_INLVL = 1; |
|
SW_2_TRIS = 1; |
SW_2_INLVL = 1; |
|
STEP_CURRENT_TRIS = 1; |
POT_CURRENT_TRIS = 1; |
|
SPI_MOSI_TRIS = 0; |
SPI_D_C_TRIS = 0; |
SPI_CLK_TRIS = 0; |
SPI_DC_SELECT_TRIS = 0; |
SPI_DC_SELECT_LAT = 0; |
SPI_RESET_TRIS = 0; |
SPI_RESET_LAT = 0; |
} |
|
OPERATING_MODE currMode; |
STEPPER_MICROSTEP currStep; |
|
int main(void) { |
// Set internal oscillator speed to 32MHz |
75,17 → 80,69 |
Pins_Init(); |
|
IOC_Init(); |
|
|
SPI_DATA spi_data; |
SPI_Init(&spi_data, SPI2_FOSC_16); |
|
SSD1306_DATA ssd1306_data; |
SSD1306_Init(&ssd1306_data); |
|
Interrupt_Init(); |
Interrupt_Enable(); |
|
currMode = SINGLE_STEP; |
currStep = STEP_1_1; |
|
|
SSD1306_Begin(SSD1306_SWITCHCAPVCC); |
|
while(1) { |
|
Update_OLED(); |
|
switch (STEPPER_Get_Auto()) { |
case DELAY_1000MS: |
STEPPER_Step(); |
__delay_ms(1000); |
break; |
case DELAY_500MS: |
STEPPER_Step(); |
__delay_ms(500); |
break; |
case DELAY_333MS: |
STEPPER_Step(); |
__delay_ms(333); |
break; |
case DELAY_250MS: |
STEPPER_Step(); |
__delay_ms(250); |
break; |
case DELAY_100MS: |
STEPPER_Step(); |
__delay_ms(100); |
break; |
case DELAY_50MS: |
STEPPER_Step(); |
__delay_ms(50); |
break; |
case DELAY_25MS: |
STEPPER_Step(); |
__delay_ms(25); |
break; |
case DELAY_10MS: |
STEPPER_Step(); |
__delay_ms(10); |
break; |
case DELAY_5MS: |
STEPPER_Step(); |
__delay_ms(5); |
break; |
case DELAY_1MS: |
STEPPER_Step(); |
__delay_ms(1); |
break; |
case DELAY_STOPPED: |
break; |
} |
} |
|
} |
|
void Set_Next_Mode() { |
94,6 → 151,9 |
currMode = AUTO_STEP; |
break; |
case AUTO_STEP: |
currMode = SET_DELAY; |
break; |
case SET_DELAY: |
currMode = SET_MICROSTEP; |
break; |
case SET_MICROSTEP: |
107,27 → 167,83 |
return currMode; |
} |
|
void Set_Next_Step() { |
switch (currStep) { |
case STEP_1_1: |
currStep = STEP_1_2; |
void Update_OLED(void) { |
SSD1306_Clear_Display(); |
SSD1306_Set_Text_Size(2); |
SSD1306_Set_Text_Wrap(0); |
switch (currMode) { |
case SINGLE_STEP: |
Draw_Manual_Text(1); |
Draw_Auto_Text(0); |
Draw_Step_Text(STEPPER_Get_Cur_Step(), 0); |
Draw_Speed_Text(STEPPER_Get_Cur_Speed(), 0); |
break; |
case STEP_1_2: |
currStep = STEP_1_4; |
case AUTO_STEP: |
Draw_Manual_Text(0); |
Draw_Auto_Text(1); |
Draw_Step_Text(STEPPER_Get_Cur_Step(), 0); |
Draw_Speed_Text(STEPPER_Get_Cur_Speed(), 0); |
break; |
case STEP_1_4: |
currStep = STEP_1_8; |
case SET_DELAY: |
Draw_Manual_Text(0); |
Draw_Auto_Text(0); |
Draw_Step_Text(STEPPER_Get_Cur_Step(), 1); |
Draw_Speed_Text(STEPPER_Get_Cur_Speed(), 0); |
break; |
case STEP_1_8: |
currStep = STEP_1_16; |
case SET_MICROSTEP: |
Draw_Manual_Text(0); |
Draw_Auto_Text(0); |
Draw_Step_Text(STEPPER_Get_Cur_Step(), 0); |
Draw_Speed_Text(STEPPER_Get_Cur_Speed(), 1); |
break; |
case STEP_1_16: |
currStep = STEP_1_32; |
break; |
case STEP_1_32: |
default: |
currStep = STEP_1_1; |
break; |
} |
STEPPER_Set_Microstep(currStep); |
SSD1306_Display(); |
} |
|
void Draw_Manual_Text(uint8_t selected) { |
uint8_t stringManual[] = "MANUAL"; |
if (selected) { |
SSD1306_Fill_Rect(0, 0, 75, 16, 1); |
SSD1306_Set_Text_Color(0); |
} else { |
SSD1306_Set_Text_Color(1); |
} |
SSD1306_Set_Cursor(3, 1); |
SSD1306_Write_String(stringManual, 6); |
} |
|
void Draw_Auto_Text(uint8_t selected) { |
uint8_t stringAuto[] = "AUTO"; |
if (selected) { |
SSD1306_Fill_Rect(76, 0, 53, 16, 1); |
SSD1306_Set_Text_Color(0); |
} else { |
SSD1306_Set_Text_Color(1); |
} |
SSD1306_Set_Cursor(79, 1); |
SSD1306_Write_String(stringAuto, 4); |
} |
|
void Draw_Speed_Text(STEPPER_SPEED speed, uint8_t selected) { |
uint8_t stringSpeed[] = "1000Hz"; |
if (selected) { |
SSD1306_Fill_Rect(53, 16, 76, 16, 1); |
SSD1306_Set_Text_Color(0); |
} else { |
SSD1306_Set_Text_Color(1); |
} |
SSD1306_Set_Cursor(55, 17); |
SSD1306_Write_String(stringSpeed, 6); |
} |
|
void Draw_Step_Text(STEPPER_MICROSTEP step, uint8_t selected) { |
uint8_t stringStepping[] = "1/32"; |
if (selected) { |
SSD1306_Fill_Rect(0, 16, 52, 16, 1); |
SSD1306_Set_Text_Color(0); |
} else { |
SSD1306_Set_Text_Color(1); |
} |
SSD1306_Set_Cursor(2, 17); |
SSD1306_Write_String(stringStepping, 4); |
} |