freescale

来自「Freescale 系列单片机常用模块与综合系统设计」· 代码 · 共 188 行

TXT
188
字号
#include "LCD1602.h"
#include "MMA1220_60D.h"
#include "ST.h"

/******************************************************************************
* Uchar AccDataConvGs(Uchar u8RawValue) 													*		
*					Subroutine convert the data acquired from the Accelerometers	*
*					to G units.	This depends on the Accelerometer actually 			*
*					selected.																		*
*																										*
* Parameters:	u8RawValue is an 8-bit value that contains the data to be		*
*					converted to Gs.																*
*																										*
* Return: 		8-bit value containing the data formatted to Gs.					*
******************************************************************************/
Uchar AccDataConvGs(Uchar u8RawValue) {

	Uint u16FormattedValue;

	if(gu8AccSelect == MMA1220) {
	      u16FormattedValue = u8RawValue*100;		//conversion to Gs for MMA1220
	      u16FormattedValue /= MMA1220_CONST;
		}
	else if(gu8AccSelect == MMA1260){
	      u16FormattedValue = u8RawValue*10;		//conversion to Gs for MMA1260
	      u16FormattedValue /= MMA1260_CONST;
		}
	return((Uchar)u16FormattedValue);
}

/******************************************************************************
* void AccTest(Uchar u8AccUnderTest) 														*		
*					Subroutine to test the Accelerometers status. The routine sets	*
*					the Self-test pin to high to test the Accelerometer function	*
*					to be within specs. This is tried a maximum amount of 5 times.	*
*					The subroutine reflects the output to the LCD.						*
*																										*
* Parameters:	u8AccUnderTest is an 8-bit value that specifies the				*
*					Accelerometer to be tested.												*
*																										*
* Return: 		None																				*
******************************************************************************/
void AccTest(Uchar u8AccUnderTest){

	bool bTestStatus = FAIL;
	Uchar u8SelfTestCount = 0;
	Uchar u8Value1 = 0;
	Uchar u8Value2 = 0;

	Lcd_Write_Command(CLEAR_LCD);
	Lcd_Write_Command(CURSOR_HOME);
							
	Lcd_Disp_Str(0x00, 0x00, "Testing ");   // Display acc under test

	if(u8AccUnderTest == MMA1220)
	{
		Lcd_Disp_Str(0x00, 0x08, "MMA1220 ");
	}
	else if(u8AccUnderTest == MMA1260)
	{
	  Lcd_Disp_Str(0x00, 0x08, "MMA1260 ");
	}

	do 
	{
	   ST_ClrVal();										// Self-test pin low
	   WaitNms(25);
	   													
		u8Value1 = AD1_GetValue16(u8AccUnderTest);	//	Generate new ADC conversion
																// Acc output without Self Test

	   ST_SetVal() ;										// Self-test pin high
	   WaitNms(25);
	   
	   u8Value2 = AD1_GetValue16(u8AccUnderTest);	// Generate new ADC conversion
	   														// Acc output with Self Test
	   
	   if(u8Value2 > u8Value1)    // Output change is positive?
	   {						
	   	u8Value2 -= u8Value1;						
	   	
	   	/* Check if change within accelerometer electrical specs */
	   	if((u8AccUnderTest == MMA1220) && (u8Value2 > MMA1220_MIN_DELTA) && (u8Value2 < MMA1220_MAX_DELTA) )
	   	{
	   			bTestStatus = OK;
	   			break;
	   	}
	   	if((u8AccUnderTest == MMA1260) && (u8Value2 > MMA1260_MIN_DELTA) && (u8Value2 < MMA1260_MAX_DELTA) )
	   	{
	   			bTestStatus = OK;
	   			break;
	   	}
	   	
	   }
	      
	   u8SelfTestCount++;								// Increase the testing counter 

	}while(u8SelfTestCount<5);


	ST_ClrVal();											// Self test pin low

	Lcd_Disp_Str(0x01, 0x00, "Self-Test");
	
	if (bTestStatus == OK) 
	{							
	   Lcd_Disp_Str(0x01, 0x0B, "OK"); // Self test successful
	}
	else 
	{  					
	   Lcd_Disp_Str(0x01, 0x0B, "Failed"); // Self-test unsuccessful 
	}														// after 5 tries

	WaitNms(250);											// Wait for 0.5 seconds
	WaitNms(250);

	return;
}

/******************************************************************************
* bool AccDataFormat(Uchar u8AccData) 														*		
*					This function formats the data acquired from the Adc to the 	*
*					the LCD Message Buffer.	The data is formatted to G units and 	*
*					positioned in the Message Buffer to be displayed in the LCD		*
*																										*
* Parameters:	u8AccData is an 8-bit value with the data acquired from the 	*
*					Adc.																				*
*																										*
* Return: 		bNewTopFlag is a boolean variable that reflects if there is a 	*
*					new top value.																	*
******************************************************************************/
bool AccDataFormat(Uchar u8AccData)
{
	Uchar u8BufferPosition;
	bool bNewTopFlag = FALSE;

														// Format  ?0.0 G
	if(u8AccData < 128)      //数据是负值
	{                  	
	   gau8LcdFirstLine[9] = '-';				 
	   u8AccData = 128 - u8AccData;			// Remove offset
	}	
	else                      //数据是正值
	{							 					
	   gau8LcdFirstLine[9] = ' ';
	   u8AccData = u8AccData - 128;			// Remove offset
	}
				
	if((u8AccData > gu8TopAccValue) || (gbResetTop))
	{
	   gu8TopAccValue = u8AccData;			// Update Top Value
	   bNewTopFlag = TRUE;						
	   gbResetTop = FALSE;						
	   gbOveraccState = FALSE;
	}
	   
	u8AccData = AccDataConvGs(u8AccData);	// Data converted to Gs

	if(gu8AccSelect == MMA1220)         	// Change Acc indicator to MMA1220
	{			
	   gau8LcdFirstLine[5] = '2';
	   if(u8AccData>80) gbOveraccState = TRUE;
	}
	   
	if(gu8AccSelect == MMA1260)          // Change Acc indicator to MMA1260
	{				
	   gau8LcdFirstLine[5] = '6';
	   if(u8AccData>15) gbOveraccState = TRUE;
	}   
	 
	if(u8AccData<10)
	{
	   gau8LcdFirstLine[10]='0';				// Add 0 in case of G<1
	}

	u8BufferPosition = 12;						// BCD Converson to Message Buffer
	do
	{
	   gau8LcdFirstLine[u8BufferPosition--]= (u8AccData%10) + '0';
	   u8BufferPosition--;
	}while((u8AccData /= 10) > 0); 

	return(bNewTopFlag);							// Return Top Value Flag
}

/******************************************************************************
* End Accelerometer.c																			*
******************************************************************************/

⌨️ 快捷键说明

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