void InitSpiaGpio()
{
EALLOW;/* Enable internal pull-up for the selected pins */
// Pull-ups can be enabled or disabled by the user.
// This will enable the pullups for the specified pins.
// Comment out other unwanted lines.
GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0; // Enable pull-up on GPIO16 (SPISIMOA)
GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0; // Enable pull-up on GPIO17 (SPISOMIA)
GpioCtrlRegs.GPAPUD.bit.GPIO18 = 0; // Enable pull-up on GPIO18 (SPICLKA)
GpioCtrlRegs.GPAPUD.bit.GPIO19 = 0; // Enable pull-up on GPIO19 (SPISTEA)/* Set qualification for selected pins to asynch only */
// This will select asynch (no qualification) for the selected pins.
// Comment out other unwanted lines.
GpioCtrlRegs.GPAQSEL2.bit.GPIO16 = 3; // Asynch input GPIO16 (SPISIMOA)
GpioCtrlRegs.GPAQSEL2.bit.GPIO17 = 3; // Asynch input GPIO17 (SPISOMIA)
GpioCtrlRegs.GPAQSEL2.bit.GPIO18 = 3; // Asynch input GPIO18 (SPICLKA)
GpioCtrlRegs.GPAQSEL2.bit.GPIO19 = 3; // Asynch input GPIO19 (SPISTEA)/* Configure SPI-A pins using GPIO regs*/
// This specifies which of the possible GPIO pins will be SPI functional pins.
// Comment out other unwanted lines.
GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 1; // Configure GPIO16 as SPISIMOA
GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 1; // Configure GPIO17 as SPISOMIA
GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1; // Configure GPIO18 as SPICLKA
GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1; // Configure GPIO19 as SPISTEA
EDIS;
}
//---------------------------------------------------------------------------
// SPI_write:
//---------------------------------------------------------------------------
// This function is based on the TXBUFFULLFLAG's value
//if the value is 1,stating the a character is written to the SPITransmit buffer SPITXBUF
//elseif the value is 0,stating the shifting out of a previous character is complete
voidSPI_write(unsigned int data)
{
data=data<<8; //transmission is right-justified
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG); //the shifting out of a previous character is complete
SpiaRegs.SPITXBUF=data;
}
因为我们没有使用中断了,所以直接判断TX BUF FULL FLAG的值,当为1时,说明还是写,为0说明已经完全写完了。 说完了写操作,那读就好理解了,直接贴程序
//---------------------------------------------------------------------------// SPI_read://---------------------------------------------------------------------------// This function is based on the INT_FLAG's value//if the value is 1,stating a receive or transmit operation completes before the previous character has been read from the bufferunsignedint SPI_read()
{
unsignedint data;
while(!SpiaRegs.SPISTS.bit.INT_FLAG);
data=SpiaRegs.SPIRXBUF&0x00ff;
return data;
}
好,函数都准备完成了,我们现在写程序测试下
void main(void)
{
unsigned int temp;
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example functionisfoundintheDSP2802x_SysCtrl.cfile.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example functionisfoundintheDSP2802x_Gpio.cfileand
// illustrateshowtosettheGPIOtoit'sdefaultstate.
// InitGpio();
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This functionisfoundintheDSP2802x_PieCtrl.cfile.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful fordebug purposes.
// The shell ISR routines are found in DSP2802x_DefaultIsr.c.
// This functionisfoundinDSP2802x_PieVect.c.
InitPieVectTable();
// Step 4. Initialize all the Device Peripherals:
// This functionisfoundinDSP2802x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
// Step 5. User specific code:
InitSpiGpio();
InitSpi();
SPI_write(0x0055);
temp = SPI_read();
while(1);
}