原创 SP5768锁相环程序

2008-12-11 22:58 2425 4 4 分类: MCU/ 嵌入式

//程序环境:CVAVR V1.24.6
//作者    :姬瑞江
//版权    :四川西江电子(西江工作室)
//日期    :2005年10月7日
#include "sp_pll.h"
extern  unsigned char wu_yd,wu_y_d,wu_jm_int,wu_jie_ma_a,wu_jmL,wu_jmH,wu_jm_ovr,wu_jm_temp,wu_temp,wu_tmpa,wu_tmpb,wu_tmpc;
extern  unsigned char dai_ji,addr_jqID0,addr_sbID0,addr_jqID1,addr_sbID1,audio_sb,gu_zhan,gong_lv;
extern  unsigned int  adc_data,wu_jmjs,wu_xs1,wu_xs2,IF2_plz,IF2_R,IF2_N,IF2_B,RF1_plz,RF1_plx,RF1_R,RF1_N,RF1_B,IF1_plz,IF3_plz;
extern  unsigned char open,save,P_xg,com_b,wu_tmpxxx,funid,URCHAR,for_temp,TIM1_tem;
extern  unsigned int  RF_z,RF_x,IF_z,IF_x,IRF_B;
 extern  unsigned  long  RF_JCQ;  
extern  unsigned char jie_ma_a[8];
extern  unsigned char jie_ma_b[8];


extern  unsigned char  PWM1_a,PWM1_b;


////数据合成:RF_N为N分频,CR_B为频率步进,R_FP为R分频,P_command为计算模式2
////             P3 P2 P1 P0 T0 C1 C0 R2 R1 R0 RD
////SP5768初始为: 1  1  1  1  0  1  1      0x7b
void DATA_S(unsigned char CR_B,unsigned int R_FP,unsigned long data,unsigned char P_command,unsigned char CE_ID)
                    {
                       unsigned long dat_a;
                       unsigned int  d_ata;   
                       unsigned char  PU_zf;
                      if((IF_z&0x8000)!=0)
                          {
                            IF_z = IF_z&0x7fff;
                            PU_zf = 1; 
                          }
                       else  PU_zf = 0;
                       if(P_command == 1)        ////用步进计算输出RN
                          {
                            dat_a = 0x7B;
                            d_ata = SP_R_F(4000,CR_B);        ////***得出R分频比
                            dat_a  = dat_a<<4;
                            dat_a |= SP_R_M(d_ata);            ////***得出R分频预值.
                            dat_a  = dat_a<<17;
                            dat_a  |= SP_N_F(RF_z,RF_x,IF_z,IF_x,IRF_B,PU_zf);
                          }
                       if(P_command == 2)        ////用R分频计算输出步进和RN
                          {
                            dat_a = 0x7B;
                            dat_a  = dat_a<<4;
                            dat_a |= SP_R_M(R_FP);            ////***得出R分频预值.
                            dat_a  = dat_a<<17;
                            dat_a  |= SP_N_F(RF_z,RF_x,IF_z,IF_x,IRF_B,PU_zf);
                          }   
                       if(P_command == 3)        ////用R分频计算输出步进和RN
                          {
                            dat_a = 0x7B;
                            dat_a  = dat_a<<4;
                            dat_a |= CR_B;            ////***得出R分频预值.
                            dat_a  = dat_a<<17;
                            dat_a |= SP_N_F(RF_z,RF_x,IF_z,IF_x,IRF_B,PU_zf);
                          }       
                       if(P_command == 4)        ////用R分频计算输出步进和RN
                          dat_a  = data;
                         
                       if(P_command == 5)        ////用R分频计算输出步进和RN
                          {
                           // dat_a  = 0x320000;
                              dat_a  = 0x320000;
                           //  dat_a  |= data & 0xffff0000;
                             dat_a  |= data;
                         
                          }
                         
                   //  SPI_datatx(dat_a,27,CE_ID);     //SP5768
                       SPI_datatx(dat_a,26,CE_ID);     //SP5768
                       
                  }



////R分频步进计算
unsigned int SP_R_F(unsigned int CYR,unsigned char CR_B)
              {     
                unsigned int  SP_RB;
                if((CR_B%25)==0) SP_RB = CYR/CR_B;
                else   SP_RB = 0;            
                return SP_RB;     
              }
             


////R分频的送数计算
unsigned char SP_R_M(unsigned int R_FP)
                   {
                     unsigned char  SP_5678_R;
                   if((R_FP == 2)||(R_FP == 3)||(R_FP == 4)||(R_FP == 5)||(R_FP == 8)||(R_FP == 10)||(R_FP == 16)||(R_FP == 20)||(R_FP == 32)||(R_FP == 40)||(R_FP == 64)||(R_FP == 80)||(R_FP == 128)||(R_FP == 160)||(R_FP == 256)||(R_FP == 320))
                    {
                     if(R_FP == 2) 
                       SP_5678_R = 0;
                     if(R_FP == 3) 
                       SP_5678_R = 1;
                     if(R_FP == 4)   
                       SP_5678_R = 2; 
                     if(R_FP == 5) 
                       SP_5678_R = 3; 
                     if(R_FP == 8) 
                       SP_5678_R = 4;
                     if(R_FP == 10) 
                       SP_5678_R = 5;
                     if(R_FP == 16) 
                       SP_5678_R = 6;
                     if(R_FP == 20) 
                       SP_5678_R = 7;  
                     if(R_FP == 32) 
                       SP_5678_R = 8; 
                     if(R_FP == 40) 
                       SP_5678_R = 9;
                     if(R_FP == 64) 
                       SP_5678_R = 10;
                     if(R_FP == 80) 
                       SP_5678_R = 11;
                     if(R_FP == 128) 
                       SP_5678_R = 12;  
                     if(R_FP == 160) 
                       SP_5678_R = 13;
                     if(R_FP == 256) 
                       SP_5678_R = 14;
                     if(R_FP == 320) 
                       SP_5678_R = 15;
               
                    }
                  else   SP_5678_R = 0xff;
                 
                  return SP_5678_R;
                }


////步进计算计算
unsigned int SP_R_B(unsigned char R_FP,unsigned int ji_zun)
                   {
                     unsigned int  SP_5678_R;
                   if(R_FP <16)
                    {
                     if(R_FP == 0)     
                       SP_5678_R = 2;  
                     if(R_FP == 1)     
                       SP_5678_R = 3;   
                     if(R_FP == 2)   
                       SP_5678_R = 4; 
                     if(R_FP == 3) 
                       SP_5678_R = 5; 
                     if(R_FP == 4) 
                       SP_5678_R = 8;
                     if(R_FP == 5) 
                       SP_5678_R = 10;
                     if(R_FP == 6) 
                       SP_5678_R = 16;
                     if(R_FP == 7) 
                       SP_5678_R = 20;  
                     if(R_FP == 8) 
                       SP_5678_R = 32; 
                     if(R_FP == 9) 
                       SP_5678_R = 40;
                     if(R_FP == 10) 
                       SP_5678_R = 64;
                     if(R_FP == 11) 
                       SP_5678_R = 80;
                     if(R_FP == 12) 
                       SP_5678_R = 128;  
                     if(R_FP == 13) 
                       SP_5678_R = 160;
                     if(R_FP == 14) 
                       SP_5678_R = 256;
                     if(R_FP == 15) 
                       SP_5678_R = 320;
                     SP_5678_R = ji_zun/SP_5678_R;
                    }
                  else   SP_5678_R = 0;
                  return SP_5678_R;
                }


               
////N分频步进计算
////RF_Pz:输出频率整数部分,RF_Px输出频率小数部分,IF_Pz:输入中频频率的整数部分,IF_Px:输入中频频率的小数部分,CR_B:频率步进,PU_zf:频率正谱倒谱
unsigned long SP_N_F(unsigned int RF_Pz,unsigned int RF_Px,unsigned int IF_Pz,unsigned int IF_Px,unsigned char CR_B,unsigned char PU_zf)
              {     
                unsigned long  SP_Ra,SP_Rb;           
                SP_Ra = RF_Pz*1000;                         
                SP_Ra = SP_Ra + RF_Px;                     
                SP_Rb = IF_Pz*1000;                        
                SP_Rb = SP_Rb+IF_Px;                         
                if(PU_zf == 1)  SP_Ra= SP_Ra+SP_Ra;       ////PU_zf=1为倒谱,0为正谱.    
                else SP_Ra = SP_Ra-SP_Rb;               
                SP_Ra = SP_Ra/CR_B;                   
                return SP_Ra;                               
              }


          
                

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
4
关闭 站长推荐上一条 /3 下一条