当前位置:首页 > 资讯 > info5 > 正文

【菜鸟入门】stm32 之 eeprom

发表于: 2013-11-18   作者:ieczw   来源:转载   浏览:
摘要: 楼管大叔来催了,实验室要关门了,我今天就先把代码放着了,后期再做详细说明:/*at24c02.c*/ #include #include"init.h" #include"iic.h" #definesend_slave_addr(a)iic_send_byte(a) #definesend_rom_addr(a)iic_send_byte(a) #defineROM_SIZE(2*1024)/

楼管大叔来催了,实验室要关门了,我今天就先把代码放着了,后期再做详细说明:

/* at24c02.c */
#include <stm32f10x.h>
#include "init.h"
#include "iic.h"

#define send_slave_addr(a)	iic_send_byte(a)
#define send_rom_addr(a)		iic_send_byte(a)
#define ROM_SIZE		(2*1024)			//2K
#define ADDR_SIZE		256
#define PAGE_SIZE		8
#define PAGE				32

int at24c02_init(unsigned char slave_addr)
{
	iic_init();
	iic_start();
	send_slave_addr(slave_addr|IIC_WRITE);
	if(iic_recv_ack())
		return 0;
	else
		return 1;
}
int set_rom_addr(unsigned addr)
{
	send_rom_addr(addr);
	if(iic_recv_ack())
		return 0;
	else
		return 1;
}
int at24c02_write_unpage(	unsigned char slave_addr,
													unsigned char start_addr,
													char buff[],
													unsigned char size)
{
		delay_ms(1);
	if(0 == at24c02_init(slave_addr))
		return -1;
	if(0 == set_rom_addr(start_addr))
		return -1;
	if(size > iic_send_str(buff,size))
	{
			iic_stop();
			return 0;
	}
  else 
	{
			iic_stop();
			return 1;
	}
}
int at24c02_write_page(unsigned char slave_addr,
											 unsigned char start_addr,
											 char buff[])
{
	delay_ms(1);
	if(0 == at24c02_init(slave_addr))
		return -1;
	if(0 == set_rom_addr(start_addr))
		return -1;
	if(8 > iic_send_str(buff,8))
	{
			iic_stop();
			return 0;
	}
  else 
	{
			iic_stop();
			return 1;
	}
}
int at24c02_write(unsigned char slave_addr,
									unsigned char start_addr,
									char buff[],
									unsigned char size)
{
	unsigned char addr = 0;
	unsigned char send_byte;
	unsigned char i;
	char temp;
	addr = 8 + ((unsigned char)(start_addr/8))*8;//Get Next Page Frist Address
	send_byte = addr - start_addr;
	if(size <= send_byte)
	{
		if(-1 == at24c02_write_unpage(slave_addr,start_addr,buff,size))
			return -1;
		else
			return 0;
	}
	else
	{
		if(-1 == at24c02_write_unpage(slave_addr,start_addr,buff,send_byte))
			return -1;
	}
	size -= send_byte;
	buff += send_byte;
	delay_ms(5);
	temp = (char)(size/8);
	for(i=0; i<temp;i++)
	{
		start_addr = addr;
		if(-1 == at24c02_write_page(slave_addr,start_addr,buff))
			return -1;
		addr += 8;
		size -= 8;
		buff += 8;
		delay_ms(5);
	}
	start_addr = addr;
	if(-1 == at24c02_write_unpage(slave_addr,start_addr,buff,size))
			return -1;
	return 0;
}
int at24c02_read( unsigned char slave_addr,
									unsigned char start_addr,
									char buff[],
									unsigned char size)
{
	delay_ms(1);
	if(0 == at24c02_init(slave_addr))
		return -1;
	if(0 == set_rom_addr(start_addr))
		return -1;
	iic_start();
	send_slave_addr(slave_addr|IIC_READ);
	if(iic_recv_ack())
		return -1;
	iic_recv_str(buff,size);
	iic_stop();
	return size;
}
应用例子:

int main()
{
	char str[512];
	int length = 0;
	sys_init(9);
	rs232_init(CPU_72M,9600);
	rs232_send_byte('\n');
	
	if(-1 == at24c02_write(SLAVE_ADDR,6,__NOTE,strlen(__NOTE)))
		rs232_send_str("Write AT24C02 Fail!\n",20);
	delay_ms(1000);
	if(-1 == (length = at24c02_read(SLAVE_ADDR,0,str,64)))
		rs232_send_str("Read  AT24C02 Fail!\n",20);
	
	rs232_send_str("Read:",strlen("Read:"));
	rs232_send_str(str,strlen(str));
	
	return 0;
}




【菜鸟入门】stm32 之 eeprom

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
啊啊啊,先叫两声,释放释放今天的压抑。。搞了一天终于把中断给搞定。 中断寄存器怎么说呢,两个字
经过今天一天的努力终于完成了我的stm32第一个程序;我也是今天才开始接触stm32。 由于苦于没有资料
一、基础学习 1、何为CSS CSS是Cascading Style Sheets(层叠样式表)的简称,是一种标记语言,它不需
先直接上代码 #include "stm32f0xx.h" #include "stm32_lib/inc/stm32f0xx_rcc.h" #include "stm32_
不好意思,忘了,使用CooCox IDE,还需要安装交叉编译工具 gcc-arm-none-eabi-XXXXXXXXX版本,自己
1、CooCox IDE当前最新版本为V2,不过个人看网上的示例大都是基于旧版本的,个人也试过新版本,发现
#include "stm32f0xx.h" #include "stm32_lib/inc/stm32f0xx_rcc.h" #include "stm32_lib/inc/stm32
1、struts2处理web应用请求过程mvc实现 2、HTTP请求 提交 Struts2StrutsPrepareAndExecuteFilter 核
有时候需要从网页上获取数据,比如别一些网页上的新闻获取到放到应用上,其实这也算是一个简单的网
排序,是将一组任意排列的数据元素重新排列成一个按键值有序的序列的过程,一般以键值的比较和记录
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号