有哪位用FM25H20铁电啊? 我现在用IO模拟SPI接口从FM25H20读出来数据全部为0XFF。急啊!!! 下面为程序,哪位帮我看看,问题出再哪儿?
麻烦指导一下!! 谢谢了
#include "common.h"
void Fram_delay(int i)
{
while(i--)
{
asm("NOP"); //asm("NOP");
}
}
static void Write_Byte(unsigned char SByte)
{
unsigned char i,btemp;
btemp = SByte;
for(i=0; i<8; i++)
{
SPI2_SCK_L;
Fram_delay(100);
if(btemp & 0x80)
SPI2_MOSI_H;
else
SPI2_MOSI_L;
btemp<<=1;
Fram_delay(100);
SPI2_SCK_H;
Fram_delay(100);
}
}
static unsigned char Read_Byte(void)
{
unsigned char i,btemp;
btemp = 0x00;
for(i=0; i<8; i++)
{
btemp<<=1;
SPI2_SCK_L;
Fram_delay(2);
SPI2_SCK_H;
Fram_delay(100);
btemp |= SPI2_MISO;
}
return btemp;
}
/*
功能:
- 从FM25H20批量读取数据
参数:
- address:FM25H20的地址,pdata:读出数据的存储指针,length:读出数据的长度
*/
void FM25_read(unsigned long address, unsigned char* pdata, unsigned long length)
{
unsigned char* padr;
unsigned long temp_adr,temp_len,i;
temp_adr = address;
temp_len = length;
if(temp_adr >= FM_SIZE)
temp_adr = temp_adr%FM_SIZE;
padr=(unsigned char*)&temp_adr;
FM_CS1_L; //片选使能
Fram_delay(100);
Write_Byte(CMD_READ); //读指令
Write_Byte(padr[2]); //读地址高两位
Write_Byte(padr[1]); //读地址中间字节
Write_Byte(padr[0]); //读地址低字节
/*读出数据*/
for(i=0;i<temp_len;i++)
pdata[i]=Read_Byte();
Fram_delay(100);
FM_CS1_H; //片选禁止
Fram_delay(100);
}
/*
功能:
- 从FM25H20批量:写入数据
参数:
- address:FM25H20的地址,pdata:写入数据的存储指针,length:写入数据的长度
*/
void FM25_write(unsigned long address, unsigned char* pdata, unsigned long length)
{
unsigned char* padr;
unsigned long temp_adr,temp_len,i;
temp_adr = address;
temp_len = length;
if(temp_adr >= FM_SIZE)
temp_adr = temp_adr%FM_SIZE;
padr=(unsigned char*)&temp_adr;
FM_CS1_L; //片选使能,写使能
Fram_delay(100);
Write_Byte(CMD_WREN); //写使能
Fram_delay(100);
FM_CS1_H;
Fram_delay(100);
FM_CS1_L;
Fram_delay(100);
Write_Byte(CMD_WRITE); //写指令
Write_Byte(padr[2]); //写地址高两位
Write_Byte(padr[1]); //写地址中间字节
Write_Byte(padr[0]); //写地址低字节
/*写入数据*/
for(i=0;i<temp_len;i++)
Write_Byte(pdata[i]);
Fram_delay(100);
FM_CS1_H; //片选禁止写保护
Fram_delay(100);
}
void TestEERW1(void)
{
unsigned int i;
char buf1[100],buf2[100];
memset(buf1, 0, sizeof buf1);
memset(buf2, 0, sizeof buf2);
FM25_read(100, buf1, 100);
for(i = 0; i < 100; i++)
{
buf2[i] = i;
}
memset(buf1, 0, sizeof buf1);
FM25_write(100, buf2, 100);
FM25_read(100, buf1, 100);
for(i = 0; i < 100; i++)
{
buf1[i] = i;
}
}
/*
功能:
- 初始化FM控制接口,IO模拟SPI
-
*/
void FM25_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = SPI2_MOSI_PIN;
GPIO_Init(FM_SPI_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = FM_CS1_PIN;
GPIO_Init(FM_SPI_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = SPI2_SCK_PIN;
GPIO_Init(FM_SPI_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //GPIO_PuPd_NOPULL; //; //GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = SPI2_MISO_PIN;
GPIO_Init(FM_SPI_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = FM_WP_PIN | FM_HOLD_PIN;
GPIO_Init(FM_SPI_PORT, &GPIO_InitStructure);
FM_WP_UNLOCK;
FM_HOLD_H;
FM_CS1_H; //片选置高
SPI2_SCK_L; //时钟置低
SPI2_MOSI_H; //数据置高
TestEERW1();
}
|