📄 pll.cpp
字号:
// pll.cpp : 僀儞僾儕儊儞僥乕僔儑儞 僼傽僀儖
//
#include "stdafx.h"
#include "demo.h"
#include "pll.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Cpll 僟僀傾儘僌
//-----
#include"MainFrm.h"
#include"nim.h"
//-----
Cpll::Cpll(CWnd* pParent /*=NULL*/)
: CDialog(Cpll::IDD, pParent)
{
m_pParent=pParent;
m_nID=Cpll::IDD;
//{{AFX_DATA_INIT(Cpll)
m_freq = _T("");
m_byte1 = _T("");
m_byte2 = _T("");
m_byte3 = _T("");
m_byte4 = _T("");
m_byte5 = _T("");
m_lpf = _T("");
//}}AFX_DATA_INIT
}
void Cpll::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(Cpll)
DDX_CBString(pDX, IDC_COMBO_freq, m_freq);
DDX_Text(pDX, IDC_EDIT_byte1, m_byte1);
DDX_Text(pDX, IDC_EDIT_byte2, m_byte2);
DDX_Text(pDX, IDC_EDIT_byte3, m_byte3);
DDX_Text(pDX, IDC_EDIT_byte4, m_byte4);
DDX_Text(pDX, IDC_EDIT_byte5, m_byte5);
DDX_CBString(pDX, IDC_COMBO_LPF, m_lpf);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(Cpll, CDialog)
//{{AFX_MSG_MAP(Cpll)
ON_BN_CLICKED(IDC_BUTTON_test, OnBUTTONtest)
ON_BN_CLICKED(IDC_CHECK_byte10, OnCHECKbyte10)
ON_BN_CLICKED(IDC_BUTTON_freq_set, OnBUTTONfreqset)
ON_CBN_KILLFOCUS(IDC_COMBO_freq, OnKillfocusCOMBOfreq)
ON_CBN_KILLFOCUS(IDC_COMBO_LPF, OnKillfocusComboLpf)
ON_EN_KILLFOCUS(IDC_EDIT_byte1, OnKillfocusEDITbyte1)
ON_EN_KILLFOCUS(IDC_EDIT_byte2, OnKillfocusEDITbyte2)
ON_EN_KILLFOCUS(IDC_EDIT_byte3, OnKillfocusEDITbyte3)
ON_EN_KILLFOCUS(IDC_EDIT_byte4, OnKillfocusEDITbyte4)
ON_EN_KILLFOCUS(IDC_EDIT_byte5, OnKillfocusEDITbyte5)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Cpll 儊僢僙乕僕 僴儞僪儔
void Cpll::OnOK()
{
//-----
// ((CMainFrame*)m_pParent)->BoxDone_pll();
((Cnim*)m_pParent)->BoxDone_pll();
//DestroyWindow();
//-----
CDialog::OnOK();
}
void Cpll::OnCancel()
{
// TODO: 偙偺埵抲偵摿暿側屻張棟傪捛壛偟偰偔偩偝偄丅
// CDialog::OnCancel();
}
void Cpll::OnBUTTONtest()
{
int data[10];
int num=2;
data[0]=0xAA;
data[1]=0xAA;
i2c_write(data,num);
}
BOOL Cpll::Create()
{
return CDialog::Create(m_nID,m_pParent);
}
void Cpll::i2c_write(int *data, int num)
{
int data_[10];
// unsigned char data_[10];
int i;
for(i=1;i<num;i++) data_[i-1]=data[i];
((Cnim*)m_pParent)->i2c_read_write(1,data[0],&data_[0],num-1);
// ((CMainFrame*)m_pParent)->m_i2c.i2c_write(data,num);
// CString t,tt;
// int i;
// for(i=0;i<num;i++){
// t.Format("%X\n",data[i]);
// tt+=t;
// }
//
// MessageBox(tt);
}
void Cpll::OnCHECKbyte10()
{
UpdateData(TRUE);
}
//___________________________________________________________________________
//_____QM1D1B0004____________________________________________________________
void Cpll::pll_setdata_QM1D1B0004(int *byte_)
{
int data[10];
//
// bus_open(); //bus open
//ix2410
//<Initialization>
long byte2,byte4,byte5;
byte2=*(byte_+1); //byte2
byte4=*(byte_+3); //byte4
byte5=*(byte_+4); //byte5
//
//<<Initialize>>
*(byte_+3)&=0xE3; //TM=0,LPF=4MHz
*(byte_+4)&=0xF3; //LPF=4MHz
//byte2 / BG=01(VCO wait:2ms)
*(byte_+1)&=0x9F;
*(byte_+1)|=0x20;
((Cnim*)m_pParent)->bus_open();
i2c_write(byte_,5);//write i2c data
//<<Adjustment>>
*(byte_+3)|=0x04; //TM=1
//V0.01[030402]
data[0]=*(byte_);
data[1]=*(byte_+3);
((Cnim*)m_pParent)->bus_open();
i2c_write(&data[0],2);//write i2c data
//<<Wait>>
// Sleep(6); //wait 6ms
Sleep(10); //wait 10ms
//set dummy wait <- PC time is unstable
Sleep(20);
//<<TM=1,LPF setting>>
//Data write
//[040121] set original value (not comment out)
*(byte_+3)=byte4; //byte_4 original value
//[040108] TM bit always finis with "1".
*(byte_+3)|=0x04; //TM=1
/********************************************************************
*(byte_+3)|=0x04; //TM=1
// *(byte_+3)=byte4; //byte_4 original value
********************************************************************/
*(byte_+4)=byte5; //byte_5 original value
//V0.01[030402]
data[0]=*(byte_);
data[1]=*(byte_+3);
data[2]=*(byte_+4);
((Cnim*)m_pParent)->bus_open();
i2c_write(&data[0],3); //write i2c data
//<<BG set>>
//byte2 / BG=original BG value <- 01(VCO wait:2ms)
*(byte_+1)=byte2;
((Cnim*)m_pParent)->bus_open();
i2c_write(byte_,2);//set original BG value
//((Cnim*)m_pParent)->bus_close();
// bus_close(); //bus open
}
double Cpll::calculate_pll_xtal_QM1D1B0004()
{
return 4000.;
}
void Cpll::calculate_pll_lpf_bw_from_baud_QM1D1B0004(long baud, int *byte_)
{
long LPF;
if(baud>0){ //calculate LPF automatically
LPF=calculate_LPF_from_baud(baud);
calculate_pll_lpf_to_byte(LPF,byte_);
//CString t; t.Format("%d",LPF); MessageBox(t);
}
else{
//CString t; t.Format("baud=%d",baud); MessageBox(t);
}
}
void Cpll::tun_setfreq_QM1D1B0004(long freq, long baud, int *byte_) //freq,baud[kHz]
{
// CString t,tt;
// int i;
// t.Format(" %ld %ld \n",freq,baud);
// tt+=t;
// for(i=0;i<5;i++) {
// t.Format(" %.2X \n",byte_[i]); tt+=t;
// }
// MessageBox(tt);
calculate_pll_vco_QM1D1B0004(freq,byte_); //[040108]丂PSC傪divder_byte寁嶼慜偵愝掕偡傞丅
calculate_pll_divider_byte_QM1D1B0004(freq,byte_); //[040108]丂PSC愝掕偵墳偠偰divder_byte寁嶼丅
calculate_pll_lpf_bw_from_baud_QM1D1B0004(baud,byte_);
pll_setdata_QM1D1B0004(byte_);
}
void Cpll::calculate_pll_vco_QM1D1B0004(long freq, int *byte_)
{
//QM1D1B0004 [031225]
int band;
band=calculate_local_frequency_band(freq);
calculate_band_to_byte(band, byte_);
}
void Cpll::calculate_pll_divider_byte_QM1D1B0004(long freq, int *byte_)
{
//[040108]丂PSC偵墳偠偰divder_byte寁嶼丅
//QM1D1B0004 [031225]
long data;
data=long( double(freq)/calculate_pll_step_QM1D1B0004(*(byte_+3)) +.5 );
int P,N,A;
P=calculate_dividing_factor_of_prescaler(byte_);
N=data/P;
A=data-P*N;
data=(N<<5)|A;
//040408
//BG should not be changed...
*(byte_+1)&=0x60; //byte2:BG set
*(byte_+1)|=int((data>>8)&0x1F);//byte2
*(byte_+2)=int(data&0xFF); //byte3
}
double Cpll::calculate_pll_step_QM1D1B0004(int byte4)
{
int REF;
int R;
double pll_step;
REF=byte4&0x01;
if (REF==0) R=4;
else R=8;
pll_step=calculate_pll_xtal_QM1D1B0004()/double(R);
return pll_step;
}
//_____QM1D1B0004____________________________________________________________
//___________________________________________________________________________
//
int Cpll::calculate_local_frequency_band(long freq)
{
int band;
// if(950000<=freq && freq<986000)
if(freq<986000) band=1;
if(986000<=freq && freq<1073000) band=2;
if(1073000<=freq && freq<1154000) band=3;
if(1154000<=freq && freq<1291000) band=4;
if(1291000<=freq && freq<1447000) band=5;
if(1447000<=freq && freq<1615000) band=6;
if(1615000<=freq && freq<1791000) band=7;
if(1791000<=freq && freq<1972000) band=8;
if(1972000<=freq) band=9;
return band;
}
void Cpll::calculate_band_to_byte(int band, int *byte_)
{
//byte5
int PSC;
if( (band==1)||(band==2)) PSC=1;
else PSC=0;
*(byte_+4)&=0xEF;
*(byte_+4)|=(PSC<<4);
//
int DIV,BA210;
if( band==1 ) DIV=1,BA210=0x05;
if( band==2 ) DIV=1,BA210=0x06;
if( band==3 ) DIV=1,BA210=0x07;
if( band==4 ) DIV=0,BA210=0x01;
if( band==5 ) DIV=0,BA210=0x02;
if( band==6 ) DIV=0,BA210=0x03;
if( band==7 ) DIV=0,BA210=0x04;
if( band==8 ) DIV=0,BA210=0x05;
if( band==9 ) DIV=0,BA210=0x06;
*(byte_+4)&=0x1D;
*(byte_+4)|=(DIV<<1);
*(byte_+4)|=(BA210<<5);
}
int Cpll::calculate_dividing_factor_of_prescaler(int *byte_)
{
int PSC;
PSC=(*(byte_+4)>>4);
PSC&=0x01;
if(PSC) return 16; //PSC=1
else return 32; //PSC=0
}
void Cpll::OnBUTTONfreqset()
{
//ix2410
get_dialog();
long freq,baud;
freq=long( ((Cnim*)m_pParent)->tuner.freq )/1000; //Hz -> kHz
// freq=long( ((Cnim*)m_pParent)->tuner.freq_ ); //kHz
baud=-1;//LPF BW:manual
//baud=long( ((Cnim*)m_pParent)->demod.symbolrate_ ); //kHz
// //baud=long( ((Cnim*)m_pParent)->demod.symbolrate )/1000; //Hz -> kHz
tun_setfreq(freq,baud,&((Cnim*)m_pParent)->tuner.byte[0]);
set_dialog();
/*
//QM1D1B0004
get_dialog();
long freq,baud;
freq=long( ((Cnim*)m_pParent)->tuner.freq_ ); //kHz
//baud=long( ((Cnim*)m_pParent)->demod.symbolrate_ ); //kHz
baud=-1;//LPF BW:manual
//baud=long( ((Cnim*)m_pParent)->demod.symbolrate )/1000; //Hz -> kHz
tun_setfreq_QM1D1B0004(freq,baud,&((Cnim*)m_pParent)->tuner.byte[0]);
set_dialog();
*/
}
void Cpll::init_combo()
{
char text[100];
CComboBox* pCB_freq=(CComboBox*)GetDlgItem(IDC_COMBO_freq);
while( (pCB_freq->DeleteString(0))>0 );
CComboBox* pCB_lpf=(CComboBox*)GetDlgItem(IDC_COMBO_LPF);
while( (pCB_lpf->DeleteString(0))>0 );
int i;
for(i=950 ; i<=2150; i+=100){
sprintf( text,"%.3lf",double(i) );
pCB_freq->InsertString(0,text);
}
// for(i=0x01;i<=0x0F;i++){ //6-34M
for(i=0x03 ; i<=0x0F ; i++){ //10M-34M
sprintf( text,"%.1lf",double(calculate_pll_lpf(i)/1000) );
pCB_lpf->InsertString(0,text);
}
// CString t;
// double lpf;
// for(i=0x03;i<=0x0D;i++)
// {
// lpf=double((i+2)*2);
// t.Format("%.2lf",lpf); pCB_lpf->InsertString(0,t);
// }
}
long Cpll::calculate_pll_lpf(int PD2345)
{
//ix2410
long LPF;//kHz
LPF=((PD2345+2)*2)*1000;
return LPF;
// //QM1D1B0004
// long LPF;//kHz
// LPF=10000+2000*(PD2345-3);
// return LPF;
}
BOOL Cpll::OnInitDialog()
{
CDialog::OnInitDialog();
init_combo();
set_dialog();
return TRUE; // 僐儞僩儘乕儖偵僼僅乕僇僗傪愝掕偟側偄偲偒丄栠傝抣偼 TRUE 偲側傝傑偡
// 椺奜: OCX 僾儘僷僥傿 儁乕僕偺栠傝抣偼 FALSE 偲側傝傑偡
}
void Cpll::set_dialog()
{
CString t;
//freq
t.Format(" %.3lf",double(((Cnim*)m_pParent)->tuner.freq)/1000000.); //Hz -> MHz
// t.Format(" %.3lf",((Cnim*)m_pParent)->tuner.freq_/1000. ); //Hz -> MHz
//byte
m_freq=t;
t.Format(" %.2X",((Cnim*)m_pParent)->tuner.byte[0]);
m_byte1=t;
t.Format(" %.2X",((Cnim*)m_pParent)->tuner.byte[1]);
m_byte2=t;
t.Format(" %.2X",((Cnim*)m_pParent)->tuner.byte[2]);
m_byte3=t;
t.Format(" %.2X",((Cnim*)m_pParent)->tuner.byte[3]);
m_byte4=t;
t.Format(" %.2X",((Cnim*)m_pParent)->tuner.byte[4]);
m_byte5=t;
//LPF setting
t.Format(" %.1lf",double(calculate_pll_lpf(&((Cnim*)m_pParent)->tuner.byte[0]))/1000.);
m_lpf=t;
UpdateData(FALSE);
}
void Cpll::get_dialog()
{
double data_double;
UpdateData(TRUE);
sscanf(m_freq, "%lf", &data_double );
((Cnim*)m_pParent)->tuner.freq=long(data_double*1000000.);
// ((Cnim*)m_pParent)->tuner.freq_=long(data_double*1000.);
}
long Cpll::calculate_pll_lpf(int *byte)
{
//ix2410
int data,pd2,pd3,pd4,pd5;
pd2=((*(byte+4))>>2)&0x01;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -