我用DSPIC30F6011A读写AT93C46,写了个0X55,读到的数据老是0.
程序如下:
#include "p30f6011a.h"
_FOSC(XT_PLL4);
_FWDT(WDT_OFF);
//初始化系统程序
void InitializeSys();
//SPI输出初始化
void InitializeSPIOut();
//系统初始化子程序,放在程序首部
void InitializeSys()
{
SRbits.IPL = 7; //关闭所有可屏蔽中断
}
void InitializeSPIOut()
{
TRISB&=0XFEFF; //RB8片选
//TRISF = TRISF&0xfebf; //
TRISG = 0xfcbf; //RG9输出,RG6、RG8输出
//SPI2CON = 0x0379; // 字节通讯模式,CKP=1,CKE=1,FOSC/32,时钟上升沿发送数据
SPI2CON = 0x0279; //字节通讯模式,CKP=1,CKE=0,FOSC/32,时钟下降沿发送数据
SPI2STATbits.SPIEN = 1; //SPI1EN=1
IFS1bits.SPI2IF = 0; // 清除SPI1IF标志
PORTB=0X0100; //使能
}
int main()
{ unsigned long int i;
unsigned char temp;
InitializeSys();
InitializeSPIOut();
SPI2BUF = 0x02;
while(SPI2STATbits.SPITBF == 1);
SPI2BUF = 0x60; //写使能
while(SPI2STATbits.SPITBF == 1);
SPI2BUF = 0x02;
while(SPI2STATbits.SPITBF == 1);
SPI2BUF = 0x80; //写地址0
while(SPI2STATbits.SPITBF == 1);
SPI2BUF = 0x55; //写数据
while(SPI2STATbits.SPITBF == 1);
for(i=0;i<600000;i++);
SPI2BUF = 0x03;
while(SPI2STATbits.SPITBF == 1);
SPI2BUF = 0x00; //读地址
while(SPI2STATbits.SPITBF == 1);
SPI2BUF = 0x00; //虚写
while(SPI2STATbits.SPITBF == 1);
//while(SPI2STATbits.SPIRBF == 0);
temp=SPI2BUF;
while(1)
{
}
}
请高手指教,谢谢!
|
我用DSPIC30F6011A读写AT93C46,写了个0X55,读到的数据老是0.
程序如下:
#include "p30f6011a.h"
_FOSC(XT_PLL4);
_FWDT(WDT_OFF);
//初始化系统程序
void InitializeSys();
//SPI输出初始化
void InitializeSPIOut();
//系统初始化子程序,放在程序首部
void InitializeSys()
{
SRbits.IPL = 7; //关闭所有可屏蔽中断
}
void InitializeSPIOut()
{
TRISB&=0XFEFF; //RB8片选
//TRISF = TRISF&0xfebf; //
TRISG = 0xfcbf; //RG9输出,RG6、RG8输出
//SPI2CON = 0x0379; // 字节通讯模式,CKP=1,CKE=1,FOSC/32,时钟上升沿发送数据
SPI2CON = 0x0279; //字节通讯模式,CKP=1,CKE=0,FOSC/32,时钟下降沿发送数据
SPI2STATbits.SPIEN = 1; //SPI1EN=1
IFS1bits.SPI2IF = 0; // 清除SPI1IF标志
PORTB=0X0100; //使能
}
int main()
{ unsigned long int i;
unsigned char temp;
InitializeSys();
InitializeSPIOut();
SPI2BUF = 0x02;
while(SPI2STATbits.SPITBF == 1);
SPI2BUF = 0x60; //写使能
while(SPI2STATbits.SPITBF == 1);
SPI2BUF = 0x02;
while(SPI2STATbits.SPITBF == 1);
SPI2BUF = 0x80; //写地址0
while(SPI2STATbits.SPITBF == 1);
SPI2BUF = 0x55; //写数据
while(SPI2STATbits.SPITBF == 1);
for(i=0;i<600000;i++);
SPI2BUF = 0x03;
while(SPI2STATbits.SPITBF == 1);
SPI2BUF = 0x00; //读地址
while(SPI2STATbits.SPITBF == 1);
SPI2BUF = 0x00; //虚写
while(SPI2STATbits.SPITBF == 1);
//while(SPI2STATbits.SPIRBF == 0);
temp=SPI2BUF;
while(1)
{
}
}
请高手指教,谢谢!
|
|