本人现在在用MSP430F149单片机,控制PS021。通过SPI口对PS021进行配置的时候发现,单步调试配置正确,PS021工作正常,要是直接运行就不工作,求大神指教,已经困扰我两个星期了!!!下面是我的程序。
void ClkInit(void)
{
char i;
BCSCTL1 &= ~XT2OFF; //打开XT2振荡器
IFG1&=~OFIFG; //清除振荡错误标志
while((IFG1&OFIFG)!=0)
{
for(i=0;i<0xff;i++);
IFG1&=~OFIFG; //清除振荡错误标志
}
BCSCTL2 |= SELM_2+SELS+DIVS_0; //MCLK为8MHz,SMCLK为8MHz
}
void SPI_INIT(void)
{
long int brclk;
brclk=8000000; //
P5SEL |= 0x0E; // P5.1,2,3 SPI option select
P4SEL &=~0x03;
P4DIR |=0x03;
U1CTL |=SWRST;
U1CTL = CHAR + SYNC + MM ; // 8-bit, SPI, Master
U1TCTL &=~ (SSEL0+SSEL1);
U1TCTL &=~ CKPL;
U1TCTL &= ~CKPH;
U1TCTL = SSEL1 + STC ; // Polarity, SMCLK=8000000, 3-wire
int fen=brclk/115200;
U1BR0 = 0x45; //波特率设置
U1BR1 = 0x00;
U1MCTL = 0x00;
ME2 |= USPIE1; // Module enable
U1CTL &= ~SWRST; // SPI enable
IE2 |= URXIE1 + UTXIE1; // RX and TX interrupt enable
}
void SPI_Write_PS021_Register(uchar *p)
{
int i=0;
P4OUT|=BIT0;
P4OUT&=~BIT0;
asm("NOP");
for( i=0;i<4;i++)
{
//SpiWriteDat(*p);
TXBUF1=*p;
p++;
while (!(IFG2 & UTXIFG1));
IFG2 &= ~UTXIFG1;
}
P4OUT|=BIT0;
}
void SPI_Write_PS021_Opcode(uchar Opcode)
{
P4OUT|=BIT0;
P4OUT&=~BIT0;
asm("NOP");
asm("NOP");
//SpiWriteDat(Opcode);
TXBUF1=Opcode;
while ((IFG2 & UTXIFG1)==0);
IFG2 &= ~UTXIFG1;
P4OUT|=BIT0;
}
void initPS021(void)
{
//SPI_Write_SCA103T_Opcode(0x00);
SPI_Write_PS021_Opcode(0x50); //上电复位
SPI_Write_PS021_Register(PS021_Register0);
SPI_Write_PS021_Register(PS021_Register1);
SPI_Write_PS021_Register(PS021_Register2);
SPI_Write_PS021_Register(PS021_Register3);
SPI_Write_PS021_Register(PS021_Register4);
SPI_Write_PS021_Register(PS021_Register5);
SPI_Write_PS021_Register(PS021_Register6);
SPI_Write_PS021_Register(PS021_Register7);
SPI_Write_PS021_Register(PS021_Register8);
SPI_Write_PS021_Register(PS021_Register9);
SPI_Write_PS021_Register(PS021_Register10);
SPI_Write_PS021_Register(PS021_Register11);
SPI_Write_PS021_Register(PS021_Register12);
SPI_Write_PS021_Register(PS021_Register13);
SPI_Write_PS021_Register(PS021_Register14);
SPI_Write_PS021_Register(PS021_Register15);
SPI_Write_PS021_Opcode(0xC0); //初始化系统,不改变寄存器的值
for(int i=0;i<9;i++)
{
SPI_Write_PS021_Opcode(0x01); //发送九次测量命令,消除零漂
Delay(20000);
}
} |