📄 example_281xcputimer.c
字号:
//###########################################################################
//
// FILE: Example_281xCpuTimer.c
//
// TITLE: DSP281x Device Getting Started Program.
//
// 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 configuration, no other hardware configuration
// is required.
//
// DESCRIPTION:
//
// This example configures CPU Timer0 and increments
// a counter each time the timer asserts an interrupt.
//
// Watch Variables:
// CpuTimer0.InterruptCount
//
//###########################################################################
//
// Ver | dd mmm yyyy | Who | Description of changes
// =====|=============|======|===============================================
// 1.00| 11 Sep 2003 | L.H. | No change since previous version (v.58 Alpha)
//###########################################################################
#include "DSP281x_Device.h" // DSP281x Headerfile Include File
#include "DSP281x_Examples.h" // DSP281x Examples Include File
#include "math.h"
//Prototype statements for ad7714
# define uchar unsigned char
#define sclk77to0 GpioDataRegs.GPACLEAR.bit.GPIOA1//CLEAR TO 0
#define sclk77to1 GpioDataRegs.GPASET.bit.GPIOA1//SET TO 1
#define din77to0 GpioDataRegs.GPACLEAR.bit.GPIOA2//CLEAR TO 0
#define din77to1 GpioDataRegs.GPASET.bit.GPIOA2//SET TO 1
#define drdy77 GpioDataRegs.GPADAT.bit.GPIOA3//in
#define RESET0 GpioDataRegs.GPACLEAR.bit.GPIOA4//CLEAR TO 0
#define RESET1 GpioDataRegs.GPASET.bit.GPIOA4//SET TO 1
#define dout77DAT GpioDataRegs.GPADAT.bit.GPIOA5//in
int i;
Uint32 ad_temp[1024];
Uint32 adc;
float temp;
void Send(uchar data);//add for ad7714
uchar ReadByte();//add for ad7714
Uint32 ReadAd(); /**/
void InitAd(); /**/
void ResetAd();
void delay(unsigned int t);
// Prototype statements for ads8361
interrupt void cpu_timer0_isr(void);
//unsigned int i=0;
unsigned int SPI_UB_Temp;
unsigned int SPI_LB_Temp;
unsigned int test[2];
void delay_loop(int count);
void spi_init(void);
void spi_fifo_init(void);
void spi_xmit(void);
void Toggle_A0(int);
void Toggle_STE(int);
void Toggle_M0(int);
void Toggle_M1(int);
void ToggleRD_CONVST(void);
void SYNC_UP(void);
void main(void)
{
// 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 the 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();
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
//for ads8361
PieVectTable.TINT0 = &cpu_timer0_isr;
GpioMuxRegs.GPEMUX.all=0x0000; // Select GPIOs to be McBSP pins
GpioMuxRegs.GPEDIR.all=0x0001; // Select GPIOF3 as OUT, /CS to ADC
GpioMuxRegs.GPFMUX.all=0x0007; // Select GPIOs to be McBSP pins
GpioMuxRegs.GPFDIR.all=0x7F08; // Select GPIOF3 as OUT, /CS to ADC
GpioMuxRegs.GPAMUX.all=0x0000; // Select GPIOA2 & 3 for FSx and FSr
GpioMuxRegs.GPADIR.bit.GPIOA0=1; // Select GPIOA2 as OUT, 3 as IN
// for ad7714
GpioMuxRegs.GPADIR.bit.GPIOA1=1;//77sclk:output
GpioMuxRegs.GPADIR.bit.GPIOA2=1;//77din:output
GpioMuxRegs.GPADIR.bit.GPIOA3=0;//77DRDY:input
GpioMuxRegs.GPADIR.bit.GPIOA4=1;//77RESET:output
GpioMuxRegs.GPADIR.bit.GPIOA5=0;//77DOUT:input
EDIS; // This is needed to disable write to EALLOW protected registers
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP281x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
InitCpuTimers(); // For this example, only initialize the Cpu Timers
// Configure CPU-Timer 0 to interrupt every second:
// 100MHz CPU Freq, 1 second Period (in uSeconds)
ConfigCpuTimer(&CpuTimer0, 150, 10000);
spi_init();
spi_fifo_init();
// Step 5. User specific code, enable interrupts:
// 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;
// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
// Step 6. IDLE loop. Just sit and loop forever (optional):
sclk77to0=1;
din77to0=1;
RESET1=1;
ResetAd();
delay(10000);
InitAd();
delay(1000);
adc=0;
i=0;
SYNC_UP();
StartCpuTimer0();
Send(0x5c);
for(;;)
{
Send(0x5c);
// ad_temp[i]=ReadAd();
// i++;
adc=ReadAd();
//temp=((float)(ad)-0x1000000)*0.00000014782;
// temp=ad;
}
}
//end of main
//for ads8361
interrupt void cpu_timer0_isr(void)
{
//CpuTimer0.InterruptCount++;
Toggle_STE(0);
// for(ch=0; ch<Channels; ch++)
// {
ToggleRD_CONVST();
spi_xmit();
SPI_LB_Temp = SpiaRegs.SPIRXBUF>>13;
SPI_UB_Temp = SpiaRegs.SPIRXBUF<<3;
while(SpiaRegs.SPIFFRX.bit.RXFFST ==0) { }
//SPI_Data[ch][idx] = (SPI_UB_Temp + SPI_LB_Temp);
test[0]= (SPI_UB_Temp + SPI_LB_Temp);
// Acknowledge this interrupt to receive more interrupts from group 1
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}
void delay_loop(int count)
{
long i;
for (i = 0; i < count; i++) {}
}
void SYNC_UP()
{
Toggle_M0(0); // Clear M0
Toggle_M1(0); // Clear M1
Toggle_STE(0); // Clear STE
// Toggle_RD_CONVST via GPIO;
ToggleRD_CONVST();
spi_xmit();
SPI_LB_Temp = SpiaRegs.SPIRXBUF>>13;
SPI_UB_Temp = SpiaRegs.SPIRXBUF<<3;
while(SpiaRegs.SPIFFRX.bit.RXFFST ==0) { }
Toggle_M1(0); // Set M1
Toggle_A0(1); // Choose A0/B0(0) or A1/B1(1)
// Toggle_RD_CONVST via GPIO; ToggleRD_CONVST();
ToggleRD_CONVST();
spi_xmit();
SPI_LB_Temp = SpiaRegs.SPIRXBUF>>13;
SPI_UB_Temp = SpiaRegs.SPIRXBUF<<3;
while(SpiaRegs.SPIFFRX.bit.RXFFST ==0) { }
Toggle_STE(1); // Set STE
}
void spi_xmit()
{
SpiaRegs.SPITXBUF = 0xF000; //Alternative to Toggle_A0
SpiaRegs.SPITXBUF = 0x0000;
while(SpiaRegs.SPIFFTX.bit.TXFFST !=0) { }
}
void spi_init()
{
SpiaRegs.SPICCR.all =0x000F; // Reset on, rising edge, 16-bit char bits
SpiaRegs.SPICTL.all =0x0006; // Enable master mode, normal phase,
// enable talk, and SPI int disabled.
SpiaRegs.SPIBRR =0x0002;
// Release Reset for SPI
SpiaRegs.SPICCR.all =0x008F; // Relinquish SPI from Reset
// Bit 4 loop back mode enabled
}
void spi_fifo_init()
{
// Initialize SPI FIFO registers
SpiaRegs.SPIFFTX.all=0xE140;
SpiaRegs.SPIFFRX.all=0x2041;
SpiaRegs.SPIFFCT.all=0x02;
}
void Toggle_STE(int state)
{
switch(state)
{
case 0:
GpioDataRegs.GPFCLEAR.all = 0x0008; // Clear M0
break;
case 1:
GpioDataRegs.GPFSET.all = 0x0008; // Set M0 High
break;
}
}
void Toggle_M1(int state)
{
switch(state)
{
case 0:
GpioDataRegs.GPFCLEAR.all = 0x0100; // Clear M1
break;
case 1:
GpioDataRegs.GPFSET.all = 0x0100; // Set M1 High
break;
}
}
void Toggle_M0(int state)
{
switch(state)
{
case 0:
GpioDataRegs.GPFCLEAR.all = 0x0400; // Clear M0
break;
case 1:
GpioDataRegs.GPFSET.all = 0x0400; // Set M0 High
break;
}
}
void Toggle_A0(int state)
{
switch(state)
{
case 0:
GpioDataRegs.GPFCLEAR.all = 0x0800; // Clear A0
break;
case 1:
GpioDataRegs.GPFSET.all = 0x0800; // Set A0
break;
}
}
void ToggleRD_CONVST()
{
GpioDataRegs.GPACLEAR.all = 0x0001; // Test Clear
asm (" RPT #30 || NOP");
GpioDataRegs.GPASET.all = 0x0001; // Test Set
}
//for ad7714
void Send( uchar data)//add for ad7714 2005.10.30
{
uchar i=0;
delay(60);
//delay(20);
for (i=0;i<8;i++)
{
sclk77to0=1;
delay(40);
if((data<<i)&0x80)
{ din77to1=1;}
else
{din77to0=1;}
delay(40);
sclk77to1=1;
delay(40);
sclk77to0=1;
}
//while( drdy77==1 ) {};//wait for /drdy to go low
delay(40);
//delay(20);
}
uchar ReadByte()//add for ad7714
{
uchar b=0, i=0;
//cs77to0=1;//cs low
delay(60);
while( drdy77==1 ) {};
for( i=0; i<8; i++ )
{
sclk77to1=1;
delay(20);
delay(20);
//F0=dout77DAT;
if ( dout77DAT == 1 )
{
b = b|0x01;
b = b<<1;
}
else
b = b<<1;
sclk77to0=1;
delay(40);
}
delay(40);
//delay(20);
//cs77to1=1;//cs high
return( b );
}
Uint32 ReadAd()
{
Uint32 a=0,b=0,c=0;
Uint32 ad=0;
//cs77to0=1;
a = ReadByte();
b = ReadByte();
c = ReadByte();
//cs77to1=1;
ad = (a<<16) | (b<<8) | c;
return( ad );
}
//some errors down here
void InitAd()
{
Send(0x24); // set communication register to write high filter register
Send(0xc7); // biplor 24 bit (high filter register)
Send(0x34); // set communication register to write low filter register
Send(0x80); // max frequency word(0xFA0(=4000)----4.8khz;0x13(=19)----1khz;
//while( drdy77==1 ) {};
Send(0x14); // set communication register to write mode register
Send(0x20); // gain=1,burnout current off,no filter sync,do self calibration
//while( drdy77==1 ) {};
//Delay();
}
void ResetAd()
{
RESET0=1;
delay(30);
RESET1=1;
delay(10);
}
void delay(unsigned int t)
{
unsigned int i;
for (i=0;i<t;i++);
}
//===========================================================================
// No more.
//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -