单片机用的是DSPIC3013f,DAC是MCP4821,MCP4821的SDI接单片机的SDO,SCK接SCK,CS接RB2,LDAC接地,程序如下:
#include "p30f3013.h"
_FOSC(CSW_FSCM_OFF&FRC_PLL4);
_FWDT(WDT_OFF);
_FBORPOR(PBOR_OFF&MCLR_EN);
_FGS(CODE_PROT_OFF);
unsigned int table[30]={
0x3fff,0x3fd2,0x3f4e,0x3e78,0x3d5a,0x3bff,0x3a78,0x38d6,0x3857,0x3729,0x3400,0x32a5,0x3187,
0x30b1,0x302d,0x3000,0x302d,0x30b1,0x3187,0x32a5,0x3400,0x3729,0x3857,0x38d6,0x3a78,0x3bff,
0x3d5a,0x3e78,0x3f4e,0x3fd2
};
int i;
void InitializeSPIOut()
{
TRISF=TRISF&0xff37;
TRISB=TRISB&0xfffb;
SPI1CON=0x0439;
SPI1CONbits.MODE16 = 1;
SPI1CONbits.SSEN = 0;
SPI1CONbits.MSTEN = 1;
SPI1STATbits.SPIROV = 0;
SPI1STATbits.SPIEN=1;
SPI1CONbits.DISSDO = 0;
IFS0bits.SPI1IF=0;
}
void dly(unsigned int cnt)
{
int k;
for(k=0;k<cnt;k++);
}
void InitializeSay()
{
SRbits.IPL=7;
}
void mcp4821()
{
for(i=0;i<30;i++)
{
PORTBbits.RB2=0;
SPI1BUF=table;
while(SPI1STATbits.SPITBF==1);
dly(20);
PORTBbits.RB2=1;
dly(20);
}
}
int main(void)
{
InitializeSay();
InitializeSPIOut();
while(1)
{
mcp4821();
}
}
发现MCP4821端口输出非期待的值,请问这个CS该如何设置,其他地方还有什么问题 |