bit time_over_flag;
unsigned char xdata dsa_time;
unsigned int DSA_read(void); unsigned char DSA_write(unsigned char command,unsigned char parameter);
unsigned int DSA_read(void) { unsigned int idata i = 0x0000; unsigned char idata j;
DSA_CLK = 0; time_over_flag= 0;
dsa_time = DSATIME; while((!DSA_DATA) && (!time_over_flag)); //wait data_dsa be high if(time_over_flag) goto read_exit; DSA_CLK = 1;
for(j = 0; j < 16; j ++) { dsa_time = DSATIME; while(DSA_STB && (!time_over_flag)); //wait stb_dsa be low if(time_over_flag) goto read_exit;
i *= 2; if(DSA_DATA) i++;
DSA_CLK = 0; //get this bit
dsa_time = DSATIME; while((!DSA_STB) && (!time_over_flag)); //wait stb_dsa be high if(time_over_flag) goto read_exit;
DSA_CLK = 1; }
dsa_time = DSATIME; while(DSA_CLK && (!time_over_flag)); //wait ack_dsa be low if(time_over_flag) goto read_exit;
DSA_DATA = 1; //pull up data_dsa DSA_STB = 0; //pull down stb_dsa
dsa_time = DSATIME; while((!DSA_CLK) && (!time_over_flag)); //wait ack_dsa be high if(time_over_flag) goto read_exit;
DSA_STB = 1; //pull up stb_dsa return(i);
read_exit: DSA_DATA = 1; DSA_STB = 1; DSA_CLK = 1; return(0xaa); }
unsigned char DSA_write(unsigned char command,unsigned char parameter) { unsigned int i = command * 256 + parameter; unsigned char j;
DSA_DATA = 0; time_over_flag = 0;
dsa_time = DSATIME; while(DSA_CLK && (!time_over_flag)); //wait ack_dsa be low if(time_over_flag) goto write_exit;
DSA_DATA = 1;
dsa_time = DSATIME; while((!DSA_CLK) && (!time_over_flag)); //wait ack_dsa be high if(time_over_flag) goto write_exit;
for(j = 0; j < 16; j ++) { DSA_DATA = (bit)(i & 0x8000); i <<= 1; DSA_STB = 0;
dsa_time = DSATIME; while(DSA_CLK && (!time_over_flag)); //wait ack_dsa be low if(time_over_flag) goto write_exit;
DSA_STB = 1; //pull up stb_dsa
dsa_time = DSATIME; while((!DSA_CLK) && (!time_over_flag)); //wait ack_dsa be high if(time_over_flag) goto write_exit; } DSA_DATA = 1; DSA_CLK = 0; //pull down ack_dsa
dsa_time = DSATIME; while(DSA_STB && (!time_over_flag)); //wait stb_dsa be low if(time_over_flag) goto write_exit;
DSA_CLK = 1; //pull up ack_dsa
dsa_time = DSATIME; while((!DSA_STB) && (!time_over_flag)); //wait stb_dsa be high if(time_over_flag) goto write_exit;
return(0x01);
write_exit: DSA_DATA = 1; DSA_STB = 1; DSA_CLK = 1; return(0xaa); } //Check the DSA if(dsa_time) { dsa_time --; if(!dsa_time) time_over_flag = 1; } |