马老师你书上"CodeVisionAVR中的I2C函数读写2402"这一段的例程,怎么不
马老师,你好,你用你书上"CodeVisionAVR中的I2C函数读写2402"这一段的例程,怎么不行呢,你测试过吗以下蓝色文字由站长:armok 于:2007-11-26,17:43:12 加入。你的原标题:“<font color=black>请教马老师一个问题” 不合符规定。请更改成能说明帖子大意的标题
试想一下,如果本论坛的帖子标题清一色的‘帮助!’,‘ADC求救!’等笼统的标题,你在阅读的过程中会造成许多麻烦。
所以本论坛规定:一定要起一个能说明帖子大意的标题。不允许“关于 AVR 的 ADC 使用!”这种笼统标题。作为标题,必须说明清楚:
①你是在请教问题,还是在介绍有关的知识?
②ADC的问题有许多,你是想说那方面的内容?
注意以上两点,标题应该改成如:“请教:ADC 可以不使用中断吗?”。
起一个能说明帖子大意的标题,除了减轻我们的帖子分类整理工作,还方便其它人阅读,节省大家的时间。
注:使用合格的标题,对你来说只是举手之劳,但却会为我们的管理工作提供很大的便利。谢谢你的支持。
./bbs_upload/files_6/armok01122228.gif 当然测试过的,书中所有例程都是我自己写的,并测试过的.尽管书中是操作24C256,但属于24系列的.
你用它操作2402应该没有问题,但不是照搬.2402和24C256的指令有区别的.
你出现了什么问题? #include <mega16.h>
#include <delay.h>
#include <i2c.h>
#asm
.equ __i2c_port=0x15 //PORTC
.equ __sda_bit=1
.equ __scl_bit=0
#endasm
#define EEPROM_BUS_ADDRESS 0xa0
/* read a byte from the EEPROM */
unsigned char eeprom_read(unsigned char address) {
unsigned char data;
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS);
i2c_write(address);
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS|1);
data=i2c_read(0);
delay_ms(10);
i2c_stop();
return data;
}
/* write a byte to the EEPROM */
void eeprom_write(unsigned char address, unsigned char data) {
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS);
i2c_write(address);
i2c_write(data);
i2c_stop();
/* 10ms delay to complete the write operation */
//delay_ms(10);
}
main()
{
i2c_init();
eeprom_write(0x30,0x88);
i=eeprom_read(0x30);
}
2402加不加上拉电阻用以上程序读写不正常,请问是什么原因呢 以下是CVAVR中提供的例子
Example how to access an Atmel 24C02 256 byte I2C EEPROM:
/* the I2C bus is connected to PORTB */
/* the SDA signal is bit 3 */
/* the SCL signal is bit 4 */
#asm
.equ __i2c_port=0x18
.equ __sda_bit=3
.equ __scl_bit=4
#endasm
/* now you can include the I2C Functions */
#include <i2c.h>
/* function declaration for delay_ms */
#include <delay.h>
#define EEPROM_BUS_ADDRESS 0xa0
/* read a byte from the EEPROM */
unsigned char eeprom_read(unsigned char address) {
unsigned char data;
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS);
i2c_write(address);
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS | 1);
data=i2c_read(0);
i2c_stop();
return data;
}
/* write a byte to the EEPROM */
void eeprom_write(unsigned char address, unsigned char data) {
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS);
i2c_write(address);
i2c_write(data);
i2c_stop();
/* 10ms delay to complete the write operation */
delay_ms(10);
}
void main(void) {
unsigned char i;
/* initialize the I2C bus */
i2c_init();
/* write the byte 55h at address AAh */
eeprom_write(0xaa,0x55);
/* read the byte from address AAh */
i=eeprom_read(0xaa);
while (1); /* loop forever */
}
========================================================
从你的程序中,你是怎么证明读写不正常的?你的硬件如何连接的? 我就是按照CVAVR中提供的这个例子做的,我写入的是0x88,但读出来就不是0x88了.2402第5脚接AVR的23脚,第6脚接AVR的22脚,2402其余的地址脚接法与地址A0对应 程序没问题,我自己,包括学生都用过.可能是其它地方出了问题. 我是内部频率8M,有问题吗.或者编译器不行? 先把频率降低的1M或2M试试.
另外,你使用8M,那么工程配置中,设置为8M吗?如果工程设置中为确省的1M,编译器按1M延时,而实际为8M,那么你的程序就快了8倍.
那么可能24C02就跟不上,标准I2C为400K 你手里有我的书,第2个例子中(P477)的问题2专门谈到这个问题的. 现在我在1M的频率下试了一下,不行,也不知道哪里出了问题,看来只有编译器了? 手头就一片24C02吗?换片试试,硬件在查查问题.通常不是编译器的问题. 我再试试吧
页:
[1]