dsp写X25043--DSP的SPI对EEPROM接口
/*串行EEPROMMAX25045写使能命令*/
void WREN_X5045(void) /*写使能*/
{
SPIBUF=RSDR_X5045();
while ((SPIBUF & 0x01) != 0){SPIBUF=RSDR_X5045();}/*等待内部写结束*/
PFDATDIR=PFDATDIR | 0x08; /*取消写保护*/
TIME(1);
PFDATDIR=PFDATDIR & 0xFFFB; /*选择X5045*/
SPICCR = 0x07; /*复位上升沿,8位字符长度*/
SPICTL = 0x0e; /*有延时,主方式,禁止SPI中断*/
SPIBRR = 0x13; /*波特率为1.5MHz*/
SPIPRI = 0x40; /*低中断优先级*/
SPICCR = 0x87; /*进入工作状态*/
SPITXBUF = 0X0600; /*发送写使能命令*/
while (SPISTS == 0){}
SPIBUF=SPIRXBUF;
SPISTS=0xFF; /*清除标志位*/
PFDATDIR=PFDATDIR | 0x0004; /*清除选择X5045*/
TIME(1); /*延时1US*/
}
void WRSR_X5045(unsigned int COM) /*写状态*/
{
WREN_X5045(); /*写使能*/
PFDATDIR=PFDATDIR & 0xFFFB; /*选择X5045*/
SPICCR = 0x0F; /*复位上升沿,15位字符长度*/
SPICTL = 0x0e; /*有延时,主方式,禁止SPI中断*/
SPIBRR = 0x13; /*波特率为1.5MHz0d*/
SPIPRI = 0x40; /*低中断优先级*/
SPICCR = 0x8f; /*进入工作状态*/
SPITXBUF = COM | 0x0100; /*发送写状态命令*/
while (SPISTS == 0){}
SPIBUF=SPIRXBUF;
SPISTS=0xFF; /*清除标志位*/
PFDATDIR=PFDATDIR | 0x0004; /*清除选择X5045*/
PFDATDIR=PFDATDIR & 0xFFF7; /*写保护*/
}
/*写EEPROM*//*RA_ADDR为RAM数据首地址,EEP_ADDR为EEP地址,N<4个数8位,H为8选择高地址*/
void WRITE_X5045(unsigned int * RA_ADDR,unsigned int EEP_ADDR,unsigned int N,unsigned int H)
{
unsigned int I;
SPIBUF=RSDR_X5045();
while ((SPIBUF & 0x01) != 0){SPIBUF=RSDR_X5045();}/*等待内部写结束*/
WREN_X5045(); /*写使能*/
PFDATDIR=PFDATDIR & 0xFFFB; /*选择X5045*/
SPICCR = 0x07; /*复位上升沿,8位字符长度*/
SPICTL = 0x0e; /*有延时,主方式,禁止SPI中断*/
SPIBRR = 0x13; /*波特率为1.5MHz*/
SPIPRI = 0x40; /*低中断优先级*/
SPICCR = 0x87; /*进入工作状态*/
SPIBUF=((H | 0x02) << 8);
SPITXBUF = SPIBUF; /*发送写命令*/
while (SPISTS == 0){}
SPIBUF=SPIRXBUF;
SPISTS=0xFF; /*清除标志位*/
SPITXBUF = (EEP_ADDR << 8); /*发送地址*/
while (SPISTS == 0){}
SPIBUF=SPIRXBUF;
SPISTS=0xFF; /*清除标志位*/
for (I=0;I<N;I++)
{
SPITXBUF =( * (RA_ADDR+I) << 8); /*发送数据*/
while (SPISTS == 0){}
SPIBUF=SPIRXBUF;
SPISTS=0xFF; /*清除标志位*/
}
PFDATDIR=PFDATDIR | 0x0004; /*清除选择X5045*/
PFDATDIR=PFDATDIR & 0xFFF7; /*写保护*/
}
/*读EEPROM*//*RA_ADDR为RAM数据首地址,EEP_ADDR为EEP地址,N为字符个数8位,H为8选择高地址*/
void READ_X5045(unsigned int * RA_ADDR,unsigned int EEP_ADDR,unsigned int N,unsigned int H)
{
unsigned int I;
SPIBUF=RSDR_X5045();
while ((SPIBUF & 0x01) != 0){SPIBUF=RSDR_X5045();}/*等待内部写结束*/
PFDATDIR=PFDATDIR & 0xFFFB; /*选择X5045*/
SPICCR = 0x07; /*复位上升沿,8位字符长度*/
SPICTL = 0x0e; /*有延时,主方式,禁止SPI中断*/
SPIBRR = 0x13; /*波特率为1.0MHz*/
SPIPRI = 0x40; /*低中断优先级*/
SPICCR = 0x87; /*进入工作状态*/
SPITXBUF = (H | 0x03) << 8; /*发送读命令*/
while (SPISTS == 0){}
SPIBUF=SPIRXBUF;
SPISTS=0xFF; /*清除标志位*/
SPITXBUF = EEP_ADDR << 8; /*发送地址*/
while (SPISTS == 0){}
SPIBUF=SPIRXBUF;
SPISTS=0xFF; /*清除标志位*/
for (I=0;I<N;I++)
{
SPITXBUF = 0; /*发送伪数据*/
while (SPISTS == 0){}
*(RA_ADDR+I) = SPIRXBUF & 0xff;
SPISTS=0xFF; /*清除标志位*/
}
PFDATDIR=PFDATDIR | 0x0004; /*清除选择X5045*/
}
/*读状态寄存器*/
unsigned int RSDR_X5045(void)
{
TIME(1);
PFDATDIR=PFDATDIR & 0xFFFB; /*选择X5045*/
SPICCR = 0x07; /*复位上升沿,8位字符长度*/
SPICTL = 0x0e; /*有延时,主方式,禁止SPI中断*/
SPIBRR = 0x13; /*波特率为1.0MHz*/
SPIPRI = 0x40; /*低中断优先级*/
SPICCR = 0x87; /*进入工作状态*/
SPITXBUF =0x0500; /*发送读命令*/
while (SPISTS == 0){}
SPIBUF=SPIRXBUF;
SPISTS=0xFF; /*清除标志位*/
SPITXBUF = 0; /*发送伪数据*/
while (SPISTS == 0){}
SPIBUF=SPIRXBUF;
SPISTS=0xFF; /*清除标志位*/
PFDATDIR=PFDATDIR | 0x0004; /*清除选择X5045*/
return SPIBUF;
}
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。