Subversion Repositories Code-Repo

Compare Revisions

Ignore whitespace Rev 315 → Rev 316

/PIC Stuff/PICX_16F1825_Stepper_Driver/main.c
26,7 → 26,10
#include "STEPPER.h"
#include "IOC.h"
#include "SPI.h"
#include "ADC.h"
#include "OLED_SSD1306.h"
#include "TIMER.h"
#include <stdio.h>
 
void Pins_Init(void) {
// RA0 and RA1 pins as analog input
39,21 → 42,19
// SDO1 on RC2
APFCON0bits.SDOSEL = 0;
 
// Stepper Driver
STEP_TRIS = 0;
STEP_LAT = 0;
 
M0_TRIS = 0;
M0_LAT = 0;
 
M1_TRIS = 0;
M1_LAT = 0;
 
M2_TRIS = 0;
M2_LAT = 0;
 
// I/O
SW_1_TRIS = 1;
SW_1_INLVL = 1;
 
SW_2_TRIS = 1;
SW_2_INLVL = 1;
 
60,6 → 61,7
STEP_CURRENT_TRIS = 1;
POT_CURRENT_TRIS = 1;
 
// SPI
SPI_MOSI_TRIS = 0;
SPI_CLK_TRIS = 0;
SPI_DC_SELECT_TRIS = 0;
81,6 → 83,11
 
IOC_Init();
 
ADC_Init();
 
TIMER_DATA timer_data;
TIMER_Init(&timer_data);
SPI_DATA spi_data;
SPI_Init(&spi_data, SPI2_FOSC_16);
 
95,53 → 102,8
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;
}
// __delay_ms(1);
}
}
 
151,9 → 113,6
currMode = AUTO_STEP;
break;
case AUTO_STEP:
currMode = SET_DELAY;
break;
case SET_DELAY:
currMode = SET_MICROSTEP;
break;
case SET_MICROSTEP:
176,31 → 135,25
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 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 SET_DELAY:
case SET_MICROSTEP:
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 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;
}
Draw_Stepper_Current();
Draw_Pot_Value();
SSD1306_Display();
}
 
void Draw_Manual_Text(uint8_t selected) {
// Draw and/or highlight the stepping mode in the top left corner
uint8_t stringManual[] = "MANUAL";
if (selected) {
SSD1306_Fill_Rect(0, 0, 75, 16, 1);
213,6 → 166,7
}
 
void Draw_Auto_Text(uint8_t selected) {
// Draw and/or highlight the stepping mode in the top right corner
uint8_t stringAuto[] = "AUTO";
if (selected) {
SSD1306_Fill_Rect(76, 0, 53, 16, 1);
224,20 → 178,34
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_Stepper_Current() {
// Draw the stepper motor current draw in the bottom right corner
uint8_t buffer[6] = {0};
uint16_t potVoltage = ADC_Read(STEP_ADC_CHANNEL);
// Compute current from voltage reading (2x)
uint16_t current = potVoltage * 0.3222 * 2;
uint8_t preDecimal = current / 100;
uint8_t postDecimal = current % 100;
sprintf(buffer, "%1d.%02dA", preDecimal, postDecimal);
SSD1306_Set_Text_Color(1);
SSD1306_Set_Cursor(67, 17);
SSD1306_Write_String(buffer, 5);
}
 
void Draw_Pot_Value() {
// Draw a line indicating auto rotation delay
uint8_t potVoltage = ADC_Read(POT_ADC_CHANNEL) >> 4;
SSD1306_Draw_Line(127, 31, 127 - potVoltage, 31, 1);
}
 
void Draw_Step_Text(STEPPER_MICROSTEP step, uint8_t selected) {
uint8_t stringStepping[] = "1/32";
// Draw and/or highlight the stepping in the bottom left corner
uint8_t stringStepping32[] = "1/32";
uint8_t stringStepping16[] = "1/16";
uint8_t stringStepping8[] = "1/8";
uint8_t stringStepping4[] = "1/4";
uint8_t stringStepping2[] = "1/2";
uint8_t stringStepping1[] = "1/1";
if (selected) {
SSD1306_Fill_Rect(0, 16, 52, 16, 1);
SSD1306_Set_Text_Color(0);
244,6 → 212,23
} else {
SSD1306_Set_Text_Color(1);
}
SSD1306_Set_Cursor(2, 17);
SSD1306_Write_String(stringStepping, 4);
if (step == STEP_1_32) {
SSD1306_Set_Cursor(2, 17);
SSD1306_Write_String(stringStepping32, 4);
} else if (step == STEP_1_16) {
SSD1306_Set_Cursor(2, 17);
SSD1306_Write_String(stringStepping16, 4);
} else if (step == STEP_1_8) {
SSD1306_Set_Cursor(8, 17);
SSD1306_Write_String(stringStepping8, 3);
} else if (step == STEP_1_4) {
SSD1306_Set_Cursor(8, 17);
SSD1306_Write_String(stringStepping4, 3);
} else if (step == STEP_1_2) {
SSD1306_Set_Cursor(8, 17);
SSD1306_Write_String(stringStepping2, 3);
} else if (step == STEP_1_1) {
SSD1306_Set_Cursor(8, 17);
SSD1306_Write_String(stringStepping1, 3);
}
}