Microduino

Dependencies:   mbed

Fork of BalanceCar by Li Weiyi

Committer:
lixianyu
Date:
Sat Jun 04 03:16:52 2016 +0000
Revision:
0:a4d8f5b3c546
Child:
2:99785a1007a4
Pass compile!!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 0:a4d8f5b3c546 1 // Microduino_Stepper.cpp
lixianyu 0:a4d8f5b3c546 2 //
lixianyu 0:a4d8f5b3c546 3 // Copyright (C) 2009-2013 Shenyang
lixianyu 0:a4d8f5b3c546 4 // $Id: Microduino_Stepper.cpp,v 1.00 2016/04/07 $
lixianyu 0:a4d8f5b3c546 5
lixianyu 0:a4d8f5b3c546 6 #ifndef __MICRODUINO_STEPPER_H__
lixianyu 0:a4d8f5b3c546 7 #define __MICRODUINO_STEPPER_H__
lixianyu 0:a4d8f5b3c546 8
lixianyu 0:a4d8f5b3c546 9 #include "mbed.h"
lixianyu 0:a4d8f5b3c546 10 #include "MicroduinoPinNames.h"
lixianyu 0:a4d8f5b3c546 11 #include <stdlib.h>
lixianyu 0:a4d8f5b3c546 12 //#include <Arduino.h>
lixianyu 0:a4d8f5b3c546 13
lixianyu 0:a4d8f5b3c546 14 //#define _useTimer1
lixianyu 0:a4d8f5b3c546 15 #define MAX_SPEED_S 4096
lixianyu 0:a4d8f5b3c546 16 #define TIMER_COMP 327680/MAX_SPEED_S
lixianyu 0:a4d8f5b3c546 17
lixianyu 0:a4d8f5b3c546 18
lixianyu 0:a4d8f5b3c546 19 #define PIN_SET(pin) (*portOutputRegister(digitalPinToPort(pin)) |= digitalPinToBitMask(pin))
lixianyu 0:a4d8f5b3c546 20 #define PIN_CLR(pin) (*portOutputRegister(digitalPinToPort(pin)) &= ~digitalPinToBitMask(pin))
lixianyu 0:a4d8f5b3c546 21
lixianyu 0:a4d8f5b3c546 22 #define MAX_STEPPERS 4
lixianyu 0:a4d8f5b3c546 23 #define INVALID_STEPPER 255
lixianyu 0:a4d8f5b3c546 24
lixianyu 0:a4d8f5b3c546 25 #if 0
lixianyu 0:a4d8f5b3c546 26 #define PIN_EN 4 //PORTB,0
lixianyu 0:a4d8f5b3c546 27 #define PIN_DIRA A0 //PORTA,7
lixianyu 0:a4d8f5b3c546 28 #define PIN_STEPA 5 //PORTB,1
lixianyu 0:a4d8f5b3c546 29 #define PIN_DIRB A1 //PORTA,6
lixianyu 0:a4d8f5b3c546 30 #define PIN_STEPB 6 //PORTB,2
lixianyu 0:a4d8f5b3c546 31 #define PIN_DIRC A2 //PORTA,5
lixianyu 0:a4d8f5b3c546 32 #define PIN_STEPC 7 //PORTB,3
lixianyu 0:a4d8f5b3c546 33 #define PIN_DIRD A3 //PORTA,4
lixianyu 0:a4d8f5b3c546 34 #define PIN_STEPD 8 //PORTD,6
lixianyu 0:a4d8f5b3c546 35 #else
lixianyu 0:a4d8f5b3c546 36 #define PIN_EN D4
lixianyu 0:a4d8f5b3c546 37 #define PIN_DIRA A0
lixianyu 0:a4d8f5b3c546 38 #define PIN_STEPA D5
lixianyu 0:a4d8f5b3c546 39 #define PIN_DIRB A1
lixianyu 0:a4d8f5b3c546 40 #define PIN_STEPB D6
lixianyu 0:a4d8f5b3c546 41 #define PIN_DIRC A2
lixianyu 0:a4d8f5b3c546 42 #define PIN_STEPC D7
lixianyu 0:a4d8f5b3c546 43 #define PIN_DIRD A3
lixianyu 0:a4d8f5b3c546 44 #define PIN_STEPD D8
lixianyu 0:a4d8f5b3c546 45 #endif
lixianyu 0:a4d8f5b3c546 46
lixianyu 0:a4d8f5b3c546 47 #define DEFAULT_ACCEL 120
lixianyu 0:a4d8f5b3c546 48
lixianyu 0:a4d8f5b3c546 49 void stepperAllEnable();
lixianyu 0:a4d8f5b3c546 50 void stepperAllDisable();
lixianyu 0:a4d8f5b3c546 51
lixianyu 0:a4d8f5b3c546 52 class Stepper
lixianyu 0:a4d8f5b3c546 53 {
lixianyu 0:a4d8f5b3c546 54 public:
lixianyu 0:a4d8f5b3c546 55
lixianyu 0:a4d8f5b3c546 56 Stepper(uint8_t _dirPin, uint8_t _stepPin);
lixianyu 0:a4d8f5b3c546 57
lixianyu 0:a4d8f5b3c546 58 uint8_t begin();
lixianyu 0:a4d8f5b3c546 59
lixianyu 0:a4d8f5b3c546 60 void setSpeed(int16_t _speed);
lixianyu 0:a4d8f5b3c546 61
lixianyu 0:a4d8f5b3c546 62 void setMaxAccel(uint16_t _accel);
lixianyu 0:a4d8f5b3c546 63
lixianyu 0:a4d8f5b3c546 64 int16_t getSpeed();
lixianyu 0:a4d8f5b3c546 65
lixianyu 0:a4d8f5b3c546 66 uint16_t getMaxAccel();
lixianyu 0:a4d8f5b3c546 67
lixianyu 0:a4d8f5b3c546 68 void computeStep();
lixianyu 0:a4d8f5b3c546 69
lixianyu 0:a4d8f5b3c546 70 private:
lixianyu 0:a4d8f5b3c546 71
lixianyu 0:a4d8f5b3c546 72 gpio_t dirOUT;
lixianyu 0:a4d8f5b3c546 73 gpio_t stepOUT;
lixianyu 0:a4d8f5b3c546 74 uint8_t stepperIndex;
lixianyu 0:a4d8f5b3c546 75 uint8_t dirPin;
lixianyu 0:a4d8f5b3c546 76 uint8_t stepPin;
lixianyu 0:a4d8f5b3c546 77 int16_t speed;
lixianyu 0:a4d8f5b3c546 78 uint16_t maxAccel;
lixianyu 0:a4d8f5b3c546 79 uint16_t period;
lixianyu 0:a4d8f5b3c546 80 uint16_t counter;
lixianyu 0:a4d8f5b3c546 81
lixianyu 0:a4d8f5b3c546 82 };
lixianyu 0:a4d8f5b3c546 83
lixianyu 0:a4d8f5b3c546 84 typedef struct {
lixianyu 0:a4d8f5b3c546 85 //uint8_t isActive = false;
lixianyu 0:a4d8f5b3c546 86 uint8_t isActive;
lixianyu 0:a4d8f5b3c546 87 Stepper *stepper;
lixianyu 0:a4d8f5b3c546 88 } Stepper_t;
lixianyu 0:a4d8f5b3c546 89
lixianyu 0:a4d8f5b3c546 90 #endif