hello,
i have written the code for DATA Aquisition CHANNEL for 4 channels.
In which i'm using AD574 adc,AT89C52 microcontroller,4051 multiplixer & 1pal IC(16v8).here i'm selecting channels from multiplixer & which ever channel is selected acc to tht,adc converts the data of tht particular channel.pal ic is used just to control some pins of AD574.
the problem is that-
my code is working fine in my keil debugger but in actual hardware it is not givg any response although all my old module r working fine.
but if i hide that while condition in main (while ch==0)
than my hardware works but as usual just 1 time.but i want to convert the data & store it too in an array before sending query to the slave.
So can any one help me in this.the code is given here below................
#include <AT89X52.H>
sbit read = P3^7; //to read pal IC
sbit write = P3^6; //to write pal IC
sbit a0 = P1^4; //connected to A0 of AD574(adc) IC
sbit rd_conv = P1^5; //connected to read/conversion of AD574(adc) IC
sbit status = P3^5; //connected to status of AD574(adc) IC
sbit a15 = P2^7; //for controlling pal ic
sbit a14 = P2^6; //for controlling pal ic
sbit a13 = P2^5; //for controlling pal ic
bit recv_flag,f,v,t,m,excep = 0,crcok = 0,m,ch=0; //used as software flags
char work_data,recv;
char epcode,ct = 0,nt = 0;
static char count,n = 0,channel=0;
idata unsigned char store[8] _at_ 0x0078; //modification done on 15-10-05
unsigned char Recv_Buff[8],q_no[20],ex_code[5],h;
unsigned int crc_accum_LO,crc_accum_HO;
char Error_H,Error_L,data_no;
unsigned int crc_accum;
void crc_calc(unsigned char); //to calculate crc
void Exception_response(epcode); //gives exception response
void query(void),chk_query(void); //to recieve query & 2function is to check query
void reset(void),slave_resp(void); //s/w reset & 2function gives slave response
void mux_channel(); //multiplixer channel
void signal_conversion(); //for signal conversion
unsigned char data_n(unsigned char t); //for taking data frm particular address
void main()
{
TMOD = 0x20;
SCON = 0x50;
P1 = 0xFF;
TH1 = 0xFD;
TL1 = 0xFD;
EA = 1;
ES = 1;
TR1 = 1;
status = 0;
a0 = 1;
rd_conv = 1;
write = 0;
a15 = 0;
a14 = 1;
a13 = 0;
write = 1;
while(1)
{
while(ch==0) //the problem lies here
signal_conversion();
query(); //to send query to slave if(count==8)
f = 1;
while(f==1)
{
char i;
crc_accum_LO = 0;
crc_accum_HO = 0;
crc_accum = 0xFFFF;
for(i=0;i<6;i++)
{
work_data = Recv_Buff;
_crc_calc(work_data);
}
crc_accum_LO = crc_accum & 0x00FF;
Error_L= crc_accum_LO;
crc_accum_HO = crc_accum & 0xFF00;
crc_accum_HO = crc_accum_HO>>8;
Error_H= crc_accum_HO;
chk_query();
channel = 0;
}
f = 0;
if(m==1)
{
m = 0;
reset();
}
}
}
void serial(void) interrupt 4
{
if(RI==1)
{
RI=0;
recv=0;
recv = SBUF;
recv_flag=1;
}
else
{
TI = 0;
if(t==1)
{
nt++;
if(nt==data_no)
{
nt = 0;
f = 0;
t = 0;
v = 0;
m = 1;
}
}
if(excep==1)
{
ct++;
if(ct==5)
{
ct = 0;
excep = 0;
f = 0;
v = 1;
m = 1;
}
}
}
}
void query(void)
{
while(recv_flag)
{
recv_flag = 0;
if(count<8)
Recv_Buff[count] = recv;
count++;
}
}
void chk_query(void)
{
if(Recv_Buff[1]!=0x03)
{
epcode = 01;
Exception_response(epcode);
}
if((Recv_Buff[2]>0xC3) || (Recv_Buff[3]>0xFF))
{
epcode = 02;
Exception_response(epcode);
}
if(v==0)
{
if((Error_L == Recv_Buff[6]) || (Error_H == Recv_Buff[7]))
{
crcok = 1;
slave_resp();
}
}
}
void crc_calc(unsigned char work)
{
int genpoly = 0xA001;
char i;
crc_accum = crc_accum ^(unsigned int)work;
for(i=8;i>0;i--)
{
if ((crc_accum) & 0x0001)
crc_accum = ((crc_accum) >> 1) ^ genpoly;
else
(crc_accum) >>= 1;
}
}
void Exception_response(epcode)
{
bit c = 1;
excep = 1;
switch(epcode)
{
char i;
case 1:
for(i=0;i<3;i++)
{
switch(i)
{
case 0:
ex_code = Recv_Buff[0];
break;
case 1:
ex_code = 0x83;
break;
case 2:
ex_code = 0x01;
}
}
break;
case 2:
for(i=0;i<3;i++)
{
switch(i)
{
case 0:
ex_code = Recv_Buff[0];
break;
case 1:
ex_code = 0x83;
break;
case 2:
ex_code = 0x02;
}
}
break;
case 3:
for(i=0;i<3;i++)
{
switch(i)
{
case 0:
ex_code = Recv_Buff[0];
break;
case 1:
ex_code = 0x83;
break;
case 2:
ex_code = 0x03;
}
}
break;
case 4:
for(i=0;i<3;i++)
{
switch(i)
{
case 0:
ex_code = Recv_Buff[0];
break;
case 1:
ex_code = 0x83;
break;
case 2:
ex_code = 0x04;
}
}
}
if(c==1)
{
char j;
c = 0;
crc_accum_LO = 0;
crc_accum_HO = 0;
crc_accum = 0xFFFF;
for(j=0;j<3;j++)
{
work_data = ex_code[j];
_crc_calc(work_data);
}
crc_accum_HO = crc_accum & 0x00FF;
ex_code[3] = crc_accum_HO;
crc_accum_LO = crc_accum & 0xFF00;
crc_accum_LO = crc_accum_LO>>8;
ex_code[4] = crc_accum_LO;
}
while(excep==1)
{
unsigned char k;
SBUF = ex_code[ct];
for(k=0;k<255;k++);
}
}
void mux_channel()
{
channel++;
/* while(status==1)
{*/
switch(channel)
{
case 1:
P1_3 = 0;
P1_2 = 0;
P1_1 = 0;
P1_0 = 1;
break;
/* case 2:
P1_3 = 0;
P1_2 = 0;
P1_1 = 1;
P1_0 = 0;
break;
case 3:
P1_3 = 0;
P1_2 = 0;
P1_1 = 1;
P1_0 = 1;
break;*/
default:
ch = 1;
// }
}
}
void signal_conversion()
{
char l;
P1_3 = 0; // Enable pin of 4051 is low
if(channel==0) //check whether n=0 or not
{
P1_3 = 0; //Select channel 0
P1_2 = 0;
P1_1 = 0;
P1_0 = 0;
}
l = channel;
rd_conv = 0; //Clear Conversion input
read = 0; //Clear read input
a15 = 0; //Clear A15 input
a14 = 1; //Set A14 input
a13 = 0; //Clear A13 input
status = 1; //Set status input
a0 = 1; //Set A0 input
while(status==1) //Remain inside this loop uptill Status = 1
{
mux_channel(); //Select Channel No.
}
read = 0; //Clear read input
P1_7 = 0; //Clear Enable Pin of 245Buffer
a0 = 0; //Clear A0 input
rd_conv = 1; //Set Conversion input
store[l] = P0;
}
void reset(void)
{
char k;
m = 0;
for(k=0;k<8;k++)
{
Recv_Buff[k] = 0;
q_no[k] = 0;
ex_code[k] = store[k] = 0;
}
count = 0;
// P0 = 0x00;
P1 = 0xFF;
ct = nt = 0;
epcode = h = t = v = ch = recv_flag = f = excep = crcok = 0;
channel = 0;
}
void slave_resp(void)
{
unsigned char k,j;
char reg,v;
q_no[0] = Recv_Buff[0];
q_no[1] = Recv_Buff[1];
reg = Recv_Buff[5];
q_no[2] = (Recv_Buff[5])*2;
// signal_conversion();
switch(reg)
{
case 1:
data_no = 7;
q_no[3] = 0x00;
v = Recv_Buff[3];
q_no[4] = data_n(v);
break;
case 2:
data_no = 9;
q_no[3] = 0x00;
v = Recv_Buff[3];
q_no[4] = data_n(v);
q_no[5] = 0x00;
v++;
q_no[6] = data_n(v);
// }
/* q_no[4] = data_n(&v);
v++;
q_no[5] = 0x00;
q_no[6] = data_n(&v);*/ //modif on 09/12/05
/* q_no[4] = store[0];
q_no[5] = 0x00;
q_no[6] = store[1];*/
/* break;
case 3:
data_no = 11;
q_no[3] = 0x00;
v = Recv_Buff[3];
q_no[4] = data_n(&v);
v++;
q_no[5] = 0x00;
q_no[6] = data_n(&v);
v++;
q_no[7] = 0x00;
q_no[8] = data_n(&v);
/* q_no[4] = store[0];
q_no[5] = 0x00;
q_no[6] = store[1];
q_no[7] = 0x00;
q_no[8] = store[2];*/
/* break;
case 4:
data_no = 13;
q_no[3] = 0x00;
v = Recv_Buff[3];
q_no[4] = data_n(&v);
v++;
q_no[5] = 0x00;
q_no[6] = data_n(&v);
v++;
q_no[7] = 0x00;
q_no[8] = data_n(&v);
v++;
q_no[9] = 0x00;
q_no[10] = data_n(&v);
/* q_no[4] = store[0];
q_no[5] = 0x00;
q_no[6] = store[1];
q_no[7] = 0x00;
q_no[8] = store[2];
q_no[9] = 0x00;
q_no[10] = store[3];*/
// break;
/* case 5:
data_no = 15;
break;
case 6:
data_no = 17;
break;
case 7:
data_no = 19;*/
}
/* for(i=3;i<data_no - 2;i++)
{
q_no = 0x00;
q_no[i+1] = store[reg-1];
}
crc_accum_LO = 0;
crc_accum_HO = 0;
crc_accum = 0xFFFF;
for(j=0;j<5;j++)
{
work_data = q_no[j];
_crc_calc(work_data);
}
crc_accum_HO = crc_accum & 0x00FF;
q_no[5] = crc_accum_HO;
crc_accum_LO = crc_accum & 0xFF00;
crc_accum_LO = crc_accum_LO>>8;
q_no[6] = crc_accum_LO;
t = 1;
while(t==1)
{
SBUF = q_no[nt];
for(k=0;k<255;k++);
}*/
crc_accum_LO = 0;
crc_accum_HO = 0;
crc_accum = 0xFFFF;
reg = data_no - 2;
for(j=0;j<reg;j++)
{
work_data = q_no[j];
_crc_calc(work_data);
}
crc_accum_HO = crc_accum & 0x00FF;
q_no[data_no - 2] = crc_accum_HO;
crc_accum_LO = crc_accum & 0xFF00;
crc_accum_LO = crc_accum_LO>>8;
q_no[data_no - 1] = crc_accum_LO;
t = 1;
while(t==1)
{
SBUF = q_no[nt];
for(k=0;k<255;k++);
}
}
unsigned char data_n(unsigned char t)
{
// unsigned char x;
switch(t)
{
case 0x78:
return(store[0]);
break;
case 0x79:
return(store[1]);
break;
case 0x7A:
return(store[2]);
break;
case 0x7B:
return(store[3]);
}
}
i have written the code for DATA Aquisition CHANNEL for 4 channels.
In which i'm using AD574 adc,AT89C52 microcontroller,4051 multiplixer & 1pal IC(16v8).here i'm selecting channels from multiplixer & which ever channel is selected acc to tht,adc converts the data of tht particular channel.pal ic is used just to control some pins of AD574.
the problem is that-
my code is working fine in my keil debugger but in actual hardware it is not givg any response although all my old module r working fine.
but if i hide that while condition in main (while ch==0)
than my hardware works but as usual just 1 time.but i want to convert the data & store it too in an array before sending query to the slave.
So can any one help me in this.the code is given here below................
#include <AT89X52.H>
sbit read = P3^7; //to read pal IC
sbit write = P3^6; //to write pal IC
sbit a0 = P1^4; //connected to A0 of AD574(adc) IC
sbit rd_conv = P1^5; //connected to read/conversion of AD574(adc) IC
sbit status = P3^5; //connected to status of AD574(adc) IC
sbit a15 = P2^7; //for controlling pal ic
sbit a14 = P2^6; //for controlling pal ic
sbit a13 = P2^5; //for controlling pal ic
bit recv_flag,f,v,t,m,excep = 0,crcok = 0,m,ch=0; //used as software flags
char work_data,recv;
char epcode,ct = 0,nt = 0;
static char count,n = 0,channel=0;
idata unsigned char store[8] _at_ 0x0078; //modification done on 15-10-05
unsigned char Recv_Buff[8],q_no[20],ex_code[5],h;
unsigned int crc_accum_LO,crc_accum_HO;
char Error_H,Error_L,data_no;
unsigned int crc_accum;
void crc_calc(unsigned char); //to calculate crc
void Exception_response(epcode); //gives exception response
void query(void),chk_query(void); //to recieve query & 2function is to check query
void reset(void),slave_resp(void); //s/w reset & 2function gives slave response
void mux_channel(); //multiplixer channel
void signal_conversion(); //for signal conversion
unsigned char data_n(unsigned char t); //for taking data frm particular address
void main()
{
TMOD = 0x20;
SCON = 0x50;
P1 = 0xFF;
TH1 = 0xFD;
TL1 = 0xFD;
EA = 1;
ES = 1;
TR1 = 1;
status = 0;
a0 = 1;
rd_conv = 1;
write = 0;
a15 = 0;
a14 = 1;
a13 = 0;
write = 1;
while(1)
{
while(ch==0) //the problem lies here
signal_conversion();
query(); //to send query to slave if(count==8)
f = 1;
while(f==1)
{
char i;
crc_accum_LO = 0;
crc_accum_HO = 0;
crc_accum = 0xFFFF;
for(i=0;i<6;i++)
{
work_data = Recv_Buff;
_crc_calc(work_data);
}
crc_accum_LO = crc_accum & 0x00FF;
Error_L= crc_accum_LO;
crc_accum_HO = crc_accum & 0xFF00;
crc_accum_HO = crc_accum_HO>>8;
Error_H= crc_accum_HO;
chk_query();
channel = 0;
}
f = 0;
if(m==1)
{
m = 0;
reset();
}
}
}
void serial(void) interrupt 4
{
if(RI==1)
{
RI=0;
recv=0;
recv = SBUF;
recv_flag=1;
}
else
{
TI = 0;
if(t==1)
{
nt++;
if(nt==data_no)
{
nt = 0;
f = 0;
t = 0;
v = 0;
m = 1;
}
}
if(excep==1)
{
ct++;
if(ct==5)
{
ct = 0;
excep = 0;
f = 0;
v = 1;
m = 1;
}
}
}
}
void query(void)
{
while(recv_flag)
{
recv_flag = 0;
if(count<8)
Recv_Buff[count] = recv;
count++;
}
}
void chk_query(void)
{
if(Recv_Buff[1]!=0x03)
{
epcode = 01;
Exception_response(epcode);
}
if((Recv_Buff[2]>0xC3) || (Recv_Buff[3]>0xFF))
{
epcode = 02;
Exception_response(epcode);
}
if(v==0)
{
if((Error_L == Recv_Buff[6]) || (Error_H == Recv_Buff[7]))
{
crcok = 1;
slave_resp();
}
}
}
void crc_calc(unsigned char work)
{
int genpoly = 0xA001;
char i;
crc_accum = crc_accum ^(unsigned int)work;
for(i=8;i>0;i--)
{
if ((crc_accum) & 0x0001)
crc_accum = ((crc_accum) >> 1) ^ genpoly;
else
(crc_accum) >>= 1;
}
}
void Exception_response(epcode)
{
bit c = 1;
excep = 1;
switch(epcode)
{
char i;
case 1:
for(i=0;i<3;i++)
{
switch(i)
{
case 0:
ex_code = Recv_Buff[0];
break;
case 1:
ex_code = 0x83;
break;
case 2:
ex_code = 0x01;
}
}
break;
case 2:
for(i=0;i<3;i++)
{
switch(i)
{
case 0:
ex_code = Recv_Buff[0];
break;
case 1:
ex_code = 0x83;
break;
case 2:
ex_code = 0x02;
}
}
break;
case 3:
for(i=0;i<3;i++)
{
switch(i)
{
case 0:
ex_code = Recv_Buff[0];
break;
case 1:
ex_code = 0x83;
break;
case 2:
ex_code = 0x03;
}
}
break;
case 4:
for(i=0;i<3;i++)
{
switch(i)
{
case 0:
ex_code = Recv_Buff[0];
break;
case 1:
ex_code = 0x83;
break;
case 2:
ex_code = 0x04;
}
}
}
if(c==1)
{
char j;
c = 0;
crc_accum_LO = 0;
crc_accum_HO = 0;
crc_accum = 0xFFFF;
for(j=0;j<3;j++)
{
work_data = ex_code[j];
_crc_calc(work_data);
}
crc_accum_HO = crc_accum & 0x00FF;
ex_code[3] = crc_accum_HO;
crc_accum_LO = crc_accum & 0xFF00;
crc_accum_LO = crc_accum_LO>>8;
ex_code[4] = crc_accum_LO;
}
while(excep==1)
{
unsigned char k;
SBUF = ex_code[ct];
for(k=0;k<255;k++);
}
}
void mux_channel()
{
channel++;
/* while(status==1)
{*/
switch(channel)
{
case 1:
P1_3 = 0;
P1_2 = 0;
P1_1 = 0;
P1_0 = 1;
break;
/* case 2:
P1_3 = 0;
P1_2 = 0;
P1_1 = 1;
P1_0 = 0;
break;
case 3:
P1_3 = 0;
P1_2 = 0;
P1_1 = 1;
P1_0 = 1;
break;*/
default:
ch = 1;
// }
}
}
void signal_conversion()
{
char l;
P1_3 = 0; // Enable pin of 4051 is low
if(channel==0) //check whether n=0 or not
{
P1_3 = 0; //Select channel 0
P1_2 = 0;
P1_1 = 0;
P1_0 = 0;
}
l = channel;
rd_conv = 0; //Clear Conversion input
read = 0; //Clear read input
a15 = 0; //Clear A15 input
a14 = 1; //Set A14 input
a13 = 0; //Clear A13 input
status = 1; //Set status input
a0 = 1; //Set A0 input
while(status==1) //Remain inside this loop uptill Status = 1
{
mux_channel(); //Select Channel No.
}
read = 0; //Clear read input
P1_7 = 0; //Clear Enable Pin of 245Buffer
a0 = 0; //Clear A0 input
rd_conv = 1; //Set Conversion input
store[l] = P0;
}
void reset(void)
{
char k;
m = 0;
for(k=0;k<8;k++)
{
Recv_Buff[k] = 0;
q_no[k] = 0;
ex_code[k] = store[k] = 0;
}
count = 0;
// P0 = 0x00;
P1 = 0xFF;
ct = nt = 0;
epcode = h = t = v = ch = recv_flag = f = excep = crcok = 0;
channel = 0;
}
void slave_resp(void)
{
unsigned char k,j;
char reg,v;
q_no[0] = Recv_Buff[0];
q_no[1] = Recv_Buff[1];
reg = Recv_Buff[5];
q_no[2] = (Recv_Buff[5])*2;
// signal_conversion();
switch(reg)
{
case 1:
data_no = 7;
q_no[3] = 0x00;
v = Recv_Buff[3];
q_no[4] = data_n(v);
break;
case 2:
data_no = 9;
q_no[3] = 0x00;
v = Recv_Buff[3];
q_no[4] = data_n(v);
q_no[5] = 0x00;
v++;
q_no[6] = data_n(v);
// }
/* q_no[4] = data_n(&v);
v++;
q_no[5] = 0x00;
q_no[6] = data_n(&v);*/ //modif on 09/12/05
/* q_no[4] = store[0];
q_no[5] = 0x00;
q_no[6] = store[1];*/
/* break;
case 3:
data_no = 11;
q_no[3] = 0x00;
v = Recv_Buff[3];
q_no[4] = data_n(&v);
v++;
q_no[5] = 0x00;
q_no[6] = data_n(&v);
v++;
q_no[7] = 0x00;
q_no[8] = data_n(&v);
/* q_no[4] = store[0];
q_no[5] = 0x00;
q_no[6] = store[1];
q_no[7] = 0x00;
q_no[8] = store[2];*/
/* break;
case 4:
data_no = 13;
q_no[3] = 0x00;
v = Recv_Buff[3];
q_no[4] = data_n(&v);
v++;
q_no[5] = 0x00;
q_no[6] = data_n(&v);
v++;
q_no[7] = 0x00;
q_no[8] = data_n(&v);
v++;
q_no[9] = 0x00;
q_no[10] = data_n(&v);
/* q_no[4] = store[0];
q_no[5] = 0x00;
q_no[6] = store[1];
q_no[7] = 0x00;
q_no[8] = store[2];
q_no[9] = 0x00;
q_no[10] = store[3];*/
// break;
/* case 5:
data_no = 15;
break;
case 6:
data_no = 17;
break;
case 7:
data_no = 19;*/
}
/* for(i=3;i<data_no - 2;i++)
{
q_no = 0x00;
q_no[i+1] = store[reg-1];
}
crc_accum_LO = 0;
crc_accum_HO = 0;
crc_accum = 0xFFFF;
for(j=0;j<5;j++)
{
work_data = q_no[j];
_crc_calc(work_data);
}
crc_accum_HO = crc_accum & 0x00FF;
q_no[5] = crc_accum_HO;
crc_accum_LO = crc_accum & 0xFF00;
crc_accum_LO = crc_accum_LO>>8;
q_no[6] = crc_accum_LO;
t = 1;
while(t==1)
{
SBUF = q_no[nt];
for(k=0;k<255;k++);
}*/
crc_accum_LO = 0;
crc_accum_HO = 0;
crc_accum = 0xFFFF;
reg = data_no - 2;
for(j=0;j<reg;j++)
{
work_data = q_no[j];
_crc_calc(work_data);
}
crc_accum_HO = crc_accum & 0x00FF;
q_no[data_no - 2] = crc_accum_HO;
crc_accum_LO = crc_accum & 0xFF00;
crc_accum_LO = crc_accum_LO>>8;
q_no[data_no - 1] = crc_accum_LO;
t = 1;
while(t==1)
{
SBUF = q_no[nt];
for(k=0;k<255;k++);
}
}
unsigned char data_n(unsigned char t)
{
// unsigned char x;
switch(t)
{
case 0x78:
return(store[0]);
break;
case 0x79:
return(store[1]);
break;
case 0x7A:
return(store[2]);
break;
case 0x7B:
return(store[3]);
}
}