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
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