(bio)}: Ո(qng)һ´aTEA5767+PT2257 ++ڿƏā(li)](mi)Ū^(gu)Ce˵ĻA(ch)ϸĵ [ӡ(y)]

: q13003765457    r(sh)g: 2015-10-13 21:14
(bio)}: Ո(qng)һ´aTEA5767+PT2257 ++ڿƏā(li)](mi)Ū^(gu)Ce˵ĻA(ch)ϸĵ
/***********************************
ӲSTC12C5A60S2 ʞ9600
************************************/
#include<reg52.h>
#include<intrins.h>
#include <stdio.h>

#define uchar unsigned char
#define uint  unsigned int
#define ulong unsigned long        int

# define  BAUD 9600                   //ʴС
# define SYS 11059200        //С
# define x   -(SYS/384/BAUD)        /*D(zhun)Q鶨r(sh)ֵĹʽע⣺@?yn)?yng)
                                                                a(b)a֪R(sh)Բٹʽǰ256 */
#define max_freq 10800
#define min_freq 870
#define write_order 0xc0
#define read_order 0xc1

uchar data fm_write[5]={0x00,0x00,0x31,0x17,0x00};                  
uchar data fm_read[5]={0,0,0,0,0};
uchar xdata table[4];
uchar i;
ulong xdata freq,PLL,frequency,Volume;
bit flag=1;
uchar count;
bit mode,hisi;

sbit SDA=P3^2;     //x(sh)(j)˿
sbit SCL=P3^3;     //xr(sh)犾˿
sbit SDA1=P3^4;     //x(sh)(j)˿1
sbit SCL1=P3^5;     //xr(sh)犾˿1
sbit UP=P0^0;      //ք(dng){(dio)_(ti)
sbit DOWN=P0^1;    //ք(dng){(dio)_(ti)
sbit WTUP=P0^2;    //΢{(dio)
sbit WTDOWN=P0^3;  //΢{(dio)

void delayms(uint ms)
{
  uint i;
  while(ms--)
   {
   for(i=0;i<100;i++);
   }
}


void iic_start()   //_(ki)ʼ
{ SDA=1;
  SCL=1;
  _nop_();
_nop_();
  _nop_();
  SDA=0;
  _nop_();
_nop_();
  _nop_();
  _nop_();
  _nop_();
  SCL=0;
}

void iic1_start()   //_(ki)ʼ1
{ SDA1=1;
  SCL1=1;
  _nop_();
_nop_();
  _nop_();
  SDA1=0;
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  SCL1=0;
}


void iic_stop()//Y(ji)
{
SDA=0;
_nop_();
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA=1;
}

void iic1_stop()//Y(ji)
{
SDA1=0;
_nop_();
_nop_();
_nop_();
SCL1=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA1=1;
}


uchar read_8bit()//xȡ8bit(sh)(j)
{
        uchar i,dat=0;
for(i=0;i<8;i++)
  { SCL=1;
    dat<<=1;
    dat|=(uchar)SDA;
    SCL=0;
   }
  return(dat);
}


void write_8bit(uchar dat)
{
        uchar i;
  for(i=0;i<8;i++)
   {SDA=(bit)(dat&0x80);
    _nop_();
    _nop_();
     SCL=1;
     _nop_();
     _nop_();
     _nop_();
     SCL=0;
     dat<<=1;
     }
}

void write1_8bit(uchar dat)
{
        uchar i;
  for(i=0;i<8;i++)
   {SDA1=(bit)(dat&0x80);
    _nop_();
    _nop_();
     SCL1=1;
     _nop_();
     _nop_();
     _nop_();
     SCL1=0;
     dat<<=1;
     }
}

void send_ack()//l(f)J(rn)λxȡ(sh)(j)J(rn)
{ SDA=0;
   _nop_();
   _nop_();
  _nop_();
   SCL=1;
   _nop_();
   _nop_();
   _nop_();
   _nop_();
  _nop_();
   SCL=0;
}

void send1_ack()//l(f)J(rn)λxȡ(sh)(j)J(rn)
{ SDA1=0;
   _nop_();
   _nop_();
  _nop_();
   SCL1=1;
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   SCL1=0;
}

void send_noack()
{
SDA=1;
_nop_();
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA=0;
SCL=0;
}

void send1_noack()
{
SDA1=1;
_nop_();
_nop_();
_nop_();
SCL1=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA1=0;
SCL1=0;
}

void iicInit(void)
{
SCL = 0;
iic_stop();
}

void iic1Init(void)
{
SCL1 = 0;
iic1_stop();
}



bit check_ack()//zy(c)_J(rn)λֵ0ʾ(sh)(j)ݔɹ
{  
   bit ack_bit;
   SDA=1;
  _nop_();
  _nop_();
  SCL=1;
  _nop_();//ڵھł(g)ڃ(ni)Ƭጷsda,ȴoһ(g)_J(rn)λ
  _nop_();
  _nop_();
  _nop_();
  ack_bit=SDA;
   SCL=0;
   return(ack_bit);

}


bit check1_ack()//zy(c)_J(rn)λֵ0ʾ(sh)(j)ݔɹ
{  
   bit ack1_bit;
   SDA1=1;
  _nop_();
  _nop_();
  SCL1=1;
  _nop_();//ڵھł(g)ڃ(ni)Ƭጷsda,ȴoһ(g)_J(rn)λ
  _nop_();
  _nop_();
  _nop_();
  ack1_bit=SDA1;
   SCL1=0;
   return(ack1_bit);

}

void get_frequency()
{  
        ulong y;
   y=PLL;
   hisi=fm_write[2]&0x10;
    if(hisi)
   freq=(y*8192+225000)/10000;
   else
   freq=(y*8192-225000)/10000 ;
}


void get_pll()
{
ulong freq1,y=frequency;
  freq1=y*100000;
  hisi=fm_write[2]&0x10;
  if(hisi)
  PLL=((freq1+225000)*4)/32768;
  else
  PLL=((freq1-225000)*4)/32768;
}


void read_5byte()
{
uchar i,tempH,tempL;
  iic_start();
   write_8bit(read_order);
   check_ack();
    for(i=0;i<5;i++)
    {fm_read[i]=read_8bit();
     send_ack();
    }
        send_noack();
    iic_stop();
   tempH=fm_read[0];
   tempL=fm_read[1];
   tempH&=0x3f;
   PLL=tempH*256+tempL;
   get_frequency();
}


void write_5byte()
{       
        uchar i;
   iic_start();
   write_8bit(write_order);
   check_ack();
   for(i=0;i<5;i++)
   {
   write_8bit(fm_write[i]);
     check_ack();
    }
  iic_stop();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();

}

void Volume_Write(uchar Vol)
{
        uchar Temp_H,Temp_L;
        Temp_H=(Vol/10)|0xe0;
        Temp_L=(Vol%10)|0xd0;                                                   
    iic1_start();        //p•ͬr(sh)׃-10dB/Step
    write1_8bit(0x88);            //(xi)PT2257ַ
        if(!check1_ack())
        {
            write1_8bit(Temp_H);
                send1_ack();
                write1_8bit(Temp_L);
                send1_ack();
        }
    iic1_stop();
}

void refurbish_5767()//FMоƬ(sh)(j)ˢһ
{

get_pll();          //Ӌ(j)PLL,
fm_write[0]=PLL/256;
fm_write[1]=PLL%256;

fm_write[2]=0x31;
fm_write[3]=0x11;
fm_write[4]=0x00;

//if(mode)fm_write[2]|=0x80;//oSUD=1ģʽ
//else fm_write[2]&=0x7f;//oSUD=0ģʽ

write_5byte();
delayms(60);
read_5byte();

}


void hand_search()  //ք(dng)_(ti)ӳ
{
      read_5byte();
      refurbish_5767();

}

void wt_search()  //΢{(dio)ӳ
{
read_5byte();
   if(mode)
   {frequency+=1;
        if(frequency>max_freq)
        frequency=min_freq;
    }
   else
    {frequency-=1;
      if(frequency<min_freq)
         frequency=max_freq;
     }
      refurbish_5767();
}
void Uart_init(void)
{
        SM0=0;
        SM1=1;                   //ڹʽO(sh)
        REN=1;

        ES=1;                   //_(ki)Д

        TMOD=0x20;           //r(sh)1Ĺʽ8Ԅ(dng)b

        TH1= x;
        TL1= x;                   //ֵ

        EA = 1;                   //_(ki)Д࿂_(ki)P(gun)
        ET1 = 0        ;      //ֹr(sh) ,ֻʰl(f)
        TR1 = 1;           //_(ki)r(sh)1Д_(ki)P(gun)
}
void send_string(uchar *pl)          //l(f)͔(sh)(j)Ӻ(sh)          
{
     ES = 0;
         do  
        {
      SBUF = *pl++;
             while(!TI);                           //ȴ(sh)(j)l(f)
          TI=0 ;
        } while(*pl);                           //ֱ'\0'ĕr(sh)Y(ji)
        ES=1;
}
void send_data(unsigned long int dat)
{
        uchar xdata buf[32] ;
        sprintf(buf , "%1.0f",(float) dat);  //ͨ^(gu)sprintf(sh)є(sh)(j)D(zhun)QַͰl(f)
        send_string(buf);
}

void Enter()
{
                  ES=0;
                  SBUF = '\r';
                  while(!TI);                           //ȴ(sh)(j)l(f)
                  TI=0 ;       
                  SBUF = '\n';
                     while(!TI);                           //ȴ(sh)(j)l(f)
                  TI=0 ;       
                  ES=1;
}


void main(void)
{   
    iicInit();
     iic1Init();
        Uart_init();
    delayms(1000);
        send_string("ʼC(j)");
        Enter();
        frequency=1076;       //ĬJ(rn)_(ti)107.6MHz
        get_pll();             //Ӌ(j)PLL
        refurbish_5767();
        Volume_Write(70);

while(1)
{
        if(flag)
        {
           if(i==4)
            {
                hand_search();
                Enter();
                send_string("(dng)ǰlǣ");
                send_data(frequency/10);
                send_string(".");
                send_data(frequency%10);
                send_string("MHZ");
                Enter();
                flag=0;
             }
            if(i==1)
             {   
              Volume_Write(Volume);
               Enter();
               send_string("(dng)ǰǣ");
               send_data(Volume);
               Enter();
               flag=0;
              }
        }

}
}

void Uart(void) interrupt 4 using 1        //ДӺ(sh)
{
        if(1==RI)        //M(jn)ДſԌ(sh)F(xin)ДČ(du)(yng)푑(yng)
        {
                RI = 0;
                table[i] = SBUF;
                i++;
                if(i==4)
                {
                        i=0;
            frequency=(table[0]-48)*1000+(table[1]-48)*100+(table[2]-48)*10+(table[3]-48);
                  //frequency=table[0]-48;
                        flag=1;
                }
          if(i==1)
          {
              i=0;
             Volume=table[0]*10+table[1];
                 flag=1;
                 }
        }
}
                 






gӭR (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1