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

📄 example_281xdac7731_demo.c

📁 使用TMS320F2812的McBSP接口驱动DAC7731输出正弦波形的示例程序。使用TI提供的C/C++头文件与公用文件库。
💻 C
字号:
//###########################################################################
//
// FILE:	Example_281xDac7731_demo.c
//
// TITLE:	DSP281x Drive Dac7731 Output. 
//
// ASSUMPTIONS:
//
//          This program requires the DSP281x V1.00 header files.  
//          As supplied, this project is configured for "boot to H0" operation.
//
//          Other then boot mode pin configuration, no other hardware configuration
//          is required.  
//
// DESCRIPTION:
//
//          Digital to Analog Convert Test by using the McBSP peripheral.
//
//          Use the serial word sizes 16  to test, for Dac7731 is a 16bit DAC.  
// 
//          Before compiling this project:
//          
//          * Select the FIFO level by using the #define statement 
//            at the beginning of the code, 1 as defualt.
//			
//			This example will generate a -10 to +10V sine wave, which can be 
//			observed by an oscilloscope, by altering the i value in function
//			"delay_loop()", you can change the frequency of the sine wave. 
//
//          This example does not use interrupts.  
//
//          This program will execute until terminated by the user. 
//
// Unnecessary Functions:
//			init_mcbsp();
//          mcbsp_fifo_init();
//			Still, it is not recommended to remove them because they may be 
//			useful on occasion.
//###########################################################################
//
// Original Author: S.S.
// 
//  Ver | dd mmm yyyy | Who  | Description of changes
// =====|=============|======|===============================================
//  1.00| 11 Sep 2003 | L.H. | Major cleanup of the example
//  1.01| 01 Oct 2008 | M.R.H| Rewrite some code for a DAC test
//###########################################################################

#include "DSP281x_Device.h"     // DSP281x Headerfile Include File
#include "DSP281x_Examples.h"   // DSP281x Examples Include File
#include "math.h"		//this program uses sin function

// Define the level of the FIFO 1-16
#define FIFO_LEVEL 2
// Choose a WAVE_SIZE
#define WAVE_SIZE    1000   //the number of points of a Sin Wave
// Define Pi
#define PI 3.1415

// Prototype statements for functions found within this file.
void delay_loop(void);
void init_mcbsp(void);
void init_mcbsp_16bit(void);
void mcbsp_xmit(int a);
void mcbsp_fifo_init(void);
void init_dac7731(void);
void init_output_wave(void);	

// Global data for this example
Uint16 sdata1 = 0x0000;    // Sent Data
Uint16 rdata1 = 0x0000;
float fltSinData[1000];	//Sent wave data array

void main(void)
{

   Uint16 i;
   Uint16 intSinIndex = 0;

// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP281x_SysCtrl.c file.
   InitSysCtrl();                            
   
// Step 2. Initalize GPIO: 
// This example function is found in the DSP281x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();  // Skipped for this example  

// 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 function is found in the DSP281x_PieCtrl.c file.
   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 for debug purposes.
// The shell ISR routines are found in DSP281x_DefaultIsr.c.
// This function is found in DSP281x_PieVect.c.
   InitPieVectTable();

// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP281x_InitPeripherals.c
// InitPeripherals();     // Not required for this example
   mcbsp_fifo_init();     // Initialize the Mcbsp FIFO
   init_mcbsp();      	 // Initialize the Mcbsp in non loopback test mode
   init_dac7731(); 		  //Initialize the Dac7731
   init_output_wave();	  //Initialize the Sin Wave Data Array

// Step 5. User specific code, send sine data to Dac7731 in a loop round:
   init_mcbsp_16bit();		//Initialize 16bit mcbsp registers.
                     
          for(;;)	//loop forever
          {
			for(i = 1; i <= FIFO_LEVEL; i++)
		//if(McbspaRegs.SPCR2.bit.XRDY==1)	
             {
				 sdata1 = (Uint16)fltSinData[intSinIndex] ; // fetch a 16-bit sine value
				 mcbsp_xmit(sdata1);	//send the data
				 while(!McbspaRegs.SPCR2.bit.XEMPTY){}	//Important!
				 //Next sent data must wait until the XSR become Empty.
             } 
             //asm("    nop");                             // Good place for a breakpoint
			delay_loop();
		    	intSinIndex++;
			 if(intSinIndex >= 1000)
			 {
				intSinIndex = 0;
			 }
          }
           
}     


// Some Useful local functions
void delay_loop()
{
    long      i;
    for (i = 0; i < 1000; i++) {}
}


void init_mcbsp()
{
    // McBSP register settings for Digital loop back 
    McbspaRegs.SPCR2.all=0x0000;
    McbspaRegs.SPCR1.all=0x0000;	//disable digital loopback
    McbspaRegs.RCR2.all=0x0;
    McbspaRegs.RCR1.all=0x0;
    McbspaRegs.XCR2.all=0x0;
    McbspaRegs.XCR1.all=0x0;
    McbspaRegs.SRGR2.all=0x2000;                      
    McbspaRegs.SRGR1.all=0xFFFF;
    McbspaRegs.MCR2.all=0x0;
    McbspaRegs.MCR1.all=0x0;
    McbspaRegs.PCR.all=0x0a08;

}


void init_mcbsp_16bit()
{

    McbspaRegs.SPCR1.bit.RJUST=0;       // word Rjustifed 
    McbspaRegs.RCR2.bit.RCOMPAND=00;    // No R/XCOMPAND 
    McbspaRegs.XCR2.bit.XCOMPAND=00; 
    McbspaRegs.RCR1.bit.RWDLEN1=2;      // 16-bit word
    McbspaRegs.XCR1.bit.XWDLEN1=2;      // 16-bit word
    McbspaRegs.SPCR2.bit.XRST=1;        // enable XRST/RRST
    McbspaRegs.SPCR1.bit.RRST=1;  
    McbspaRegs.SPCR2.all |=0x00C0;      // Only enable FRST,GRST 
	//Very Important!
	//Must Configed or the demo won't work
	McbspaRegs.SPCR1.bit.CLKSTP=0x2;		//The clock stop mode
}


void mcbsp_xmit(int a)
{
    McbspaRegs.DXR1.all=a;
}

void mcbsp_fifo_init()                                        
{

    McbspaRegs.MFFTX.all=0x0000;
    McbspaRegs.MFFRX.all=0x001F;
    McbspaRegs.MFFCT.all=0x0;
    McbspaRegs.MFFINT.all=0x0;
    McbspaRegs.MFFST.all=0x0;  
    McbspaRegs.MFFTX.bit.MFFENA=1;         // Enable FIFO
    McbspaRegs.MFFTX.bit.TXFIFO_RESET=1;  // Enable Transmit channel
    McbspaRegs.MFFRX.bit.RXFIFO_RESET=1;  // Enable Receive channel

}  

void init_dac7731()
{

	//Config the GpioB13/14/15 as Dac Control Lines
	EALLOW;
 	GpioMuxRegs.GPBMUX.all &= 0x1FFF;  //Set GpioB15/14/13 as I/Os
	GpioMuxRegs.GPBDIR.all |= 0xE000;  //Set GpioB15/14/13 as Output Ports
	GpioMuxRegs.GPFMUX.all |= 0x1500;
	EDIS;

	//Initialize Dac7731
	GpioDataRegs.GPBDAT.bit.GPIOB14 = 0;	//Chip Select Enable
	GpioDataRegs.GPBDAT.bit.GPIOB15 = 1;	//If Reset, dac7731 Reset to the middle scale.
	GpioDataRegs.GPBDAT.bit.GPIOB13 = 0;  //Reset dac7731
	 asm (" RPT #6 || NOP");		//delay some time
	GpioDataRegs.GPBDAT.bit.GPIOB13 = 1;	//Disable Reset Signal

}

void init_output_wave()
{
	Uint16 i;
	//Initialize a period of Sin Wave, containing WAVE_SIZE discret data points.
	for(i = 0; i < WAVE_SIZE; i++)
	{
		fltSinData[i] = sin(2*PI*i/WAVE_SIZE)*0x7FFF + 0x7FFF;
	}

}

//===========================================================================
// No more.
//===========================================================================

⌨️ 快捷键说明

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