Created
May 25, 2014 13:20
-
-
Save wattnotions/010685c6347b93c94c25 to your computer and use it in GitHub Desktop.
Used in the 2d mapping project to control the stepper motor and send the current step value over serial to a computer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <xc.h> | |
#include <stdio.h> | |
#include <delays.h> | |
#pragma config FOSC=IRC,MCLRE=OFF,WDTEN=0,LVP=OFF,BOREN=OFF | |
//Functions | |
void setup(void); | |
void config_usart(void); | |
void step_pos(int n); | |
void step_neg(int n); | |
//Variables | |
int x_state = 1; | |
int count = 0; | |
char c; | |
void main(void) | |
{ | |
setup(); | |
config_usart(); | |
while(1) | |
{ | |
if (PIR1bits.RCIF == 1 ) | |
{ | |
c = RCREG; | |
if( c == 'g') | |
{ | |
step_pos(2048); | |
while( c != 'h') | |
{ | |
c = RCREG; | |
printf("o\n"); | |
} | |
step_neg(2048); | |
} | |
} | |
} | |
} | |
void setup(void) | |
{ | |
OSCCON = 0b01100011; //internal oscillator block, 8MHz clock speed. | |
TRISB = 0b00100000; // all outputs except for rx pin | |
TRISC = 0b00000000; | |
ANSELHbits.ANS11 = 0; // disable analog input on rx pin | |
} | |
void config_usart(void) | |
{ | |
TXSTAbits.TXEN = 1; // transmit enabled | |
TXSTAbits.SYNC = 0; // asynchronous mode | |
RCSTAbits.SPEN = 1; // usart enabled, tx set as output | |
RCSTAbits.CREN = 1; // enable rx | |
SPBRG = 12; // baud rate = 9600 | |
} | |
void putch(char data) // this function is required for printf to work properly, got it from the xc8 manual | |
{ | |
while (!TXIF) | |
continue; | |
TXREG = data; | |
} | |
void step_pos(int n) | |
{ | |
count = 0; | |
while( count < n ) | |
{ | |
if (count == n) break; | |
LATCbits.LATC2 = 1; | |
Delay10KTCYx(2); | |
LATCbits.LATC2 = 0; | |
count++; | |
printf("%d\n",count); | |
if (count == n) break; | |
LATBbits.LATB4 = 1; | |
Delay10KTCYx(2); | |
LATBbits.LATB4 = 0; | |
count++; | |
printf("%d\n",count); | |
if (count == n) break; | |
LATCbits.LATC7 = 1; | |
Delay10KTCYx(2); | |
LATCbits.LATC7 = 0; | |
count++; | |
printf("%d\n",count); | |
if (count == n) break; | |
LATCbits.LATC6 = 1; | |
Delay10KTCYx(2); | |
LATCbits.LATC6 = 0; | |
count++; | |
printf("%d\n",count); | |
} | |
} | |
void step_neg(int n) | |
{ | |
count = 0; | |
while( count < n ) | |
{ | |
if (count == n) break; | |
LATCbits.LATC6 = 1; | |
Delay10KTCYx(2); | |
LATCbits.LATC6 = 0; | |
count++; | |
printf("%d\n",count); | |
if (count == n) break; | |
LATCbits.LATC7 = 1; | |
Delay10KTCYx(2); | |
LATCbits.LATC7 = 0; | |
count++; | |
printf("%d\n",count); | |
if (count == n) break; | |
LATBbits.LATB4 = 1; | |
Delay10KTCYx(2); | |
LATBbits.LATB4 = 0; | |
count++; | |
printf("%d\n",count); | |
if (count == n) break; | |
LATCbits.LATC2 = 1; | |
Delay10KTCYx(2); | |
LATCbits.LATC2 = 0; | |
count++; | |
printf("%d\n",count); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment