DSP28335 GPIO模块分为三类IO口:PORTA(0-31),PORTB(32-63),PORTC(64-87)
对GPIO模块的设置主要通过三类寄存器来完成,分别是:控制寄存器、数据寄存器、中断寄存器。
GPxCTRL; // GPIO x Control Register (GPIO0 to 31)
//设置采样窗周期T=2*GPXCTRL*Tsysclk;
GPxQSEL1; // GPIO x Qualifier Select 1 Register (GPIO0 to 15)(32-47)
GPxQSEL2; // GPIO x Qualifier Select 2 Register (GPIO16 to 31)(48-63)
//每两位控制一个引脚,确定是3周期采样还是6周期采样或者不用采样
GPxMUX1; // GPIO x Mux 1 Register (GPIO0 to 15)(32-47)(64-79)
GPxMUX2; // GPIO x Mux 2 Register (GPIO16 to 31)(48-63)(80-95)
//配置各个引脚的功能,0:I/O功能,1:外设功能。
GPxDIR; // GPIO x Direction Register (GPIO0 to 31)(32-63)(64-95)
//配置每个引脚是输入还是输出,0:数字量输入;1:数字量输出。
GPxPUD; // GPIO x Pull Up Disable Register (GPIO0 to 31)(32-63)(64-95)
//使能或禁止内部上拉 0:开启上拉,1:禁止上拉
GPxSET; // GPIO Data Set Register (GPIO0 to 31)(32-63)(64-95)——置位
GPxCLEAR; // GPIO Data Clear Register (GPIO0 to 31)(32-63)(64-95)
GPxTOGGLE; // GPIO Data Toggle Register (GPIO0 to 31)(32-63)(64-95)—反转
GPIOXINT2SEL; // XINT2 GPIO Input Selection
GPIOXNMISEL; // XNMI_Xint13 GPIO Input Selection
GPIOXINT3SEL; // XINT3 GPIO Input Selection
GPIOXINT4SEL; // XINT4 GPIO Input Selection
GPIOXINT5SEL; // XINT5 GPIO Input Selection
GPIOXINT6SEL; // XINT6 GPIO Input Selection
GPIOXINT7SEL; // XINT7 GPIO Input Selection
GPIOLPMSEL; // Low power modes GP I/O input select
可以对GPIO0-63进行外部中断设置;
具体定义在DSP28335Gpio.h中,如下:
struct GPIO_CTRL_REGS { union GPACTRL_REG GPACTRL; // GPIO A Control Register (GPIO0 to 31) union GPA1_REG GPAQSEL1; // GPIO A Qualifier Select 1 Register (GPIO0 to 15) union GPA2_REG GPAQSEL2; // GPIO A Qualifier Select 2 Register (GPIO16 to 31) union GPA1_REG GPAMUX1; // GPIO A Mux 1 Register (GPIO0 to 15) union GPA2_REG GPAMUX2; // GPIO A Mux 2 Register (GPIO16 to 31) union GPADAT_REG GPADIR; // GPIO A Direction Register (GPIO0 to 31) union GPADAT_REG GPAPUD; // GPIO A Pull Up Disable Register (GPIO0 to 31) Uint32 rsvd1; union GPBCTRL_REG GPBCTRL; // GPIO B Control Register (GPIO32 to 63) union GPB1_REG GPBQSEL1; // GPIO B Qualifier Select 1 Register (GPIO32 to 47) union GPB2_REG GPBQSEL2; // GPIO B Qualifier Select 2 Register (GPIO48 to 63) union GPB1_REG GPBMUX1; // GPIO B Mux 1 Register (GPIO32 to 47) union GPB2_REG GPBMUX2; // GPIO B Mux 2 Register (GPIO48 to 63) union GPBDAT_REG GPBDIR; // GPIO B Direction Register (GPIO32 to 63) union GPBDAT_REG GPBPUD; // GPIO B Pull Up Disable Register (GPIO32 to 63) Uint16 rsvd2[8]; union GPC1_REG GPCMUX1; // GPIO C Mux 1 Register (GPIO64 to 79) union GPC2_REG GPCMUX2; // GPIO C Mux 2 Register (GPIO80 to 95) union GPCDAT_REG GPCDIR; // GPIO C Direction Register (GPIO64 to 95) union GPCDAT_REG GPCPUD; // GPIO C Pull Up Disable Register (GPIO64 to 95) }; struct GPIO_DATA_REGS { union GPADAT_REG GPADAT; // GPIO Data Register (GPIO0 to 31) union GPADAT_REG GPASET; // GPIO Data Set Register (GPIO0 to 31) union GPADAT_REG GPACLEAR; // GPIO Data Clear Register (GPIO0 to 31) union GPADAT_REG GPATOGGLE; // GPIO Data Toggle Register (GPIO0 to 31) union GPBDAT_REG GPBDAT; // GPIO Data Register (GPIO32 to 63) union GPBDAT_REG GPBSET; // GPIO Data Set Register (GPIO32 to 63) union GPBDAT_REG GPBCLEAR; // GPIO Data Clear Register (GPIO32 to 63) union GPBDAT_REG GPBTOGGLE; // GPIO Data Toggle Register (GPIO32 to 63) union GPCDAT_REG GPCDAT; // GPIO Data Register (GPIO64 to 95) union GPCDAT_REG GPCSET; // GPIO Data Set Register (GPIO64 to 95) union GPCDAT_REG GPCCLEAR; // GPIO Data Clear Register (GPIO64 to 95) union GPCDAT_REG GPCTOGGLE; // GPIO Data Toggle Register (GPIO64 to 95) Uint16 rsvd1[8]; }; struct GPIO_INT_REGS { union GPIOXINT_REG GPIOXINT1SEL; // XINT1 GPIO Input Selection union GPIOXINT_REG GPIOXINT2SEL; // XINT2 GPIO Input Selection union GPIOXINT_REG GPIOXNMISEL; // XNMI_Xint13 GPIO Input Selection union GPIOXINT_REG GPIOXINT3SEL; // XINT3 GPIO Input Selection union GPIOXINT_REG GPIOXINT4SEL; // XINT4 GPIO Input Selection union GPIOXINT_REG GPIOXINT5SEL; // XINT5 GPIO Input Selection union GPIOXINT_REG GPIOXINT6SEL; // XINT6 GPIO Input Selection union GPIOXINT_REG GPIOXINT7SEL; // XINT7 GPIO Input Selection union GPADAT_REG GPIOLPMSEL; // Low power modes GP I/O input select };
1、GPxMUX寄存器(功能选择寄存器)
每个I/O口都有一个功能选择寄存器,功能选择寄存器主要用于选择I/O工作在特殊功能还是通用数组I/O模式。在复位时,所有GPIO配置成通用数字模式。
1)如果GPxMUX.bit = 0,配置成通用数字I/O功能;
2)如果GPxMUX.bit = 1,配置成特殊外设功能口(如SCI、CAN);
I/O的输入功能和外设的输入通道总是被使能的,输出通道是通用数组I/O和特殊外设复用的。如果引脚配置成通用数组I/O功能,相应的外设功能将被禁止。
2、GPxDIR(方向控制寄存器)
每个I/O口都有数据方向控制寄存器,数据方向控制寄存器用于设置通用数字I/O为输入还是输出口,在复位时,引脚的默认状态为输入状态。
1)如果GPxDIR.bit = 0,引脚设置为通用数字量输入;
2)如果GPxDIR.bit = 1,引脚设置为通用数字量输出;
复位时,GPxMUX和GPxDIR默认值都为0,所以在复位时,引脚的默认状态为数字I/O输入。
3、GPxDAT寄存器(数据寄存器)
每个I/O口都有一个数据寄存器,数据寄存器是可读可写寄存器。
1)I/O设置为输出功能时,如果GPxDAT.bit = 0,那么操作将会使相应的引脚拉低;
2)I/O口设置为输入功能时,如果GPxDAT.bit = 0,反映相应的引脚状态为低电平;
3)I/O口设置为输出功能时,如果GPxDAT.bit = 1,那么操作将会使相应的引脚拉高;
4)I/O口设置为输入功能时,如果GPxDAT.bit = 1,反映相应的引脚状态为高电平。
需要说明的是,当用户试图改变一个数字I/O的状态时,不要改变另一个I/O的引脚状态。
4、GOxSET寄存器(置位寄存器)
每个I/O口都有一个置位寄存器,置位寄存器是只写寄存器,任何读操作都返回0,如果相应的引脚配置成数据量输出,写1后相应的引脚会置高,写0时没有反映。
1)如果GPxSET.bit = 0,没有影响;
2)引脚设置为输出时,如果GPxSET.bit = 1,那么操作将会使引脚置高。
5、GPxCLEAR寄存器(清除寄存器)
每个I/O口都有一个清除寄存器,清除寄存器是只写寄存器,任何读操作都返回0。
1)如果GPxCLEAR.bit = 0,没有影响;
2)引脚设置为输出时,如果GPxCLEAR.bit = 1,将相应的引脚置成低电平。
6、GPxTOGGLE寄存器(取反触发寄存器)
每个I/O口都有一个取反触发寄存器,该寄存器是只写寄存器,任何读操作都返回0。
1)如果GPxTOGGLE.bit = 0,没有影响;
2)引脚设置为输出时,如果GPxTOGGLE.bit = 1,那么操作将使相应的引脚取反。
#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File #include "DSP2833x_Examples.h" // DSP2833x Examples Include File #define LED1 GpioDataRegs.GPBDAT.bit.GPIO60 #define LED2 GpioDataRegs.GPBDAT.bit.GPIO61 interrupt void ISRTimer0(void); void configtestled(void); Uint16 i; void main(void) { InitSysCtrl(); InitXintf16Gpio(); DINT; InitPieCtrl(); // Disable CPU interrupts and clear all CPU interrupt flags: IER = 0x0000; IFR = 0x0000; InitPieVectTable(); EALLOW; // This is needed to write to EALLOW protected registers PieVectTable.TINT0 = &ISRTimer0; //配置中断函数地址 //PieVectTable.XINT13 = &cpu_timer1_isr; //PieVectTable.TINT2 = &cpu_timer2_isr; EDIS; // This is needed to disable write to EALLOW protected registers InitCpuTimers(); // For this example, only initialize the Cpu Timers // Configure CPU-Timer 0, 1, and 2 to interrupt every second: // 150MHz CPU Freq, 1 second Period (in uSeconds) ConfigCpuTimer(&CpuTimer0, 150, 1000000); //配置定时时间 1s //ConfigCpuTimer(&CpuTimer1, 150, 1000000); //ConfigCpuTimer(&CpuTimer2, 150, 1000000); StartCpuTimer0(); //定时器计数 // Enable CPU int1 which is connected to CPU-Timer 0, CPU int13 // which is connected to CPU-Timer 1, and CPU int 14, which is connected // to CPU-Timer 2: IER |= M_INT1; //IER |= M_INT13; //IER |= M_INT14; // Enable TINT0 in the PIE: Group 1 interrupt 7 PieCtrlRegs.PIEIER1.bit.INTx7 = 1;//使能PIE中断 // Enable global Interrupts and higher priority real-time debug events: EINT; // Enable Global interrupt INTM ERTM; // Enable Global realtime interrupt DBGM configtestled(); LED1 = 0; LED2 = 0; i = 0; for(; ;) { } } interrupt void ISRTimer0(void) { CpuTimer0.InterruptCount++; // Acknowledge this interrupt to receive more interrupts from group 1 PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;//PIEACK清0 CpuTimer0Regs.TCR.bit.TIF=1; //外设中断标志位清0 CpuTimer0Regs.TCR.bit.TRB=1; //重新装载 LED1=~LED1; LED2=~LED2; } void configtestled(void) { EALLOW; GpioCtrlRegs.GPBMUX2.bit.GPIO60 = 0; // GPIO60 = GPIO60配置为普通数字IO GpioCtrlRegs.GPBDIR.bit.GPIO60 = 1; // 方向为输出 GpioCtrlRegs.GPBMUX2.bit.GPIO61 = 0; // GPIO61 = GPIO61 GpioCtrlRegs.GPBDIR.bit.GPIO61 = 1; EDIS; } //=========================================================================== // No more. //===========================================================================