Skip to content

Instantly share code, notes, and snippets.

@hayview
Created January 29, 2015 05:44
Show Gist options
  • Save hayview/d5a030bba4e8db67c1e1 to your computer and use it in GitHub Desktop.
Save hayview/d5a030bba4e8db67c1e1 to your computer and use it in GitHub Desktop.
for stepper motor control
#include "delay.h"
void delayMicroSecond (unsigned int time)
{
while (time--)
{
_delay_cycles(MICRO_SECOND);
}
}
void delayMiliSecond (unsigned int time)
{
while (time--)
{
_delay_cycles(MILI_SECOND);
}
}
#ifndef DELAY_H
#define DELAY_H
//define the cpu clock speed
#define CPU_CLOCK_KHZ 16000/*changing this value according to dco clock frequency*/
#define MICRO_SECOND CPU_CLOCK_KHZ/1000
#define MILI_SECOND CPU_CLOCK_KHZ
void delayMicroSecond (unsigned int time);
void delayMiliSecond (unsigned int time);
#endif
/*
* easydriver.c
*
* Created on: Jan 13, 2015
* Author: hayview
*/
#include "easydriver.h"
void motorAInit(void){
MOTOR_A_INIT();
MOTOR_A_SLEEP();
}
void motorBInit(void){
MOTOR_B_INIT();
MOTOR_B_SLEEP();
}
void motorsInit(void){
MOTORS_INIT();
MOTORS_SLEEP();
}
void motorARunCycles(int stepper_cycles)
{
int step_counts;
if (stepper_cycles<0){
MOTOR_A_ANTI_CW();
stepper_cycles=abs(stepper_cycles);
}
else{
MOTOR_A_CW();
}
MOTOR_A_WAKEUP();
while(stepper_cycles>0){
for(step_counts=0;step_counts<=1600;step_counts++){
MOTOR_A_STEP();
_delay_cycles(MOTOR_SPEED);
}
stepper_cycles--;
}
MOTOR_A_SLEEP();
}
void motorBRunCycles(int stepper_cycles)
{
int step_counts;
if (stepper_cycles<0){
MOTOR_A_ANTI_CW();
stepper_cycles=abs(stepper_cycles);
}
else{
MOTOR_B_CW();
}
MOTOR_B_WAKEUP();
while(stepper_cycles>0){
for(step_counts=0;step_counts<=1600;step_counts++){
MOTOR_B_STEP();
_delay_cycles(MOTOR_SPEED);
}
stepper_cycles--;
}
MOTOR_B_SLEEP();
}
void motorsRunCycles(int stepper_cycles)
{
int step_counts;
if (stepper_cycles<0){
MOTORS_ANTI_CW();
stepper_cycles=abs(stepper_cycles);
}
else{
MOTORS_CW();
}
MOTORS_WAKEUP();
while(stepper_cycles>0){
for(step_counts=0;step_counts<=1600;step_counts++){
MOTORS_STEP();
_delay_cycles(MOTOR_SPEED);
}
stepper_cycles--;
}
MOTORS_SLEEP();
}
void motorAGotoPosition(int stepper_degrees)
{
long step_degrees=1600;
int step_counts;
if(stepper_degrees<0){
MOTOR_A_ANTI_CW();
stepper_degrees=abs(stepper_degrees);
}
else{
MOTOR_A_CW();
}
step_degrees=step_degrees*(long)stepper_degrees;
step_degrees=step_degrees/360;
step_counts=(int)step_degrees;
MOTOR_A_WAKEUP();
while(step_counts>0){
MOTOR_A_STEP();
_delay_cycles(MOTOR_SPEED);
step_counts--;
}
MOTOR_A_SLEEP();
}
void motorBGotoPosition(int stepper_degrees)
{
long step_degrees=1600;
int step_counts;
if(stepper_degrees<0){
MOTOR_B_ANTI_CW();
stepper_degrees=abs(stepper_degrees);
}
else{
MOTOR_B_CW();
}
step_degrees=step_degrees*(long)stepper_degrees;
step_degrees=step_degrees/360;
step_counts=(int)step_degrees;
MOTOR_B_WAKEUP();
while(step_counts>0){
MOTOR_B_STEP();
_delay_cycles(MOTOR_SPEED);
step_counts--;
}
MOTOR_B_SLEEP();
}
void motorsGotoPosition(int stepper_degrees)
{
long step_degrees=1600;
int step_counts;
if(stepper_degrees<0){
MOTORS_ANTI_CW();
stepper_degrees=abs(stepper_degrees);
}
else{
MOTORS_CW();
}
step_degrees=step_degrees*(long)stepper_degrees;
step_degrees=step_degrees/360;
step_counts=(int)step_degrees;
MOTORS_WAKEUP();
while(step_counts>0){
MOTORS_STEP();
_delay_cycles(MOTOR_SPEED);
step_counts--;
}
MOTORS_SLEEP();
}
/*
* easydriver.h
*
* Created on: Jan 13, 2015
* Author: hayview
*/
#ifndef EASYDRIVER_H_
#define EASYDRIVER_H_
#include <msp430.h>
/*
************************************************************
* CONSTANTS
************************************************************
*/
#define MOTOR_A_DIR_PIN BIT0
#define MOTOR_A_STEP_PIN BIT1
#define MOTOR_A_SLEEP_PIN BIT2
#define MOTOR_A_PINS (MOTOR_A_DIR_PIN|MOTOR_A_STEP_PIN|MOTOR_A_SLEEP_PIN)
#define MOTOR_B_DIR_PIN BIT3
#define MOTOR_B_STEP_PIN BIT4
#define MOTOR_B_SLEEP_PIN BIT5
#define MOTOR_B_PINS (MOTOR_B_DIR_PIN|MOTOR_B_STEP_PIN|MOTOR_B_SLEEP_PIN)
#define STEPS_PER_SECOND 3200
#define MOTOR_SPEED (16000000/STEPS_PER_SECOND)
/*
************************************************************
* MACROS
************************************************************
*/
#define MOTOR_A_INIT() (P2DIR|= MOTOR_A_PINS)
#define MOTOR_B_INIT() (P2DIR|= MOTOR_B_PINS)
#define MOTOR_A_SLEEP() (P2OUT&= ~MOTOR_A_SLEEP_PIN)
#define MOTOR_B_SLEEP() (P2OUT&= ~MOTOR_B_SLEEP_PIN)
#define MOTOR_A_WAKEUP() (P2OUT|= MOTOR_A_SLEEP_PIN)
#define MOTOR_B_WAKEUP() (P2OUT|= MOTOR_B_SLEEP_PIN)
#define MOTOR_A_CW() (P2OUT&= ~MOTOR_A_DIR_PIN)
#define MOTOR_B_CW() (P2OUT&= ~MOTOR_B_DIR_PIN)
#define MOTOR_A_ANTI_CW() (P2OUT|= MOTOR_A_DIR_PIN)
#define MOTOR_B_ANTI_CW() (P2OUT|= MOTOR_B_DIR_PIN)
#define MOTOR_A_STEP() {P2OUT|= MOTOR_A_STEP_PIN; P2OUT&=~MOTOR_A_STEP_PIN;}
#define MOTOR_B_STEP() {P2OUT|= MOTOR_B_STEP_PIN; P2OUT&=~MOTOR_B_STEP_PIN;}
#define MOTORS_INIT() (P2DIR|= (MOTOR_A_PINS|MOTOR_B_PINS))
#define MOTORS_SLEEP() (P2OUT&=~(MOTOR_A_SLEEP_PIN|MOTOR_B_SLEEP_PIN))
#define MOTORS_WAKEUP() (P2OUT|= (MOTOR_A_SLEEP_PIN|MOTOR_B_SLEEP_PIN))
#define MOTORS_CW() (P2OUT&=~(MOTOR_A_DIR_PIN|MOTOR_B_DIR_PIN))
#define MOTORS_ANTI_CW() (P2OUT|= (MOTOR_A_DIR_PIN|MOTOR_B_DIR_PIN))
#define MOTORS_STEP() {P2OUT|= (MOTOR_A_STEP_PIN|MOTOR_B_STEP_PIN); P2OUT&=~(MOTOR_A_STEP_PIN|MOTOR_B_STEP_PIN);}
/*
************************************************************
* FUNCTIONS DECLARATION
************************************************************
*/
void motorAInit(void);
void motorBInit(void);
void motorARunCycles(int stepper_cycles);
void motorBRunCycles(int stepper_cycles);
void motorAGotoPosition(int stepper_degrees);
void motorBGotoPosition(int stepper_degrees);
void motorsInit(void);
void motorsRunCycles(int stepper_cycles);
void motorsGotoPosition(int stepper_degrees);
#endif /* EASYDRIVER_H_ */
#include <msp430.h>
#include "easydriver.h"
#include "delay.h"
/*
* main.c
*/
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;
if (CALBC1_16MHZ != 0xFF) {
/* Adjust this accordingly to your VCC rise time */
__delay_cycles(100000);
/* Follow recommended flow. First, clear all DCOx and MODx bits. Then
* apply new RSELx values. Finally, apply new DCOx and MODx bit values.
*/
DCOCTL = 0x00;
BCSCTL1 = CALBC1_16MHZ; /* Set DCO to 16MHz */
DCOCTL = CALDCO_16MHZ;
}
motorsInit();
while(1){
motorsRunCycles(+2);
delayMiliSecond(2000);
motorsRunCycles(-2);
delayMiliSecond(2000);
motorsGotoPosition(90);
delayMiliSecond(1000);
motorsGotoPosition(90);
delayMiliSecond(1000);
motorsGotoPosition(90);
delayMiliSecond(1000);
motorsGotoPosition(90);
delayMiliSecond(1000);
motorsGotoPosition(-90);
delayMiliSecond(1000);
motorsGotoPosition(-90);
delayMiliSecond(1000);
motorsGotoPosition(-90);
delayMiliSecond(1000);
motorsGotoPosition(-90);
delayMiliSecond(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment