Baseado no IHM01A1 da ST
Dependencies: mbed X_NUCLEO_IHM01A1
main.cpp@0:faeebef8f92b, 2020-11-28 (annotated)
- Committer:
- kaiquedog
- Date:
- Sat Nov 28 18:13:41 2020 +0000
- Revision:
- 0:faeebef8f92b
Baseado no IHM01A1 da ST
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kaiquedog | 0:faeebef8f92b | 1 | /** |
kaiquedog | 0:faeebef8f92b | 2 | ****************************************************************************** |
kaiquedog | 0:faeebef8f92b | 3 | * @file main.cpp |
kaiquedog | 0:faeebef8f92b | 4 | * @author Davide Aliprandi, STMicroelectronics |
kaiquedog | 0:faeebef8f92b | 5 | * @version V1.0.0 |
kaiquedog | 0:faeebef8f92b | 6 | * @date October 14th, 2015 |
kaiquedog | 0:faeebef8f92b | 7 | * @brief mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1 |
kaiquedog | 0:faeebef8f92b | 8 | * Motor Control Expansion Board: control of 1 motor. |
kaiquedog | 0:faeebef8f92b | 9 | ****************************************************************************** |
kaiquedog | 0:faeebef8f92b | 10 | * @attention |
kaiquedog | 0:faeebef8f92b | 11 | * |
kaiquedog | 0:faeebef8f92b | 12 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
kaiquedog | 0:faeebef8f92b | 13 | * |
kaiquedog | 0:faeebef8f92b | 14 | * Redistribution and use in source and binary forms, with or without modification, |
kaiquedog | 0:faeebef8f92b | 15 | * are permitted provided that the following conditions are met: |
kaiquedog | 0:faeebef8f92b | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
kaiquedog | 0:faeebef8f92b | 17 | * this list of conditions and the following disclaimer. |
kaiquedog | 0:faeebef8f92b | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
kaiquedog | 0:faeebef8f92b | 19 | * this list of conditions and the following disclaimer in the documentation |
kaiquedog | 0:faeebef8f92b | 20 | * and/or other materials provided with the distribution. |
kaiquedog | 0:faeebef8f92b | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
kaiquedog | 0:faeebef8f92b | 22 | * may be used to endorse or promote products derived from this software |
kaiquedog | 0:faeebef8f92b | 23 | * without specific prior written permission. |
kaiquedog | 0:faeebef8f92b | 24 | * |
kaiquedog | 0:faeebef8f92b | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
kaiquedog | 0:faeebef8f92b | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
kaiquedog | 0:faeebef8f92b | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
kaiquedog | 0:faeebef8f92b | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
kaiquedog | 0:faeebef8f92b | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
kaiquedog | 0:faeebef8f92b | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
kaiquedog | 0:faeebef8f92b | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
kaiquedog | 0:faeebef8f92b | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
kaiquedog | 0:faeebef8f92b | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
kaiquedog | 0:faeebef8f92b | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
kaiquedog | 0:faeebef8f92b | 35 | * |
kaiquedog | 0:faeebef8f92b | 36 | ****************************************************************************** |
kaiquedog | 0:faeebef8f92b | 37 | */ |
kaiquedog | 0:faeebef8f92b | 38 | |
kaiquedog | 0:faeebef8f92b | 39 | |
kaiquedog | 0:faeebef8f92b | 40 | /* Includes ------------------------------------------------------------------*/ |
kaiquedog | 0:faeebef8f92b | 41 | |
kaiquedog | 0:faeebef8f92b | 42 | /* mbed specific header files. */ |
kaiquedog | 0:faeebef8f92b | 43 | #include "mbed.h" |
kaiquedog | 0:faeebef8f92b | 44 | |
kaiquedog | 0:faeebef8f92b | 45 | /* Helper header files. */ |
kaiquedog | 0:faeebef8f92b | 46 | #include "DevSPI.h" |
kaiquedog | 0:faeebef8f92b | 47 | |
kaiquedog | 0:faeebef8f92b | 48 | /* Component specific header files. */ |
kaiquedog | 0:faeebef8f92b | 49 | #include "L6474.h" |
kaiquedog | 0:faeebef8f92b | 50 | InterruptIn Bot(PC_13); |
kaiquedog | 0:faeebef8f92b | 51 | |
kaiquedog | 0:faeebef8f92b | 52 | /* Variables -----------------------------------------------------------------*/ |
kaiquedog | 0:faeebef8f92b | 53 | |
kaiquedog | 0:faeebef8f92b | 54 | /* Initialization parameters. */ |
kaiquedog | 0:faeebef8f92b | 55 | L6474_init_t init = { |
kaiquedog | 0:faeebef8f92b | 56 | 1, /* Acceleration rate in pps^2. Range: (0..+inf). */ |
kaiquedog | 0:faeebef8f92b | 57 | 1, /* Deceleration rate in pps^2. Range: (0..+inf). */ |
kaiquedog | 0:faeebef8f92b | 58 | 800, /* Maximum speed in pps. Range: (30..10000]. */ |
kaiquedog | 0:faeebef8f92b | 59 | 450, /* Minimum speed in pps. Range: [30..10000). */ |
kaiquedog | 0:faeebef8f92b | 60 | 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */ |
kaiquedog | 0:faeebef8f92b | 61 | L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */ |
kaiquedog | 0:faeebef8f92b | 62 | L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */ |
kaiquedog | 0:faeebef8f92b | 63 | L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */ |
kaiquedog | 0:faeebef8f92b | 64 | L6474_STEP_SEL_1_2, /* Step selection (STEP_SEL field of STEP_MODE register). */ |
kaiquedog | 0:faeebef8f92b | 65 | L6474_SYNC_SEL_8, /* Sync selection (SYNC_SEL field of STEP_MODE register). */ |
kaiquedog | 0:faeebef8f92b | 66 | L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */ |
kaiquedog | 0:faeebef8f92b | 67 | L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */ |
kaiquedog | 0:faeebef8f92b | 68 | 21, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */ |
kaiquedog | 0:faeebef8f92b | 69 | 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */ |
kaiquedog | 0:faeebef8f92b | 70 | L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */ |
kaiquedog | 0:faeebef8f92b | 71 | L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */ |
kaiquedog | 0:faeebef8f92b | 72 | L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */ |
kaiquedog | 0:faeebef8f92b | 73 | L6474_ALARM_EN_OVERCURRENT | |
kaiquedog | 0:faeebef8f92b | 74 | L6474_ALARM_EN_THERMAL_SHUTDOWN | |
kaiquedog | 0:faeebef8f92b | 75 | L6474_ALARM_EN_THERMAL_WARNING | |
kaiquedog | 0:faeebef8f92b | 76 | L6474_ALARM_EN_UNDERVOLTAGE | |
kaiquedog | 0:faeebef8f92b | 77 | L6474_ALARM_EN_SW_TURN_ON | |
kaiquedog | 0:faeebef8f92b | 78 | L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */ |
kaiquedog | 0:faeebef8f92b | 79 | }; |
kaiquedog | 0:faeebef8f92b | 80 | |
kaiquedog | 0:faeebef8f92b | 81 | /* Motor Control Component. */ |
kaiquedog | 0:faeebef8f92b | 82 | L6474 *motor; |
kaiquedog | 0:faeebef8f92b | 83 | |
kaiquedog | 0:faeebef8f92b | 84 | |
kaiquedog | 0:faeebef8f92b | 85 | /* Functions -----------------------------------------------------------------*/ |
kaiquedog | 0:faeebef8f92b | 86 | |
kaiquedog | 0:faeebef8f92b | 87 | /** |
kaiquedog | 0:faeebef8f92b | 88 | * @brief This is an example of user handler for the flag interrupt. |
kaiquedog | 0:faeebef8f92b | 89 | * @param None |
kaiquedog | 0:faeebef8f92b | 90 | * @retval None |
kaiquedog | 0:faeebef8f92b | 91 | * @note If needed, implement it, and then attach and enable it: |
kaiquedog | 0:faeebef8f92b | 92 | * + motor->attach_flag_irq(&flag_irq_handler); |
kaiquedog | 0:faeebef8f92b | 93 | * + motor->enable_flag_irq(); |
kaiquedog | 0:faeebef8f92b | 94 | * To disable it: |
kaiquedog | 0:faeebef8f92b | 95 | * + motor->disble_flag_irq(); |
kaiquedog | 0:faeebef8f92b | 96 | */ |
kaiquedog | 0:faeebef8f92b | 97 | void flag_irq_handler(void) |
kaiquedog | 0:faeebef8f92b | 98 | { |
kaiquedog | 0:faeebef8f92b | 99 | /* Set ISR flag. */ |
kaiquedog | 0:faeebef8f92b | 100 | motor->isr_flag = TRUE; |
kaiquedog | 0:faeebef8f92b | 101 | |
kaiquedog | 0:faeebef8f92b | 102 | /* Get the value of the status register. */ |
kaiquedog | 0:faeebef8f92b | 103 | unsigned int status = motor->get_status(); |
kaiquedog | 0:faeebef8f92b | 104 | |
kaiquedog | 0:faeebef8f92b | 105 | /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */ |
kaiquedog | 0:faeebef8f92b | 106 | /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */ |
kaiquedog | 0:faeebef8f92b | 107 | if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) { |
kaiquedog | 0:faeebef8f92b | 108 | printf(" WARNING: \"FLAG\" interrupt triggered. Non-performable command detected when updating L6474's registers while not in HiZ state.\r\n"); |
kaiquedog | 0:faeebef8f92b | 109 | } |
kaiquedog | 0:faeebef8f92b | 110 | |
kaiquedog | 0:faeebef8f92b | 111 | /* Reset ISR flag. */ |
kaiquedog | 0:faeebef8f92b | 112 | motor->isr_flag = FALSE; |
kaiquedog | 0:faeebef8f92b | 113 | } |
kaiquedog | 0:faeebef8f92b | 114 | |
kaiquedog | 0:faeebef8f92b | 115 | |
kaiquedog | 0:faeebef8f92b | 116 | /* Main ----------------------------------------------------------------------*/ |
kaiquedog | 0:faeebef8f92b | 117 | bool Acionar = false; |
kaiquedog | 0:faeebef8f92b | 118 | void Ativacao(){ |
kaiquedog | 0:faeebef8f92b | 119 | Acionar = true; |
kaiquedog | 0:faeebef8f92b | 120 | } |
kaiquedog | 0:faeebef8f92b | 121 | |
kaiquedog | 0:faeebef8f92b | 122 | int main() |
kaiquedog | 0:faeebef8f92b | 123 | { |
kaiquedog | 0:faeebef8f92b | 124 | /*----- Initialization. -----*/ |
kaiquedog | 0:faeebef8f92b | 125 | |
kaiquedog | 0:faeebef8f92b | 126 | /* Initializing SPI bus. */ |
kaiquedog | 0:faeebef8f92b | 127 | DevSPI dev_spi(D11, D12, D13); |
kaiquedog | 0:faeebef8f92b | 128 | |
kaiquedog | 0:faeebef8f92b | 129 | /* Initializing Motor Control Component. */ |
kaiquedog | 0:faeebef8f92b | 130 | motor = new L6474(D2, D8, D7, D9, D10, dev_spi); |
kaiquedog | 0:faeebef8f92b | 131 | if (motor->init(&init) != COMPONENT_OK) { |
kaiquedog | 0:faeebef8f92b | 132 | exit(EXIT_FAILURE); |
kaiquedog | 0:faeebef8f92b | 133 | } |
kaiquedog | 0:faeebef8f92b | 134 | |
kaiquedog | 0:faeebef8f92b | 135 | /* Attaching and enabling interrupt handlers. */ |
kaiquedog | 0:faeebef8f92b | 136 | motor->attach_flag_irq(&flag_irq_handler); |
kaiquedog | 0:faeebef8f92b | 137 | motor->enable_flag_irq(); |
kaiquedog | 0:faeebef8f92b | 138 | |
kaiquedog | 0:faeebef8f92b | 139 | /* Printing to the console. */ |
kaiquedog | 0:faeebef8f92b | 140 | printf("Testando 1 motor de passo, aperte o botao azul para girar\r\n\n"); |
kaiquedog | 0:faeebef8f92b | 141 | Bot.fall(&Ativacao); |
kaiquedog | 0:faeebef8f92b | 142 | |
kaiquedog | 0:faeebef8f92b | 143 | while (1) { |
kaiquedog | 0:faeebef8f92b | 144 | if (Acionar == true){ |
kaiquedog | 0:faeebef8f92b | 145 | motor->move(StepperMotor::FWD, 400); |
kaiquedog | 0:faeebef8f92b | 146 | motor->wait_while_active(); |
kaiquedog | 0:faeebef8f92b | 147 | Acionar = false; |
kaiquedog | 0:faeebef8f92b | 148 | } |
kaiquedog | 0:faeebef8f92b | 149 | } |
kaiquedog | 0:faeebef8f92b | 150 | } |