向IIC总线器件24c02EEPROM中发一组字符,并将它们读出送入P1,P1连接着灯,使灯逐次变化。。但我写的程序,P1口没有什么变化,真心不懂哪里有问题,求各路大神指点,谢谢~~ #include #include #define uint unsigned int #define uchar unsigned char ***it sda=P2^1; //IO口定义 ***it scl=P2^0; char code music[] = { 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar data buffer[100]; //用于缓存从24c02中读取的数据。
void delay(unsigned int m) { unsigned int n,p; for(n=m;n>0;n--) for(p=125;p>0;p--); } void nop() { _nop_(); _nop_(); } /////////24C02读写驱动程序//////////////////// void delay1(unsigned int m) { unsigned int n; for(n=0;n }
void init() //24c02初始化子程序 { scl=1; nop(); sda=1; nop(); }
void start() //启动I2C总线 { sda=1; nop(); scl=1; nop(); sda=0; nop(); scl=0; nop(); }
void stop() //停止I2C总线 { sda=0; nop(); scl=1; nop(); sda=1; nop(); }
void writebyte(unsigned char j) //写一个字节 { unsigned char i,temp; temp=j; for (i=0;i<8;i++) { temp=temp<<1; scl=0; nop(); sda=CY; //temp左移时,移出的值放入了CY中 nop(); scl=1; nop(); } scl=0; nop(); sda=1; nop(); }
unsigned char readbyte() //读一个字节 { unsigned char i,j,k=0; scl=0; nop(); sda=1; for (i=0;i<8;i++) { nop(); scl=1; nop(); if(sda==1) j=1; else j=0; k=(k<<1)|j; scl=0; } nop(); return(k); }
void clock() //I2C总线时钟 { unsigned char i=0; scl=1; nop(); while((sda==1)&&(i<255)) i++; scl=0; nop(); }
////////从24c02的地址address中读取一个字节数据///// unsigned char read24c02(unsigned char address) { unsigned char i; start(); writebyte(0xa0); clock(); writebyte(address); clock(); start(); writebyte(0xa1); clock(); i=readbyte(); stop(); delay1(100); return(i); }
//////向24c02的address地址中写入一字节数据info///// void write24c02(unsigned char address,unsigned char info) { start(); writebyte(0xa0); clock(); writebyte(address); clock(); writebyte(info); clock(); stop(); delay1(5000); } void main() { uchar e,m,w; init(); P1=0; while(1) { for(e=0;e<8;e++) { write24c02(0x00+e,music[e]); delay(5000); // P1=radd(0x00+e); // delay1(5000); } while(m!=e) { buffer[m]=read24c02(m); delay(5000); m++; } m=0; while(w!=m) {P1=buffer[w]; w++; delay(5000); } w=0; } }
|