楼管大叔来催了,实验室要关门了,我今天就先把代码放着了,后期再做详细说明:
~~~
/* 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的第一个程序--LED
- 【菜鸟入门】stm32 之 扫描按键
- 【菜鸟入门】stm32 之 中断按键
- 【菜鸟入门】stm32 之 USART
- 【菜鸟入门】stm32 之 iic
- 【菜鸟入门】stm32 之 eeprom
- 【菜鸟入门】stm32 之 pwm
- 【菜鸟入门】stm32 之 ADC 模数转换
- 【菜鸟入门】stm32 之 实时时钟
- 【菜鸟入门】stm32 之 DMA
- 【菜鸟入门】stm32 之 DAC
- 【STM库应用】stm32 之 USART
- 中断源去抖办法
- stm32 启动代码应用技巧
- 【STM库应用】stm32 之 IIC应用
- 【STM库应用】stm32 之 中断按键初始化(注意事项)
- 关于结构体初始化
- 【STM库应用】stm32 之 TIM (详解一 通用定时器)
- 【STM库应用】stm32 之 TIM (详解二 脉冲宽度、周期测量)
- 【stm32库应用】SD驱动移植(基于SDIO外设)
- SD卡fat文件系统移植
- stm32 DMA初始化选项研究
- stm32 灵活静态存储控制器(FSMC)(NORFLASH\PSRAM)
- 【stm32+uC/OS-II】ucosii移植简单详细步骤
- STM32 加入调试信息来调试代码
- NRF24L01 无线通信模块使用