dlia.c
资源名称:dlia_pro.rar [点击查看]
上传用户:inhuatv111
上传日期:2022-08-04
资源大小:292k
文件大小:6k
源码类别:
DSP编程
开发平台:
C/C++
- /*************************************************************************
- 文件名: main10_1.c
- 日期: 2007年2月9日
- 公司(作者):
- 描述和说明: main()函数中启动定时器1
- *************************************************************************/
- #include "regs2407.h"
- #include "Initializing.h"
- //#define NN 32
- //#define QQ 15
- #define NN 32
- #define QQ 4
- ioport unsigned port2000;//访问IO端口0X2000的变量
- #define LEDSPORT port2000
- const signed int Sin_Tab[32]=
- {0x0000,0x018F,0x030F,0x0471,0x05A7,0x06A6,0x0763,0x07D8,0x07FF,0x07D8,
- 0x0763,0x06A6,0x05A7,0x0471,0x030F,0x018F,0x0000,0xFE72,0xFCF2,0xFB90,
- 0xFA5A,0xF95B,0xF89E,0xF829,0xF802,0xF829,0xF89E,0xF95B,0xFA5A,0xFB90,
- 0xFCF2,0xFE72};
- const signed int Cos_Tab[32]=
- {0x07FF,0x07D8,0x0763,0x06A6,0x05A7,0x0471,0x030F,0x018F,0x0000,0xFE72,
- 0xFCF2,0xFB90,0xFA5A,0xF95B,0xF89E,0xF829,0xF802,0xF829,0xF89E,0xF95B,
- 0xFA5A,0xFB90,0xFCF2,0xFE72,0x0000,0x018F,0x030F,0x0471,0x05A7,0x06A6,
- 0x0763,0x07D8};
- const signed int t_Tab[128]=
- {0x05A7,0x0471,0x030F,0x018F,0x0000,0xFE72,0xFCF2,0xFB90,
- 0xFA5A,0xF95B,0xF89E,0xF829,0xF802,0xF829,0xF89E,0xF95B,0xFA5A,0xFB90,
- 0xFCF2,0xFE72,
- 0x0000,0x018F,0x030F,0x0471,0x05A7,0x06A6,0x0763,0x07D8,0x07FF,0x07D8,
- 0x0763,0x06A6,0x05A7,0x0471,0x030F,0x018F,0x0000,0xFE72,0xFCF2,0xFB90,
- 0xFA5A,0xF95B,0xF89E,0xF829,0xF802,0xF829,0xF89E,0xF95B,0xFA5A,0xFB90,
- 0xFCF2,0xFE72,
- 0x0000,0x018F,0x030F,0x0471,0x05A7,0x06A6,0x0763,0x07D8,0x07FF,0x07D8,
- 0x0763,0x06A6,0x05A7,0x0471,0x030F,0x018F,0x0000,0xFE72,0xFCF2,0xFB90,
- 0xFA5A,0xF95B,0xF89E,0xF829,0xF802,0xF829,0xF89E,0xF95B,0xFA5A,0xFB90,
- 0xFCF2,0xFE72,
- 0x0000,0x018F,0x030F,0x0471,0x05A7,0x06A6,0x0763,0x07D8,0x07FF,0x07D8,
- 0x0763,0x06A6,0x05A7,0x0471,0x030F,0x018F,0x0000,0xFE72,0xFCF2,0xFB90,
- 0xFA5A,0xF95B,0xF89E,0xF829,0xF802,0xF829,0xF89E,0xF95B,0xFA5A,0xFB90,
- 0xFCF2,0xFE72,
- 0x0000,0x018F,0x030F,0x0471,0x05A7,0x06A6,0x0763,0x07D8,0x07FF,0x07D8,
- 0x0763,0x06A6,
- };
- unsigned int t1_count=0; // 全局变量,用于定时器中断计数
- unsigned int count_flag=0; //
- extern signed int U_data_in[640]; // 40个周波AD采样数据,为16位2的补码
- long offsettemp, temp1=0, temp2=0;
- //extern signed int U_data_out[320]; // 40个周波AD采样数据,为16位2的补码
- void delay()//1 sec
- {
- unsigned int i, j;
- for (i=0; i<200; i++)
- for (j=0; j<1000; j++);
- }
- unsigned int search_offset(signed int *U_data)
- {
- signed int i,j;
- unsigned int index=0;
- signed int buff[6];
- signed int min=1000;
- min=1000;
- for (i=0; i<96; i=i+6)
- {
- buff[0] = U_data[i];
- buff[1] = U_data[i+1];
- buff[2] = U_data[i+2];
- buff[3] = U_data[i+3];
- buff[4] = U_data[i+4];
- buff[5] = U_data[i+5];
- if ((buff[0]<=0) && (buff[5]>=0))//判断数据是否由负变正
- {
- //if (/*(buff[4]>0) && */(buff[5]>=0))
- //{
- if(buff[0]<0)
- {
- buff[0] = 0 - buff[0];
- }
- if(buff[1]<0)
- {
- buff[1] = 0 - buff[1];
- }
- if(buff[2]<0)
- {
- buff[2] = 0 - buff[2];
- }
- if(buff[3]<0)
- {
- buff[3] = 0 - buff[3];
- }
- if(buff[4]<0)
- {
- buff[4] = 0 - buff[4];
- }
- if(buff[5]<0)
- {
- buff[5] = 0 - buff[5];
- }
- for (j=0; j<6; j++)
- {
- if (buff[j]<min)
- {
- min = buff[j];
- }
- }
- for (j=0; j<6; j++)
- {
- if (buff[j] == min)
- {
- //temp = i+j;
- return (i+j);
- }
- }
- //}
- }
- }
- return index;
- }
- /*if(buff[1]<buff[2])//比较哪个数据最接近零
- {
- //index = i+1;
- if(buff[3]<buff[4])//比较哪个数据最接近零
- {
- //index = i+3;
- if(buff[1]<buff[3])
- {
- index = i+1;//buff[1]最小
- }
- else
- {
- index = i+3;//buff[3]最小
- }
- //return index;
- }
- else
- {
- //index = i+4;
- if(buff[1]<buff[4])
- {
- index = i+1;//buff[1]最小
- }
- else
- {
- index = i+4;//buff[4]最小
- }
- //return index;
- }
- return index;
- }
- else
- {
- //index = i+2;
- if(buff[3]<buff[4])
- {
- if(buff[2]<buff[3])
- {
- index = i+2;//buff[2]最小
- }
- else
- {
- index = i+3;//buff[3]最小
- }
- //return index;
- }
- else
- {
- if(buff[2]<buff[4])
- {
- index = i+2;//buff[2]最小
- }
- else
- {
- index = i+4;//buff[4]最小
- }
- //return index;
- }
- return index;
- }*/
- long sor_caculation(const signed int *U_data, const signed int *C_data, unsigned int offset)
- //long sor_caculation(signed int *U_data, const signed int *C_data, unsigned int offset)
- {
- unsigned int i, j;
- long sum1 = 0, sum2;
- // sum1 = 0;
- for (i=0; i<NN; i++)//一週期採樣點數
- {
- sum2 = 0;
- for(j=0; j<QQ; j++)//15周期
- {
- sum2 = sum2 + U_data[j*32 + i + offset];
- }
- sum2 = sum2 * C_data[i];
- // sum2 = sum2 / 480 ;
- sum1 += sum2;
- }
- return sum1;
- }
- void main(void)
- {
- //unsigned int i;
- DSP2407_Initializing(); // DSP2407芯片初始化
- IOPort_Initializing(); // 数字IO端口初始化
- Timer1_Initializing(); // 定时器1初始化
- Sampling_Initializing(); // AD7862初始化
- asm(" CLRC INTM"); // 允许总中断
- LEDSPORT = 0xff;
- LEDSPORT = 0x00;
- PCDATDIR = 0xFF00;//test
- PCDATDIR = 0xFFff;//test
- PCDATDIR = 0xFF00;//test
- if(PCDATDIR == 0xffff)
- {
- PCDATDIR = 0xff00;
- }
- else
- {
- PCDATDIR = 0xffff;
- }
- if(PCDATDIR == 0xffff)
- {
- PCDATDIR = 0xff00;
- }
- else
- {
- PCDATDIR = 0xffff;
- }
- PCDATDIR = 0xFFff;//test
- LEDSPORT = 0xff;
- T1CON=T1CON|0x0040; // 位6[TENABLE]1: 启动Timer1
- while(1) // 主循环,可分配非实时性任务
- {
- //temp = sor_caculation(U_data_in, t_Tab);
- asm(" nop "); // 空操作,等待中断
- if(count_flag)
- {
- count_flag = 0;
- asm(" setc INTM"); // 禁止总中断
- offsettemp = search_offset(U_data_in);
- offsettemp = 0;
- temp1 = sor_caculation(t_Tab, Sin_Tab, offsettemp);
- temp2 = sor_caculation(t_Tab, Cos_Tab, offsettemp);
- //delay();
- //delay();
- //delay();
- //delay();
- asm(" CLRC INTM"); // 允许总中断
- }
- }
- }