📄 da.c
字号:
//###########################################################################
//
// FILE: da.c
//###########################################################################
#include "DSP280x_Device.h" // DSP280x Headerfile Include File
#include "DSP280x_Examples.h" // DSP280x Examples Include File
void spi_xmit(Uint16 dataA,Uint16 dataB,Uint16 channel,Uint16 count);
void spi_init();
void wait(unsigned int l);
void main(void)
{
Uint16 i;
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP280x_SysCtrl.c file.
InitSysCtrl();
EALLOW;
//SysCtrlRegs.HISPCP.bit.HSPCLK=1;//systemclock/2
SysCtrlRegs.LOSPCP.bit.LSPCLK=0;//systemclock/1
EDIS;
// Step 2. Initalize GPIO:
// This example function is found in the DSP280x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
// Setup only the GP I/O only for SPI-A functionality
InitSpiaGpio();
// Step 3. Initialize PIE vector table:
// Disable and clear all CPU interrupts
DINT;
IER = 0x0000;
IFR = 0x0000;
// Initialize PIE control registers to their default state:
// This function is found in the DSP280x_PieCtrl.c file.
InitPieCtrl();
// 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 for debug purposes.
// The shell ISR routines are found in DSP280x_DefaultIsr.c.
// This function is found in DSP280x_PieVect.c.
InitPieVectTable();
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP280x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
spi_init(); // Initialize the SPI only
// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM // Enable Global Interrupts
// Step 6. IDLE loop. Just sit and loop forever (optional):
for(;;)
{
for(i=0;i<4095;i+=5)
spi_xmit(i,i,2,2);//output 2 channel
//spi_xmit(i,i,1,1);//output A channel
//spi_xmit(i,i,2,1);//output B channel
wait(5);
}
}
void spi_init()
{
SpiaRegs.SPICCR.all =0x004F;//下降沿输出数据,16位的字长
SpiaRegs.SPICTL.all =0x0006;//无延时,设置为主机,使能发送
SpiaRegs.SPIBRR =59; //波特率为 60m/ 60=1m
SpiaRegs.SPICCR.bit.SPISWRESET=1;//spi准备发送和接收下一个字
SpiaRegs.SPIPRI.bit.FREE = 1; // Set so breakpoints don't disturb xmission
}
void wait(unsigned int l)
{ int k;
int j;
for(j=0;j<l;j++)
{
for(k=0;k<0xf;k++)
{;}
}
}
/*
count: output channal count,1 or 2
channel:output channal,1(output A) or 2(output B)
dataA: A channal output value
dataB: B channal output value
*/
void spi_xmit(Uint16 dataA,Uint16 dataB,Uint16 channel,Uint16 count)
{
switch(count)
{
case 1://ouput 1 channel
{
if(channel==1)
{
SpiaRegs.SPITXBUF=0xc000+dataA;
while(SpiaRegs.SPISTS.bit.INT_FLAG!=1); //必须加这个判断,否则输出数据不正确
SpiaRegs.SPIRXBUF=SpiaRegs.SPIRXBUF; //虚读,清除SpiaRegs.SPISTS.bit.INT_FLAG这个标志
}
if(channel==2)
{
SpiaRegs.SPITXBUF=0x4000+dataB;
while(SpiaRegs.SPISTS.bit.INT_FLAG!=1); //必须加这个判断,否则输出数据不正确
SpiaRegs.SPIRXBUF=SpiaRegs.SPIRXBUF; //虚读,清除SpiaRegs.SPISTS.bit.INT_FLAG这个标志
}
};break;
case 2://ouput 2 channel
{
SpiaRegs.SPITXBUF=0x1000+dataA;
while(SpiaRegs.SPISTS.bit.INT_FLAG!=1); //必须加这个判断,否则输出数据不正确
SpiaRegs.SPIRXBUF=SpiaRegs.SPIRXBUF; //虚读,清除SpiaRegs.SPISTS.bit.INT_FLAG这个标志
SpiaRegs.SPITXBUF=0x8000+dataB;
while(SpiaRegs.SPISTS.bit.INT_FLAG!=1); //必须加这个判断,否则输出数据不正确
SpiaRegs.SPIRXBUF=SpiaRegs.SPIRXBUF; //虚读,清除SpiaRegs.SPISTS.bit.INT_FLAG这个标志
};break;
}
}//===========================================================================
// No more.
//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -