⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi_dac.c

📁 dspf2812 dac测试程序 用ccs2000编写
💻 C
字号:
// TI File $Revision: /main/3 $
// Checkin $Date: July 2, 2007   11:33:46 $
//###########################################################################
//
// FILE:   SPI_DAC.c
//
// TITLE:  DSP2812 DAC Example Program.
//
// ASSUMPTIONS:
//
//   This program requires the DSP281x V1.00 header files.  
//   As supplied, this project is configured for "boot to H0" operation.
//
//   Make sure the CPU clock speed is properly defined in 
//   DSP281x_Examples.h before compiling this example.
//
//    
//
//###########################################################################
// $TI Release: DSP281x Header Files V1.11 $
// $Release Date: September 26, 2007 $
//###########################################################################

#include "DSP281x_Device.h"     // DSP281x Headerfile Include File
#include "DSP281x_Examples.h"   // DSP281x Examples Include File

// Prototype statements for functions found within this file.
void DA_OUT(unsigned CHANNEL,unsigned int RNG,unsigned int SPI_DATA); 
interrupt void cpu_timer0_isr(void);
void Gpio_select(void); 
void SPI_Init();
unsigned int t0=0,i=0;  
int Voltage=0;
main() 
{

   /*初始化系统*/
   InitSysCtrl();

   /*关中断*/
   DINT;
   IER = 0x0000;
   IFR = 0x0000;
   
   
   /*初始化PIE*/
   InitPieCtrl();
   
   
   /*初始化PIE中断矢量表*/
   InitPieVectTable();
    
   Gpio_select(); 
   
   EALLOW;  
   PieVectTable.TINT0 = &cpu_timer0_isr;
   EDIS;    
   
// For this example, only initialize the Cpu Timers
   InitCpuTimers();   
// Configure CPU-Timer 0 to interrupt every second:
   ConfigCpuTimer(&CpuTimer0, 100, 1000);
   StartCpuTimer0();
   
   SPI_Init();

// Enable CPU INT1 which is connected to CPU-Timer 0:
   IER |= M_INT1;

// Enable TINT0 in the PIE: Group 1 interrupt 7
   PieCtrlRegs.PIEIER1.bit.INTx7 = 1;

   EINT;  
   ERTM;   

   for(;;);

}

void DA_OUT(unsigned CHANNEL,unsigned int RNG,unsigned int SPI_DATA)
{  
    unsigned int flag=0;
    SpiaRegs.SPITXBUF=(CHANNEL<<14)|(RNG<<13)|(SPI_DATA<<5) ;  //bit10,9~CHANNEL;bit8~RNG倍数
    for(;;)
    {
    flag=SpiaRegs.SPISTS.all&0x40; 
    if(flag==0x40)     break;       //读中断标志位,判断是否发送完毕
    }
    SpiaRegs.SPIRXBUF=SpiaRegs.SPIRXBUF;/*虚读寄存器以清除中断标志*/ 
     
    GpioDataRegs.GPADAT.bit.GPIOA0=0;   //CS_DA=0,更新模拟信号输出   
   	for(i=0;i<5;i++);              //延时
    GpioDataRegs.GPADAT.bit.GPIOA0=1;   //CS_DA=1,锁存数据   
    
}

interrupt void cpu_timer0_isr(void)
{
   CpuTimer0.InterruptCount++;

   // Acknowledge this interrupt to receive more interrupts from group 1
   PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;

    if(Voltage<0)	Voltage=0;
    DA_OUT(0,0,Voltage);            //Voltage范围0-255对应0-3.3V
    DA_OUT(1,0,Voltage);            //Voltage范围0-255对应0-3.3V
    DA_OUT(2,0,192);                //2.475V,Voltage范围0-255对应0-3.3V
    DA_OUT(3,0,128);                //1.65V,Voltage范围0-255对应0-3.3V
    if(t0<255)		Voltage++;
      else		Voltage--; 
    if(Voltage<0)	Voltage=0;
    if(t0==508)     t0=0;
       
    t0++;  

}
void SPI_Init()             //SPI-DA初始化
{
    GpioDataRegs.GPADAT.bit.GPIOA0=1;  //CS_DA=1
    SpiaRegs.SPICCR.all=0x004a;        //11bit数据
    SpiaRegs.SPICTL.all=0x0006;        //禁止中断
    SpiaRegs.SPISTS.all=0x0000;        //清除SPI中断状态位
    SpiaRegs.SPIBRR=0x0063;            //1M波特率
    SpiaRegs.SPICCR.all=SpiaRegs.SPICCR.all|0x0080; //(关键设置位)使SPI退出复位状态
}

void Gpio_select(void)
{
	EALLOW; 
	GpioMuxRegs.GPFMUX.bit.SPISIMOA_GPIOF0=1; //SPISIMO特殊功能方式 
	GpioMuxRegs.GPFMUX.bit.SPICLKA_GPIOF2=1;  //SPICLK特殊功能方式 	
	GpioMuxRegs.GPAMUX.bit.PWM1_GPIOA0=0;
	GpioMuxRegs.GPADIR.bit.GPIOA0=1;
	EDIS;
}


    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -