|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
GPIO通常用来做普通的输入输出,比如按键啊,LED啊,或者芯片的片选段之类的,算是最基本的操作。8 I* v- N* x6 A/ o; I; o8 m2 {$ C
话不多说,直接上 代码+注释 记录一下,以备以后使用6 R: h. X4 S F6 w, G/ ~: y: @
我用的板子是研旭实用板
" Y8 {# n$ a# ]2 Z' n' i9 Q- R! m: r, P0 {% h6 U
led.c
$ l6 ~( T, B" K. v7 _; J8 T#include "led.h"
6 @* _! r. f, z; o9 [) G* U//LED和蜂鸣器初始化* H- ]/ l( L7 ]$ U0 P. A3 r+ E& ]- |
void Led_Gpoi_Init(void)5 X$ m/ L8 N5 B+ Q: w
{
; T* K2 C2 w- `EALLOW;' S/ t, j: @9 x& U
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0; // GPIO0复用为GPIO功能; C; r8 n& T8 C6 f
GpioCtrlRegs.GPADIR.bit.GPIO0 = 1; // GPIO0设置为输出7 i y; T& T1 w5 L: @- q
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0; // GPIO1 = GPIO1; h8 K. ^( `# h b; d
GpioCtrlRegs.GPADIR.bit.GPIO1 = 1; //GPIO0设置为输出
3 f% }- y% G1 CGpioCtrlRegs.GPAMUX1.bit.GPIO6 = 0; //配置同上
& T2 c# ]6 }- a6 y _+ a8 b AGpioCtrlRegs.GPADIR.bit.GPIO6 = 1;
) N6 z$ e5 X/ CGpioCtrlRegs.GPAMUX1.bit.GPIO7 = 0; //; v, L+ B: x0 v ]
GpioCtrlRegs.GPADIR.bit.GPIO7 = 1;
% g0 p1 g) h; g2 j& t& }GpioCtrlRegs.GPBMUX2.bit.GPIO60 = 0; //蜂鸣器GPIO设置,GPIO60复用为GPIO功能
( H5 Z( W) x. f) L7 M+ j' T GpioCtrlRegs.GPBDIR.bit.GPIO60 = 1; //设置为输出
! [; y- }1 W* k/ F LED1 = 1; LED2 = 1; LED3 = 1; LED4 = 1;BUZZ_OFF
7 a) M j2 h6 v( }# S: G; IEDIS;1 ?+ y( q ]' H7 j
}/ c+ l+ D" T/ P7 N2 o
" o, X* k- A/ w
led.h3 p% Z. l* F+ N& q1 s
#ifndef LED_H_* X8 }! H8 B6 y$ A, p& q
#define LED_H_! ~3 W' N4 Z5 J, s
9 D7 \2 |1 {3 m# z6 q; R; V& C#include "main.h"
$ l: }# o9 V. T0 V( o/ ^( D1 R* b: t5 S! S: e: r% F
//根据原理图,低电平LED亮
& n+ K+ X) f6 R& R#define LED3 GpioDataRegs.GPADAT.bit.GPIO0 //宏定义LED的数据寄存器
6 s% w- r1 [3 {" F* g# f4 y/ L: ?: M#define LED4 GpioDataRegs.GPADAT.bit.GPIO1: L5 H- T S" I# ?9 j, e1 h
#define LED1 GpioDataRegs.GPADAT.bit.GPIO6
2 }; ]( M G4 t#define LED2 GpioDataRegs.GPADAT.bit.GPIO7& x, F: g/ J2 b; I. d3 G
2 F' U- }0 a; Y$ `. B#define BUZZ_OFFGpioDataRegs.GPBSET.bit.GPIO60=1; //蜂鸣器不鸣叫
' ^) n0 G( ^: G0 o#define BUZZ_ONGpioDataRegs.GPBCLEAR.bit.GPIO60=1;//蜂鸣器鸣叫
# |5 Z4 T# w+ X7 ?9 h; B7 |* j, O* _% U, P8 q& p1 W" R' E
void Led_Gpoi_Init(void);5 {6 P: ?- L4 w9 y' ?
) r) I( y9 Q7 }4 N5 h" k+ N. i
#endif /* LED_H_ */
& K! ~8 u M* i& L0 x' V1 l: G, X
9 O; s. Q7 e& i" {, U Y1 Vkey.c
8 Q& V7 ^$ j" u! ^" W#include "key.h"& z: b1 N9 Q. Y$ b
//按键IO口初始化! [( n5 X6 u3 l/ X0 y6 R
void Key_Gpoi_Init(void)
9 G( |/ ?; ^5 l- @{
# @. A. m( \5 T5 r5 ]- oEALLOW;
* b; v$ W8 r& a; I7 {GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0;//按键GPIO设置,GPIO13复用为GPIO功能0 b8 ^# L/ k: c
GpioCtrlRegs.GPADIR.bit.GPIO13 = 0; //设置为输入
6 ~* y u: E) f3 Z$ F: BEDIS;7 U$ D( C) F" |1 F* N
}8 i( d* V* a# a u1 L5 I- G
4 _ m) o* r$ x4 e
! X \5 W2 X3 ^5 b1 q# x {: I. o
u8 KeyScan(void)" b6 \6 O4 P, a1 f
{
9 q+ F; i4 }5 mstatic u8 key_up=1;//按键按松开标志
( U. r3 l, n3 D7 v- c9 C8 i0 E# L* `# P9 K
. |1 {: S# j2 ?5 @0 ^
if( key_up&&(KEY0==0) ) //有键按下 ||KEY1==0||KEY2==0)
4 X* m* E4 | d1 e) q- k{
0 b( I0 x2 o" G, |! \$ D DELAY_US(10000);//去抖动2 j& L1 e0 N5 _# ^" _. F2 c
key_up=0; //表示按键没松开 R3 ]) }4 m; X* o. w7 q
if(KEY0==0) //按下KEY0
2 K# H6 t+ M9 M' G9 ^/ } ]# I {
3 L% s/ L/ E- N! `& X! F7 e+ b return 1;0 m, X8 s6 H2 X" S
}
/ ]: {3 h$ u; g; P* w. K// else if(KEY1==0)0 I# N. c. I4 }5 F4 |1 |
// {return 2;}
2 _8 ], \$ c3 K; o( c// else if(KEY2==1)
3 b" v& B" g: @: e// {return 3;}}
7 {0 N ]6 @* J. j" j' C4 {}: y6 D: ?7 E/ F8 D4 {; }' J
else if(KEY0==1)key_up=1; //&&KEY1==1&&KEY2==1没有键按下
7 T g- b" E( V$ z$ f: C- q return 0;// 无按键按下
9 u# X: v5 W, m2 l8 k}2 z& b, f# {7 p! ?: \: M: i; t+ h
& z2 m# y. Q) \* w ]' Ykey.h
" A$ m1 p' ]$ ]#ifndef KEY_H_
6 ~" n" I# o3 a5 R3 p3 Y2 k#define KEY_H_
, v+ b4 ^- C( {1 l2 m9 v& u#define KEY0 GpioDataRegs.GPADAT.bit.GPIO13& `7 ?* k& U/ r: V' y
#include "main.h"! u; h9 [$ z6 M% N! g
! E) W b1 R$ p2 o4 P0 S: j
void Key_Gpoi_Init(void);
5 T# S0 A) _$ ju8 KeyScan(void);* p- t5 u# G$ a0 Y/ ?/ A. e
9 R( g$ C/ M- m% d0 ^' x5 z, H
#endif /* KEY_H_ */
- P& N B8 L1 D4 @2 M+ L0 l$ k/ r Z5 i, _) I: @" \
MAIN.C7 `% m& E6 g" G6 L0 R( I
void main(void)8 X( @/ S. _' l
{+ r8 y( Q6 }5 C& I5 ^8 i
u8 key=0;
% S3 F4 w1 x' Y$ c//初始化
2 j3 R$ M0 V( K' s$ R) T InitSysCtrl(); //初始化系统时钟到150M,关看门狗,开启外设时钟
, k" p; e, B9 t InitGpio(); // 初始化GPIO到复位状态4 e. i7 ~6 n9 m" ^1 v# [. J
3 G$ a2 `3 r9 ^
DINT; //关闭CPU中断
W0 E" X5 e3 J9 j4 A% C InitPieCtrl();, v& S: j% I s! T& M- ?/ ^
IER = 0x0000;. b% a( f6 L6 }% V( r/ N
IFR = 0x0000;6 o) T, O& [1 g1 S3 R" [% d
InitPieVectTable(); //初始化中断向量表
3 N _2 W* x! @: z& E
4 @! k2 Y$ V; h2 p5 X5 X* V //外设配置: C/ M& Q( K: G# y8 `8 |- Q, t
Led_Gpoi_Init(); //led端口初始化
0 d1 Y. T! T U" [7 s4 @ Key_Gpoi_Init(); //按键IO初始化3 ]$ ?: p2 C: a/ M1 C
while(1) F# C( `+ c( m6 ^
{
- n. y$ T0 V: M8 C* R key = KeyScan() ; //扫描按键
1 ^0 g [7 Y' t3 k switch(key) //根据键值来做出不同的相应# W2 I, v% Q4 z& u2 b8 G
{* U3 S0 I( J% h# {' P ^* r7 `. L
case 1ED1=0;break;5 C8 n+ w7 Z( {3 ^5 u
case 2:....... Q5 G. w$ ~8 o5 n7 Z
.....9 K g4 z8 c8 e% b8 i
........ t0 {5 X; B2 ]% V! g& a# \7 B
}
/ x5 e3 Z( `% v/ D4 G }
) ^0 r0 w! a- i W7 o( z7 l; R}
0 ]- W8 O2 L7 P3 [+ l% v# ^9 I1 B
" d3 O. T: C+ e fmain.h
: S- K: S9 G! Z#ifndef _MAIN_H
+ o" g7 S- x8 e _3 z5 m; ?+ z#define _MAIN_H' \) b$ h0 b& c
5 }: n0 q0 x' q4 C7 ]/ u#include "DSP2833x_Device.h" // Headerfile Include File
+ c+ ]! A' b' l9 g8 E#include "DSP2833x_Examples.h" // Examples Include File0 s. n2 l1 W& k+ ^8 k4 ^- ?
$ _( E1 @: A" t" K/ L4 ^/ f4 o
#include "flash_ram.h"- @( L, m' u, B) ?& S8 \6 A
#include "led.h"
1 }2 U l+ u# n' ~6 s#include "key.h"9 k! B! A' e/ U8 J
#include "extint.h"
; \: r A3 ^; t- M% N4 i4 u#include "timer.h"
, x- {; W5 I5 z" W6 H, e#include "sci.h"4 O3 [0 k3 u) F% s- V/ V3 a; c
#include "pwm.h"+ }% R) D+ y( P0 C. W" o8 D
#include "cap.h"
) o4 P/ m5 x( G* K, G' d#include "ad.h"
' l% o- f% j. i" ]9 v$ X1 p#include "can.h"
% P5 }6 f' m: H* {/ h' Y#include "spi.h"2 N- Z& o1 {* u1 A$ H- P
Q* t5 o, r, L% v, j- Y$ g8 Q7 o
#include "NRF2401.h"
; o% [. w6 A% P, `, Q2 I8 M8 A; v; |' G6 O7 A
#endif
( Z/ c* e6 a6 k C3 k |
|