Subversion Repositories Code-Repo

Rev

Rev 315 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
// PIC16F1825 Configuration Bit Settings

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
// </editor-fold>

#include "defines.h"
#include "INTERRUPTS.h"
#include "STEPPER.h"
#include "IOC.h"

void Pins_Init(void) {
    // RA0 and RA1 pins as analog input
    ANSELA = 0x3;
    ANSELC = 0x0;

//    // Enable weak pull-up if WPU bit is set
//    OPTION_REGbits.nWPUEN = 0;

    // SDO1 on RC2
    APFCON0bits.SDOSEL = 0;

    STEP_TRIS = 0;
    STEP_LAT = 0;

    DIR_TRIS = 0;
    DIR_LAT = 0;

    M0_TRIS = 0;
    M0_LAT = 0;

    M1_TRIS = 0;
    M1_LAT = 0;

    M2_TRIS = 0;
    M2_LAT = 0;

    SW_1_TRIS = 1;
    SW_2_TRIS = 1;
    STEP_CURRENT_TRIS = 1;
    POT_CURRENT_TRIS = 1;

    SPI_MOSI_TRIS = 0;
    SPI_D_C_TRIS = 0;
    SPI_CLK_TRIS = 0;
}

OPERATING_MODE currMode;
STEPPER_MICROSTEP currStep;

int main(void) {
    // Set internal oscillator speed to 32MHz
    OSCCONbits.SPLLEN = 1;  // 4x PLL enable (overwritten by config bits)
    OSCCONbits.IRCF = 0xE;  // Base frequency @ 8MHz
    OSCCONbits.SCS = 0b00;  // System clock determined by config bits

    // Initialize I/O
    Pins_Init();

    IOC_Init();
    
    Interrupt_Init();
    Interrupt_Enable();

    currMode = SINGLE_STEP;
    currStep = STEP_1_1;
    
    while(1) {
        
    }

}

void Set_Next_Mode() {
    switch (currMode) {
        case SINGLE_STEP:
            currMode = AUTO_STEP;
            break;
        case AUTO_STEP:
            currMode = SET_MICROSTEP;
            break;
        case SET_MICROSTEP:
        default:
            currMode = SINGLE_STEP;
            break;
    }
}

OPERATING_MODE Get_Cur_Mode(void) {
    return currMode;
}

void Set_Next_Step() {
    switch (currStep) {
        case STEP_1_1:
            currStep = STEP_1_2;
            break;
        case STEP_1_2:
            currStep = STEP_1_4;
            break;
        case STEP_1_4:
            currStep = STEP_1_8;
            break;
        case STEP_1_8:
            currStep = STEP_1_16;
            break;
        case STEP_1_16:
            currStep = STEP_1_32;
            break;
        case STEP_1_32:
        default:
            currStep = STEP_1_1;
            break;
    }
    STEPPER_Set_Microstep(currStep);
}