查看: 597|回复: 0

[经验] 关于28335的GPIO基本的操作

[复制链接]

该用户从未签到

发表于 2021-3-2 09:19:29 | 显示全部楼层 |阅读模式
分享到:
GPIO通常用来做普通的输入输出,比如按键啊,LED啊,或者芯片的片选段之类的,算是最基本的操作。

话不多说,直接上 代码+注释 记录一下,以备以后使用

我用的板子是研旭实用板



  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">led.c</span>

  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "led.h"</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//LED和蜂鸣器初始化</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void Led_Gpoi_Init(void)</span>
  5. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  6. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">EALLOW;</span>
  7. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0; // GPIO0复用为GPIO功能</span>
  8. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPADIR.bit.GPIO0 = 1;  // GPIO0设置为输出</span>
  9. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0; // GPIO1 = GPIO1</span>
  10. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPADIR.bit.GPIO1 = 1;  //GPIO0设置为输出</span>
  11. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 0; //配置同上</span>
  12. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPADIR.bit.GPIO6 = 1;</span>
  13. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 0; //</span>
  14. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPADIR.bit.GPIO7 = 1;</span>
  15. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPBMUX2.bit.GPIO60 = 0; //蜂鸣器GPIO设置,GPIO60复用为GPIO功能</span>
  16. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">       GpioCtrlRegs.GPBDIR.bit.GPIO60 = 1;  //设置为输出</span>
  17. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">        LED1 = 1;  LED2 = 1;  LED3 = 1;  LED4 = 1;BUZZ_OFF</span>
  18. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">EDIS;</span>
  19. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>



  20. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">led.h</span>

  21. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#ifndef LED_H_</span>
  22. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define LED_H_</span>

  23. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "main.h"</span>

  24. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//根据原理图,低电平LED亮</span>
  25. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define  LED3 GpioDataRegs.GPADAT.bit.GPIO0   //宏定义LED的数据寄存器</span>
  26. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define  LED4 GpioDataRegs.GPADAT.bit.GPIO1</span>
  27. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define  LED1 GpioDataRegs.GPADAT.bit.GPIO6</span>
  28. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define  LED2 GpioDataRegs.GPADAT.bit.GPIO7</span>

  29. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define  BUZZ_OFFGpioDataRegs.GPBSET.bit.GPIO60=1; //蜂鸣器不鸣叫</span>
  30. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define  BUZZ_ONGpioDataRegs.GPBCLEAR.bit.GPIO60=1;//蜂鸣器鸣叫</span>

  31. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void Led_Gpoi_Init(void);</span>

  32. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#endif /* LED_H_ */</span>



  33. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">key.c</span>

  34. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "key.h"</span>
  35. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//按键IO口初始化</span>
  36. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void Key_Gpoi_Init(void)</span>
  37. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  38. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">EALLOW;</span>
  39. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0;//按键GPIO设置,GPIO13复用为GPIO功能</span>
  40. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPADIR.bit.GPIO13 = 0;  //设置为输入</span>
  41. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">EDIS;</span>
  42. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>


  43. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">u8 KeyScan(void)</span>
  44. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  45. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static u8 key_up=1;//按键按松开标志</span>


  46. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    if( key_up&&(KEY0==0) )  //有键按下 ||KEY1==0||KEY2==0)</span>
  47. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  48. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">       DELAY_US(10000);//去抖动</span>
  49. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  key_up=0;     //表示按键没松开</span>
  50. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  if(KEY0==0)    //按下KEY0</span>
  51. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  {</span>
  52. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  return 1;</span>
  53. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  }</span>
  54. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//   else if(KEY1==0)</span>
  55. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//   {return 2;}</span>
  56. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//   else if(KEY2==1)</span>
  57. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//   {return 3;}}</span>
  58. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
  59. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    else if(KEY0==1)key_up=1;  //&&KEY1==1&&KEY2==1没有键按下</span>
  60. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   return 0;// 无按键按下</span>
  61. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>



  62. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">key.h</span>

  63. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#ifndef KEY_H_</span>
  64. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define KEY_H_</span>

  65. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define KEY0  GpioDataRegs.GPADAT.bit.GPIO13</span>
  66. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "main.h"</span>

  67. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void Key_Gpoi_Init(void);</span>
  68. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">u8 KeyScan(void);</span>

  69. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#endif /* KEY_H_ */</span>



  70. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">MAIN.C</span>

  71. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void main(void)</span>
  72. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  73. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    u8 key=0;   </span>

  74. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//初始化</span>
  75. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">     InitSysCtrl(); //初始化系统时钟到150M,关看门狗,开启外设时钟</span>
  76. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">     InitGpio();    // 初始化GPIO到复位状态</span>

  77. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    DINT;              //关闭CPU中断</span>
  78. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    InitPieCtrl();</span>
  79. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    IER = 0x0000;</span>
  80. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    IFR = 0x0000;</span>
  81. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    InitPieVectTable();  //初始化中断向量表</span>

  82. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    //外设配置</span>
  83. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    Led_Gpoi_Init();   //led端口初始化</span>

  84. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    Key_Gpoi_Init(); //按键IO初始化</span>

  85. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    while(1)</span>

  86. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    {</span>

  87. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">       key = KeyScan()  ; //扫描按键</span>

  88. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">      switch(key)        //根据键值来做出不同的相应</span>

  89. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">      {</span>

  90. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">     case 1:LED1=0;break;</span>

  91. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">     case 2:.......</span>

  92. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">      .....</span>

  93. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    .......</span>

  94. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    }</span>

  95. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   }</span>

  96. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>



  97. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">main.h</span>

  98. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#ifndef _MAIN_H</span>
  99. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define _MAIN_H</span>

  100. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "DSP2833x_Device.h"     // Headerfile Include File</span>
  101. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "DSP2833x_Examples.h"   // Examples Include File</span>

  102. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "flash_ram.h"</span>
  103. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "led.h"</span>
  104. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "key.h"</span>
  105. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "extint.h"</span>
  106. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "timer.h"</span>
  107. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "sci.h"</span>
  108. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "pwm.h"</span>
  109. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "cap.h"</span>
  110. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "ad.h"</span>
  111. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "can.h"</span>
  112. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "spi.h"</span>


  113. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "NRF2401.h"</span>


  114. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#endif</span>
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /2 下一条



手机版|小黑屋|与非网

GMT+8, 2024-4-20 08:09 , Processed in 0.108080 second(s), 15 queries , MemCache On.

ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.