1、/此为待写入24c02的数据。为了便于验证结果,数组的内容为周期重复的。char code music = 0x55, 0xaa, 0x0f, 0xf0, 0x55, 0xaa, 0x0f, 0xf0, 0x55, 0xaa, 0x0f, 0xf0, 0x55, 0xaa, 0x0f, 0xf0, 0x55, 0xaa, 0x0f, 0xf0, 0x55, 0xaa, 0x0f, 0xf0; /由于最后还要讲这些数据读出送到P1口,故可发现P1口相连的led有规律地闪烁15个周期uchar data buffer100; /用于缓存从24c02中读取的数据。delay(unsigned int
2、 m) unsigned int n, p; for (n = m; n0; n-) for (p = 125; p p-);void nop() _nop_(); scl = 0; nop(); sda = CY; /temp左移时,移出的值放入了CY中 scl = 1; /待sda线上的数据稳定后,将scl拉高 scl = 0; nop(); sda = 1;unsigned char readbyte() /读一个字节 unsigned char i, j, k = 0; for (i = 0; i8; i+) scl = 1; if (sda = 1) j = 1; else j =
3、0; k = (k 1) | j; return(k);void clock() /I2C总线时钟 unsigned char i = 0; while (sda = 1) & (i255) i+;/从24c02的地址address中读取一个字节数据/unsigned char read24c02(unsigned char address) unsigned char i; start(); writebyte(0xa0); clock(); writebyte(address); writebyte(0xa1); i = readbyte(); stop(); delay1(100); r
4、eturn(i);/向24c02的address地址中写入一字节数据info/void write24c02(unsigned char address, unsigned char info) writebyte(info); delay1(5000); /这个延时一定要足够长,否则会出错。因为24c02在从sda上取得数据后,还需要一定时间的烧录过程。void main() uchar add, i, j, m; init(); /初始化24C02 P1 = 0X55; while (add != sizeof(music) write24c02(0x00 + add, musicadd); add+; if (add % 4 = 0) P1 = P1; P1 = 0X00; /到此为止,向24C02中写入数据的过程均已结束。下面的程序为附加的。 /将已写入的数据再读出,送到P1口,通过观察led的亮灭来检验写入的数据是否正确。 while (m != add) bufferi = read24c02(m); m+; while (j ! P1 = bufferj; j+; delay(1000); while (1);