Hi guys I'm having trouble to get this code works. I want to multiply and divide and I use the following code: valor1=ADC0; valor1=valor1*2; valor1=valor1*12; valor1=valor1/5; valor1=valor1/4095; with valor1 as float but the variable valor1 never is the right number. what is wrong?? can be another way to do this multiplication: valor1*2*2.42/4095 Thanks
what do you mean, "never is the right number?" Is it close? Remember that floating-point calculations are not exact! http://www.8052.com/forum/read.phtml?id=82066
For example I read from ADC the value 925 and I expect to get in the valor1 variable the number 1.09 but I got 1.8 or 2.3 or 2.4 but never 1.0xx Please, another solution??
I'm not a fan of floating point but can you combine it all into one step and do something like this?
valor1 = ADC/853.3;
I just tried the following modified example:
void main (void) { float valor1; valor1=925; valor1=valor1*2; valor1=valor1*12; valor1=valor1/5; valor1=valor1/4095; while(1); }
just a guess how do you KNOW that the valor1=ADC0; put the same result in valor1 every time. My guess is that you think an ADC will read the same value for the same voltage every time, which it only will do if you have spent 1000+ hours on circuit design. Otherwise you WILL have some amount of noise. Erik
The curious thing is when i put breakpoints the value is correct in valor1 variable but when I jump this set of operations I got a wrong number. When I read the adc I read 925 and 924 values but when I don't put brakpoints the calculation is bad. The code is the following: void main (void) { //float temperature; long valoradc; xdata float valor1, valor2, valor3, valor4, valor5, valor6, valor7; WDTCN = 0xde; // disable watchdog timer WDTCN = 0xad; SYSCLK_Init (); // initialize oscillator PORT_Init (); // initialize crossbar and GPIO UART0_Init (); // initialize UART // sample rate ADC_Init (); // init ADC while(1) { SFRPAGE = ADC0_PAGE; AMX0SL = 0x00; delay_ms(1); AD0BUSY=1; while(AD0BUSY==1); AD0INT = 0; valor1=ADC0; valor1=valor1*2; valor1=valor1*12; valor1=valor1/5; valor1=valor1/4096; AMX0SL = 0x02; delay_ms(1); AD0BUSY=1; while(AD0BUSY==1); AD0INT = 0; valor2=ADC0; valor2=valor2*2; valor2=valor2*2.42/4096; AMX0SL = 0x04; delay_ms(1); AD0BUSY=1; while(AD0BUSY==1); AD0INT = 0; valor3=ADC0; valor3=valor3*2.42/4095; AMX0SL = 0x05; delay_ms(1); AD0BUSY=1; while(AD0BUSY==1); AD0INT = 0; valor4=ADC0; valor4=valor4*2.42/4095; AMX0SL = 0x06; delay_ms(1); AD0BUSY=1; while(AD0BUSY==1); AD0INT = 0; valor5=ADC0; valor5=valor5*2.42/4095; AMX0SL = 0x07; delay_ms(1); AD0BUSY=1; while(AD0BUSY==1); AD0INT = 0; valor6=ADC0; valor6=valor6*2.42/4095; SFRPAGE = UART0_PAGE; delay_ms(2000); printf ("El valor del adc diferencial 1 es %f V. \n", valor1); printf ("El valor del adc diferencial 2 es %f V. \n", valor2); printf ("El valor del adc 4 es %f V. \n", valor3); printf ("El valor del adc 5 es %f V. \n", valor4); printf ("El valor del adc 6 es %f V. \n", valor5); printf ("El valor del adc 7 es %f V. \n", valor6); } }
The curious thing is when i put breakpoints the value is correct in valor1 variable but when I jump this set of operations I got a wrong number. print both ADC result and result after calculation. I have a string feeling that is IS noise. Erik
"When I read the adc I read 925 and 924 values but when I don't put brakpoints the calculation is bad." Erik's point is: how do you know that it's the calculation that's bad? How do you know that you're not just reading a bad value from the ADC? As Erik said, noise could cause this. You could also have a timing problem, such that your ADC reading only works when you delay the code with a breakpoint! You might also have a faulty interrupt that is corrupting stuff... You need to save the actual value read from the ADC, and include that in your diagnostic printout.
Thanks guys but the problem was a delay between tha adc0 refresh value. I resolve the problem thanks