这个程序的问题就是 DSP运行起来不稳定,一会能出信号,可能多运行一会,暂停在运行,信号就乱了。 还有滤波器系数是截止频率按30Hz算的,当然有20-30的10Hz的过渡带,为什么20Hz以下的信号也能通过?问题太多,没有头绪。。。 求各大神给予建议指导,不胜感激!!谢谢大家!
#include <TIMER_interrupt_dspL138.h>
#include "evmomapl138.h"
#include "evmomapl138_gpio.h"
#include "evmomapl138_spi.h"
#include "evmomapl138_mcbsp.h"
#include "evmomapl138_fir.h"
#include "types.h"
#include "stdio.h"
/*-----------------------------------------------------------------------------
*
* BEGIN EXAMPLE: TIMER_interrupt_dspL138
*
*---------------------------------------------------------------------------*/
#define N 727//365//727
//#define NUM 1000
uint16_t int_flag = 0;
volatile uint16_t spi_rxdata0=0;
//volatile int16_t spi_rxdata0=0;
float iData[N];
//float data[NUM];// test
float daData;
uint16_t i=0;
uint16_t nfilt=N;
uint16_t fill=1;
uint16_t m=0;
// kaiser filter coefficients
float b[727]={-0.000014, -0.000016, -0.000018, -0.000019, -0.000021, -0.000022, -0.000023, -0.000024, -0.000025, -0.000025,
-0.000024, -0.000024, -0.000023, -0.000022, -0.000020, -0.000018, -0.000015, -0.000012, -0.000008, -0.000005,
-0.000001, 0.000004, 0.000008, 0.000013, 0.000018, 0.000023, 0.000028, 0.000033, 0.000038, 0.000043,
0.000047, 0.000051, 0.000054, 0.000057, 0.000060, 0.000061, 0.000062, 0.000062, 0.000062, 0.000060,
0.000058, 0.000054, 0.000050, 0.000045, 0.000039, 0.000032, 0.000024, 0.000016, 0.000007, -0.000003,
-0.000013, -0.000023, -0.000034, -0.000044, -0.000055, -0.000065, -0.000075, -0.000085, -0.000093, -0.000101,
-0.000108, -0.000114, -0.000119, -0.000123, -0.000125, -0.000125, -0.000124, -0.000121, -0.000117, -0.000110,
-0.000102, -0.000093, -0.000082, -0.000069, -0.000055, -0.000039, -0.000023, -0.000005, 0.000013, 0.000032,
0.000051, 0.000071, 0.000090, 0.000109, 0.000127, 0.000145, 0.000161, 0.000176, 0.000189, 0.000200,
0.000209, 0.000216, 0.000220, 0.000222, 0.000221, 0.000217, 0.000210, 0.000200, 0.000187, 0.000172,
0.000153, 0.000132, 0.000109, 0.000083, 0.000056, 0.000026, -0.000004, -0.000036, -0.000069, -0.000101,
-0.000134, -0.000166, -0.000196, -0.000226, -0.000254, -0.000279, -0.000302, -0.000321, -0.000338, -0.000350,
-0.000359, -0.000363, -0.000363, -0.000358, -0.000349, -0.000335, -0.000316, -0.000293, -0.000265, -0.000233,
-0.000197, -0.000158, -0.000115, -0.000069, -0.000021, 0.000029, 0.000079, 0.000131, 0.000182, 0.000233,
0.000283, 0.000330, 0.000375, 0.000416, 0.000453, 0.000486, 0.000514, 0.000536, 0.000552, 0.000562,
0.000565, 0.000561, 0.000549, 0.000531, 0.000505, 0.000473, 0.000433, 0.000387, 0.000335, 0.000277,
0.000213, 0.000146, 0.000074, 0.000000, -0.000077, -0.000154, -0.000232, -0.000310, -0.000385, -0.000458,
-0.000528, -0.000592, -0.000652, -0.000704, -0.000750, -0.000787, -0.000815, -0.000835, -0.000844, -0.000843,
-0.000831, -0.000809, -0.000776, -0.000733, -0.000679, -0.000615, -0.000542, -0.000460, -0.000371, -0.000274,
-0.000171, -0.000063, 0.000048, 0.000162, 0.000277, 0.000391, 0.000504, 0.000613, 0.000718, 0.000817,
0.000908, 0.000990, 0.001062, 0.001123, 0.001172, 0.001207, 0.001228, 0.001235, 0.001226, 0.001202,
0.001162, 0.001107, 0.001036, 0.000951, 0.000852, 0.000740, 0.000615, 0.000480, 0.000335, 0.000182,
0.000023, -0.000140, -0.000307, -0.000473, -0.000639, -0.000800, -0.000956, -0.001105, -0.001243, -0.001370,
-0.001483, -0.001581, -0.001661, -0.001724, -0.001766, -0.001788, -0.001788, -0.001766, -0.001721, -0.001654,
-0.001565, -0.001454, -0.001322, -0.001170, -0.000999, -0.000812, -0.000609, -0.000393, -0.000167, 0.000068,
0.000308, 0.000551, 0.000794, 0.001033, 0.001266, 0.001490, 0.001702, 0.001899, 0.002077, 0.002235,
0.002370, 0.002478, 0.002560, 0.002612, 0.002633, 0.002622, 0.002578, 0.002501, 0.002390, 0.002247,
0.002072, 0.001866, 0.001631, 0.001369, 0.001081, 0.000772, 0.000444, 0.000100, -0.000255, -0.000618,
-0.000985, -0.001351, -0.001711, -0.002062, -0.002399, -0.002716, -0.003010, -0.003277, -0.003511, -0.003710,
-0.003869, -0.003985, -0.004057, -0.004080, -0.004053, -0.003975, -0.003845, -0.003663, -0.003428, -0.003142,
-0.002807, -0.002424, -0.001997, -0.001528, -0.001022, -0.000484, 0.000082, 0.000670, 0.001273, 0.001886,
0.002501, 0.003111, 0.003709, 0.004287, 0.004837, 0.005353, 0.005825, 0.006247, 0.006612, 0.006911,
0.007139, 0.007290, 0.007357, 0.007336, 0.007221, 0.007010, 0.006698, 0.006285, 0.005767, 0.005145,
0.004419, 0.003589, 0.002659, 0.001631, 0.000509, -0.000703, -0.001998, -0.003371, -0.004813, -0.006318,
-0.007876, -0.009478, -0.011115, -0.012776, -0.014450, -0.016127, -0.017796, -0.019445, -0.021063, -0.022638,
-0.024161, -0.025620, -0.027005, -0.028306, -0.029514, -0.030619, -0.031615, -0.032493, -0.033247, -0.033871,
-0.034362, -0.034715, -0.034928, 0.964987, -0.034928, -0.034715, -0.034362, -0.033871, -0.033247, -0.032493,
-0.031615, -0.030619, -0.029514, -0.028306, -0.027005, -0.025620, -0.024161, -0.022638, -0.021063, -0.019445,
-0.017796, -0.016127, -0.014450, -0.012776, -0.011115, -0.009478, -0.007876, -0.006318, -0.004813, -0.003371,
-0.001998, -0.000703, 0.000509, 0.001631, 0.002659, 0.003589, 0.004419, 0.005145, 0.005767, 0.006285,
0.006698, 0.007010, 0.007221, 0.007336, 0.007357, 0.007290, 0.007139, 0.006911, 0.006612, 0.006247,
0.005825, 0.005353, 0.004837, 0.004287, 0.003709, 0.003111, 0.002501, 0.001886, 0.001273, 0.000670,
0.000082, -0.000484, -0.001022, -0.001528, -0.001997, -0.002424, -0.002807, -0.003142, -0.003428, -0.003663,
-0.003845, -0.003975, -0.004053, -0.004080, -0.004057, -0.003985, -0.003869, -0.003710, -0.003511, -0.003277,
-0.003010, -0.002716, -0.002399, -0.002062, -0.001711, -0.001351, -0.000985, -0.000618, -0.000255, 0.000100,
0.000444, 0.000772, 0.001081, 0.001369, 0.001631, 0.001866, 0.002072, 0.002247, 0.002390, 0.002501,
0.002578, 0.002622, 0.002633, 0.002612, 0.002560, 0.002478, 0.002370, 0.002235, 0.002077, 0.001899,
0.001702, 0.001490, 0.001266, 0.001033, 0.000794, 0.000551, 0.000308, 0.000068, -0.000167, -0.000393,
-0.000609, -0.000812, -0.000999, -0.001170, -0.001322, -0.001454, -0.001565, -0.001654, -0.001721, -0.001766,
-0.001788, -0.001788, -0.001766, -0.001724, -0.001661, -0.001581, -0.001483, -0.001370, -0.001243, -0.001105,
-0.000956, -0.000800, -0.000639, -0.000473, -0.000307, -0.000140, 0.000023, 0.000182, 0.000335, 0.000480,
0.000615, 0.000740, 0.000852, 0.000951, 0.001036, 0.001107, 0.001162, 0.001202, 0.001226, 0.001235,
0.001228, 0.001207, 0.001172, 0.001123, 0.001062, 0.000990, 0.000908, 0.000817, 0.000718, 0.000613,
0.000504, 0.000391, 0.000277, 0.000162, 0.000048, -0.000063, -0.000171, -0.000274, -0.000371, -0.000460,
-0.000542, -0.000615, -0.000679, -0.000733, -0.000776, -0.000809, -0.000831, -0.000843, -0.000844, -0.000835,
-0.000815, -0.000787, -0.000750, -0.000704, -0.000652, -0.000592, -0.000528, -0.000458, -0.000385, -0.000310,
-0.000232, -0.000154, -0.000077, 0.000000, 0.000074, 0.000146, 0.000213, 0.000277, 0.000335, 0.000387,
0.000433, 0.000473, 0.000505, 0.000531, 0.000549, 0.000561, 0.000565, 0.000562, 0.000552, 0.000536,
0.000514, 0.000486, 0.000453, 0.000416, 0.000375, 0.000330, 0.000283, 0.000233, 0.000182, 0.000131,
0.000079, 0.000029, -0.000021, -0.000069, -0.000115, -0.000158, -0.000197, -0.000233, -0.000265, -0.000293,
-0.000316, -0.000335, -0.000349, -0.000358, -0.000363, -0.000363, -0.000359, -0.000350, -0.000338, -0.000321,
-0.000302, -0.000279, -0.000254, -0.000226, -0.000196, -0.000166, -0.000134, -0.000101, -0.000069, -0.000036,
-0.000004, 0.000026, 0.000056, 0.000083, 0.000109, 0.000132, 0.000153, 0.000172, 0.000187, 0.000200,
0.000210, 0.000217, 0.000221, 0.000222, 0.000220, 0.000216, 0.000209, 0.000200, 0.000189, 0.000176,
0.000161, 0.000145, 0.000127, 0.000109, 0.000090, 0.000071, 0.000051, 0.000032, 0.000013, -0.000005,
-0.000023, -0.000039, -0.000055, -0.000069, -0.000082, -0.000093, -0.000102, -0.000110, -0.000117, -0.000121,
-0.000124, -0.000125, -0.000125, -0.000123, -0.000119, -0.000114, -0.000108, -0.000101, -0.000093, -0.000085,
-0.000075, -0.000065, -0.000055, -0.000044, -0.000034, -0.000023, -0.000013, -0.000003, 0.000007, 0.000016,
0.000024, 0.000032, 0.000039, 0.000045, 0.000050, 0.000054, 0.000058, 0.000060, 0.000062, 0.000062,
0.000062, 0.000061, 0.000060, 0.000057, 0.000054, 0.000051, 0.000047, 0.000043, 0.000038, 0.000033,
0.000028, 0.000023, 0.000018, 0.000013, 0.000008, 0.000004, -0.000001, -0.000005, -0.000008, -0.000012,
-0.000015, -0.000018, -0.000020, -0.000022, -0.000023, -0.000024, -0.000024, -0.000025, -0.000025, -0.000024,
-0.000023, -0.000022, -0.000021, -0.000019, -0.000018, -0.000016, -0.000014}; // fcuts=[30,40] fs=2k
volatile int runExample, runTimerDelay, counter, errorCount,DoutCount;
float filter(float* x, uint16_t xlen,float* b, uint16_t nfilt);
void main (void)
{
volatile uint32_t temp=0;
// uint16_t i=0;
spi_config_t sCfgSPI;
sCfgSPI.mode = SPI_MODE_MASTER;
sCfgSPI.pin_option = SPI_4PIN_CS;
sCfgSPI.cs_active = SPI_CS_ACTIVE_LOW;//
sCfgSPI.shift_dir = SPI_SHIFT_MSB;
sCfgSPI.polarity = 0;
sCfgSPI.phase = 0;
sCfgSPI.freq = 150;//50MHz/(freq-1)=500KHz
CSR=0x100; /* disable all interrupts */
IER=1; /* disable all interrupts except reset */
ICR=0xffff; /* clear all pending interrupts */
//系统初始化,包括clock、 power、 sleep 配置
do{
temp = EVMOMAPL138_init();
}while(temp==ERR_INIT_FAIL);
do{
temp = USTIMER_init();
}while(temp==ERR_INIT_FAIL);
USTIMER_delay(100);
//关闭flash片选
close_flash();
//初始化GPIO,用于驱动LED
GPIO_Init();
GPIO_setDir(GPIO_BANK7,GPIO_PIN11,GPIO_OUTPUT);//LED2
GPIO_setDir(GPIO_BANK7,GPIO_PIN12,GPIO_OUTPUT);//LED3
GPIO_setDir(GPIO_BANK7,GPIO_PIN8,GPIO_OUTPUT);//LED4
GPIO_setDir(GPIO_BANK7,GPIO_PIN9,GPIO_OUTPUT);//LED5
GPIO_setOutput(GPIO_BANK7,GPIO_PIN11,OUTPUT_HIGH);
GPIO_setOutput(GPIO_BANK7,GPIO_PIN12,OUTPUT_LOW);
GPIO_setOutput(GPIO_BANK7,GPIO_PIN8,OUTPUT_HIGH);
GPIO_setOutput(GPIO_BANK7,GPIO_PIN9,OUTPUT_LOW);
//初始化SPI接口
SPI_init((spi_regs_t *)SPI0_REG_BASE, &sCfgSPI);
GPIO_setDir(GPIO_BANK1,GPIO_PIN7,GPIO_OUTPUT);//AD0_CNVST
USTIMER_delay(100);
SPI_init((spi_regs_t *)SPI1_REG_BASE, &sCfgSPI);
GPIO_setDir(GPIO_BANK2,GPIO_PIN15,GPIO_OUTPUT);//AD1_CNVST
USTIMER_delay(100);
//初始化McBSP为SPI接口
McBSP_init((mcbsp_regs_t *)McBSP0_REG_BASE);
USTIMER_delay(100);
McBSP_init((mcbsp_regs_t *)McBSP1_REG_BASE);
USTIMER_delay(100);
// Initialize OMAPL138 EVM (Config Timer w/DSP)
init_OMAPL138();
// Configure the 64 Bit Timer0 as 32 Bit Unchain
setup_Timer0();
// Map system interrupts to the DSP interrupt controller
setup_DSP_INTC();
// Run Example
TIMER_interrupt_DSPexample();
}/* End of main */
/*-----------------------------------------------------------------------------
*
* Internal Functions Defined
*
*---------------------------------------------------------------------------*/
static void init_OMAPL138 (void)
{
// Open Permissions to SYSCFG Registers
CSL_FINS(sysRegs->KICK0R, SYSCFG_KICK0R_KICK0, KICK0_KEY);
CSL_FINS(sysRegs->KICK1R, SYSCFG_KICK1R_KICK1, KICK1_KEY);
// Configure Timer0 with the DSP CPU
CSL_FINST(sysRegs->SUSPSRC, SYSCFG_SUSPSRC_TIMER64P_0SRC, DSP);
// Close Permissions to SYSCFG Registers
CSL_FINS(sysRegs->KICK0R, SYSCFG_KICK0R_KICK0, KICK_LOCK);
}/* init_OMAPL138 */
/*---------------------------------------------------------------------------*/
static void setup_Timer0 (void)
{
// Remove Timer0:12 from Reset
CSL_FINST(tmr0Regs->TGCR, TMR_TGCR_TIM12RS, RESET);
// Set Timer0 as 32 Bit Unchain
CSL_FINST(tmr0Regs->TGCR, TMR_TGCR_TIMMODE, 32BIT_UNCHAIN);
// Reset the Counter for Timer0:12
CSL_FINST(tmr0Regs->TIM12, TMR_TIM12_TIM12, RESETVAL);
// Select Internal Clock for Timer0:12 (24 MHz)
CSL_FINST(tmr0Regs->TCR, TMR_TCR_CLKSRC12, INTERNAL);
// Set Timer0:12 Period (1 Second)
CSL_FINS(tmr0Regs->PRD12, TMR_PRD12_PRD12, (CSL_ASYNC_2_FREQ/FREQ));
// CSL_FINS(tmr0Regs->PRD12, TMR_PRD12_PRD12, (CSL_ASYNC_2_FREQ/(FREQ*10)));
// Disable the New Timer Features
CSL_FINST(tmr0Regs->TGCR, TMR_TGCR_PLUSEN, DISABLE);
// Remove Timer0:12 from Reset
CSL_FINST(tmr0Regs->TGCR, TMR_TGCR_TIM12RS, NO_RESET);
}/* setup_Timer0 */
/*---------------------------------------------------------------------------*/
static void setup_DSP_INTC (void)
{
// Map Timer0:12 system interrupts to DSP INT4
CSL_FINS(dspintcRegs->INTMUX1, DSPINTC_INTMUX1_INTSEL4,
CSL_INTC_EVENTID_T64P0_TINT12);
// Assign the address of the IST to the IST pointer
ISTP = (unsigned int)intcVectorTable;
// Clear all CPU maskable interrupts
ICR = DSPINTC_IST_ALL_MASK_INT;
// Enable INT4 interrupts
IER = DSPINTC_IST_NMI | DSPINTC_IST_INT4;
}/* setup_DSP_INTC */
/*---------------------------------------------------------------------------*/
volatile int g_bFlag = 0;
static void TIMER_interrupt_DSPexample (void)
{
/* printf("Example: TIMER_interrupt_DSPexample\n"
"Scope: Timer0:12 runs continuously causing an "
"interrupt every second. After 5 interrupts, example exits.\n"
"Begin...\n");*/
volatile uint32_t t=0,ii=0;
runExample = 1, counter = 0,DoutCount = 0;
// Intrinsic Function to Enable Interrupts
_enable_interrupts();
// Enable Timer0:12 Continuously
CSL_FINST(tmr0Regs->TCR, TMR_TCR_ENAMODE12, EN_CONT);
// Exit After Five Interrupts Occur
while(runExample)
{
if(fill==1)
{
for(i = 0; i < N; i ++)
{
//wait for recv
while(!g_bFlag){;}
g_bFlag = 0;
//判断SPIBUF状态,非空读取数据
while (CHKBIT(SPI0->SPIBUF, RXEMPTY)) {}
spi_rxdata0 = SPI0->SPIBUF;
iData[i]=(float)((spi_rxdata0-32768)/32768.0*5);
// iData[i] = (float)((spi_rxdata0/32768.0)*5.0);
}
fill=0;
}
else
{
//wait for recv
while(!g_bFlag){;}
g_bFlag = 0;
while (CHKBIT(SPI0->SPIBUF, RXEMPTY)) {}
spi_rxdata0 = SPI0->SPIBUF;
// for real-time performance, add shifting
for(i = 0; i < N-1; i++)
{
iData[i] = iData[i+1];
}
// spi_rxdata0 = SPI0->SPIBUF^0x8000;
iData[N-1] = (float)(((spi_rxdata0-32768)/32768.0)*5);
// iData[N-1] = (float)(spi_rxdata0/32768.0*5.0);
}
daData=filter(iData,N,b,nfilt);
/* if(m<=NUM)
{
data[m]=daData;
printf("%5.6f\n", data[m]);
m++;
}*/
spi_rxdata0=(uint16_t)((daData/5)*32768+32768.0);
// spi_rxdata0=(uint16_t)(daData/5.0*32768.0);
}
// Disable Timer0:12
CSL_FINST(tmr0Regs->TCR, TMR_TCR_ENAMODE12, DISABLE);
// Intrinsic Function to Disable Interrupts
_disable_interrupts();
printf("End of example.\n\n");
}/* TIMER_interrupt_DSPexample */
/*-----------------------------------------------------------------------------
*
* Interrupt Functions
*
*---------------------------------------------------------------------------*/
interrupt void TIMER0_12_isr (void)
{
if (int_flag)
{
GPIO_setOutput(GPIO_BANK7,GPIO_PIN11,OUTPUT_HIGH);
GPIO_setOutput(GPIO_BANK7,GPIO_PIN12,OUTPUT_LOW);
GPIO_setOutput(GPIO_BANK7,GPIO_PIN8,OUTPUT_HIGH);
GPIO_setOutput(GPIO_BANK7,GPIO_PIN9,OUTPUT_LOW);
int_flag = 0;
}
else
{
GPIO_setOutput(GPIO_BANK7,GPIO_PIN11,OUTPUT_LOW);
GPIO_setOutput(GPIO_BANK7,GPIO_PIN12,OUTPUT_HIGH);
GPIO_setOutput(GPIO_BANK7,GPIO_PIN8,OUTPUT_LOW);
GPIO_setOutput(GPIO_BANK7,GPIO_PIN9,OUTPUT_HIGH);
int_flag = 1;
}
//产生CNVST信号
GPIO_setOutput(GPIO_BANK1,GPIO_PIN7,OUTPUT_LOW);
// USTIMER_delay(5);
GPIO_setOutput(GPIO_BANK1,GPIO_PIN7,OUTPUT_HIGH);
//发送数据,产生SPI脉冲
while (CHKBIT(SPI0->SPIBUF, TXFULL)) {}
SPI0->SPIDAT0 = 0x1234;
if(!fill)
{
McBSP0->McBSPDXR = spi_rxdata0;
// McBSP0->McBSPDXR = spi_rxdata0^0x8000;
}
g_bFlag = 1;
}/* TIMER0_12_isr */
//filter
float filter(float* x, uint16_t xlen,float* b, uint16_t nfilt)
{
uint16_t j;
float y;
j=0;
y=0;
for(j=0;(j<xlen)&&(j<nfilt);j++)
y+=b[j]*x[xlen-1-j];
return y;
}