本帖最后由 xyz549040622 于 2013-6-14 22:54 编辑
DA所用为AD5662,官方给的一段程序为
/*******************************************************************
* Function: AD5662_reg_write
* Description: writes to the AD5662 via the SPI port.
P1.4->SCLK,P1.6->DIN,P2.5->/SYNC
*******************************************************************/
void AD5662_reg_write(unsigned int Mode, unsigned int RegisterData)
{
unsigned int ControlValue = 0;
unsigned int i = 0;
//Create the 24-bit
ControlValue = (Mode << 16) | RegisterData;
SET_CS();
CLR_SCL();
delay(3);
CLR_CS(); //bring CS low
delay(3);
//Write out the control word
for(i=0; i<24; i++)
{
SET_SCL();
if(0x800000 == (ControlValue & 0x800000))
{
SET_SDO(); //Send one to SDO pin
}
else
{
CLR_SDO(); //Send zero to SDO pin
}
delay(3);
CLR_SCL();
delay(3);
ControlValue <<= 1; //Rotate data
}
SET_CS(); //bring CS high again
}
移植到PIC16F1936后的代码为
//************************DA写数据程序***********************************************
/*******************************************************************
* Function: AD5662_reg_write
* Description: 输入参数为选通的通道号码,进行的模式,写入的数据
*******************************************************************/
void AD5662_reg_write(unsigned int En_DA_Num, unsigned long Mode, unsigned long RegisterData)
{
unsigned long ControlValue = 0;
unsigned int i = 0;
ControlValue = (Mode << 16)| RegisterData;
SET_CS();//片选位拉高
CLR_SCL();//时钟拉高
delayR();//延时
delayR();//延时
delayR();//延时
switch(En_DA_Num)//根据使能的口,拉低片选位,使数据有效
{
case 1: CLR_CS1(); break;
case 2: CLR_CS2(); break;
case 3: CLR_CS3(); break;
case 4: CLR_CS4(); break;
case 5: CLR_CS5(); break;
case 6: CLR_CS6(); break;
case 7: CLR_CS7(); break;
default: SET_CS(); break;//如果都不是,Y1-Y7都关闭,此时无法写入数据
}
delayR();//延时
//Write out the control word
for(i=0; i<24; i++)
{
SET_SCL();//时钟拉高
if(0x800000 == (ControlValue & 0x800000))//发送数据
{
SET_SDO(); //Send one to SDO pin
}
else
{
CLR_SDO(); //Send zero to SDO pin
}
delayR();//数据保持
CLR_SCL();//时钟拉低
delayR();//数据保持
ControlValue <<= 1; //Rotate data
delayR();//数据保持
}
SET_CS();//把片选位拉高
delayR();//数据保持
}
void delayR()//延时1us,16MHZ
{
NOP();NOP();NOP();NOP();
}
只修改延时函数,但是DA结果无输出,官方的程序应该也是没错的,那么移植过程哪里出错了呢?
|