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

📄 2440lib.c

📁 2440 开发板的测试程序。包括uart,iic,lcd
💻 C
📖 第 1 页 / 共 3 页
字号:

	return 0 ;
}

//====================================================================
void Uart_GetString( char* string )
{
	char* string2 = string;
	char c;
	while ( ( c = Uart_Getch() ) != '\r' )
	{
		if ( c == '\b' )
		{
			if ( ( int ) string2 < ( int ) string )
			{
				Uart_Printf( "\b \b" );
				string--;
			}
		}
		else
		{
			*string++ = c;
			Uart_SendByte( c );
		}
	}
	*string = '\0';
	Uart_SendByte( '\n' );
}

//=====================================================================
int Uart_GetIntNum( void )
{
	char str[30];
	char* string = str;
	int base = 10;
	int minus = 0;
	int result = 0;
	int lastIndex;    
	int i;

	Uart_GetString( string );

	if ( string[0] == '-' )
	{
		minus = 1;
		string++;
	}

	if ( string[0] == '0' && ( string[1] == 'x' || string[1] == 'X' ) )
	{
		base = 16;
		string += 2;
	}

	lastIndex = strlen( string ) - 1;

	if ( lastIndex < 0 )
		return -1;

	if ( string[lastIndex] == 'h' || string[lastIndex] == 'H' )
	{
		base = 16;
		string[lastIndex] = 0;
		lastIndex--;
	}

	if ( base == 10 )
	{
		result = atoi( string );
		result = minus ? ( -1 * result ) : result;
	}
	else
	{
		for ( i = 0; i <= lastIndex; i++ )
		{
			if ( isalpha( string[i] ) )
			{
				if ( isupper( string[i] ) )
					result = ( result << 4 ) + string[i] - 'A' + 10;
				else
					result = ( result << 4 ) + string[i] - 'a' + 10;
			}
			else
				result = ( result << 4 ) + string[i] - '0';
		}
		result = minus ? ( -1 * result ) : result;
	}
	return result;
}

//*****************************************************************************
//get a number for the uart
//*****************************************************************************
int Uart_GetIntNum_GJ( void )
{
	char string[16] ;
	char* p_string = string ;
	char c;
	int i = 0 ;
	int data = 0 ;

	while ( ( c = Uart_Getch() ) != '\r' )
	{
		if ( c == '\b' )
			p_string--;
		else
			*p_string++ = c;

		Uart_SendByte( c ) ;
	}

	*p_string = '\0';

	i = 0 ;
	while ( string[i] != '\0' )
	{
		data = data * 10 ;
		if ( string[i] < '0' || string[i] > '9' )
			return -1 ;
		data = data + ( string[i] - '0' ) ;
		i++ ;
	}	

	return data ;
}
//*****************************************************************************

void Uart_SendByte( int data )
{
	U16 m ;

	if ( whichUart == 0 )
	{
		if ( data == '\n' )
		{
			while ( !( rUTRSTAT0 & 0x2 ) );
			for( m = 127; m > 0 ; )  m-- ;		//because the slow response of hyper_terminal 
			WrUTXH0( '\r' );
		}
		while ( !( rUTRSTAT0 & 0x2 ) );   //Wait until THR is empty.
		for( m = 127; m > 0 ; )  m-- ;		//because the slow response of hyper_terminal 
		WrUTXH0( data );
	}
	else if ( whichUart == 1 )
	{
		if ( data == '\n' )
		{
			while ( !( rUTRSTAT1 & 0x2 ) );
			for( m = 63; m > 0 ; )  m-- ;		//because the slow response of hyper_terminal 
			RS485_1_Out_In( TRUE ) ;
			for( m = 63; m > 0 ; )  m-- ;		//because the slow response of hyper_terminal 
			rUTXH1 = '\r';
		}
		while ( !( rUTRSTAT1 & 0x2 ) );   //Wait until THR is empty.
		for( m = 63; m > 0 ; )  m-- ;		//because the slow response of hyper_terminal 
		rUTXH1 = data;
		while ( !( rUTRSTAT1 & 0x2 ) );   //Wait until THR is empty.
		RS485_1_Out_In( FALSE ) ;
	}
	else if ( whichUart == 2 )
	{
		if ( data == '\n' )
		{
			while ( !( rUTRSTAT2 & 0x2 ) );
			for( m = 63; m > 0 ; )  m-- ;		//because the slow response of hyper_terminal 
			RS485_2_Out_In( TRUE );
			for( m = 63; m > 0 ; )  m-- ;		//because the slow response of hyper_terminal 
			rUTXH2 = '\r';
		}
		while ( !( rUTRSTAT2 & 0x2 ) );   //Wait until THR is empty.
		for( m = 63; m > 0 ; )  m-- ;		//because the slow response of hyper_terminal 
		rUTXH2 = data;
		while ( !( rUTRSTAT2 & 0x2 ) );   //Wait until THR is empty.
		RS485_2_Out_In( FALSE );
	}
}   			

//====================================================================
void Uart_SendString( char* pt )
{
	while ( *pt )
		Uart_SendByte( *pt++ );
}

//=====================================================================
//If you don't use vsprintf(), the code size is reduced very much.
void Uart_Printf( char* fmt , ... )
{
	va_list ap;
	char string[256];

	va_start( ap , fmt );
	vsprintf( string , fmt , ap );
	Uart_SendString( string );
	va_end( ap );
}

void Buzzer_Freq_Set( U32 freq )
{
	rGPBCON &= ~3;			//set GPB0 as tout0, pwm output
	rGPBCON |= 2;

	rTCFG0 &= ~0xff;
	rTCFG0 |= 15;			//prescaler = 15+1
	rTCFG1 &= ~0xf;
	rTCFG1 |= 2;			//mux = 1/8
	rTCNTB0 = ( PCLK >> 7 ) / freq;
	rTCMPB0 = rTCNTB0 >> 1;	// 50%
	rTCON &= ~0x1f;
	rTCON |= 0xb;			//disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0
	rTCON &= ~2;			//clear manual update bit
}

void Buzzer_Stop( void )
{
	rGPBCON &= ~3;			//set GPB0 as output
	rGPBCON |= 1;
	rGPBDAT &= ~1;
}

//***************************[ BOARD BEEP ]*******************************
void Beep( U32 freq , U32 ms )
{
	Buzzer_Freq_Set( freq ) ;
	Delay( ms ) ;
	Buzzer_Stop() ;
}

/****************************************************************************
【功能说明】蜂鸣器PWM测试
****************************************************************************/
void BUZZER_PWM_Test( void )
{
	U16 freq = 1000 ;

	Uart_Printf( "\nBUZZER TEST ( PWM Control )\n" );
	Uart_Printf( "Press +/- to increase/reduce the frequency of BUZZER !\n" ) ;
	Uart_Printf( "Press 'ESC' key to Exit this program !\n\n" );

	Buzzer_Freq_Set( freq ) ;

	while ( 1 )
	{
		U8 key = Uart_Getch();

		if ( key == '+' )
		{
			if ( freq < 20000 )
				freq += 10 ;

			Buzzer_Freq_Set( freq ) ;
		}

		if ( key == '-' )
		{
			if ( freq > 11 )
				freq -= 10 ;

			Buzzer_Freq_Set( freq ) ;
		}

		Uart_Printf( "\tFreq = %d\n" , freq ) ;
		if ( key == ESC_KEY )
		{
			Buzzer_Stop() ;
			return ;
		}
	}
}

//********************** BOARD LCD backlight ]****************************
void LcdBkLtSet( U32 HiRatio )
{
#define FREQ_PWM1		1000

	if ( !HiRatio )
	{
		rGPBCON = rGPBCON & ( ~( 3 << 2 ) ) | ( 1 << 2 ) ;	//GPB1设置为output
		rGPBDAT &= ~( 1 << 1 );
		return;
	}
	rGPBCON = rGPBCON & ( ~( 3 << 2 ) ) | ( 2 << 2 ) ;		//GPB1设置为TOUT1

	if ( HiRatio > 100 )
		HiRatio = 100 ;	

	rTCON = rTCON & ( ~( 0xf << 8 ) ) ;			// clear manual update bit, stop Timer1

	rTCFG0 &= 0xffffff00;					// set Timer 0&1 prescaler 0
	rTCFG0 |= 15;							//prescaler = 15+1

	rTCFG1 &= 0xffffff0f;					// set Timer 1 MUX 1/16
	rTCFG1 |= 0x00000030;					// set Timer 1 MUX 1/16

	rTCNTB1 = ( 100000000 >> 8 ) / FREQ_PWM1;		//if set inverter off, when TCNT2<=TCMP2, TOUT is high, TCNT2>TCMP2, TOUT is low
	rTCMPB1 = ( rTCNTB1 * ( 100 - HiRatio ) ) / 100 ;	//if set inverter on,  when TCNT2<=TCMP2, TOUT is low,  TCNT2>TCMP2, TOUT is high

	rTCON = rTCON & ( ~( 0xf << 8 ) ) | ( 0x0e << 8 ) ;
	//自动重装,输出取反关闭,更新TCNTBn、TCMPBn,死区控制器关闭
	rTCON = rTCON & ( ~( 0xf << 8 ) ) | ( 0x0d << 8 ) ;		//开启背光控制
}

/****************************************************************************
【功能说明】LCD背光亮度控制,PWM控制占空比
****************************************************************************/
void LCD_BackLight_Control( void )
{
	U8 HiRatio = 50 ;		
	Uart_Printf( "\nNOTE : ONLY SOME type LCD kit  SUPPORT backlight adjust!!!\n" ) ;

	Uart_Printf( "Press +/- to increase/reduce the light of LCD !\n" ) ;
	Uart_Printf( "Press 'ESC' to Exit this test program !\n\n" );

	LcdBkLtSet( HiRatio ) ;

	while ( 1 )
	{
		U8 key = Uart_Getch();

		if ( key == '+' )
		{
			if ( HiRatio < 100 )
				HiRatio += 1 ;
		}

		if ( key == '-' )
		{
			if ( HiRatio > 1 )
				HiRatio -= 1 ;
		}

		if ( key == ESC_KEY )
			break ;

		LcdBkLtSet( HiRatio ) ;
		Uart_Printf( "LCD backlight HiRatio %d\n" , HiRatio ) ;
	}
}


/********************************************************************************************************************
【函数名称】void LCD_Power_ON_OFF( unsigned char m )
【功能描述】
********************************************************************************************************************/
void LCD_Power_ON_OFF( unsigned char m )
{
	rGPGCON = ( rGPGCON & ~( 3 << 8 ) ) | ( 1 << 8 );

	if ( m == TRUE )
	{
		rGPGDAT = ( rGPGDAT & ~( 1 << 4 ) ) | ( 1 << 4 );
	}
	else
	{
		rGPGDAT = ( rGPGDAT & ~( 1 << 4 ) ) | ( 0 << 4 );
	}
}

/********************************************************************************************************************
【函数名称】void LCD_Back_Light_ON_OFF( unsigned char m )
【功能描述】
********************************************************************************************************************/
void LCD_Back_Light_ON_OFF( unsigned char m )
{
	rGPCCON = ( rGPCCON & ~( 3 << 0 ) ) | ( 1 << 0 );

	if ( m == TRUE )
	{
		rGPCDAT = ( rGPCDAT & ~( 1 << 0 ) ) | ( 1 << 0 );
	}
	else
	{
		rGPCDAT = ( rGPCDAT & ~( 1 << 0 ) ) | ( 0 << 0 );
	}
}

/********************************************************************************************************************
【函数名称】void Led_Display( int data )
【功能描述】LED灯控制
********************************************************************************************************************/
void Led_Display( int data )

⌨️ 快捷键说明

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