来自网友的提问:
小的是写MIcrochip的dsPIC33FJ64GS606,并打算以SPI读取AD7606-4的转换值
但读取到的值一直为0
也弄不清是接线问题还是程式问题
程式的话是照ADI网站上的程式码範例改写的
电路则是看datasheet上的serial mode条件所接的
线路图和程式码範例已上传至附件
下面则是我所写的程式码
#include "p33FJ64GS606.h" #include
#include
#include
_FOSCSEL(FNOSC_FRC) // Internal FRC with non PLL
_FOSC(FCKSM_CSECMD OSCIOFNC_ON) // Sw Enabled, Mon Disabled and OSCO pin has digital I/O func
tion
_FWDT(FWDTEN_OFF) // Watchdog Timer disabled
_FPOR(FPWRT_PWR128) // POR Timer Value 128mSec
_FGS(GWRP_OFF) // General Segment may be written
_FICD(ICS_PGD1 JTAGEN_OFF); // Use PGC1/EMUC1 and PGD1/EMUD1
unsigned char j, k;
unsigned int TempA, TempB, DoutA[3], DoutB[3];
unsigned char Busy;
int main(){
SPI2CON1 = 0x073B;
SPI2STAT = 0xA000;
// I/O Port設定,TRIS為設定資料流向(1:輸入 , 0:輸出)。Port為設定高、低電位(1:logic high , 0:logic low)
TRISGbits.TRISG9=0;
PORTGbits.RG9=1;//CS
TRISBbits.TRISB2=0;
PORTBbits.RB2=1;//CNVST
TRISBbits.TRISB3=0;
PORTBbits.RB3=0;//RESET
TRISGbits.TRISG6=0;
PORTGbits.RG6=1;//sck
TRISBbits.TRISB4=1;
PORTBbits.RB4=0;//doutA
TRISBbits.TRISB5=1;
PORTBbits.RB5=0;//doutB
TRISBbits.TRISB1=1;
PORTBbits.RB1=0;//busy
TRISBbits.TRISB8=0;
PORTBbits.RB8=1;//PAR/SER/BYTE SEL
TRISBbits.TRISB9=0;
PORTBbits.RB9=1;//STBY
TRISBbits.TRISB10=0;
PORTBbits.RB10=0;//RANGE
TRISBbits.TRISB11=0;
PORTBbits.RB11=1;//REF SELECT
while(1)
{
PORTBbits.RB2=0;
Delay_1mS_Cnt;
PORTBbits.RB2=1;
Delay_1mS_Cnt;
Busy=PORTBbits.RB1;
while(Busy==1){
Delay_1mS_Cnt;
Busy=PORTBbits.RB1;
}
PORTGbits.RG9=0;
for(j=0; j<4; j++)
{
TempA=0;
TempB=0;
for(k=0; k<16; k++){
PORTGbits.RG6=0;
TempA=(TempA<<1) + LATBbits.LATB4;
TempB=(TempB<<1) + LATBbits.LATB5;
LATGbits.LATG6=1;
}
DoutA[j]=TempA;
DoutB[j]=TempB;
}
PORTGbits.RG9=1;
}
}