移植并修改成功的模拟iic读写EEPROM at24c02
2010-04-24 12:58:00
注:如果要读24c128或264,地址位为16位的。现在的地址位为8位。
protues仿真图
源程序如下:
#include <iom16v.h>
#include <macros.h>
#include "delay.h"
#define I2C_DDR DDRB
#define I2C_PORT PORTB
#define I2C_PIN PINB
#define SDA PB1
#define SCL PB0
#define SDA_OUT()
(I2C_DDR |= (1<<SDA))
#define SCL_OUT() (I2C_DDR |= (1<<SCL))
// 释放SDA,上拉输入
#define SDA_RLS() do{ I2C_PORT |= (1<<SDA); I2C_DDR &=
~(1<<SDA); }while(0)
#define SCL_RLS() do{ I2C_PORT |= (1<<SCL); I2C_DDR &=
~(1<<SCL); }while(0)
#define SDA_SET() (I2C_PORT |= (1<<SDA))
#define SDA_CLR() (I2C_PORT &= ~(1<<SDA))
#define SCL_SET() (I2C_PORT |= (1<<SCL))
#define SCL_CLR() (I2C_PORT &= ~(1<<SCL))
#define READ_SDA() ((I2C_PIN & (1<<SDA)) ? 1 : 0)
#define
i2c_delay() delay_us(1) // 根据系统时钟进行调整
#define AT24C02_WRITE_ADDR 0xA0
#define AT24C02_READ_ADDR 0xA1
#define PAGE_SIZE 8
#define PAGE_MASK (PAGE_SIZE-1)
// 发送起始信号
void i2c_start(void)
{
SDA_SET();
i2c_delay();
SCL_SET();
i2c_delay();
SDA_CLR();
i2c_delay();
SCL_CLR();
//i2c_delay();
}
// 发送停止信号
void i2c_stop(void)
{
SDA_CLR();
i2c_delay();
SCL_SET();
i2c_delay();
SDA_SET();
// i2c_delay();
}
// 向总线写1字节,并返回从机有无应答
unsigned char i2c_write(unsigned char byte)
{
unsigned char i, ack;
ack = 0;
for( i = 0; i < 8; ++i ){
if ( byte & 0x80)
SDA_SET();
else
SDA_CLR();
byte <<= 1;
SCL_SET();
i2c_delay();
SCL_CLR();
}
SDA_RLS();
i2c_delay();
SCL_SET();
i2c_delay();
ack = READ_SDA();
SCL_CLR();
i2c_delay();
SDA_OUT();
return ack;
}
// 读1字节,ack=1时,发送ACK,ack=0,发送nACK
unsigned char i2c_read(unsigned char ack)
{
unsigned char i, r = 0;
SDA_RLS();
for( i = 0; i < 8; i++ ){
SCL_SET();
i2c_delay();
r <<= 1;
if ( READ_SDA() == 1)
{
r |= 1;
}
SCL_CLR();
i2c_delay();
}
SDA_OUT();
// Send ACK
if (!ack)
SDA_SET(); // Send nACK
else
SDA_CLR(); // Send ACK
i2c_delay();
SCL_SET();
i2c_delay();
SCL_CLR();
i2c_delay();
return r;
}
void at24c02_write1byte(unsigned
int byte_addr, unsigned char byte)
{
i2c_start();
i2c_write(AT24C02_WRITE_ADDR);
//i2c_write(byte_addr>>8); //输入15-9高位
i2c_write(byte_addr);
i2c_write(byte);
i2c_stop();
delay_ms(20); // 等待10ms,保证24C02内部写操作完成
}
void at24c02_page_write(unsigned
char byte_addr, unsigned char *buff, unsigned char num)
{
unsigned char i;
i2c_start();
i2c_write(AT24C02_WRITE_ADDR);
i2c_write(byte_addr);
for( i = 1; i <= num; ++i )
{
i2c_write(*buff);
buff++;
}
i2c_stop();
delay_ms(20);
}
void at24c02_writenbytes(unsigned
char byte_addr, unsigned char *buff, unsigned char num)
{
unsigned char n_tmp;
n_tmp = PAGE_SIZE - (unsigned char)(byte_addr &
PAGE_MASK); // 本页内剩余空间数量
if ((num > n_tmp) && (n_tmp != 0))
{
// 先将本页剩余空间写满
at24c02_page_write(byte_addr, buff,
n_tmp);
byte_addr += n_tmp;
num -= n_tmp;
buff += n_tmp;
}
while( num >= PAGE_SIZE ){
at24c02_page_write(byte_addr, buff,
PAGE_SIZE);
byte_addr += PAGE_SIZE;
num -= PAGE_SIZE;
buff += PAGE_SIZE;
}
if (num != 0)
{
at24c02_page_write(byte_addr, buff,
num);
}
}
unsigned char
at24c02_read_curr_addr(void)
{
unsigned char data;
i2c_start();
i2c_write(AT24C02_READ_ADDR);
data = i2c_read(0); // 读1字节,返回nACK
i2c_stop();
return data;
}
unsigned char
at24c02_read1byte(unsigned char byte_addr)
{
unsigned char data;
i2c_start();
i2c_write(AT24C02_WRITE_ADDR);
//i2c_write(byte_addr>>8); //输入15-9高位
i2c_write(byte_addr);
i2c_start();
i2c_write(AT24C02_READ_ADDR);
data = i2c_read(0); // 读1字节,返回nACK
i2c_stop();
return data;
}
void at24c02_readnbytes(unsigned
char byte_addr, unsigned char *buff, unsigned char num)
{
unsigned char i;
i2c_start();
i2c_write(AT24C02_WRITE_ADDR);
i2c_write(byte_addr);
i2c_start();
i2c_write(AT24C02_READ_ADDR);
for( i = 1; i <= num-1; ++i )
{
*buff =
i2c_read(1); // 读1字节返回ack
buff++;
}
*buff = i2c_read(0);
i2c_stop();
}
void port_init(void)
{
PORTA=0xff;
DDRA=0xff;
SDA_OUT();
SCL_OUT();
}
void main(void)
{
unsigned char temp1[8]={1,2,3,4,5,6,7,8};
unsigned char temp2[8]={0};
port_init();
at24c02_writenbytes(0x03,temp1,8);// write the byte 01h at
address 03h
at24c02_readnbytes(0x03,temp2,8); // read the
byte 01h at address 03h
//SDA_RLS() ;
//SCL_RLS() ;
while (1)
{
if(temp2[0]==1)
{
PORTA=0XFE;
delay_ms(100);
PORTA=0XFF;
delay_ms(50);
}
if(temp2[1]==2)
{
PORTA=0XFD;
delay_ms(100);
PORTA=0XFF;
delay_ms(50);
}
if(temp2[2]==3)
{
PORTA=0XFB;
delay_ms(100);
PORTA=0XFF;
delay_ms(50);
}
if(temp2[3]==4)
{
PORTA=0XF7;
delay_ms(100);
PORTA=0XFF;
delay_ms(50);
}
if(temp2[4]==5)
{
PORTA=0XEF;
delay_ms(100);
PORTA=0XFF;
delay_ms(50);
}
if(temp2[5]==6)
{
PORTA=0XDF;
delay_ms(100);
PORTA=0XFF;
delay_ms(50);
}
if(temp2[6]==7)
{
PORTA=0XBF;
delay_ms(100);
PORTA=0XFF;
delay_ms(50);
}
if(temp2[7]==8)
{
PORTA=0X7F;
delay_ms(100);
PORTA=0XFF;
delay_ms(50);
}
}
}