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

📄 func_auto_namg.c

📁 2.4G无线音箱的接收程序,采用ATMEGA8单片机和STS公司的无线模块完成
💻 C
字号:
/******************************************************************************
*
* Purpose:      Func_Auto_NAMG.c
*
* Creator:      Rob Lansbergen
*
* Version:		$Revision: 6 $
*
* File Name:	$Workfile: Func_Auto_NAMG.c $
*
* Author:		Rob Lansbergen
*
* Check in:		$Author: Chong.cheeleong $
*
* The information is provided 揳s is?without any express or implied warranty  
* of any kind, * including warranties of merchantability, noninfringement of   
* intellectual property, or fitness for any particular purpose. In no event sha
* Wireless Sound Solutions and/or its affiliate companies, or its suppliers be 
* liable for any damages whatsoever arising out of the use of or inability to  
* use the information or the materials. Wireless Sound Solutions, its affiliate
* companies, and its suppliers further do not warrant the accuracy or          
* completeness of information, text, graphics, or other items contained within 
* materials. Wireless Sound Solutions, Inc., may make changes to materials, or 
* the products described within, at any time, without notice.                  
* ?007 Wireless Sound Solutions. All rights reserved. Wireless Sound Solutions
* STS and STS-wireless are trademarks of Wireless Sound Solutions.        
******************************************************************************/
/*
** Include files
** -------------
*/
#include <stdio.h>
#include "defines.h"

/*
 * NACK Message on MU 2 CU
 * Byte 1  : 93
 * Byte 2  : AGC down level
 * Byte 16 : Checksum.
 */
#define MSG_TYPE_AGCD_MU 	93 /* 0x5D */

/* Parameter settings */
unsigned char NACK_Time 	= 0x01; /* MU AGCD Report Time Interval in Seconds */
unsigned char NAMG_Offset	= 83;   /* NAMG Offset (ranging 77 -- 83) */

#define NAMG_MIN 107
#define NAMG_MAX 117


/* Variable declarations */
unsigned char NACK_Time_Cnt = 0x01;	/* Report Time Counter */
unsigned char AGC_Max 		= 0x10; /* Start Value for AGC Max Tracking */
unsigned char NAMG_shd 	    = 0x74; /* Shadow Register of NAMG, 116 decimal, is Default Reset Value */
unsigned char NAMG_MAX_Reduction_Tmr = 0x00;/*Counter for message received*/		
unsigned char AGCD_Msg_Send_Tmr 	= 0x00;/*Counter for message send*/		
unsigned char MAXNAMG_TimeOut_Tmr 	= 0x00;	/*Set NAMG to MAX value if over the time limit*/		
unsigned char DT_AGCD_Level[5];	/*Data Table to get the average value of AGCD and eliminate the highest value*/	
unsigned char AGCD_Level    = 0x00;     /*AGC Detect Level*/
unsigned char NAMG_Level    = 0x00;     /*NAMG Level*/
/******************************************************************************
*
* Function Name :   Handle_Auto_NAMG_MainLoop
*
* Purpose       :   Handel the Auto NAMG Procedure.
*
* Arguments     :   none
*
* Return value  :   none
*
******************************************************************************/
void Handle_Auto_NAMG_MainLoop(void)
{		

    //#ifdef Target_MU          /******* WRONG **********************************/
    #ifdef Target_CU            /******* CORRECTION, PLEASE USE THIS LINE *******/     
    if (Use_ModuleMode == CU)
	{
		if(NAMG_MAX_Reduction_Tmr == NAMG_MAX_DEDUCT_2SEC)		
		{
			NAMG_MAX_Reduction_Tmr = 0;
			/* AGC Max processing */
            if (AGC_Max !=0)
	   			AGC_Max--;			                  
		}
	}
   //#endif /*Target_MU*/ /******* WRONG **********************************/
   #endif /*Target_CU*/ /******* CORRECTION, PLEASE USE THIS LINE *******/  

	if (Event_NACK )		
	{
		Event_NACK = 0;
	   #ifdef Target_MU
		unsigned char sncd=0;
		if (Use_ModuleMode == MU)		
		{								
			DT_AGCD_Level[AGCD_Msg_Send_Tmr] = I2C_Read_Byte(AGCD_ADDR) & 0x7F;	
			AGCD_Msg_Send_Tmr++;				
			
			if(AGCD_Msg_Send_Tmr ==5)			
			{								
				sncd = I2C_Read_Byte(SNCD_ADDR);			                                           
				/* search for next insync Stream */
				for (int i = 0; i < 4; i++)
				{								
					/* Check if Stream is In Sync */
					if (((sncd  >> (i* 2)) & 0x03) != 0x00)
					{					
                       /* Next In Sync Channel Found */
						if(InSync_Detected == 1)
						{
			               	break;
						}
					}

					if((i == 3) && (InSync_Detected == 1))
                   	{
						InSync_Detected = 0;

                   	}
				
				}



				AGCD_Msg_Send_Tmr = 0;					
				if (NACK_Time_Cnt == 1)
				{
					/* New NACK Report Requested */
					NACK_Time_Cnt = NACK_Time;
				
					/*###### MU ######*/
			
					/* Send New AGCD Level to CU */
					TX_Data_send_array[0] = MSG_TYPE_AGCD_MU;
					TX_Data_send_array[1] = Calculate_Average_AGCD();


					/* send always a data msg */
					if (Data_Free_Up) /* is it a NACK application ? */
					{
						DATA_LED_ON;
						Send_Message(STR_FREE, 1); /* use free slots in nack applic */
						DATA_LED_OFF;
					}

				}
				else
				{
					/* Counting Down in Seconds for Next Report */
					if (NACK_Time_Cnt != 0) NACK_Time_Cnt--;
					else 					NACK_Time_Cnt = NACK_Time;
				}


			}
		}
	     #endif /*Target_MU*/
	}
}


/******************************************************************************
*
* Function Name :   Handle_Auto_NAMG_RXMsg
*
* Purpose       :   Handle Incomming NACK Messages.
*
* Arguments     :   none
*
* Return value  :   none
*
******************************************************************************/
void Handle_Auto_NAMG_RXMsg(void)
{
 #ifdef Target_CU                
	if (Use_ModuleMode == CU)
	{
		/*###### CU ######*/
		if (RX_Data_recv_array[0] == MSG_TYPE_AGCD_MU)
		{

			
			DATA_LED_ON;
			MAXNAMG_TimeOut_Tmr = 0;		/*Reset everytime receive message*/
			Data_Led_Counter = 10; /* after 100 mSec Data Led goes OFF again */
			/* msg to GUI */
			/* prepare parameters here and do some action */
			/* paralist: AGCD */
			AGCD_Level = RX_Data_recv_array[1];

			/*** Processing data ***/		
			/* AGC Max processing */
			
			if (AGC_Max < AGCD_Level) 
			{
				AGC_Max = AGCD_Level;
				NAMG_MAX_Reduction_Tmr =0;
			}
			
			/* NAMG_Level Processing */
			NAMG_Level = (AGC_Max >> 1) + NAMG_Offset;

			if ((NAMG_Level > NAMG_MAX) ||(AGC_Max >=65)) NAMG_Level = NAMG_MAX;
			if (NAMG_Level < NAMG_MIN) NAMG_Level = NAMG_MIN;

			/* writing new level to DARR80 */
			if (NAMG_Level != NAMG_shd)
			{
				NAMG_shd = NAMG_Level;
				I2C_Write_Byte(NAMG_ADDR, NAMG_Level);
			}

			/* tracking report */
#ifndef	NO_GUI
            Show_NAMG_Status();
#endif	/*NO_GUI*/
			/*** End Processing data ***/
		}
	}
 #endif /*Target_CU*/
}

/******************************************************************************
*
* Function Name :   Handle_Auto_NAMG_RXMsg_TimeOut
*
* Purpose       :   Set the NAMG to max after timeout.
*
* Arguments     :   none
*
* Return value  :   none
*
******************************************************************************/
void Handle_Auto_NAMG_RXMsg_TimeOut(void)
{

  #ifdef Target_CU       
	if(MAXNAMG_TimeOut_Tmr == NO_AGCD_RXMsg_2SEC )		
	{
		MAXNAMG_TimeOut_Tmr = 0;
		if (Use_ModuleMode == CU)
		{
			/* writing new level to DARR80 */
			if (NAMG_MAX != NAMG_shd)
			{
				NAMG_shd = NAMG_MAX;
				I2C_Write_Byte(NAMG_ADDR, NAMG_MAX);

			}

				/* tracking report */	
#ifndef	NO_GUI
            Show_NAMG_Timeout_Status();
#endif	/*NO_GUI*/

		}
	}
 #endif /*Target_CU*/	

}

/******************************************************************************
*
* Function Name :   Calculate_Average_AGCD
*
* Purpose       :   Calculate Average AGCD Value and eliminate the Max value.
*
* Arguments     :   none
*
* Return value  :   AGCD_Level
*
******************************************************************************/
unsigned char Calculate_Average_AGCD(void)
{
#ifdef Target_MU
	unsigned char a;
	unsigned char b;
	unsigned char AGCD_MAX;
	unsigned char i;
	unsigned int AGCD_Level=0;

	a =((DT_AGCD_Level[0] > DT_AGCD_Level[1]) ? DT_AGCD_Level[0] : DT_AGCD_Level[1]);
	b =((DT_AGCD_Level[2] > DT_AGCD_Level[3]) ? DT_AGCD_Level[2] : DT_AGCD_Level[3]);
	AGCD_MAX =((a> b) ? a : b);
	AGCD_MAX =((AGCD_MAX> DT_AGCD_Level[4]) ? AGCD_MAX : DT_AGCD_Level[4]);	

	for (i=0; i<5; i++)
	{
		AGCD_Level += DT_AGCD_Level[i];
	}

	AGCD_Level -= AGCD_MAX;
	AGCD_Level /= 0x04;
#endif /*Target_MU*/

	return(AGCD_Level);
}

/******************************************************************************
*
* Function Name :   Handle_Auto_NAMG_Func_Timer
*
* Purpose       :   Auto NAMG Function Timer.
*
* Arguments     :   none
*
* Return value  :   none
*
******************************************************************************/
void Handle_Auto_NAMG_Func_Timer(void)
{

	NAMG_MAX_Reduction_Tmr++;		
	MAXNAMG_TimeOut_Tmr++;
	Event_NACK ++;		

}


⌨️ 快捷键说明

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