FM25CL64芯片手册:https://wenku.baidu.com/view/dabf5e71168884868762d6aa.html
#ifndef FW25CL64_H
#define FW25CL64_H#include "reg51.h"
#include "intrins.h"#define WREN 0X06 //写使能操作码
#define WRDI 0X04 //写禁止
#define RDSR 0X05 //读状态寄存器
#define WRSR 0X01 //写状态寄存器
#define READ 0X03 //读存储器数据
#define WRITE 0X02 //写存储器数据//**** FM25CL64 *****************
//sbit SI = P1^7; //芯片串行输入脚
//sbit SO = P1^6; //芯片串行输出脚
//sbit SCK = P1^5; //时钟线
//sbit CS = P1^4; //片选sbit SCK = P2^5; //铁电的时钟线
sbit SIO = P2^6; //铁电的数据线
sbit CS = P2^7; //铁电的片选void SPI_write_FM25CL64(unsigned char *arr_to_send, unsigned int address,unsigned char arr_num);
void SPI_read_FM25CL64( unsigned char *arr_to_read, unsigned int address,unsigned char arr_read_num);#endif
#include "fw25cl64.h"#define uchar unsigned char
#define uint unsigned intuchar FW25[8] = {0}; //输入输出数组
uint Address = 0; //输入输出地址//*********************
//FM25CL64的写子程序
//
//**********************
static void send_data(uchar w_data)
{uchar i = 0;SCK = 0; //上升沿数据移到芯片的SI脚CS = 0;for(i=0; i<8; i++) //数据的8位从高位到低位进行发送{SCK = 0;if((w_data<<i)&0X80) {SIO = 1;}else{SIO = 0;}_nop_();_nop_();SCK = 1; //上升沿数据移到了芯片中_nop_();_nop_();}
// CS = 1;
}//********************
//FM25CL64的读子程序
//
//********************
static uchar read_data(void)
{uchar i = 0, read = 0;CS = 0;SCK = 1; //下降沿从芯片读取一位数据for(i=0; i<8; i++){_nop_();_nop_();_nop_();_nop_();SCK = 0;if(SIO == 1){read = (read << 1) + 1;}else{read = read << 1;}SCK = 1;}
// CS = 1;return read;
}//**********************
//FM25CL64的写一个字节程序
//data_to_send:是要写进去的数据
//address: 要写数据进去的地址
//**********************
void SPI_write(uchar data_to_send, uint address)
{volatile uchar addr_tempH = 0,addr_tempL = 0;addr_tempH = (uchar)((address&0xff00)>>8); //获取高8位地址addr_tempL = (uchar)(address&0x00ff); //获取低8位地址CS = 0;_nop_();send_data(WREN); //先发写允许信号_nop_();CS = 1;_nop_();CS = 0;_nop_();send_data(WRITE); //发送写存储器指令send_data(addr_tempH); //发送高8位地址send_data(addr_tempL); //发送低8位地址send_data(data_to_send); //发送一个字节的数据到SPI芯片CS = 1;
}//***********************
//FM25CL64的读一个字节的程序
//address:要读的地址
//返回值是读到的数据
//***********************
uchar SPI_read(uint address)
{volatile uchar data_temp = 0, addr_tempH = 0,addr_tempL = 0;addr_tempH = (uchar)((address&0xff00)>>8); //获取高8位地址addr_tempL = (uchar)(address&0x00ff); //获取低8位地址CS = 1;_nop_();CS = 0;send_data(READ); //发送读存储器指令send_data(addr_tempH); //发送高8位地址send_data(addr_tempL); //发送低8位地址data_temp = read_data(); //接收一个字节的数据CS = 1;return data_temp;
}//***********************
//FM25CL64的写多个字节程序
//arr_to_send:是要写进去的数据数组的地址
//address: 要写数据进去的地址
//arr_num: 要写进去的字节数
//***********************
void SPI_write_FM25CL64(uchar *arr_to_send, uint address,uchar arr_num)
{volatile uchar i = 0;volatile uchar addr_tempH = 0,addr_tempL = 0;addr_tempH = (uchar)((address&0xff00)>>8); //获取高8位地址addr_tempL = (uchar)(address&0x00ff); //获取低8位地址CS = 0;_nop_();send_data(WREN); //先发写允许信号_nop_();CS = 1;_nop_();CS = 0;send_data(WRITE); //发送写存储器指令send_data(addr_tempH); //发送高8位地址send_data(addr_tempL); //发送低8位地址for(i=0; i<arr_num; i++){send_data(arr_to_send[i]); //发送一个字节的数据到SPI芯片}CS = 1;
}//***********************
//FM25CL64的读多个字节的程序
//arr_to_read:是读出数据存放的数组地址
//address:要读的地址
//arr_read_num: 要读的字节数
//***********************
void SPI_read_FM25CL64(uchar *arr_to_read,uint address,uchar arr_read_num)
{volatile uchar i = 0;volatile uchar addr_tempH = 0,addr_tempL = 0;addr_tempH = (uchar)((address&0xff00)>>8); //获取高8位地址addr_tempL = (uchar)(address&0x00ff); //获取低8位地址CS = 0;send_data(READ); //发送读存储器指令send_data(addr_tempH); //发送高8位地址send_data(addr_tempL); //发送低8位地址for(i=0; i<arr_read_num; i++){arr_to_read[i] = read_data(); //接收一个字节的数据}CS = 1;
}