www.pudn.com > PT2258.rar > PT2258.C, change:2009-04-14,size:4081b


#include "regx52.h" 
#include "PT2258.h" 
#include "intrins.h" 
 
#define uchar unsigned char  
#define uint    unsigned int     
 
#define SLA_W          0x88 
#define Global         0x00 
#define Mute_Control 0xf8 
 
uchar const Ch_Addr [] = { 
       0xe0,0xd0,0x90,0x80,0x50,0x40,0x10,0x00,0x30,0x20,0x70,0x60,0xb0,0xa0} ; 
 
void I2C_Delay(void) 
{ 
     _nop_(); 
     _nop_(); 
     _nop_(); 
     _nop_(); 
} 
 
//I2C Bus Start 
void I2C_Start(void) 
{ 
      SDA = 1 ; 
      SCL = 1 ;  
      _nop_(); 
      _nop_(); 
      SDA = 0 ; 
      I2C_Delay(); 
      SCL = 0 ; 
} 
 
//I2C bus stop 
void I2C_Stop(void) 
{ 
      SDA    = 0 ;  
      _nop_();     
      _nop_(); 
      SCL = 1 ; 
      I2C_Delay(); 
      SDA    = 1 ; 
} 
 
//Check the answer bit 
bit I2C_Cack(void) 
{ 
      bit Flag = 0x00; 
 
      SDA = 1    ;  
      _nop_(); 
      SCL = 1    ; 
      I2C_Delay(); 
      Flag = SDA ; 
      SCL = 0    ; 
      return (Flag); 
} 
 
//I2C write a byte 
void I2C_WrByte (uchar I2C_data) 
{ 
       uchar cnt; 
 
       for(cnt=0; cnt<8; cnt++) 
       { 
           I2C_data <<= 1; 
           SDA = CY ; 
           SCL = 1    ; 
           I2C_Delay(); 
           SCL = 0    ;         
       }  
} 
 
bit PT2258_WriteByte (uchar inData) 
{ 
      uchar step_Flag = 0, Time_out = 0;     
      bit ACK_Flag; 
 
      while (step_Flag < 0x02) 
      { 
         if (step_Flag ==    0x00) 
         { 
            I2C_Start(); 
            I2C_WrByte ( SLA_W ); 
            ACK_Flag = I2C_Cack(); 
            if (!ACK_Flag)     step_Flag += 1; 
            else I2C_Stop (); 
         } 
         else if (step_Flag == 0x01 ) 
         { 
            I2C_WrByte (inData); 
            ACK_Flag = I2C_Cack(); 
            if (!ACK_Flag)     step_Flag += 1; 
            else             
            {      
               I2C_Stop (); 
               step_Flag = 0; 
            } 
          } 
          Time_out ++; 
          if (Time_out > SafeCounter)    break; 
      } 
      I2C_Stop(); 
      return (ACK_Flag); 
} 
 
// 
bit PT2258_Write2Byte (uchar inData1,uchar inData2) 
{ 
      uchar step_Flag = 0, Time_out = 0;     
      bit ACK_Flag; 
 
      while (step_Flag < 0x03) 
      { 
         if (step_Flag ==    0x00) 
         { 
            I2C_Start(); 
            I2C_WrByte ( SLA_W ); 
            ACK_Flag = I2C_Cack(); 
            if (!ACK_Flag)    step_Flag += 1; 
            else I2C_Stop (); 
         } 
         else if (step_Flag == 0x01 ) 
         { 
            I2C_WrByte (inData1); 
            ACK_Flag = I2C_Cack(); 
            if (!ACK_Flag)     step_Flag += 1; 
            else             
           {      
              I2C_Stop (); 
             step_Flag = 0; 
          } 
       } 
       else if (step_Flag == 0x02 ) 
       { 
            I2C_WrByte (inData2); 
            ACK_Flag = I2C_Cack(); 
            if (!ACK_Flag)     step_Flag += 1; 
            else             
            {      
                I2C_Stop (); 
               step_Flag = 0; 
            }  
         } 
         Time_out ++; 
         if (Time_out > SafeCounter)    break; 
      } 
      I2C_Stop(); 
      return (ACK_Flag); 
} 
 
//**********************************************************************// 
// Set the PT2258's Volume        // 
// Input :    uchar Channel : Select the Channel of what you want to set // 
//            signed char Volume    (0 ~ -79dB)       // 
// Output:    The I2C ack flag : 0 = Has ACK    ; 1 = No ACK      // 
//**********************************************************************//            
bit SetVolume (uchar Channel, signed char Volume) 
{ 
     uchar tmp,Data1,Data2; 
 
     tmp     = -Volume;  
     Data1 = tmp / 10; 
     Data1 = Data1 | Ch_Addr[Channel+1];     
     Data2 = tmp % 10; 
     Data2 = Data2 | Ch_Addr[Channel]; 
     return (PT2258_Write2Byte (Data1,Data2)) ; 
} 
 
bit MuteGlobal (void) 
{  
     return ( PT2258_WriteByte (Mute_Control | 0x01)); 
} 
 
//  
bit unMuteGlobal (void) 
{     
     return ( PT2258_WriteByte (Mute_Control)); 
}