|
GPIO通常用来做普通的输入输出,比如按键啊,LED啊,或者芯片的片选段之类的,算是最基本的操作。
话不多说,直接上 代码+注释 记录一下,以备以后使用
我用的板子是研旭实用板
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">led.c</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "led.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//LED和蜂鸣器初始化</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void Led_Gpoi_Init(void)</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">EALLOW;</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0; // GPIO0复用为GPIO功能</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPADIR.bit.GPIO0 = 1; // GPIO0设置为输出</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0; // GPIO1 = GPIO1</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPADIR.bit.GPIO1 = 1; //GPIO0设置为输出</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 0; //配置同上</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPADIR.bit.GPIO6 = 1;</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 0; //</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPADIR.bit.GPIO7 = 1;</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPBMUX2.bit.GPIO60 = 0; //蜂鸣器GPIO设置,GPIO60复用为GPIO功能</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> GpioCtrlRegs.GPBDIR.bit.GPIO60 = 1; //设置为输出</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> LED1 = 1; LED2 = 1; LED3 = 1; LED4 = 1;BUZZ_OFF</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">EDIS;</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">led.h</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#ifndef LED_H_</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define LED_H_</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "main.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//根据原理图,低电平LED亮</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define LED3 GpioDataRegs.GPADAT.bit.GPIO0 //宏定义LED的数据寄存器</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define LED4 GpioDataRegs.GPADAT.bit.GPIO1</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define LED1 GpioDataRegs.GPADAT.bit.GPIO6</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define LED2 GpioDataRegs.GPADAT.bit.GPIO7</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define BUZZ_OFFGpioDataRegs.GPBSET.bit.GPIO60=1; //蜂鸣器不鸣叫</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define BUZZ_ONGpioDataRegs.GPBCLEAR.bit.GPIO60=1;//蜂鸣器鸣叫</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void Led_Gpoi_Init(void);</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#endif /* LED_H_ */</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">key.c</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "key.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//按键IO口初始化</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void Key_Gpoi_Init(void)</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">EALLOW;</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0;//按键GPIO设置,GPIO13复用为GPIO功能</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">GpioCtrlRegs.GPADIR.bit.GPIO13 = 0; //设置为输入</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">EDIS;</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">u8 KeyScan(void)</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static u8 key_up=1;//按键按松开标志</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> if( key_up&&(KEY0==0) ) //有键按下 ||KEY1==0||KEY2==0)</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> DELAY_US(10000);//去抖动</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> key_up=0; //表示按键没松开</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> if(KEY0==0) //按下KEY0</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> {</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> return 1;</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> }</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// else if(KEY1==0)</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// {return 2;}</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// else if(KEY2==1)</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// {return 3;}}</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> else if(KEY0==1)key_up=1; //&&KEY1==1&&KEY2==1没有键按下</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> return 0;// 无按键按下</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">key.h</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#ifndef KEY_H_</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define KEY_H_</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define KEY0 GpioDataRegs.GPADAT.bit.GPIO13</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "main.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void Key_Gpoi_Init(void);</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">u8 KeyScan(void);</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#endif /* KEY_H_ */</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">MAIN.C</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void main(void)</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> u8 key=0; </span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//初始化</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> InitSysCtrl(); //初始化系统时钟到150M,关看门狗,开启外设时钟</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> InitGpio(); // 初始化GPIO到复位状态</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> DINT; //关闭CPU中断</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> InitPieCtrl();</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> IER = 0x0000;</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> IFR = 0x0000;</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> InitPieVectTable(); //初始化中断向量表</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> //外设配置</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> Led_Gpoi_Init(); //led端口初始化</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> Key_Gpoi_Init(); //按键IO初始化</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> while(1)</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> {</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> key = KeyScan() ; //扫描按键</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> switch(key) //根据键值来做出不同的相应</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> {</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> case 1:LED1=0;break;</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> case 2:.......</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> .....</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> .......</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> }</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> }</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">main.h</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#ifndef _MAIN_H</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#define _MAIN_H</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "DSP2833x_Device.h" // Headerfile Include File</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "DSP2833x_Examples.h" // Examples Include File</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "flash_ram.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "led.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "key.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "extint.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "timer.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "sci.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "pwm.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "cap.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "ad.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "can.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "spi.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "NRF2401.h"</span>
- <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#endif</span>
复制代码
|
|