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

📄 drv_timer0.c.bak

📁 CC1000 Rf modem C codes for philips sLpc213X ARM MCU.
💻 BAK
📖 第 1 页 / 共 2 页
字号:
#include "config_GD61.h"
#include "include_GD61.h"
#include <stdio.h>
#include <math.h>

volatile int32 ADI[AD_SAMPLE_NUM];		// A/D sample working buffer for I
volatile int32 ADU[AD_SAMPLE_NUM];		// A/D sample working buffer for U
volatile int32 ADI_BK[AD_SAMPLE_NUM];	// A/D sample result  buffer for I
volatile int32 ADU_BK[AD_SAMPLE_NUM];	// A/D sample result  buffer for U

volatile uint8  ADC_Flag;
volatile uint16 ADCmS_Counter;
volatile uint16 ADC_Period_mS;
volatile uint16 mSecondTick;		// for delay function
volatile uint16 RSSIF_mSecondTick;	// for delay function

volatile uint16 RTC_Failed_Time=3000;   //3000ms
volatile uint16 RTC_mSecond;  			//1000ms
volatile uint8 RTC_Failed_Flag;
volatile uint8 ADC_Channel_num=5;
volatile uint16 Channel_Max_Time=2*Channel_Input_Time;

volatile uint16 MP_FailedmSecondCnt;		// for main power failed detector

uint16 ADC_EventCounter;		// before calibration

int32 CurInputScale_I;			// before calibration
int32 CurInputScale_U;

int32 ADC_IIK_Value;					// calibrate K for I
int32 ADC_UUK_Value;					// calibrate K for U
int32 ADC_WPK_Value;					// calibrate K for WP
int32 ADC_QPK_Value;					// calibrate K for QP
int32 ADC_SPK_Value;					// calibrate K for SP

/*
int32 CurInput_II;						// after calibration
int32 CurInput_UU;
int32 CurInput_WP;
int32 CurInput_QP;
int32 CurInput_SP;
int32 CurInput_PF;						// PF = P/S

int32 CurInputULevel;
int32 CurInputILevel;

int32 CurOutput_I;
int32 CurOutput_U;
int32 CurOutput_WP;
int32 CurOutput_QP;
int32 CurOutput_SP;
int32 CurOutput_PF;

int32 CurOutputULevel;
int32 CurOutputILevel;
*/

int32 StdOutput_RO;
int32 StdOutput_LC;

int16  ADC_I_Filter[AD_FILTER_SIZE];	//noise filter buffer
int16  ADC_U_Filter[AD_FILTER_SIZE];
int16  ADC_WP_Filter[AD_FILTER_SIZE];
int16  ADC_QP_Filter[AD_FILTER_SIZE];
int16  ADC_SP_Filter[AD_FILTER_SIZE];
int16  ADC_UL_Filter[AD_FILTER_SIZE];
int16  ADC_IL_Filter[AD_FILTER_SIZE];

uint16 ADC_FilterP;					// pointer for the filter buffer above
uint8  SysRunState;
uint8  ChannelState;

extern volatile uint16 ADC_ChannelTimer;
extern volatile uint16 LD_AlarmCounter;
extern volatile uint16 LD_AlarmEnTimer;
extern volatile uint16 OutReadyTimer;
extern volatile uint8  PW_FLAG;

/////////////////////////////////////////////////////////////////////////////	

void DelayMS_(uint16 dly);
void __irq IRQ_Timer0 (void);
void Calculate_IUP(void);
void ClearFilter(void);
void TimerInitialize(void);
void ADC_Init(void);
void AD_ChannelSwitch(void);
void TestADC(void);
/****************************************************************************
** 函数名称 :DelayMS()		 ;delay 1 mil second 
** 函数功能 :长软件延时
** 入口参数 :dly	延时参数,值越大,延时越久
** 出口参数 :无
****************************************************************************/
void DelayMS_(uint16 dly)
{
	mSecondTick=(dly+1);		//resolution is 1 mS
	while(mSecondTick>1);	
}

/****************************************************************************
** 函数名称 :IRQ_Timer0()
** 函数功能 :定时器0中断服务程序,取反LED9控制口。
** 入口参数 :无
** 出口参数 :无
*****************************************************************************/
void __irq IRQ_Timer0(void)
{
uint8 i;
////
	TraceFunAddr((void*)IRQ_Timer0);
////
	if(ADCmS_Counter < AD_SAMPLE_NUM){			//0,1,2,3,...19. ????

#ifdef INCLUDE_DEBUGGING
		IO1CLR = LED1;	// LED1点亮,for debugging
#endif

////get I sample
	if(PW_FLAG==0)	//掉电报警的时候不做电压电流采样
	{
		AD0CR  =(1 << 3)						|	// SEL=8,选择通道3
				((LPC_FPCLK / 3000000 - 1) << 8)	|	// CLKDIV=LPC_FPCLK/3000000-1,转换时钟为1MHz
				(0 << 16)						|	// BURST=0,软件控制转换操作
				(0 << 17)						|	// CLKS=0, 使用11clock转换
				(1 << 21)						|  	// PDN=1,正常工作模式
				(0 << 22)						|  	// TEST1:0=00,正常工作模式
//				(1 << 24)						|	// START=1,直接启动ADC转换
				(0 << 27);						 	// 直接启动ADC转换时,此位无效

		AD0CR |= 1 << 24 ;						// 进行第一次转换
		while((AD0DR & 0x80000000) == 0);		// 等待转换结束
		AD0CR |= 1 << 24;						// 再次启动转换
		while((AD0DR & 0x80000000) == 0);		// 等待转换结束
		ADI[ADCmS_Counter]=AD0DR;				// 读取ADC结果

////get U sample
		AD0CR  =(1 << 2)						|	// SEL=8,选择通道2
				((LPC_FPCLK / 3000000 - 1) << 8)	|	// CLKDIV=LPC_FPCLK/3000000-1,转换时钟为1MHz
				(0 << 16)						|	// BURST=0,软件控制转换操作
				(0 << 17)						|	// CLKS=0, 使用11clock转换
				(1 << 21)						|  	// PDN=1,正常工作模式
				(0 << 22)						|  	// TEST1:0=00,正常工作模式
//				(1 << 24)						|	// START=1,直接启动ADC转换
				(0 << 27);						 	// 直接启动ADC转换时,此位无效

		AD0CR |= 1 << 24 ;						// 进行第一次转换
		while((AD0DR & 0x80000000) == 0);		// 等待转换结束		
		AD0CR |= 1 << 24;						// 再次启动转换
		while((AD0DR & 0x80000000) == 0);		// 等待转换结束
		ADU[ADCmS_Counter]=AD0DR;				// 读取ADC结果
		
		
	}
	
	

#ifdef INCLUDE_DEBUGGING
		IO1SET = LED1;					// LED1 off,for debugging
#endif

	}

////set the sample ok flag		
	if((ADCmS_Counter==(AD_SAMPLE_NUM-1))&&	//check if 20 samples is ready
	   (ADC_Flag      != AD_AVAILABLE   )){		//check if the data last time already be used.

		for(i=0;i<AD_SAMPLE_NUM;i++){			//push new data into result buffer
			ADI_BK[i]=ADI[i];
			ADU_BK[i]=ADU[i];
		}
		ADC_Flag = AD_AVAILABLE;				// 20 new samples get ready in result buffer.
	}

////
	ADCmS_Counter++; 					// for A/D convertting
	if(ADCmS_Counter>=ADC_Period_mS){
		ADCmS_Counter=0; 
	}
	
////
#ifdef EN_MAIN_POWER_ALARM					//enable the counter for power detecting in pulse mode.
	if(MP_FailedmSecondCnt <= MAIN_POWER_FAILED_MSECOND){	//power failed detecting in pulse mode.
		MP_FailedmSecondCnt++;		//power failed detector stop at (MAIN_POWER_FAILED_MSECOND+1).
	}
#endif

	if(mSecondTick!=0){
		mSecondTick--;						// ms running, for delay function
	}
	
	
	if(RSSIF_mSecondTick!=0){
		RSSIF_mSecondTick--;						// ms running, for delay function
	}
	
	if(RTC_Failed_Time!=0)
	{
		RTC_Failed_Time--;
		if(RTC_Failed_Time==0) 
		{
			RTC_mSecond=1000;
			ILR=0x03;										// 清除增量中断标志
			CCR=CCR&0x1e;								// 禁止RTC计数
			VICIntEnClr=(1<<INT_RTC);		// 禁止RTC中断
		}
	//	if(RTC_Failed_Flag==1) VICIntEnable = (1<<INT_RTC);	// 恢复RTC中断
	}
	
	else 
	{
	//	RTC_Failed_Flag=1;
		
		RTC_mSecond--;
		if(RTC_mSecond==0) 
		{
			RTC_Time0_Sencond();
			RTC_mSecond=1000;
		}
	}
	
////
	T0IR = 0x01;			/* 清除中断标志*/
	VICVectAddr = 0x00;		/* 通知VIC中断处理结束*/
}

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
void Calculate_IUP(void)
{

int32 ADI_Real;
int32 ADI_Image;
int32 ADC_IAC;

int32 ADU_Real;
int32 ADU_Image;
int32 ADC_UAC;

int32 ADU_DCLevel;
int32 ADI_DCLevel;

int32 ADC_WP;			// WP = (Ur*Ir+Ui*Ii)
int32 ADC_QP;			// QP = (Ui*Ir-Ur*Ii)
int32 ADC_SP;			// SP = sqrt(WP^2+QP^2)

int32 CurCurrentOrg;	// before calibration
int32 CurVoltageOrg;

int32 CurW_PowerOrg;
int32 CurQ_PowerOrg;
int32 CurS_PowerOrg;

int32 CurRO_LoadOrg;
int32 CurLC_LoadOrg;

int32 CurPFactorOrg;

int32 CurU_DCLevelOrg;
int32 CurI_DCLevelOrg;

uint8 i;

////////////////////////////////////////////////////////////////////////
//	check if AD simple is available
		if(ADC_Flag != AD_AVAILABLE) return;	//data not ready

////////////////////////////////////////////////////////////////////////
// the following belongs to the condition: ADC_Flag == AD_AVAILABLE
#ifdef  INCLUDE_DEBUGGING
		Uart0SendString("calculation IUP...");	
#endif
		for(i=0;i<AD_SAMPLE_NUM;i++){
//get I sample
			ADI_BK[i]=(ADI_BK[i]>>6)&0x3ff;		// 10 bits A/D
			ADI_BK[i]= ADI_BK[i]*3000;			// 参考电压3000 mV
			ADI_BK[i]= ADI_BK[i]/1024;
//get U sample
			ADU_BK[i]=(ADU_BK[i]>>6)&0x3ff;
			ADU_BK[i]= ADU_BK[i]*3000;			// 参考电压3000 mV
			ADU_BK[i]= ADU_BK[i]/1024;
		}

////calculate Current(I)
		ADI_Real = (951* (ADI_BK[0]+ADI_BK[18]-ADI_BK[8]-ADI_BK[10])+809*(ADI_BK[1]+ADI_BK[17]-ADI_BK[7]-ADI_BK[11])+
					588* (ADI_BK[2]+ADI_BK[16]-ADI_BK[6]-ADI_BK[12])+309*(ADI_BK[3]+ADI_BK[15]-ADI_BK[5]-ADI_BK[13])+
					1000*(ADI_BK[19]-ADI_BK[9]));
//		ADI_Real =	ADI_Real/10000;

////
		ADI_Image= (309* (ADI_BK[0]+ADI_BK[8]-ADI_BK[10]-ADI_BK[18])+588*(ADI_BK[1]+ADI_BK[7]-ADI_BK[11]-ADI_BK[17])+
					809* (ADI_BK[2]+ADI_BK[6]-ADI_BK[12]-ADI_BK[16])+951*(ADI_BK[3]+ADI_BK[5]-ADI_BK[13]-ADI_BK[15])+
					1000*(ADI_BK[4]-ADI_BK[14]));
//		ADI_Image=	ADI_Image/10000;

////	
//		ADC_IAC  = ADI_Image*ADI_Image + ADI_Real*ADI_Real;
//		ADC_IAC  = sqrt((double)ADC_IAC);

////
		ADI_DCLevel=  ADI_BK[0] +ADI_BK[1] +ADI_BK[2] +ADI_BK[3] +ADI_BK[4] +ADI_BK[5] +ADI_BK[6] +ADI_BK[7] +ADI_BK[8] +ADI_BK[9]+
				    ADI_BK[10]+ADI_BK[11]+ADI_BK[12]+ADI_BK[13]+ADI_BK[14]+ADI_BK[15]+ADI_BK[16]+ADI_BK[17]+ADI_BK[18]+ADI_BK[19];
//		ADI_DCLevel= ADI_DCLevel/20;

////calculate Voltage(U)
		ADU_Real = (951* (ADU_BK[0]+ADU_BK[18]-ADU_BK[8]-ADU_BK[10])+809*(ADU_BK[1]+ADU_BK[17]-ADU_BK[7]-ADU_BK[11])+
					588* (ADU_BK[2]+ADU_BK[16]-ADU_BK[6]-ADU_BK[12])+309*(ADU_BK[3]+ADU_BK[15]-ADU_BK[5]-ADU_BK[13])+
					1000*(ADU_BK[19]-ADU_BK[9]));
//		ADU_Real =	ADU_Real/10000;

////
		ADU_Image= (309* (ADU_BK[0]+ADU_BK[8]-ADU_BK[10]-ADU_BK[18])+588*(ADU_BK[1]+ADU_BK[7]-ADU_BK[11]-ADU_BK[17])+
					809* (ADU_BK[2]+ADU_BK[6]-ADU_BK[12]-ADU_BK[16])+951*(ADU_BK[3]+ADU_BK[5]-ADU_BK[13]-ADU_BK[15])+
					1000*(ADU_BK[4]-ADU_BK[14]));
//		ADU_Image=	ADU_Image/10000;

////	
//		ADC_UAC  = ADU_Image*ADU_Image + ADU_Real*ADU_Real;
//		ADC_UAC  = sqrt((double)ADC_UAC);

////
		ADU_DCLevel= 	ADU_BK[0] +ADU_BK[1] +ADU_BK[2] +ADU_BK[3] +ADU_BK[4] +ADU_BK[5] +ADU_BK[6] +ADU_BK[7] +ADU_BK[8] +ADU_BK[9]+
				    ADU_BK[10]+ADU_BK[11]+ADU_BK[12]+ADU_BK[13]+ADU_BK[14]+ADU_BK[15]+ADU_BK[16]+ADU_BK[17]+ADU_BK[18]+ADU_BK[19];
//		ADU_DCLevel= ADU_DCLevel/20;

		ADC_Flag = AD_AVAILABLE+1;		// flag the data in result buffer has already be used.
										// data in result buffer can be refresh.
////									// system can receive new data from Timer0 interrupt serving again.
		ADI_Real  = ADI_Real/10000;
		ADI_Image = ADI_Image/10000;
		ADI_DCLevel = ADI_DCLevel/AD_SAMPLE_NUM;

		ADU_Real  = ADU_Real/10000;
		ADU_Image = ADU_Image/10000;
		ADU_DCLevel = ADU_DCLevel/AD_SAMPLE_NUM;

////
		ADC_IAC  = ADI_Image*ADI_Image + ADI_Real*ADI_Real;
		ADC_IAC  = sqrt((double)ADC_IAC);

		ADC_UAC  = ADU_Image*ADU_Image + ADU_Real*ADU_Real;
		ADC_UAC  = sqrt((double)ADC_UAC);

////calculate Power Rate(P), 			WP = (Ur*Ir+Ui*Ii)
		ADC_WP = (ADU_Real*ADI_Real + ADU_Image*ADI_Image)/1000;
		

⌨️ 快捷键说明

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