原创 PT2258的应用(C语言)

2010-7-2 15:41 5262 8 11 分类: MCU/ 嵌入式
#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));
}

文章评论3条评论)

登录后参与讨论

用户1567315 2013-8-12 17:24

博主有没电路?

用户377235 2013-8-3 13:11

pt2258.h 文件在哪 QQ277152571

xucun915_925777961 2011-5-25 15:01

很干净很少有注释-_-
相关推荐阅读
用户999308 2011-10-11 10:32
MASK(掩模)、OTP(一次性可编程)、FlashROM的区别
OTP(One Time Programable)是MCU的一种存储器类型MCU按其存储器类型可分为MASK(掩模)ROM、OTP(一次性可编程)ROM、FLASH ROM等类型。MASK ROM的M...
用户999308 2011-08-17 16:49
wireless
今天是玩无线遥控器的第三天,主要的编码芯片有PT2262,ev1527,解码芯片有PT2272....
用户999308 2011-08-06 22:00
VB教程
http://www.enet.com.cn/eschool/video/21hulian_vb/...
用户999308 2011-08-06 21:51
共享一个PIC12F629调光程序
使用MPLIB自带的Hi-tech lite版c编译器编译。不知道为何,Timer0定时中断总有问题,无法用作延时,只好采用实时延时方式,反正MCU也没别的事情好干。延时不是太精确,没仔细去调整。欢迎...
用户999308 2011-05-23 15:51
安规电容X,Y
X电容是用于差模滤波的,即并联于输入的两端.滤除L,N线之间的差模信号;Y电容用于共模滤波,它接于L于地或N于地之间,滤除L对地或N对地的共模信号,(Y电容通常对称使用) 它们都是安规电容. 在交流...
我要评论
3
8
关闭 站长推荐上一条 /3 下一条