智能车制作
标题:
关于SPI口传输问题的请教!
[打印本页]
作者:
hzy198911
时间:
2010-4-10 22:09
标题:
关于SPI口传输问题的请教!
我的程序如下:
#include <hidef.h> /* common defines and macros */
#include <mc9s12xdp512.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xdp512"
#define uchar unsigned char
#define uint unsigned int
uchar putchar_SPI0(uchar cx)
{
uchar te;
PTM_PTM3=0;
te=SPI0SR;
while(!(SPI0SR_SPTEF));//检查寄存器是否为空
SPI0DR=cx;
while(!(SPI1SR_SPIF));//确认数据传输完毕
//clear the SPIF flag
PTM_PTM3=1;
}
uchar getchar_SPI0()
{
PTM_PTM3=0;
//uchar te;
//while(!(SPI0SR_SPTEF));
while(!(SPI0SR_SPIF));
//te=SPI0DR;
return(SPI0DR);
PTM_PTM3=1;
}
void MCU_int()
{
CLKSEL=0x00;
PLLCTL_PLLON=1;
SYNR=2;
REFDV=1;
_asm(nop);
_asm(nop);
while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it;
CLKSEL_PLLSEL =1; //engage PLL to system;
PTJ_PTJ0 = 0;
DDRJ_DDRJ0 = 0;
PPSJ_PPSJ0 = 1; //PTJ上升沿中断
PIEJ_PIEJ0 = 1; //中断使能
PIFJ_PIFJ0 = 0;
DDRK=0xff;
PORTK=0xff;
}
void SpiInit()
{
DDRM|=0x38; //SCK0=1,MOSI=1,SS0=1
MODRR_MODRR4=1; //使用PM口
PTM_PTM3=1;
SPI0CR1=0x52;
SPI0CR2=0x10;
SPI0BR=0x04;
}
void main(void) {
uchar m;
/* put your own code here */
EnableInterrupts;
MCU_int();
SpiInit();
for(;;)
{
putchar_SPI0(0X05);
getchar_SPI0;
m=SPI1DR;
PORTK=0xee;
} /* wait forever */
/* please make sure that you never leave this function */
}
为什么总在while(!(SPI1SR_SPIF));处出现死循环,而且数据无法写入SPI0DR;SPI0DR总为0;急盼各位高手的解答
换了几个SPI口结果都一样。。。
作者:
llyangxue
时间:
2011-4-18 21:21
回复
1#
hzy198911
哥们 真想抱你一下 我也是遇到这个问题了 调了一个多星期了 一直在那里死循环 不过很遗憾啊 现在还没找到毛病在哪里呢
作者:
llyangxue
时间:
2011-4-18 21:55
我的QQ号453297565 咱俩可以商量商量
作者:
lchx320
时间:
2011-4-26 08:49
哥们不知道你们怎么解决的 我也是这个问题啊 从SPI0DR里面取出来的数都是255 而且死死循环,
作者:
lchx320
时间:
2011-4-26 08:55
te=SPI0DR;这一句我是这么写的SPI0DR=te;虽然不出现死死循环,但读出来的数都是255 不明白什么原因哎
作者:
zyx
时间:
2011-5-11 10:11
先启动从机,再运行主机
作者:
xixilv
时间:
2012-4-19 08:07
来一个已经验证过的发送和接受的程序,和大家分享。。。
欢迎光临 智能车制作 (http://www.znczz.com/)
Powered by Discuz! X3.2