第一个程序不能用,第二个程序只能一个字节一个字节读写,不能连续读写,有什么办法解决我下面这个子程序问题??多谢! nclude "reg52.h" #include "absacc.h" #include "math.h" #include "stdio.h" #include "intrins.h" #include "ad7896h.h" #include "ds18b20h.h" #include "timer.h" #include "24c16.h" #include "keyh.h" #include "dac.h" #include "pwmh.h"
uchar IDATA c_SumValue[7]; uchar IDATA VlotageSum[7]; void delay10(uint x); void I_init(void);
void wait(void) { byte i=50; for(i=0;i<8;i++); } void delay10(uint x) { uint i=0; for(i=0;i<x;i++) _nop_(); } void I_init(void) { SCL = 1 ; SDA=1 ; wait(); } /* void start_bit(void) { SCL=1;_nop_(); SDA=1;_nop_(); SDA=0;_nop_(); SCL=0;_nop_(); }
void stop_bit(void) { SDA=0;_nop_(); SCL=1;_nop_(); SDA=1;_nop_(); }
void mast_ack(void) { SCL=0;_nop_(); SDA=0;_nop_(); SCL=1;_nop_(); SCL=0;_nop_(); SDA=1;_nop_(); }
void ack(void) { //unsigned char i=0; SDA=1; SCL=0;_nop_(); SCL=1;_nop_(); //while(SDA){;} //while((SDA==1)&&(i<255))i++; _nop_(); wait(); SCL=0;_nop_(); }
void no_ack(void) { //unsigned char i=0; SDA=1;_nop_(); SCL=1;_nop_(); //while(SDA){;} //while((SDA==1)&&(i<255))i++; _nop_(); wait(); SCL=0;_nop_(); }
write_8bit(uchar ch) { uchar i=8; SCL=0;_nop_(); while(i--) { SDA=(bit)(ch&0x80);_nop_(); ch<<=1; SCL=1;_nop_(); SCL=0;_nop_(); } }
uchar read24c16(uint address) { //unsigned char page=(address/256)<<1; uchar data rdata; uchar i=8; EA=0;// start_bit(); //write_8bit(OP_WRITE|page); write_8bit(0XA0); ack(); write_8bit(address); ack();//αд start_bit(); //write_8bit(OP_READ|page); write_8bit(0XA1); ack(); while(i--) { rdata<<=1; SCL=0;_nop_(); SCL=1;_nop_(); if(SDA) rdata|=0x01; } no_ack(); stop_bit(); EA=1; return rdata; }
void write24c16(uint address,uchar ddata) { //unsigned char page=(address/256)<<1; EA=0; // start_bit(); //write_8bit(OP_WRITE|page); write_8bit(0XA0); ack(); write_8bit(address); ack(); write_8bit(ddata); ack(); stop_bit(); EA=1; //delay10(10); }
void page_wr(uint firstw_ad,uint counter,uchar *firstr_ad) { //unsigned char page=(firstw_ad/256)<<1; uchar data *ufirstr_ad; ufirstr_ad=firstr_ad; start_bit(); //write_8bit(OP_WRITE|page); write_8bit(0XA2); ack(); write_8bit(firstw_ad); ack(); while(counter--) { write_8bit(*ufirstr_ad); ufirstr_ad++; ack(); } stop_bit(); //delay10(10); }
void page_rd(uint firstrd_ad,uint count,uchar *firstwr_ad) { //unsigned char page=(firstrd_ad/256)<<1; uchar j=8; uchar data *ufirstwr_ad; ufirstwr_ad=firstwr_ad; start_bit(); //write_8bit(OP_WRITE|page); write_8bit(0XA2); ack(); write_8bit(firstrd_ad); ack(); start_bit(); //write_8bit(OP_READ|page); write_8bit(0XA3); ack(); while(count--) { uchar i=8; while(i--) { (*ufirstwr_ad)<<=1; SCL=0;_nop_();_nop_();SCL=1; if(SDA) (*ufirstwr_ad)|=0x01; } ufirstwr_ad++; mast_ack(); } while(j--) { (*ufirstwr_ad)<<=1; SCL=0;_nop_();_nop_();SCL=1; if(SDA) (*ufirstwr_ad)|=0x01; } no_ack(); stop_bit(); }
void read_wri16(void) { c_SumValue[0]=VariablePi&0xff; c_SumValue[1]=(VariablePi&0xff00)>>8; page_wr(0,2,c_SumValue); page_rd(0,2,c_SumValue); VariableVl=VlotageSum[1]*256+VlotageSum[0]; VariableVl=VariableVl&0xfff; } */ void ack(void) { unsigned char i=0; SDA=1; SCL=0;_nop_(); SCL=1;_nop_(); while((SDA==1)&&(i<255))i++; _nop_(); SCL=0;_nop_(); } void I_start(void) { SCL = HIGH ; wait(); SDA=LOW; wait(); SCL=LOW; wait(); } void I_stop(void) { SDA=LOW; wait(); SCL = HIGH ; wait(); SDA=HIGH ; wait(); SCL=LOW; wait(); } void I_send(byte a) { byte i; for(i=0;i<8;i++) { if(a & 0x80) SDA=HIGH; else SDA=LOW; a=(a<<1); SCL = HIGH ; wait(); SCL=LOW; wait(); } SDA=HIGH; wait(); SCL = HIGH ; wait(); SCL=LOW; wait(); } byte I_receive(void) { byte a=0; byte i; for(i=0;i<8;i++) { a = a*2; SCL = HIGH ; wait(); if(SDA) a |= 1; SCL=LOW; wait(); } SDA=HIGH; wait(); return(a); } void I_addres_wri(byte ds,byte x,byte a) { I_start(); I_send(ds); I_send(x); I_send(a); I_stop(); delay10(0x500); } byte I_addres_red(byte ds,byte x) { byte a; I_start(); I_send(ds); I_send(x); I_start(); I_send(ds+1); a=I_receive(); I_stop(); return(a); } void wri_n(uchar z,uchar * x,uchar y,byte ds)
{ uchar i; uchar a; for(i=0;i<y;i++) { a=*(x+i); I_addres_wri(ds,z+i,a); } } rea_n(uchar z,uchar * x,uchar y,byte ds) { uchar i; for(i=0;i<y;i++) { *(x+i)=I_addres_red(ds,z+i); } }
void read_wri16(void) { c_SumValue[0]=VariablePi&0xff; c_SumValue[1]=(VariablePi&0xff00)>>8; wri_n(0,c_SumValue,2,0xa2); rea_n(0,VlotageSum,2,0Xa2); VariableVl=VlotageSum[1]*256+VlotageSum[0]; VariableVl=VariableVl&0xfff; }
|