Ðề tài: Giao tiếp I2C
View Single Post
Old 09-10-2008, 11:39 AM   #114
tungtot_vl
Đệ tử 6 túi
 
tungtot_vl's Avatar
 
Tham gia ngày: Jul 2007
Bài gửi: 154
:
i2c

chào các bác mình đang làm giao tiếp i2c với 24c64.
mình coppy trong file hell của ccs ra và chạy thử cho 1 byte.
khi hàm ghi và đọc được dùng ở ngoài vòng lặp white() thì chương trình đọc đúng giá trị ghi vào.
nhưng khi bỏ hàm ghi và đọc và vòng lặp white() thì chương trình chỉ đọc đúng lần đầu tiên. khi lặp lại vòng lặp thì bị sai.
hoặc nếu ghi và đọc nhiều lần cho từng byte thì data dọc ra cũng bị sai. mình không rỏ vì sao mong các bạn chỉ giúp.
#include<18f4680.h>
#fuses NOLVP,NOWDT,PUT,hs,NOPROTECT,NOBROWNOUT
#device 18f4680*=16 ADC=10 // high_ints=true
#use delay(clock=20000000)
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(e)
int k;

////////////////////////////////////////////////////////////////////////
#ifndef EEPROM_SDA

#define EEPROM_SDA PIN_b1 //b1
#define EEPROM_SCL PIN_b0//b0

#endif

#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)

#define EEPROM_ADDRESS long int
#define EEPROM_SIZE 8192

void init_ext_eeprom()
{
output_FLOAT(EEPROM_SCL);
output_FLOAT(EEPROM_SDA);
}

void write_ext_eeprom(long int address, BYTE data)
{
short int status;
i2c_start();
i2c_write(0xa0);
i2c_write((address>>8)&0x1f);
i2c_write(address);
i2c_write(data);
i2c_stop();
status=i2c_write(0xa0);
while(status==1)
{
i2c_start();
status=i2c_write(0xa0);
}
}

BYTE read_ext_eeprom(long int address) {
BYTE data;
i2c_start();
i2c_write(0xa0);
i2c_write((address>>8)&0x1f);
i2c_write(address);
i2c_start();
i2c_write(0xa1);
data=i2c_read(0);
i2c_stop();
output_d(data);
return(data);
}

void main()
{
set_tris_A(0x00);
set_tris_b(0x00);
set_tris_C(0x00);
set_tris_D(0x00);
set_tris_E(0x00);
/////////////////////////
write_ext_eeprom(10,8);
read_ext_eeprom(10);
while(1)
{
delay_ms(1000);

}
}
/////////////////////////////////////////////////
nếu bỏ các hàm vào vòng lặp thì chỉ chạy đúng lần đầu tiên. các lần còn lại là sai
while(1)
{
write_ext_eeprom(10,8);
read_ext_eeprom(10);
delay_ms(1000);

}
//////////////////////////////
ghi và đọc nhiều lần cũng bị sai data
while(1)
{
write_ext_eeprom(10,8);
read_ext_eeprom(10);
delay_ms(1000);
write_ext_eeprom(12,6);
read_ext_eeprom(12);
delay_ms(1000);


}
tungtot_vl vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn