TMS320F28027之GPIO

来源:互联网 发布:淘宝聚划算抢购软件 编辑:程序博客网 时间:2024/05/21 09:25

http://blog.csdn.net/w471176877/article/details/8010011

输入限制:

       输入限制电路设计的十分灵活,你可以通过配置GPxQSELn寄存器来选择输入限制的类型。在GPIO的输入模式中,可以配置为与SYSCLKOUT同步或者采样窗口限制。对于外设的输入,还可以配置成异步模式。

       不同步(异步输入):此模式用于不要求同步或者本身就要异步的外设中,比如SCI,SPI和I2C。或者独立于SYSCLKOUT的ePWM触发。通用数字接口不能用此模式。

        采样窗口限制:在此模式中,输入信号首先与SYSCLKOUT同步,然后用一定的周期来确定采样信号。用3个或者6个,如果连续3个或者6个采样周期都采样到一样的电平,这认为是有效信号。采样周期通过GPACTRL寄存器设置。

 


采样周期:


能采样到的最短信号电平宽度:

 


 

 

配置步骤(参考文档SPRUFN3C):

1.     总体规划IO口。

2.     使能或者除能上拉电阻(GPxPUD)。

ePWM模式默认除能,其他功能默认使能。模拟IO没有上拉电阻。

3.     输入采样设置(GPxCTRL、 GPxQSELn)。

4.     选择引脚功能(GPxMUXn or AIOMUXn)。

5.     如果是数字IO的话,选择方向(GPxDIR or AIODIR)。

6.     选择低功耗模式下的唤醒输入引脚(GPIOLPMSEL)。

7.     选择中断源(GPIOxINTnSEL),极性(xINTnCR)。

 

 

程序设计:

       实现板上的LED的驱动程序, LED相关IO的初始化都用同一个函数,分别提供了两种种方法改变LED的状态,一,通过一个函数实现。二、通过宏定义实现。第一种效率不高,占用程序存储空间较多,需要装载参数和调用指令,子函数里还有switch造成的跳转表等,但是可以用过同一个函数设置LED的状态,第二种效率高,通过三个调用设置三种状态,只是一个复制语句,相当于三条汇编指令。建议使用第二种,通过条件编译,保留第一种,但不编译。最后,写成两个LED.c和LED.h文件,供日后使用。

 

 

程序:

[plain] view plaincopy
  1. /*********************************************  
  2.     标题:LED_test.c  
  3.     软件平台:CCS v5.2  
  4.     硬件平台:C2000 LaunchPad  
  5.     主频:60M  
  6.   
  7.       描述:练习gpio,测试led  
  8.   
  9.       基于2802x C/C++ Header Files V1.26  
  10.   
  11.   
  12.     author:小船  
  13.     data:2012-09-23  
  14.   
  15.     As supplied, this project is configured for "boot to SARAM"  
  16.     operation.  The 2802x Boot Mode table is shown below.  
  17.   
  18.     $Boot_Table  
  19.     While an emulator is connected to your device, the TRSTn pin = 1,  
  20.     which sets the device into EMU_BOOT boot mode. In this mode, the  
  21.     peripheral boot modes are as follows:  
  22.   
  23.       Boot Mode:   EMU_KEY        EMU_BMODE  
  24.                    (0xD00)       (0xD01)  
  25.       ---------------------------------------  
  26.       Wait       !=0x55AA           X  
  27.       I/O          0x55AA            0x0000  
  28.       SCI          0x55AA            0x0001  
  29.       Wait         0x55AA            0x0002  
  30.       Get_Mode     0x55AA            0x0003  
  31.       SPI          0x55AA            0x0004  
  32.       I2C          0x55AA            0x0005  
  33.       OTP          0x55AA            0x0006  
  34.       Wait         0x55AA            0x0007  
  35.       Wait         0x55AA            0x0008  
  36.       SARAM        0x55AA            0x000A   <-- "Boot to SARAM"  
  37.       Flash        0x55AA            0x000B  
  38.       Wait         0x55AA            Other  
  39.   
  40.    Write EMU_KEY to 0xD00 and EMU_BMODE to 0xD01 via the debugger  
  41.    according to the Boot Mode Table above. Build/Load project,  
  42.    Reset the device, and Run example  
  43.   
  44.    $End_Boot_Table  
  45. **********************************************/  
  46.   
  47. #include "DSP28x_Project.h"     // Device Headerfile and Examples Include File  
  48.   
  49. #define LED0 0x00000001  
  50. #define LED1 0x00000002  
  51. #define LED2 0x00000004  
  52. #define LED3 0x00000008  
  53.   
  54. #define LED_on(LED_num)           GpioDataRegs.GPACLEAR.all = LED_num  
  55. #define LED_off(LED_num)          GpioDataRegs.GPASET.all = LED_num  
  56. #define LED_toggle(LED_num)       GpioDataRegs.GPATOGGLE.all = LED_num  
  57.   
  58. #define config_leds_by_funtion  
  59.   
  60.   
  61. #ifdef config_leds_by_funtion  
  62. enum LED_action {on, off, toggle};  
  63. void LEDs(Uint32 LED_num, enum LED_action action);  
  64. #endif  
  65.   
  66. // Prototype statements for functions found within this file.  
  67. void Gpio_select(void);  
  68. void LEDs_init();  
  69.   
  70.   
  71. void main(void)  
  72. {  
  73.   
  74. // Step 1. Initialize System Control:  
  75. // PLL, WatchDog, enable Peripheral Clocks  
  76. // This example function is found in the DSP2802x_SysCtrl.c file.  
  77.    InitSysCtrl();  
  78.   
  79. // Step 2. Initalize GPIO:  
  80. // This example function is found in the DSP2802x_Gpio.c file and  
  81. // illustrates how to set the GPIO to it's default state.  
  82. // InitGpio();  // Skipped for this example  
  83.   
  84. // For this example use the following configuration:  
  85.    Gpio_select();  
  86.   
  87. // Step 3. Clear all interrupts and initialize PIE vector table:  
  88. // Disable CPU interrupts  
  89.    DINT;  
  90.   
  91. // Initialize PIE control registers to their default state.  
  92. // The default state is all PIE interrupts disabled and flags  
  93. // are cleared.  
  94. // This function is found in the DSP2802x_PieCtrl.c file.  
  95.    InitPieCtrl();  
  96.   
  97. // Disable CPU interrupts and clear all CPU interrupt flags:  
  98.    IER = 0x0000;  
  99.    IFR = 0x0000;  
  100.   
  101. // Initialize the PIE vector table with pointers to the shell Interrupt  
  102. // Service Routines (ISR).  
  103. // This will populate the entire table, even if the interrupt  
  104. // is not used in this example.  This is useful for debug purposes.  
  105. // The shell ISR routines are found in DSP2802x_DefaultIsr.c.  
  106. // This function is found in DSP2802x_PieVect.c.  
  107.    InitPieVectTable();  
  108.   
  109.   
  110. // Step 4. Initialize all the Device Peripherals:  
  111. // This function is found in DSP2802x_InitPeripherals.c  
  112. // InitPeripherals(); // Not required for this example  
  113.   
  114. // Step 5. User specific code:  
  115.   
  116.    LEDs_init();  
  117.   
  118.    while(1)  
  119.    {  
  120.        LEDs(LED0|LED1|LED2|LED3, off);  
  121.        DELAY_US(1000000);  
  122.        LEDs(LED1, on);  
  123.        DELAY_US(1000000);  
  124.        LEDs(LED2, toggle);  
  125.        DELAY_US(1000000);  
  126.        LED_toggle(LED1);  
  127.        DELAY_US(1000000);  
  128.    }  
  129. }  
  130.   
  131. void LEDs_init()  
  132. {  
  133.     EALLOW;  
  134.     //使能上拉电阻  
  135.     GpioCtrlRegs.GPAPUD.bit.GPIO0 = 1;  
  136.     GpioCtrlRegs.GPAPUD.bit.GPIO1 = 1;  
  137.     GpioCtrlRegs.GPAPUD.bit.GPIO2 = 1;  
  138.     GpioCtrlRegs.GPAPUD.bit.GPIO3 = 1;  
  139.   
  140.     //选择GPIO功能  
  141.     GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0x0;  
  142.     GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0x0;  
  143.     GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 0x0;  
  144.     GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 0x0;  
  145.   
  146.     //方向  
  147.     GpioCtrlRegs.GPADIR.bit.GPIO0 = 1;  
  148.     GpioCtrlRegs.GPADIR.bit.GPIO1 = 1;  
  149.     GpioCtrlRegs.GPADIR.bit.GPIO2 = 1;  
  150.     GpioCtrlRegs.GPADIR.bit.GPIO3 = 1;  
  151.   
  152.     EDIS;  
  153. }  
  154.   
  155. #ifdef config_leds_by_funtion  
  156. void LEDs(Uint32 LED_num, enum LED_action action)  
  157. {  
  158.     switch (action)  
  159.     {  
  160.         case on:{  
  161.             GpioDataRegs.GPACLEAR.all = LED_num;  
  162.             break;  
  163.         }  
  164.   
  165.         case off:{  
  166.             GpioDataRegs.GPASET.all = LED_num;  
  167.             break;  
  168.         }  
  169.   
  170.         case toggle:{  
  171.             GpioDataRegs.GPATOGGLE.all = LED_num;  
  172.             break;  
  173.         }  
  174.     }  
  175. }  
  176. #endif  
  177.   
  178. void Gpio_select(void)  
  179. {  
  180.   
  181.     EALLOW;  
  182.     GpioCtrlRegs.GPAMUX1.all = 0x00000000;  // All GPIO  
  183.     GpioCtrlRegs.GPAMUX2.all = 0x00000000;  // All GPIO  
  184.     GpioCtrlRegs.GPBMUX1.all = 0x00000000;  // All GPIO  
  185.     GpioCtrlRegs.GPADIR.all = 0xFFFFFFFF;   // All outputs  
  186.     GpioCtrlRegs.GPBDIR.all = 0x0000000F;   // All outputs  
  187.     EDIS;  
  188.   
  189. }  
  190. //===========================================================================  
  191. // No more.  
  192. //===========================================================================  




[plain] view plaincopy
  1. /************************************  
  2.     标题:LEDs.h  
  3.     软件平台:CCS v5.2  
  4.     硬件平台:C2000 LaunchPad  
  5.     主频:60M  
  6.   
  7.     author:小船  
  8.     data:2012-09-23  
  9. *************************************/  
  10. #ifndef LEDS_H_  
  11. #define LEDS_H_  
  12.   
  13. #include "DSP28x_Project.h"  
  14.   
  15. #define no_config_leds_by_funtion  
  16.   
  17. #define LED0 0x00000001  
  18. #define LED1 0x00000002  
  19. #define LED2 0x00000004  
  20. #define LED3 0x00000008  
  21.   
  22. #define LED_on(LED_num)           GpioDataRegs.GPACLEAR.all = LED_num  
  23. #define LED_off(LED_num)          GpioDataRegs.GPASET.all = LED_num  
  24. #define LED_toggle(LED_num)       GpioDataRegs.GPATOGGLE.all = LED_num  
  25.   
  26.   
  27. #ifdef config_leds_by_funtion  
  28. enum LED_action {on, off, toggle};  
  29. void LEDs(Uint32 LED_num, enum LED_action action);  
  30. #endif  
  31.   
  32. void LEDs_init();  
  33.   
  34. #endif /* LEDS_H_ */  




[plain] view plaincopy
  1. /************************************  
  2.     标题:LEDs.c  
  3.     软件平台:CCS v5.2  
  4.     硬件平台:C2000 LaunchPad  
  5.     主频:60M  
  6.   
  7.     author:小船  
  8.     data:2012-09-23  
  9. *************************************/  
  10. #include "LEDs.h"  
  11.   
  12. void LEDs_init()  
  13. {  
  14.     EALLOW;  
  15.     //使能上拉电阻  
  16.     GpioCtrlRegs.GPAPUD.bit.GPIO0 = 1;  
  17.     GpioCtrlRegs.GPAPUD.bit.GPIO1 = 1;  
  18.     GpioCtrlRegs.GPAPUD.bit.GPIO2 = 1;  
  19.     GpioCtrlRegs.GPAPUD.bit.GPIO3 = 1;  
  20.   
  21.     //选择GPIO功能  
  22.     GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0x0;  
  23.     GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0x0;  
  24.     GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 0x0;  
  25.     GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 0x0;  
  26.   
  27.     //方向  
  28.     GpioCtrlRegs.GPADIR.bit.GPIO0 = 1;  
  29.     GpioCtrlRegs.GPADIR.bit.GPIO1 = 1;  
  30.     GpioCtrlRegs.GPADIR.bit.GPIO2 = 1;  
  31.     GpioCtrlRegs.GPADIR.bit.GPIO3 = 1;  
  32.   
  33.     EDIS;  
  34. }  
  35.   
  36. #ifdef config_leds_by_funtion  
  37. void LEDs(Uint32 LED_num, enum LED_action action)  
  38. {  
  39.     switch (action)  
  40.     {  
  41.         case on:{  
  42.             GpioDataRegs.GPACLEAR.all = LED_num;  
  43.             break;  
  44.         }  
  45.   
  46.         case off:{  
  47.             GpioDataRegs.GPASET.all = LED_num;  
  48.             break;  
  49.         }  
  50.   
  51.         case toggle:{  
  52.             GpioDataRegs.GPATOGGLE.all = LED_num;  
  53.             break;  
  54.         }  
  55.     }  
  56. }  
  57. #endif  


原创粉丝点击