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

📄 freqcnt.c

📁 基于5402的虚拟仪器源码
💻 C
字号:
#include <string.h>
#include <stdlib.h>
#include "globleio.h"
//#include "table.h"
/////////////////////////////////////////////////////////////////////////////
#define first_half 0x0001
#define half_empty 0x0002
#define wave_up    0x0004
/////////////////////////////////////////////////////////////////////////////
#pragma DATA_SECTION(RxBuf,  "dmrx_sect")
signed int RxBuf[1024];
#pragma DATA_SECTION(OutBuf,  "out_sect")
signed int OutBuf[6];
//#pragma DATA_SECTION(CirBuf,  "circle_sect")
//signed int CirBuf[96];
unsigned int status;
extern void dspinit();
//extern void bsp0init();
extern void bsp1init();
extern void dmainit();
extern void bspstop();
extern void computer( int *p);
/////////////////////////////////////////////////////////////
//int *RxIndex,*CirBufIndex;
int i;
int Vmax,Vmin,Vpp,Freq,Vdc;
int Hthreshold,Lthreshold,Vhigh,Vlow;
unsigned long nfreq,Vsum;
int Vtemp[64];
int Ftemp[64];
int Pfreq,nAverage;
long Flong,Vlong;
unsigned int st_portc;
/////////////////////////////////////////////////////////////

void count(int *buf)
{
	unsigned int counter;
	int Vcurrent;
	counter=512;
	Vsum=0;									//每512点清最大,最小			
	Vmax=0;
	Vmin=0xfff;
	while(counter)
	{
		nfreq++;
		Vcurrent=*(buf+512-counter);
		if(Vcurrent>Vmax)
		{
			Vmax=Vcurrent;					//最大
		}
		if(Vcurrent<Vmin)
		{
			Vmin=Vcurrent;					//最小
		}
		if((status&wave_up)==wave_up)		//上拱?
		{
			if(Vcurrent<Lthreshold)			//超过上阈植?
			{
				status&=~wave_up;
				Vlow=0xfff;
			}
			if(Vcurrent>Vhigh)
			{
				Vhigh=Vcurrent;				//波峰
			}
		}
		else
		{
			if(Vcurrent>Hthreshold)			//低于下阈值?
			{
				status|=wave_up;
				Pfreq++;
				if(Pfreq>=64)
				{
					Pfreq=0;
				}
				Vtemp[Pfreq]=Vhigh-Vlow;		//峰峰值
				Ftemp[Pfreq]=64000/nfreq;		//频率=采样率/周期点数
				Vhigh=0;
				nfreq=0;
			}
			if(Vcurrent<Vlow)
			{
				Vlow=Vcurrent;				//找波谷
			}
			
		}
		Vsum=Vsum+Vcurrent;
		counter--;
	}
	Vdc=Vsum/512;							//512点均差为直流分量
	Hthreshold=Vdc+Vtemp[Pfreq]/8;			//上阈值
	Lthreshold=Vdc-Vtemp[Pfreq]/8;			//下阈值
}

int hexbcd(int value)						//BCD码转换
{
	int bcdtemp1,bcdtemp2,bcdtemp3,bcdtemp4;
	bcdtemp1=value/1000;
	value=value-bcdtemp1*1000;
	bcdtemp2=value/100;
	value=value-bcdtemp2*100;
	bcdtemp3=value/10;
	value=value-bcdtemp3*10;
	bcdtemp4=(bcdtemp1<<12)+(bcdtemp2<<8)+(bcdtemp3<<4)+value;
	return bcdtemp4;
}

void output()
{
	int nTemp;
	long bcdtemp;
	Flong=0;
	Vlong=0;
	if(Ftemp[Pfreq]>1000)					//频率及Vpp取平均
	{
		nAverage=64;
	}
	else if(Ftemp[Pfreq]>100)				//频率高则需平均的点数多
	{
		nAverage=8;
	}
	else
	{
		nAverage=1;
	}
	for(i=0;i<nAverage;i++)
	{
		if((Pfreq-i)>=0)
		{
			nTemp=Pfreq-i;
		}
		else
		{
			nTemp=Pfreq-i+64;
		}
		Flong=Flong+Ftemp[nTemp];
		Vlong=Vlong+Vtemp[nTemp];
	}
	bcdtemp=Flong/nAverage;
//	bcdtemp=Ftemp[Pfreq];
	OutBuf[0]=hexbcd(bcdtemp/10000);
	bcdtemp=bcdtemp%10000;
	OutBuf[1]=hexbcd((int)bcdtemp);
	bcdtemp=(unsigned long)Vmax*500/1023;			//500对应5V
	OutBuf[2]=hexbcd((int)bcdtemp);
	Vpp=Vlong/nAverage;
	bcdtemp=(unsigned long)Vpp*500/1023;			//AD最大0x3ff
	OutBuf[3]=hexbcd((int)bcdtemp);
	bcdtemp=(unsigned long)Vmin*500/1023;
	OutBuf[4]=hexbcd((int)bcdtemp);					//51使用BCD码
	bcdtemp=(unsigned long)Vdc*500/1023;
	OutBuf[5]=hexbcd((int)bcdtemp);
}

void main()
{
	dspinit();				//CPU参数初始化
	dmainit();				//DMA初始化
    bsp1init();				//BSP初始化
	
	st_portc=0xffff;
	st_portc&=~BSP_C1;
	portcfee=st_portc;
	
	for(i=0;i<64;i++)
		Vtemp[i]=0;			//Vpp初始化	
//	RxIndex=TxBuf;
//	CirBufIndex=CirBuf;
	Pfreq=0;
	status=0;
	asm(" STM #3fffh, IFR");		
	asm(" STM #3000h, IMR");
	asm(" RSBX INTM");		//使能DMA中断
	while(1)
	{
		asm(" RSBX XF");
		if((status&half_empty)==half_empty)		//DMA半区满
		{
			asm(" SSBX XF");
			status&=~half_empty;
			if((status&first_half)==first_half)	//前半区
			{
				count(RxBuf);					//主处理函数
				
			}
			else
			{
				count(&RxBuf[512]);
			
			}
			output();							//输出
		}
	}
}


interrupt void dmtx()							//DMA中断
{
	asm(" STM #3fffh, IFR");
	if((status&first_half)==first_half)
	{
		status&=~first_half;
		status|=half_empty;						//置半区满
	}
	else
	{
		status|=first_half;
		status|=half_empty;
	}
}


⌨️ 快捷键说明

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