Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BalanceCar by
Microduino_Stepper.h@3:c6caae712d5d, 2016-06-07 (annotated)
- Committer:
- lixianyu
- Date:
- Tue Jun 07 08:14:15 2016 +0000
- Revision:
- 3:c6caae712d5d
- Parent:
- 2:99785a1007a4
??????????work? ?????
Who changed what in which revision?
| User | Revision | Line number | New 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 | 2:99785a1007a4 | 5 | #if 0 |
| lixianyu | 0:a4d8f5b3c546 | 6 | #ifndef __MICRODUINO_STEPPER_H__ |
| lixianyu | 0:a4d8f5b3c546 | 7 | #define __MICRODUINO_STEPPER_H__ |
| lixianyu | 0:a4d8f5b3c546 | 8 | |
| lixianyu | 2:99785a1007a4 | 9 | //#include "MicroduinoPinNames.h" |
| lixianyu | 0:a4d8f5b3c546 | 10 | #include "mbed.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 | 2:99785a1007a4 | 35 | #elif 0 |
| 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 | 2:99785a1007a4 | 45 | #else |
| lixianyu | 2:99785a1007a4 | 46 | #define PIN_EN P0_25 |
| lixianyu | 2:99785a1007a4 | 47 | #define PIN_DIRA P0_7 |
| lixianyu | 2:99785a1007a4 | 48 | #define PIN_STEPA P0_24 |
| lixianyu | 2:99785a1007a4 | 49 | #define PIN_DIRB P0_6 |
| lixianyu | 2:99785a1007a4 | 50 | #define PIN_STEPB P0_1 |
| lixianyu | 2:99785a1007a4 | 51 | #define PIN_DIRC P0_14 |
| lixianyu | 2:99785a1007a4 | 52 | #define PIN_STEPC P0_20 |
| lixianyu | 2:99785a1007a4 | 53 | #define PIN_DIRD P0_23 |
| lixianyu | 2:99785a1007a4 | 54 | #define PIN_STEPD P0_19 |
| lixianyu | 0:a4d8f5b3c546 | 55 | #endif |
| lixianyu | 0:a4d8f5b3c546 | 56 | |
| lixianyu | 0:a4d8f5b3c546 | 57 | #define DEFAULT_ACCEL 120 |
| lixianyu | 0:a4d8f5b3c546 | 58 | |
| lixianyu | 0:a4d8f5b3c546 | 59 | void stepperAllEnable(); |
| lixianyu | 0:a4d8f5b3c546 | 60 | void stepperAllDisable(); |
| lixianyu | 0:a4d8f5b3c546 | 61 | |
| lixianyu | 0:a4d8f5b3c546 | 62 | class Stepper |
| lixianyu | 0:a4d8f5b3c546 | 63 | { |
| lixianyu | 0:a4d8f5b3c546 | 64 | public: |
| lixianyu | 0:a4d8f5b3c546 | 65 | |
| lixianyu | 0:a4d8f5b3c546 | 66 | Stepper(uint8_t _dirPin, uint8_t _stepPin); |
| lixianyu | 0:a4d8f5b3c546 | 67 | |
| lixianyu | 0:a4d8f5b3c546 | 68 | uint8_t begin(); |
| lixianyu | 0:a4d8f5b3c546 | 69 | |
| lixianyu | 0:a4d8f5b3c546 | 70 | void setSpeed(int16_t _speed); |
| lixianyu | 0:a4d8f5b3c546 | 71 | |
| lixianyu | 0:a4d8f5b3c546 | 72 | void setMaxAccel(uint16_t _accel); |
| lixianyu | 0:a4d8f5b3c546 | 73 | |
| lixianyu | 0:a4d8f5b3c546 | 74 | int16_t getSpeed(); |
| lixianyu | 0:a4d8f5b3c546 | 75 | |
| lixianyu | 0:a4d8f5b3c546 | 76 | uint16_t getMaxAccel(); |
| lixianyu | 0:a4d8f5b3c546 | 77 | |
| lixianyu | 0:a4d8f5b3c546 | 78 | void computeStep(); |
| lixianyu | 0:a4d8f5b3c546 | 79 | |
| lixianyu | 0:a4d8f5b3c546 | 80 | private: |
| lixianyu | 0:a4d8f5b3c546 | 81 | |
| lixianyu | 0:a4d8f5b3c546 | 82 | gpio_t dirOUT; |
| lixianyu | 0:a4d8f5b3c546 | 83 | gpio_t stepOUT; |
| lixianyu | 0:a4d8f5b3c546 | 84 | uint8_t stepperIndex; |
| lixianyu | 0:a4d8f5b3c546 | 85 | uint8_t dirPin; |
| lixianyu | 0:a4d8f5b3c546 | 86 | uint8_t stepPin; |
| lixianyu | 0:a4d8f5b3c546 | 87 | int16_t speed; |
| lixianyu | 0:a4d8f5b3c546 | 88 | uint16_t maxAccel; |
| lixianyu | 0:a4d8f5b3c546 | 89 | uint16_t period; |
| lixianyu | 0:a4d8f5b3c546 | 90 | uint16_t counter; |
| lixianyu | 0:a4d8f5b3c546 | 91 | |
| lixianyu | 0:a4d8f5b3c546 | 92 | }; |
| lixianyu | 0:a4d8f5b3c546 | 93 | |
| lixianyu | 0:a4d8f5b3c546 | 94 | typedef struct { |
| lixianyu | 0:a4d8f5b3c546 | 95 | //uint8_t isActive = false; |
| lixianyu | 0:a4d8f5b3c546 | 96 | uint8_t isActive; |
| lixianyu | 0:a4d8f5b3c546 | 97 | Stepper *stepper; |
| lixianyu | 0:a4d8f5b3c546 | 98 | } Stepper_t; |
| lixianyu | 0:a4d8f5b3c546 | 99 | |
| lixianyu | 0:a4d8f5b3c546 | 100 | #endif |
| lixianyu | 2:99785a1007a4 | 101 | #endif |
