⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 at25f4096.c

📁 AT25F4096的SPI通信
💻 C
字号:
//#include "i2c.h"
#include "decode_38.h"
#include "spi.h"
#include "at25f4096.h"

static void sector_erase(uint32 address);
static uint8 write_enable();

static uint8 status_register_4096;
static uint8 last_address_of_4096;



unsigned char program_f4096(uint8 *point_to_databuffer, int num_of_data_writeto4096)
{
	int i;
	uint8 *pdata_to_be_write = point_to_databuffer;
	
	char  data_buffer[3];//last_address_of_4096赋值给data_buffer 高位在前
	data_buffer[0] = 0;
	data_buffer[1] = 0;
	data_buffer[2] = 0;
//	sector_erase(0x00000000);
	write_enable();//maybe it need to be delete
	decode(CTL_F4096);
	SPI_SPDR = program_4096;
	while(0 == (unsigned char)(SPI_SPSR&0x80));	
	for(i=0;i<3;i++)
	{	
		SPI_SPDR = data_buffer[i];
		while(0 == (unsigned char)(SPI_SPSR&0x80));
	}
		for(i=0;i<num_of_data_writeto4096;i++)
	{	
		SPI_SPDR = pdata_to_be_write[i];
		while(0 == (unsigned char)(SPI_SPSR&0x80));
	}
	decode (CTL_NONE);
	return SPI_SPDR;//要改成返回错误,这样更稳定 在前面定义错误类型 出那种错就返回其类型	
}


unsigned char program_at25f4096(uint32 address_write_to_4096, uint8 *point_to_databuffer, int num_of_data_writeto4096)
{
	int i;
	//uint8 data_from_spdr;
	uint8 *pdata_to_be_write = point_to_databuffer;
	
	char  data_buffer[3];//last_address_of_4096赋值给data_buffer 高位在前
	data_buffer[0] = (uint8)(address_write_to_4096>>16);
	data_buffer[1] = (uint8)(address_write_to_4096>>8);
	data_buffer[2] = (uint8)address_write_to_4096;
	if(!(address_write_to_4096&0x0000ffff))
	{
		sector_erase(address_write_to_4096);
	}	
	
	write_enable();//maybe it need to be delete
	decode(CTL_F4096);
	SPI_SPDR = program_4096;
	while(0 == (unsigned char)(SPI_SPSR&0x80));	
	for(i=0;i<3;i++)
	{	
		SPI_SPDR = data_buffer[i];
		while(0 == (unsigned char)(SPI_SPSR&0x80));
		//data_from_spdr = SPI_SPDR;
	}
		for(i=0;i<num_of_data_writeto4096;i++)
	{	
		SPI_SPDR = point_to_databuffer[i];
		while(0 == (unsigned char)(SPI_SPSR&0x80));
		//data_from_spdr = SPI_SPDR;
	}
	decode (CTL_NONE);
	return SPI_SPDR;//要改成返回错误,这样更稳定 在前面定义错误类型 出那种错就返回其类型	
}


uint8 write_enable()
{
	//unsigned char data_put_spdr;
	decode(CTL_F4096);
	SPI_SPDR = wren_4096;
	while(0 == (unsigned char)(SPI_SPSR&0x80));
	//data_put_spdr = SPI_SPDR;
	decode (CTL_NONE);
	return SPI_SPDR;
}


uint8 read_status_register()
{
	decode(CTL_F4096);
	SPI_SPDR = rdsr_4096;
	while(0 == (unsigned char)(SPI_SPSR&0x80));
	status_register_4096 =  SPI_SPDR;
	while(0 == (unsigned char)(SPI_SPSR&0x80));//need to change
	decode (CTL_NONE);
	return SPI_SPDR;
}


void write_status_register()
{
	write_enable();//maybe it need to be delete
	decode(CTL_F4096);
	SPI_SPDR = wrsr_4096;
	while(0 == (unsigned char)(SPI_SPSR&0x80));
	SPI_SPDR = 0x1b;//0x1b maybe need to change
	while(0 == (unsigned char)(SPI_SPSR&0x80));
	decode (CTL_NONE);
	return SPI_SPDR;
}


void sector_erase(uint32 address)//address 为要察的sector里面的任何一个字节的地址
{
	int i;
	uint8 transfer_buffer[4];
	uint32 address_of_erase = address;
	transfer_buffer[2] = (uint8)address_of_erase;
	transfer_buffer[1] = (uint8)(address_of_erase>>8);
	transfer_buffer[0] = (uint8)(address_of_erase>>16);
	write_enable();
	decode(CTL_F4096);
	SPI_SPDR = sector_erase_4096;
	while(0 == (unsigned char)(SPI_SPSR&0x80));
	for(i=0;i<3;i++)
	{
		SPI_SPDR = transfer_buffer[i];
		while(0 == (unsigned char)(SPI_SPSR&0x80));
	}
	decode (CTL_NONE);		
}



unsigned char read_from_f4096(uint8 *point_to_databuffer, int num_of_data_readfrom4096)
{
	int i;
	uint8 *pdata_get_from_4096 = point_to_databuffer;
	
	char  data_buffer[3];//要读的储存器的首地址赋给data_buffer 高位在前
	data_buffer[0] = 0;
	data_buffer[1] = 0;
	data_buffer[2] = 0;
	decode(CTL_F4096);
	SPI_SPDR = read_4096;
	while(0 == (unsigned char)(SPI_SPSR&0x80));	
	for(i=0;i<3;i++)
	{	
		SPI_SPDR = data_buffer[i];
		while(0 == (unsigned char)(SPI_SPSR&0x80));
	}
		for(i=0;i<num_of_data_readfrom4096;i++)
	{	
		pdata_to_be_write[i] = SPI_SPDR;
		while(0 == (unsigned char)(SPI_SPSR&0x80));
	}
	decode (CTL_NONE);
	return SPI_SPDR;//要改成返回错误,这样更稳定 在前面定义错误类型 出那种错就返回其类型	
	
}

unsigned char read_from_at25f4096(uint32 address_read_from_at25f4096, uint8 *point_to_databuffer, int num_of_data_readfrom4096)
{
	int i;
	uint8 *pdata_get_from_4096 = point_to_databuffer;
	
	char  data_buffer[3];//要读的储存器的首地址赋给data_buffer 高位在前
	data_buffer[0] = (uint8)(address_read_from_at25f4096>>16);
	data_buffer[1] = (uint8)(address_read_from_at25f4096>>8);
	data_buffer[2] = (uint8)address_read_from_at25f4096;
	write_enable();
	decode(CTL_F4096);
	SPI_SPDR = read_4096;
	while(0 == (unsigned char)(SPI_SPSR&0x80));	
	for(i=0;i<3;i++)
	{	
		SPI_SPDR = data_buffer[i];
		while(0 == (unsigned char)(SPI_SPSR&0x80));
	}
		for(i=0;i<num_of_data_readfrom4096;i++)
	{	
		pdata_to_be_write[i] = SPI_SPDR;
		while(0 == (unsigned char)(SPI_SPSR&0x80));
	}
	decode (CTL_NONE);
	return SPI_SPDR;//要改成返回错误,这样更稳定 在前面定义错误类型 出那种错就返回其类型	
	
}

void chip_erase(void)
{
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -