MSP430i2xx GPIO例程

2019-07-25 14:09发布

本帖最后由 734774645 于 2015-10-31 16:58 编辑
  1. /* --COPYRIGHT--,BSD
  2. * Copyright (c) 2014, Texas Instruments Incorporated
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * *  Redistributions of source code must retain the above copyright
  10. *    notice, this list of conditions and the following disclaimer.
  11. *
  12. * *  Redistributions in binary form must reproduce the above copyright
  13. *    notice, this list of conditions and the following disclaimer in the
  14. *    documentation and/or other materials provided with the distribution.
  15. *
  16. * *  Neither the name of Texas Instruments Incorporated nor the names of
  17. *    its contributors may be used to endorse or promote products derived
  18. *    from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. * --/COPYRIGHT--*/
  32. /*******************************************************************************
  33. * MSP430i2xx GPIO - Toggle Output
  34. *
  35. * Description: In this example, P1.4 is setup as an output pin. The pin is
  36. * toggled in the main loop to make the LED blink. The MCLK is slowed down so
  37. * the LED blinking is visible.
  38. *
  39. *
  40. *                MSP430i2041
  41. *             ------------------
  42. *         /||                  |
  43. *          | |                  |
  44. *          --|RST               |
  45. *            |                  |
  46. *            |              P1.4|-->LED
  47. *            |                  |
  48. *            |                  |
  49. *            |                  |
  50. *
  51. * Author: Zack Lalanne
  52. ******************************************************************************/

  53. #include "driverlib.h"

  54. int main(void) {
  55.     WDT_hold(WDT_BASE);

  56.     // Set P1.4 as output pin to drive the LED
  57.     GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN4);

  58.     // Slow down clock so we can see LED blink
  59.     // Configure MCLK = ~1MHz
  60.     CS_initClockSignal(CS_MCLK, CS_CLOCK_DIVIDER_16);

  61.     while(1)
  62.     {
  63.         // Toggle the LED state
  64.         GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN4);
  65.         __delay_cycles(100000);
  66.     }
  67. }
复制代码

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
8条回答
734774645
1楼-- · 2019-07-25 14:51
这个是他们提供库函数版本的例程。:lol
734774645
2楼-- · 2019-07-25 17:05
  1. /* --COPYRIGHT--,BSD
  2. * Copyright (c) 2014, Texas Instruments Incorporated
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * *  Redistributions of source code must retain the above copyright
  10. *    notice, this list of conditions and the following disclaimer.
  11. *
  12. * *  Redistributions in binary form must reproduce the above copyright
  13. *    notice, this list of conditions and the following disclaimer in the
  14. *    documentation and/or other materials provided with the distribution.
  15. *
  16. * *  Neither the name of Texas Instruments Incorporated nor the names of
  17. *    its contributors may be used to endorse or promote products derived
  18. *    from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. * --/COPYRIGHT--*/
  32. /*******************************************************************************
  33. * MSP430i2xx GPIO - Input Interrupt
  34. *
  35. * Description: In this example, P1.4 is setup as an output pin. P2.0 is set up
  36. * as an input pin that interrupts when the voltage applied goes from high to
  37. * low. When the voltage transition happens the ISR is fired and P1.4 is
  38. * toggled to show that an interrupt happened. If P2.0 is still low then the
  39. * interrupt edge is switched to trigger when P2.0 goes from low to high. This
  40. * way an interrupt occurs on every voltage change of P2.0.
  41. *
  42. *
  43. *                MSP430i2041
  44. *             ------------------
  45. *         /||                  |
  46. *          | |                  |
  47. *          --|RST           P2.0|<--Input Signal
  48. *            |                  |
  49. *            |                  |
  50. *            |              P1.4|-->LED
  51. *            |                  |
  52. *            |                  |
  53. *
  54. * Author: Zack Lalanne
  55. ******************************************************************************/

  56. #include "driverlib.h"

  57. int main(void) {
  58.     WDT_hold(WDT_BASE);

  59.     // Set P1.4 as output pin to drive the LED
  60.     GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN4);

  61.     // Setup P2.0 to interrupt on High to Low transition
  62.     GPIO_setAsInputPin(GPIO_PORT_P2, GPIO_PIN0);
  63.     GPIO_selectInterruptEdge(GPIO_PORT_P2, GPIO_PIN0,
  64.                              GPIO_HIGH_TO_LOW_TRANSITION);
  65.     GPIO_clearInterrupt(GPIO_PORT_P2, GPIO_PIN0);
  66.     GPIO_enableInterrupt(GPIO_PORT_P2, GPIO_PIN0);

  67.     // Go into LPM0 with interrupts enabled
  68.     __bis_SR_register(LPM0_bits | GIE);
  69. }

  70. #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
  71. #pragma vector=PORT2_VECTOR
  72. __interrupt
  73. #elif defined(__GNUC__)
  74. __attribute__((interrupt(PORT2_VECTOR)))
  75. #endif
  76. void PORT2_ISR(void) {
  77.     switch(__even_in_range(P2IV, 0x10))
  78.     {
  79.     case 0x00: break;                   // No interrupt
  80.     case 0x02:                          // P2.0 interrupt
  81.         // Check current state of P2.0 and set correct interrupt edge to occur
  82.         if(GPIO_getInputPinValue(GPIO_PORT_P2,
  83.                                  GPIO_PIN0) == GPIO_INPUT_PIN_HIGH)
  84.         {
  85.             GPIO_selectInterruptEdge(GPIO_PORT_P2, GPIO_PIN0,
  86.                                      GPIO_HIGH_TO_LOW_TRANSITION);
  87.         }
  88.         else
  89.         {
  90.             GPIO_selectInterruptEdge(GPIO_PORT_P2, GPIO_PIN0,
  91.                                      GPIO_LOW_TO_HIGH_TRANSITION);
  92.         }

  93.         // Toggle 1.4 to indicate an interrupt occurred
  94.         GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN4);
  95.         break;
  96.     case 0x04: break;                   // P2.1 interrupt
  97.     case 0x06: break;                   // P2.2 interrupt
  98.     case 0x08: break;                   // P2.3 interrupt
  99.     case 0x0A: break;                   // P2.4 interrupt
  100.     case 0x0C: break;                   // P2.5 interrupt
  101.     case 0x0E: break;                   // P2.6 interrupt
  102.     case 0x10: break;                   // P2.7 interrupt
  103.     }
  104. }
复制代码

lzbf
3楼-- · 2019-07-25 19:54
这个有些注释吗?
643757107
4楼-- · 2019-07-25 20:36
 精彩回答 2  元偷偷看……
DreamofOven
5楼-- · 2019-07-25 21:29
这个是官网提供的代码吧,不错的,谢谢
598330983
6楼-- · 2019-07-26 01:00
官网提供的库函数的内容

一周热门 更多>