智能车制作
标题:
SPI通讯实验,为什么SPI0DR寄存器中的值都是255?
[打印本页]
作者:
leodream
时间:
2011-8-19 17:05
标题:
SPI通讯实验,为什么SPI0DR寄存器中的值都是255?
我用的是DJ128芯片,程序如下:
#include <hidef.h> /* common defines and macros */
#include <mc9s12dg128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
//-----------------------------------------------------
//延时1毫秒
void Dly_ms(int ms)
{
int ii,jj;
if (ms<1) ms=1;
for(ii=0;ii<ms;ii++)
for(jj=0;jj<2670;jj++); //busclk:16MHz--1ms
}
//-----------------------------------------------------
//SPI写一个字节
void SPI_Write_Byte(unsigned char val)
{
SPI0DR = val;
while(!(SPI0SR_SPIF));
return;
}
//-----------------------------------------------------
//SPI读一个字节
unsigned char SPI_Read_Byte(void)
{
while(!(SPI0SR_SPIF));
return SPI0DR;
}
//-----------------------------------------------------
//SPI初始化,速率400kbps
void SPI_Init(void)
{
DDRT = 0xff;
MODRR = 0x00;
SPI0CR2 = 0x10;
SPI0CR1 = 0x5e;
SPI0BR = 0x43; //BR=busclk/((SPPR + 1)· 2^(SPR + 1))=16000/(5*8)=400k
}
//-----------------------------------------------------
//总线频率为16M
void setbusclock(void)
{
CLKSEL=0X00; //disengage PLL to system
PLLCTL_PLLON=1; //turn on PLL
SYNR=1;
REFDV=1; //pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz;
_asm(nop); //If PLL is selected (PLLSEL=1), Bus Clock = PLLCLK / 2=16M
while(!(CRGFLG_LOCK==1))
_asm(nop); //when pll is steady ,then use it;
CLKSEL_PLLSEL =1; //engage PLL to system;
}
//======================================================
void main(void) {
unsigned char spich=0;
EnableInterrupts;
setbusclock();
SPI_Init();
for(;;)
{
Dly_ms(200);
SPI_Write_Byte(0xf1);
spich=SPI_Read_Byte();
}
}
不管怎么改SPI_Write_Byte(0xf1)这条命令
,运行后发现spich的读数一直是255,这是怎么回事阿?
作者:
leodream
时间:
2011-8-20 17:48
这个没人知道吗?应该是很常用的一个例程啊。
作者:
ymucw
时间:
2011-8-21 19:27
MODRR = 0x10;
欢迎光临 智能车制作 (http://111.231.132.190/)
Powered by Discuz! X3.2