ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
楼管大叔来催了,实验室要关门了,我今天就先把代码放着了,后期再做详细说明: ~~~ /* 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; } ~~~