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

📄 cs44800.c

📁 PWM的驱动完整例子,非常有帮助,使用于各种PWM的应用
💻 C
📖 第 1 页 / 共 2 页
字号:
	
	retval = API_GetI2C(CS44800_ADDRESS,			// Get the current register
						 CS44800_CLK_CONFIG, 		// value
						 &data);
						 
	if(PWM_RUN == state)							// If run, clear the PDN bit
	{
		data &= 0xfe;
	}
	else
	{	
		data |= 0x01;						 		// Stop, set the PDN bit
	}
	
	retval |= API_SendI2C(CS44800_ADDRESS, 		// Write the register back
						   CS44800_CLK_CONFIG, 
	        			   data);
    CHECK_RETURN(retval);

   	return SUCCESS;
} // CS44800_SetPDNState

//-----------------------------------------------------------------------------
//  Function:
int PWM_SetVolume(
//  Description:
//		Sets the master volume between +12dB and -50dB plus off.
//  Notes:
//
//  Parameters:
		unsigned char vol)			// 0-63, where 0 is off, 1 is -50dB and 
									// 63 is +12dB
//  Returns: unsigned char
//		SUCCESS = 0, FAIL otherwise
//-----------------------------------------------------------------------------
{
	unsigned char retval;
	char tmpvol;
	
	if(vol > MAX_VOL)					// Qualify vol to a valid number
	{
		return FAIL;
	}
			
	if(0 == vol)						// vol = 0 is off, so this is a 
	{									// special case
		tmpvol = OFF_VOL;
	}
	else								// Else, to generate the proper 2's
	{									// complement volume value such that
		tmpvol = (char) vol	- 51;		// vol values between 1 and 63 scale
	}									// to -50 through +12, subtract 51
	
	retval = API_SendI2C(CS44800_ADDRESS, 			// Write the value to
						   CS44800_MINT_VOL, 		// the Master volume
	        			   (unsigned char) tmpvol);	// register
    CHECK_RETURN(retval);	

   	return SUCCESS;
} // CS44800_SetMasterVolume

//-----------------------------------------------------------------------------
//  Function:
unsigned char CS44800_SetChannelVolume(
//  Description:
//		Sets all channel volumes between +12dB and -50dB plus off.
//  Notes:
//
//  Parameters:
		unsigned char vol)			// 0-63, where 0, 1 is -50dB and 63 is +12dB
//  Returns: unsigned char
//		SUCCESS = 0, FAIL otherwise
//-----------------------------------------------------------------------------	
{
	unsigned char retval, i;
	char tmpvol;
	
	if(vol > MAX_VOL)					// Qualify vol to a valid number
	{
		return FAIL;
	}
			
	if(0 == vol)						// vol = 0 is off, so this is a 
	{									// special case
		tmpvol = OFF_VOL;
	}
	else								// Else, to generate the proper 2's
	{									// complement volume value such that
		tmpvol = (char) vol	- 51;		// vol values between 1 and 63 scale
	}									// to -50 through +12, subtract 51
	
	
	for(i=0;i<8;i++)
	{
		retval = API_SendI2C(CS44800_ADDRESS, 			// Write the value to
							   (CS44800_CH1INT_VOL + i),// all channel volume
	        				   (unsigned char) tmpvol);	// registers
        CHECK_RETURN(retval);
	}
	

    return SUCCESS;
} // CS44800_SetChannelVolume

//-----------------------------------------------------------------------------
//  Function:
int PWM_AmpOn(
        //
        //    Description:
        //          Turns the external High power amp on
        //           
        //    Parameters:          
        //
        //    Notes:
        //		To turn the back end on, always bring the Powerup pin high first,
        //		followed by the Enable pins.  Turning off is just the reverse.
        //		This function works for either the half-bridge or full-bridge
        //		implementation because the PWM Enable lines are connected to the same
        //		GPIO pins on both boards.
        void 
        )
//    Returns:  SUCCESS or FAIL
//-----------------------------------------------------------------------------
{
    int retval;

    DEBUG_OUT("PWM_AmpOn start\n");

	retval = API_SendI2C(CS44800_ADDRESS, 			// Set all the Enable
						  CS44800_GPIO_STAT, 		// pins high
	        			  0x1e);
    CHECK_RETURN(retval);

    DEBUG_OUT("PWM_AmpOn success\n");

    return SUCCESS;
}

//-----------------------------------------------------------------------------
//  Function:
int PWM_AmpOff(
        //
        //    Description:
        //          Turns the external High power amp off
        //           
        //    Parameters:          
        //
        //    Notes:
        //		To turn the back end on, always bring the Powerup pin high first,
        //		followed by the Enable pins.  Turning off is just the reverse.
        //		This function works for either the half-bridge or full-bridge
        //		implementation because the PWM Enable lines are connected to the same
        //		GPIO pins on both boards.
        void 
        )
//    Returns:  SUCCESS or FAIL
//-----------------------------------------------------------------------------
{
    int retval;

    DEBUG_OUT("PWM_AmpOff start\n");

	retval = API_SendI2C(CS44800_ADDRESS, 			// Set the Powerup pin low.
						   CS44800_GPIO_STAT, 		
		        		   0x20);				
    CHECK_RETURN(retval);

    DEBUG_OUT("PWM_AmpOff success\n");

    return SUCCESS;
}

//-----------------------------------------------------------------------------
//  Function:
unsigned char Write3Bytes(
//
//  Description:
//		Writes three bytes to the CS44800, starting at the register value passed
//		in.  After each byte is written, the register value is incremented.
//  Notes:
//
//  Parameters:
		unsigned char reg,			// Starting register address to write to
		unsigned char byte1,		// First data byte to write
		unsigned char byte2,		// Second data byte to write
		unsigned char byte3)		// Third data byte to write
//
//  Returns: unsigned char
//		SUCCESS = 0, FAIL otherwise
//-----------------------------------------------------------------------------
{
	unsigned char retval;
	
	retval = API_SendI2C(CS44800_ADDRESS,		// Write the three bytes
						  reg, 					
	        			  byte1);

	retval |= API_SendI2C(CS44800_ADDRESS,		
						  reg + 1, 
	        			  byte2);

	retval |= API_SendI2C(CS44800_ADDRESS,		
						  reg + 2, 
	        			  byte3);

	return retval;
} // Write3Bytes	

//-----------------------------------------------------------------------------
//  Function:
unsigned char Read3Bytes(
//
//  Description:
//		Reads three bytes from the CS44800, starting at the register value passed
//		in.  After each byte is read, the register value is incremented.
//  Notes:
//
//  Parameters:
		unsigned char reg,			// Starting register address to read from
		Uint8* byte1,		// First data byte to read
		Uint8* byte2,		// Second data byte to read
		Uint8* byte3)		// Third data byte to read
//
//  Returns: unsigned char
//		SUCCESS = 0, FAIL otherwise
//-----------------------------------------------------------------------------
{
	unsigned char retval;
	
	retval = API_GetI2C(CS44800_ADDRESS,			// Read the three bytes
						 reg, 	
						 byte1);
						 
	retval |= API_GetI2C(CS44800_ADDRESS,			
						 reg + 1, 	
						 byte2);

	retval |= API_GetI2C(CS44800_ADDRESS,			
						 reg + 2, 	
						 byte3);
						 
	return retval;
} // Read3Bytes

//-----------------------------------------------------------------------------
//  Function:
int PWM_UnMute(
        //
        //    Description:
        //          unmutes the volume
        //           
        //    Parameters:          
        void
        )
//    Returns:  SUCCESS or FAIL
//-----------------------------------------------------------------------------
{
	unsigned char retval;
	unsigned char mute_data, pdn_data;
	
	retval = SUCCESS;				// Assume success
	
	mute_data = 0x00;		// Assume unmute
	pdn_data = 0x00;
						    			   
	retval = API_SendI2C(CS44800_ADDRESS, 	// Write the Channel Mute 
			   		CS44800_CHAN_MUTE, 			// register
			   		mute_data);
    CHECK_RETURN(retval);

					
   	return SUCCESS;
} // CS44800_SetMuteState



//-----------------------------------------------------------------------------
//  Function:
int PWM_Mute(
        //
        //    Description:
        //          mutes the volume
        //           
        //    Parameters:          
        void
        )
//    Returns:  SUCCESS or FAIL
//-----------------------------------------------------------------------------
{
	unsigned char retval;
	unsigned char mute_data, pdn_data;
	
	retval = SUCCESS;				// Assume success
	
	mute_data = 0xff;
	pdn_data = 0xff;
	
	retval |= API_SendI2C(CS44800_ADDRESS, 	// Write the Channel Mute 
				   CS44800_CHAN_MUTE, 			// register
	    		   mute_data);
    CHECK_RETURN(retval);
	
   	return SUCCESS;
} // CS44800_SetMuteState


//-----------------------------------------------------------------------------
//  Function:
int CS44800_ClearGPIOInt(
//  Description:
//		Reads the CS44800 GPIO Status register to clear the GPIO interrupt.
//  Notes:
//
//  Parameters:
		void)
//  Returns: unsigned char
//		SUCCESS = 0, FAIL otherwise 
//
//-----------------------------------------------------------------------------
{
	unsigned char retval;
	unsigned char data;
	
	retval = API_GetI2C(CS44800_ADDRESS,		// Read the GPIO Status register
					 	 CS44800_GPIO_STAT, 	// to clear the GPIO interrupt
					 	 &data);			

	return retval;
} //  bsp_ClearGPIOInt						 	 

#endif   //USE_PWM

⌨️ 快捷键说明

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