View previous topic :: View next topic |
Author |
Message |
thanhhavdt
Joined: 09 Oct 2006 Posts: 12 Location: hoangsam
|
Help me why function not return exactly value |
Posted: Tue Apr 16, 2024 10:45 am |
|
|
function: send_data_DKX(); return exactly value with read_Data_DKX_song_met(), but not with read_Data_DKX_song_deximet()?
example: when Data_DKX = 10000 (a=10; b=0;c=0;d=0) is true, but Data_DKX = 10001 (a=10; b=0;c=0;d=1) not true
Please help me
Code: | #include <16f887.h>
#device *=16
#device adc=10
#fuses nowdt,xt,noput,protect,nodebug,nobrownout,nolvp
#use delay(clock=4000000)
#byte PORTA = 0x05
#byte PORTB = 0x06
#byte PORTC = 0x07
#byte PORTD = 0x08
#byte PORTE = 0x09
#byte TRISA = 0x85
#byte TRISB = 0x86
#byte TRISC = 0x87
#byte TRISD = 0x88
#byte TRISE = 0x89
#bit ra0 = 0x05.0
#bit ra1 = 0x05.1
#bit ra2 = 0x05.2
#bit ra3 = 0x05.3
#bit ra4 = 0x05.4
#bit ra5 = 0x05.5
#bit rb0 = 0x06.0 //BIT THU 9
#bit rb1 = 0x06.1 //BIT THU 10
#bit rb2 = 0x06.2 //BIT THU 11
#bit rb3 = 0x06.3 //BIT THU 12
#bit rb4 = 0x06.4 //BIT THU 13
#bit rb5 = 0x06.5 //BIT THU 14
#bit rb6 = 0x06.6 //BIT THU 15
#bit rb7 = 0x06.7 //BIT THU 16
#bit rc0 = 0x07.0
#bit rc1 = 0x07.1
#bit rc2 = 0x07.2 //DAU SONG DEXIMET1 PIN_C2
#bit rc3 = 0x07.3 //DAU_SONG_MET PIN_C3
#bit rc4 = 0x07.4 //1PR.UPR PIN_C4
#bit rc5 = 0x07.5 //2PR.UPR PIN_C5
#bit rc6 = 0x07.6 //3PR.UPR PIN_C6
#bit rc7 = 0x07.7 //4PR.UPR PIN_C7
#bit rd0 = 0x08.0 //BIT THU 1
#bit rd1 = 0x08.1 //BIT THU 2
#bit rd2 = 0x08.2 //BIT THU 3
#bit rd3 = 0x08.3 //BIT THU 4
#bit rd4 = 0x08.4 //BIT THU 5
#bit rd5 = 0x08.5 //BIT THU 6
#bit rd6 = 0x08.6 //BIT THU 7
#bit rd7 = 0x08.7 //BIT THU 8
#bit re0 = 0x09.0 // DS_PIN
#bit re1 = 0x09.1 //SHCP_PIN
#bit re2 = 0x09.2 //STCP_PIN
/* Dinh nghia cac chan cua IC74HC595 */
#define SHCP_PIN PIN_E1
#define DS_PIN PIN_E0
#define STCP_PIN PIN_E2
byte Lo_Data_DKX ;
byte Hi_Data_DKX ;
int16 a;
int16 b;
int16 c;
int16 d;
int16 Data_DKX = 0;
int16 Data_CHIA = 0;
int16 Data = 0;
void read_5bit_tram_chuc_MHz_DKX() //10-39
{
if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==1) && (input(pin_b4)==1) && (input(pin_b3)==1) && (input(pin_b2)==0)) {a =10;break;} //10
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==1) && (input(pin_b4)==1) && (input(pin_b3)==0) && (input(pin_b2)==1)) {a = 11;break;} //11
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==1) && (input(pin_b4)==1) && (input(pin_b3)==0) && (input(pin_b2)==0)) {a = 12;break;} //12
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==1) && (input(pin_b4)==0) && (input(pin_b3)==1) && (input(pin_b2)==1)) {a = 13;break;} //13
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==1) && (input(pin_b4)==0) && (input(pin_b3)==1) && (input(pin_b2)==0)) {a = 14;break;} //14
else if((input(pin_b7)==0) &&(input(pin_b6)==0) && (input(pin_b5)==1) && (input(pin_b4)==0) && (input(pin_b3)==0) && (input(pin_b2)==0)) {a = 22;break;} //22
else if((input(pin_b7)==0) &&(input(pin_b6)==0) && (input(pin_b5)==0) && (input(pin_b4)==1) && (input(pin_b3)==1) && (input(pin_b2)==1)) {a = 23;break;} //23
else if((input(pin_b7)==0) &&(input(pin_b6)==0) && (input(pin_b5)==0) && (input(pin_b4)==1) && (input(pin_b3)==1) && (input(pin_b2)==0)) {a = 24;break;} //24
else if((input(pin_b7)==0) &&(input(pin_b6)==0) && (input(pin_b5)==0) && (input(pin_b4)==1) && (input(pin_b3)==0) && (input(pin_b2)==1)) {a = 25;break;} //25
else if((input(pin_b7)==0) &&(input(pin_b6)==0) && (input(pin_b5)==0) && (input(pin_b4)==1) && (input(pin_b3)==0) && (input(pin_b2)==0)) {a = 26;break;} //26
else if((input(pin_b7)==0) &&(input(pin_b6)==0) && (input(pin_b5)==0) && (input(pin_b4)==0) && (input(pin_b3)==1) && (input(pin_b2)==1)) {a = 27;break;} //27
else if((input(pin_b7)==0) &&(input(pin_b6)==0) && (input(pin_b5)==0) && (input(pin_b4)==0) && (input(pin_b3)==1) && (input(pin_b2)==0)) {a = 28;break;} //28
else if((input(pin_b7)==0) &&(input(pin_b6)==0) && (input(pin_b5)==0) && (input(pin_b4)==0) && (input(pin_b3)==0) && (input(pin_b2)==1)) {a = 29;break;} //29
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==1) && (input(pin_b4)==0) && (input(pin_b3)==0) && (input(pin_b2)==1)) {a = 30;break;} //30
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==1) && (input(pin_b4)==0) && (input(pin_b3)==0) && (input(pin_b2)==0)) {a = 31;break;} //31
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==0) && (input(pin_b4)==1) && (input(pin_b3)==1) && (input(pin_b2)==1)) {a = 32;break;} //32
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==0) && (input(pin_b4)==1) && (input(pin_b3)==1) && (input(pin_b2)==0)) {a = 33;break;} //33
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==0) && (input(pin_b4)==1) && (input(pin_b3)==0) && (input(pin_b2)==1)) {a = 34;break;} //34
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==0) && (input(pin_b4)==1) && (input(pin_b3)==0) && (input(pin_b2)==0)) {a = 35;break;} //35
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==0) && (input(pin_b4)==0) && (input(pin_b3)==1) && (input(pin_b2)==1)) {a = 36;break;} //36
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==0) && (input(pin_b4)==0) && (input(pin_b3)==1) && (input(pin_b2)==0)) {a = 37;break;} //37
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==0) && (input(pin_b4)==0) && (input(pin_b3)==0) && (input(pin_b2)==1)) {a = 38;break;} //38
else if((input(pin_b7)==0) &&(input(pin_b6)==1) && (input(pin_b5)==0) && (input(pin_b4)==0) && (input(pin_b3)==0) && (input(pin_b2)==0)) {a = 39;break;} //39
else {a = 0;}
//! int val_a;
//! val_a=(input_b() & 0b11111100)>>2; //mid four bits from port D
//! //Masked and Moved to the bottom
}
void read_4bit_donvi_MHz_DKX()
{
//! if((input(pin_b1)==1) && (input(pin_b0)==1) && (input(pin_d7)==1) && (input(pin_d6)==1)) {b = 0;break;} //0
//! else if((input(pin_b1)==1) && (input(pin_b0)==1) && (input(pin_d7)==1) && (input(pin_d6)==0)) {b = 1;break;} //1
//! else if((input(pin_b1)==1) && (input(pin_b0)==1) && (input(pin_d7)==0) && (input(pin_d6)==1)) {b = 2;break;} //2
//! else if((input(pin_b1)==1) && (input(pin_b0)==1) && (input(pin_d7)==0) && (input(pin_d6)==0)) {b = 3;break;} //3
//! else if((input(pin_b1)==1) && (input(pin_b0)==0) && (input(pin_d7)==1) && (input(pin_d6)==1)) {b = 4;break;} //4
//! else if((input(pin_b1)==1) && (input(pin_b0)==0) && (input(pin_d7)==1) && (input(pin_d6)==0)) {b = 5;break;} //5
//! else if((input(pin_b1)==1) && (input(pin_b0)==0) && (input(pin_d7)==0) && (input(pin_d6)==1)) {b = 6;break;} //6
//! else if((input(pin_b1)==1) && (input(pin_b0)==0) && (input(pin_d7)==0) && (input(pin_d6)==0)) {b = 7;break;} //7
//! else if((input(pin_b1)==0) && (input(pin_b0)==1) && (input(pin_d7)==1) && (input(pin_d6)==1)) {b = 8;break;} //8
//! else if((input(pin_b1)==0) && (input(pin_b0)==1) && (input(pin_d7)==1) && (input(pin_d6)==0)) {b = 9;break;} //9
//! else {b = 0;}
int val_b, val11, val12;
val11= (input_b() & 0b11)<<2;
val12= ((input_d() & 0b11000000))>>6;
val_b = val11|val12;
//Masked and Moved to the bottom
val_b^= 0b1111; //b = 0; 1; 2; 3; 4; 5; 6; 7; 8; 9
b=val_b;
}
void read_4bit_tram_kHz_DKX()
{
//! if((input(pin_d5)==1) && (input(pin_d4)==1) && (input(pin_d3)==1) && (input(pin_d2)==1)) {c = 0;break;} //0
//! else if((input(pin_d5)==1) && (input(pin_d4)==1) && (input(pin_d3)==1) && (input(pin_d2)==0)) {c = 1;break;} //1
//! else if((input(pin_d5)==1) && (input(pin_d4)==1) && (input(pin_d3)==0) && (input(pin_d2)==1)) {c = 2;break;} //2
//! else if((input(pin_d5)==1) && (input(pin_d4)==1) && (input(pin_d3)==0) && (input(pin_d2)==0)) {c = 3;break;} //3
//! else if((input(pin_d5)==1) && (input(pin_d4)==0) && (input(pin_d3)==1) && (input(pin_d2)==1)) {c = 4;break;} //4
//! else if((input(pin_d5)==1) && (input(pin_d4)==0) && (input(pin_d3)==1) && (input(pin_d2)==0)) {c = 5;break;} //5
//! else if((input(pin_d5)==1) && (input(pin_d4)==0) && (input(pin_d3)==0) && (input(pin_d2)==1)) {c = 6;break;} //6
//! else if((input(pin_d5)==1) && (input(pin_d4)==0) && (input(pin_d3)==0) && (input(pin_d2)==0)) {c = 7;break;} //7
//! else if((input(pin_d5)==0) && (input(pin_d4)==1) && (input(pin_d3)==1) && (input(pin_d2)==1)) {c = 8;break;} //8
//! else if((input(pin_d5)==0) && (input(pin_d4)==1) && (input(pin_d3)==1) && (input(pin_d2)==0)) {c = 9;break;} //9
//! else {c = 0;}
int val_c;
val_c=(input_d() & 0b111100)>>2; //mid four bits from port D
//Masked and Moved to the bottom
val_c^= 0b1111; // c = 0; 1; 2; 3; 4; 5; 6; 7; 8; 9
c=val_c;
}
void read_2bit_chuc_kHz_DKX()
{
//! if(10<=a<=14) // Song met
//! {
//! if((input(pin_d1)==1) && (input(pin_d0)==1)) {d = 0;break;} //00
//! else if((input(pin_d1)==1) && (input(pin_d0)==0)) {d = 2;break;} //25/12.5
//! else if((input(pin_d1)==0) && (input(pin_d0)==1)) {d = 4;break;} //50/12.5
//! else if((input(pin_d1)==0) && (input(pin_d0)==0)) {d = 6;break;} //75/12.5
//! }
//! else if(22<=a<40)// Song deximet1
//! {
//! if((input(pin_d1)==1) && (input(pin_d0)==1)) {d = 0;break;} //00
//! else if((input(pin_d1)==1) && (input(pin_d0)==0)) {d = 1;break;} //25*2/12.5
//! else if((input(pin_d1)==0) && (input(pin_d0)==1)) {d = 2;break;} //50*2/12.5
//! else if((input(pin_d1)==0) && (input(pin_d0)==0)) {d = 3;break;} //75*2/12.5
//! }
//!
//! else {d = 0;}
int val_d;
if(10<=a<=14) // Song met
{
val_d=(input_d() & 0b00000011)<<1;
//Masked and Moved to the bottom
val_d^= 0b0110; // nhan gia tri : 0; 2; 4; 6
d=val_d;
}
else if(22<=a<40)// Song deximet1
{
val_d=(input_d() & 0b00000011);
//Masked and Moved to the bottom
val_d^= 0b0011; //nhan gia tri : 0; 1; 2; 3
d=val_d;
}
}
#separate
void read_Data_DKX_song_met()
{
if((input(pin_b5)==1) && ((input(pin_b4)==1) || (input(pin_b3)==1))) // Song met
{
Data_DKX = (((800*a)+(80*(b+25))+(8*c)+d)); Data_CHIA = (Data_DKX/2); //Data_DKX = ((10000*a)+(1000*(b+25))+(100*c)+d)/12.5;
Hi_Data_DKX=(((Data_CHIA/1000) %10) <<4) | (Data_CHIA/100)%10;
Lo_Data_DKX = ((((Data_CHIA / 10) % 10) << 4) | (Data_CHIA % 10));break;
}
}
#separate
void read_Data_DKX_song_deximet()
{
if((input(pin_b6)==0)&&(!((input(pin_b5)==1) && ((input(pin_b4)==1) || (input(pin_b3)==1)))))// Song deximet1
{
Data_DKX = (((400*a)+(40*(b+45))+(4*c)+d)); //Data_DKX = ((10000*a)+(1000*(b+45))+(100*c)+d)/25;
Data_CHIA = (Data_DKX/2);
Hi_Data_DKX=(((Data_CHIA/1000) %10) <<4) | (Data_CHIA/100)%10;
Lo_Data_DKX = ((((Data_CHIA / 10) % 10) << 4) | (Data_CHIA % 10));break;
}
else if((input(pin_b6)==1)&&(!((input(pin_b5)==1) && ((input(pin_b4)==1) || (input(pin_b3)==1))))) // Song deximet2
{
Data_DKX = ((((400*a)+(40*(b))+(4*c)+d))-1800); //Data_DKX = ((10000*a)+(1000*(b-45))+(100*c)+d)/25;
Data_CHIA = (Data_DKX/2);
Hi_Data_DKX=(((Data_CHIA/1000) %10) <<4) | (Data_CHIA/100)%10;
Lo_Data_DKX = ((((Data_CHIA / 10) % 10) << 4) | (Data_CHIA % 10));break;
}
}
void read_Data_DKX()
{
read_Data_DKX_song_met();
read_Data_DKX_song_deximet();
}
void convert_data_DKX()
{
read_5bit_tram_chuc_MHz_DKX();
read_4bit_donvi_MHz_DKX();
read_4bit_tram_kHz_DKX();
read_2bit_chuc_kHz_DKX();
read_Data_DKX();
}
void IC_74hc595(int data)
{
int i;
output_low(SHCP_PIN);
for(i=0;i<=7;i++)
{
if((data & 0x80)==0)
{output_low(DS_PIN);
delay_us(50);}
else
output_high(DS_PIN);
data=data<<1;
output_high(SHCP_PIN);
output_low(SHCP_PIN);
}
}
void IC_74HC595_Output()
{
output_low(STCP_PIN);
delay_ms(50);
output_high(STCP_PIN);
output_low(STCP_PIN);
}
void write_to_595 (byte Lo_Data_DKX, Hi_Data_DKX)
{
IC_74hc595(Hi_Data_DKX);
IC_74hc595(Lo_Data_DKX);
IC_74HC595_Output();
}
void send_data_DKX()
{
convert_data_DKX();
write_to_595(Lo_Data_DKX, Hi_Data_DKX);
}
void main(void)
{
TRISA=0x00; //PortA as OUTPUT, RA5 as INPUT
TRISB=0xFF; //PortB as INPUT
TRISC=0x00; //PortC as OUTPUT
TRISD=0xFF; //PortD as INPUT
TRISE=0x00; //PortE as OUTPUT
PORTC=0; //
PORTE=0;
while(true)
{
send_data_DKX();
}
}[/u] |
|
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 290
|
|
Posted: Tue Apr 16, 2024 12:14 pm |
|
|
Your comment and function do not match.
Quote: | Data_DKX = (((400*a)+(40*(b+45))+(4*c)+d)); //Data_DKX = ((10000*a)+(1000*(b+45))+(100*c)+d)/25; |
If the part in the commented section is correct, then it appears you divided each constant by 25 except for the constant of 1 for d. |
|
|
thanhhavdt
Joined: 09 Oct 2006 Posts: 12 Location: hoangsam
|
|
Posted: Wed Apr 17, 2024 7:18 am |
|
|
gaugeguy wrote: | Your comment and function do not match.
Quote: | Data_DKX = (((400*a)+(40*(b+45))+(4*c)+d)); //Data_DKX = ((10000*a)+(1000*(b+45))+(100*c)+d)/25; |
If the part in the commented section is correct, then it appears you divided each constant by 25 except for the constant of 1 for d. |
Sorry, comment is:
Data_DKX = (((400*a)+(40*(b+45))+(4*c)+d)); //Data_DKX = (((10000*a)+(1000*(b+45))+(100*c))/25)+d;
and Data_DKX = ((((400*a)+(40*(b))+(4*c)+d))-1800); //Data_DKX = (((10000*a)+(1000*(b-45))+(100*c))/25)+d; |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9121 Location: Greensville,Ontario
|
|
Posted: Wed Apr 17, 2024 9:09 am |
|
|
It'd be easier to see your error if you break down the equation !
ie
answer=first math term 'a'
answer =answer + 2nd math term 'b'
answer =answer + 3rd math term 'c'
answer=answer + 4th math term 'd'
Having it in one long line of code can be very confusing and hard to read.
Now once you have the correct code, you could reduce to one line.
Also put comments at the end of lines of code. Old habit from assembler days but it really,really helps and cost you nothing. Handy for when you ask 'is this right ?' or 3 days later when you ask yourself WHY did I code this mess ?? |
|
|
thanhhavdt
Joined: 09 Oct 2006 Posts: 12 Location: hoangsam
|
|
Posted: Wed Apr 17, 2024 6:12 pm |
|
|
In read_Data_DKX_song_met(), Data_DKX= 10000; 10002; ....13400
but in read_Data_DKX_song_deximet(), Data_DKX= 10001; 10002; ....13999
function read_Data_DKX_song_deximet() return exactly value with even number (Data_DKX= 10000; 10002; ....) but not with odd number (Data_DKX= 10001; 10003; ....). Please heplme |
|
|
|