View previous topic :: View next topic |
Author |
Message |
thanhhavdt
Joined: 09 Oct 2006 Posts: 12 Location: hoangsam
|
Help me: Why functions not return exactly value? |
Posted: Wed Nov 15, 2023 11:11 am |
|
|
Help me!!! Why funtions: read_5bit_tram_chuc_MHz_DKX();
read_4bit_donvi_MHz_DKX();
read_4bit_tram_kHz_DKX();
read_2bit_chuc_kHz_DKX(); not return exactly value in convert_data_DKX() function?
#include <16f887.h>
#device *=16
#device adc=10
#fuses nowdt,xt,noput,protect,nodebug,nobrownout,nolvp
#use delay(clock=4000000)
#include <internal_eeprom.c>
#bit ResetRom = 0x05.5 //RA5 (PIN 7)
#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
/* Dinh nghia cac chan cua IC74HC595 */
#define SHCP_PIN PIN_C0
#define DS_PIN PIN_C1
#define STCP_PIN PIN_C2
/* Dinh nghia cac chan cua ICMAX7219 */
byte Lo_Data_DKX ;
byte Hi_Data_DKX ;
int8 a;
int8 b;
int8 c;
int8 d;
int16 Data_DKX = 0;
void read_5bit_tram_chuc_MHz_DKX() //10-39
{
if(!(input(pin_b7)) &&(input(pin_b6)) && (input(pin_b5)) && (input(pin_b4))&& (input(pin_b3))&& (!input(pin_b2))) {a =10;break;} //10
else if(!(input(pin_b7)) &&(input(pin_b6)) && (input(pin_b5)) && (input(pin_b4))&& (!input(pin_b3))&& (input(pin_b2))) {a = 11;break;} //11
else if(!(input(pin_b7)) &&(input(pin_b6)) && (input(pin_b5)) && (input(pin_b4))&& (!input(pin_b3))&& (!input(pin_b2))) {a = 12;break;} //12
else if(!(input(pin_b7)) &&(input(pin_b6)) && (input(pin_b5)) && (!input(pin_b4))&& (input(pin_b3))&& (input(pin_b2))) {a = 13;break;} //13
else if(!(input(pin_b7)) &&(input(pin_b6)) && (input(pin_b5)) && (!input(pin_b4))&& (input(pin_b3))&& (!input(pin_b2))) {a = 14;break;} //14
else if(!(input(pin_b7)) &&(!input(pin_b6)) && (input(pin_b5)) && (!input(pin_b4))&& (!input(pin_b3))&& (!input(pin_b2))) {a = 22;break;} //22
else if(!(input(pin_b7)) &&(!input(pin_b6)) && (!input(pin_b5)) && (input(pin_b4))&& (input(pin_b3))&& (input(pin_b2))) {a = 23;break;} //23
else if(!(input(pin_b7)) &&(!input(pin_b6)) && (!input(pin_b5)) && (input(pin_b4))&& (input(pin_b3))&& (!input(pin_b2))) {a = 24;break;} //24
else if(!(input(pin_b7)) &&(!input(pin_b6)) && (!input(pin_b5)) && (input(pin_b4))&& (!input(pin_b3))&& (input(pin_b2))) {a = 25;break;} //25
else if(!(input(pin_b7)) &&(!input(pin_b6)) && (!input(pin_b5)) && (input(pin_b4))&& (!input(pin_b3))&& (!input(pin_b2))) {a = 26;break;} //26
else if(!(input(pin_b7)) &&(!input(pin_b6)) && (!input(pin_b5)) && (!input(pin_b4))&& (input(pin_b3))&& (input(pin_b2))) {a = 27;break;} //27
else if(!(input(pin_b7)) &&(!input(pin_b6)) && (!input(pin_b5)) && (!input(pin_b4))&& (input(pin_b3))&& (!input(pin_b2))) {a = 28;break;} //28
else if(!(input(pin_b7)) &&(!input(pin_b6)) && (!input(pin_b5)) && (!input(pin_b4))&& (!input(pin_b3))&& (input(pin_b2))) {a = 29;break;} //29
else if(!(input(pin_b7)) &&(input(pin_b6)) && (input(pin_b5)) && (!input(pin_b4))&& (!input(pin_b3))&& (input(pin_b2))) {a = 30;break;} //30
else if(!(input(pin_b7)) &&(input(pin_b6)) && (input(pin_b5)) && (!input(pin_b4))&& (!input(pin_b3))&& (!input(pin_b2))) {a = 31;break;} //31
else if(!(input(pin_b7)) &&(input(pin_b6)) && (!input(pin_b5)) && (input(pin_b4))&& (input(pin_b3))&& (input(pin_b2))) {a = 32;break;} //32
else if(!(input(pin_b7)) &&(input(pin_b6)) && (!input(pin_b5)) && (input(pin_b4))&& (input(pin_b3))&& (!input(pin_b2))) {a = 33;break;} //33
else if(!(input(pin_b7)) &&(input(pin_b6)) && (!input(pin_b5)) && (input(pin_b4))&& (!input(pin_b3))&& (input(pin_b2))) {a = 34;break;} //34
else if(!(input(pin_b7)) &&(input(pin_b6)) && (!input(pin_b5)) && (input(pin_b4))&& (!input(pin_b3))&& (!input(pin_b2))) {a = 35;break;} //35
else if(!(input(pin_b7)) &&(input(pin_b6)) && (!input(pin_b5)) && (!input(pin_b4))&& (input(pin_b3))&& (input(pin_b2))) {a = 36;break;} //36
else if(!(input(pin_b7)) &&(input(pin_b6)) && (!input(pin_b5)) && (!input(pin_b4))&& (input(pin_b3))&& (!input(pin_b2))) {a = 37;break;} //37
else if(!(input(pin_b7)) &&(input(pin_b6)) && (!input(pin_b5)) && (!input(pin_b4))&& (!input(pin_b3))&& (input(pin_b2))) {a = 38;break;} //38
else if(!(input(pin_b7)) &&(input(pin_b6)) && (!input(pin_b5)) && (!input(pin_b4))&& (!input(pin_b3))&& (!input(pin_b2))) {a = 39;break;} //39
else {a = 0;}
}
void read_4bit_donvi_MHz_DKX()
{
if((input(pin_b1)) && (input(pin_b0)) && (input(pin_d7))&& (input(pin_d6))) {b = 0;break;} //0
else if((input(pin_b1)) && (input(pin_b0)) && (input(pin_d7))&& (!input(pin_d6))) {b = 1;break;} //1
else if((input(pin_b1)) && (input(pin_b0)) && (!input(pin_d7))&& (input(pin_d6))) {b = 2;break;} //2
else if((input(pin_b1)) && (input(pin_b0)) && (!input(pin_d7))&& (!input(pin_d6))) {b = 3;break;} //3
else if((input(pin_b1)) && (!input(pin_b0)) && (input(pin_d7))&& (input(pin_d6))) {b = 4;break;} //4
else if((input(pin_b1)) && (!input(pin_b0)) && (input(pin_d7))&& (!input(pin_d6))) {b = 5;break;} //5
else if((input(pin_b1)) && (!input(pin_b0)) && (!input(pin_d7))&& (input(pin_d6))) {b = 6;break;} //6
else if((input(pin_b1)) && (!input(pin_b0)) && (!input(pin_d7))&& (!input(pin_d6))) {b = 7;break;} //7
else if((!input(pin_b1)) && (input(pin_b0)) && (input(pin_d7))&& (input(pin_d6))) {b = 8;break;} //8
else if((!input(pin_b1)) && (input(pin_b0)) && (input(pin_d7))&& (!input(pin_d6))) {b = 9;break;} //9
else {b = 0;}
}
void read_4bit_tram_kHz_DKX()
{
if((input(pin_d5)) && (input(pin_d4)) && (input(pin_d3))&& (input(pin_d2))) {c = 0;break;} //0
else if((input(pin_d5)) && (input(pin_d4)) && (input(pin_d3))&& (!input(pin_d2))) {c = 1;break;} //1
else if((input(pin_d5)) && (input(pin_d4)) && (!input(pin_d3))&& (input(pin_d2))) {c = 2;break;} //2
else if((input(pin_d5)) && (input(pin_d4)) && (!input(pin_d3))&& (!input(pin_d2))) {c = 3;break;} //3
else if((input(pin_d5)) && (!input(pin_d4)) && (input(pin_d3))&& (input(pin_d2))) {c = 4;break;} //4
else if((input(pin_d5)) && (!input(pin_d4)) && (input(pin_d3))&& (!input(pin_d2))) {c = 5;break;} //5
else if((input(pin_d5)) && (!input(pin_d4)) && (!input(pin_d3))&& (input(pin_d2))) {c = 6;break;} //6
else if((input(pin_d5)) && (!input(pin_d4)) && (!input(pin_d3))&& (!input(pin_d2))) {c = 7;break;} //7
else if((!input(pin_d5)) && (input(pin_d4)) && (input(pin_d3))&& (input(pin_d2))) {c = 8;break;} //8
else if((!input(pin_d5)) && (input(pin_d4)) && (input(pin_d3))&& (!input(pin_d2))) {c = 9;break;} //9
else {c = 0;}
}
void read_2bit_chuc_kHz_DKX()
{
if((input(pin_d1)) && (input(pin_d0))) {d = 0;break;} //00
else if((input(pin_d1)) && (!input(pin_d0))) {d = 25;break;} //25
else if((!input(pin_d1)) && (input(pin_d0))) {d = 50;break;} //50
else if((!input(pin_d1)) && (!input(pin_d0))) {d = 75;break;} //75
else {d = 0;}
}
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();
//a=10; b=0;c=0; d=0;
Data_DKX = ((10000*a)+(1000*(b+25))+(100*c)+d)/12.5;
// Data_DKX = 10000;
Hi_Data_DKX = (unsigned char) (Data_DKX / 256);
Lo_Data_DKX = (unsigned char) (Data_DKX % 256);
}
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 OUTPUT
TRISC=0x00; //PortC as OUTPUT
TRISD=0xFF; //PortD as OUTPUT
TRISE=0x00; //PortE as INPUT
PORTE=0x00; //
while(true)
{
send_data_DKX();
}
} |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9240 Location: Greensville,Ontario
|
|
Posted: Wed Nov 15, 2023 11:31 am |
|
|
My head hurts just looking at the code.....
Please simply just read the entire portB and use a 'mask' to compare to your test values.
You'll have clear, easy to read code, even for a 'dinosaur' like me ! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Wed Nov 15, 2023 11:47 am |
|
|
Agreed it is awful code.
However there is a very fundamental problem with this maths:
Data_DKX = ((10000*a)+(1000*(b+25))+(100*c)+d)/12.5;
The very first part of this returns values up to 39. You multiply this by
10000 using 16bit arithmetic (since 10000 is a 16bit value). 39*10000 =
390000. Largest value that can fit in an int16 is 65535. Instant overflow.
b should work, but then c, will be using int8 arithmetic (since 100 is an int8,
and c is an int8). Again overflow.
Fundamentally flawed maths. |
|
|
thanhhavdt
Joined: 09 Oct 2006 Posts: 12 Location: hoangsam
|
|
Posted: Thu Nov 16, 2023 6:29 am |
|
|
Thanks for support!!!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Thu Nov 16, 2023 8:13 am |
|
|
As a comment to this. Consider:
Code: |
void read_4bit_tram_kHz_DKX()
{
int val;
val=(input_d() & 0b111100)>>2; //mid four bits from port D
//Masked and Moved to the bottom
val^= 0b1111; //inverted
c=val;
}
|
Honestly also would be better to have the functions return the values
rather than using global values.
Same applies to each of the other functions |
|
|
thanhhavdt
Joined: 09 Oct 2006 Posts: 12 Location: hoangsam
|
|
Posted: Fri Nov 17, 2023 10:41 am |
|
|
Thanks for Ttelmah. I'm newbie, my code is awful. Can you help me rewrite the functions: read_5bit_tram_chuc_MHz_DKX();
read_4bit_donvi_MHz_DKX();
Thanks for support!!! |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Dec 13, 2023 10:54 am |
|
|
I don't think someone will write your code upon request, generally the help provided in this forum
is of an excellent level, while the consultation warrants it. If you are a newbie, I suggest you
take a look and learn how manages the data handler, that shift register logic circuit, file 74595.c
located in the CCS compiler Drivers folder.
best wishes _________________ Humber |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Dec 18, 2023 11:54 am |
|
|
Oh lordy.
Always remember when you're coding:
* If it's hard to read, it's hard to debug.
* If you had to look at this code 10yrs from now, how quickly would you be able to make sense of it?
* Compilers can have bugs. All kinds of bugs. Keep the code in manageable chunks because the error/issue you might be running into might not be the code, but some bug of the compiler -- and then look back at the first rule item I listed. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|