单片机采用的是MSP430F169,采用一般IO口模拟SPI通讯。
程序的相关IO脚需设置成输入输出的工作方式,这一点anxingedu没有贴出来,因为采用单片机不同而异。
希望能够对大家有帮助,能够节省时间。
-----------------------------------------------------------------------------------------------
//定义通讯命令
#define SPI_WREN 0x06
#define SPI_WRDI 0x04
#define SPI_RDSR 0x05
#define SPI_WRSR 0x01
#define SPI_READ 0x03
#define SPI_WRITE 0x02
//定义通讯管脚
#define Set_Fcs P4OUT=P4OUT|0x40
#define Clr_Fcs P4OUT=P4OUT&0xbf
#define Set_Fck P5OUT=P5OUT|0x01
#define Clr_Fck P5OUT=P5OUT&0xfe
#define Set_MOFI P5OUT=P5OUT|0x02
#define Clr_MOFI P5OUT=P5OUT&0xfd
/***************************************************
====================FM25L04程序开始====================
***************************************************/
//FCS-P46 MIFO-P47 MOFI-P50 FCK-P51
void write_byte(uchar tem)
{
uchar i,j,k;
k="0x80";
for(j=0;j<8;j++)
{
i=tem&k;
if(i==0)
{
Clr_MOFI;
}
else
{
Set_MOFI;
}
k="k">>1;
Set_Fck;
Clr_Fck;
}
}
uchar read_byte(void )
{
uchar j,k,tem;
tem="0";
k=0x80;
for(j=0;j<8;j++)
{
Set_Fck;
if((P4IN&0x80)==0x80)
{
tem="tem|k";
}
k="k">>1;
Clr_Fck;
}
return(tem);
}
//Status Register Read:
//Format: RDSR op-code, Data-out
uchar ReadFlSr(void )
{
uchar i;
Clr_Fcs;
write_byte(SPI_RDSR);
i="read"_byte();
Set_Fcs;
return(i);
}
//Status Register Write:
//Format: WREN op-code, WRSR op-code, Data-in
void WriteFlSr(uchar Data )
{
Clr_Fcs;
write_byte(SPI_WRSR);
write_byte(Data);
Set_Fcs;
}
//Memory Writes:
//Format: WREN op-code, WRITE op-code, MSB Address, LSB Address, Data-in, (Data-in, Data-in, ...)
void WriteMemory(uchar Addrh,uchar Addrl,uchar Data )
{
Clr_Fcs;
write_byte(SPI_WREN);
Set_Fcs;
Clr_Fcs;
write_byte(SPI_WRITE|Addrh);
write_byte(Addrl);//Addr
write_byte(Data);//Data
Set_Fcs;
}
//Memory Reads
//Format: READ op-code, MSB Address, LSB Address, Data-out, (Data-out, Data-out, ...)
uchar ReadMemory(uchar Addrh,uchar Addrl )
{
uchar i;
Clr_Fcs;
write_byte(SPI_READ|Addrh);
write_byte(Addrl);//Addr
i="read"_byte();
Set_Fcs;
return(i);
}
/***************************************************
====================FM25L04程序结束====================
***************************************************/
void main( void )
{
WDTCTL = WDTPW + WDTHOLD;// Stop watchdog timer
//init_ram();
//init_usart();
_BIS_SR(GIE);
while(1)
{
WriteMemory(0x00,0x00,0XFF);
uidatab[0]=ReadMemory(0x00,0x00);
}
}
-----------------------------------------------------------------------------------------------
文章评论(0条评论)
登录后参与讨论