| 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"
|
|
|
28 |
|
|
|
29 |
void Pins_Init(void) {
|
|
|
30 |
// RA0 and RA1 pins as analog input
|
|
|
31 |
ANSELA = 0x3;
|
|
|
32 |
ANSELC = 0x0;
|
|
|
33 |
|
|
|
34 |
// // Enable weak pull-up if WPU bit is set
|
|
|
35 |
// OPTION_REGbits.nWPUEN = 0;
|
|
|
36 |
|
|
|
37 |
// SDO1 on RC2
|
|
|
38 |
APFCON0bits.SDOSEL = 0;
|
|
|
39 |
|
|
|
40 |
STEP_TRIS = 0;
|
|
|
41 |
STEP_LAT = 0;
|
|
|
42 |
|
|
|
43 |
DIR_TRIS = 0;
|
|
|
44 |
DIR_LAT = 0;
|
|
|
45 |
|
|
|
46 |
M0_TRIS = 0;
|
|
|
47 |
M0_LAT = 0;
|
|
|
48 |
|
|
|
49 |
M1_TRIS = 0;
|
|
|
50 |
M1_LAT = 0;
|
|
|
51 |
|
|
|
52 |
M2_TRIS = 0;
|
|
|
53 |
M2_LAT = 0;
|
|
|
54 |
|
|
|
55 |
SW_1_TRIS = 1;
|
|
|
56 |
SW_2_TRIS = 1;
|
|
|
57 |
STEP_CURRENT_TRIS = 1;
|
|
|
58 |
POT_CURRENT_TRIS = 1;
|
|
|
59 |
|
|
|
60 |
SPI_MOSI_TRIS = 0;
|
|
|
61 |
SPI_D_C_TRIS = 0;
|
|
|
62 |
SPI_CLK_TRIS = 0;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
OPERATING_MODE currMode;
|
|
|
66 |
STEPPER_MICROSTEP currStep;
|
|
|
67 |
|
|
|
68 |
int main(void) {
|
|
|
69 |
// Set internal oscillator speed to 32MHz
|
|
|
70 |
OSCCONbits.SPLLEN = 1; // 4x PLL enable (overwritten by config bits)
|
|
|
71 |
OSCCONbits.IRCF = 0xE; // Base frequency @ 8MHz
|
|
|
72 |
OSCCONbits.SCS = 0b00; // System clock determined by config bits
|
|
|
73 |
|
|
|
74 |
// Initialize I/O
|
|
|
75 |
Pins_Init();
|
|
|
76 |
|
|
|
77 |
IOC_Init();
|
|
|
78 |
|
|
|
79 |
Interrupt_Init();
|
|
|
80 |
Interrupt_Enable();
|
|
|
81 |
|
|
|
82 |
currMode = SINGLE_STEP;
|
|
|
83 |
currStep = STEP_1_1;
|
|
|
84 |
|
|
|
85 |
while(1) {
|
|
|
86 |
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
void Set_Next_Mode() {
|
|
|
92 |
switch (currMode) {
|
|
|
93 |
case SINGLE_STEP:
|
|
|
94 |
currMode = AUTO_STEP;
|
|
|
95 |
break;
|
|
|
96 |
case AUTO_STEP:
|
|
|
97 |
currMode = SET_MICROSTEP;
|
|
|
98 |
break;
|
|
|
99 |
case SET_MICROSTEP:
|
|
|
100 |
default:
|
|
|
101 |
currMode = SINGLE_STEP;
|
|
|
102 |
break;
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
OPERATING_MODE Get_Cur_Mode(void) {
|
|
|
107 |
return currMode;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
void Set_Next_Step() {
|
|
|
111 |
switch (currStep) {
|
|
|
112 |
case STEP_1_1:
|
|
|
113 |
currStep = STEP_1_2;
|
|
|
114 |
break;
|
|
|
115 |
case STEP_1_2:
|
|
|
116 |
currStep = STEP_1_4;
|
|
|
117 |
break;
|
|
|
118 |
case STEP_1_4:
|
|
|
119 |
currStep = STEP_1_8;
|
|
|
120 |
break;
|
|
|
121 |
case STEP_1_8:
|
|
|
122 |
currStep = STEP_1_16;
|
|
|
123 |
break;
|
|
|
124 |
case STEP_1_16:
|
|
|
125 |
currStep = STEP_1_32;
|
|
|
126 |
break;
|
|
|
127 |
case STEP_1_32:
|
|
|
128 |
default:
|
|
|
129 |
currStep = STEP_1_1;
|
|
|
130 |
break;
|
|
|
131 |
}
|
|
|
132 |
STEPPER_Set_Microstep(currStep);
|
|
|
133 |
}
|