#include <18F2550.h>
#DEVICE ADC=8
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN
#use delay
(clock=
48000000)#include <bootloader.h>
#include <math.h>
#fuses HS,PUT,NOPROTECT,NOBROWNOUT,NOWDT,NOLVP
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,parity=N)
#include <lcd.c>
#use fast_io(A)
#use fast_io(C)
#bit vital = 0xf80.5
#bit ajusteup= 0xf82.0
#bit ajustedown= 0xf82.1
float valor;
float Lux;
float volts;
float ajuste=1;
void inicializar(void);
void Desplegar(void);
#int_TIMER1
TIMER1_isr() {
vital=!vital;
}
void inicializar(){
setup_port_A(AN0);
set_tris_a(0b00000001);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
delay_us(10);
lcd_init();
set_tris_c(0b10000011);
vital=1;
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(global);
}
void desplegar(){
lcd_gotoxy(1,1);
printf(LCD_PUTC,"volt:%4.3g %4.1g\r\nLux: %4.3g Lux",volts,ajuste,Lux);
printf("%4.3g %4.3g %4.3g \n",volts,ajuste,lux);
delay_ms(250);
}
void main() {
inicializar();
while(true){
valor=read_adc();
if(ajusteup==1){
ajuste=ajuste+0.10000;
}
if(ajustedown==1){
ajuste=ajuste-0.10000;
}
valor=ajuste*valor;
volts=valor*0.01960;
if (volts>=2.02500 && volts<5){
Lux=15.2*volts*volts-135.576*volts+303.96; //[Luxes]
desplegar();
}
if (volts>=1.16000 && volts<2.02500){
Lux=350.054*volts*volts-1460.704*volts+1617.819; //[Luxes]
desplegar();
}
if (volts>=0.80100 && volts<1.16000){
Lux=3018.2*volts*volts-7588.27611*volts+5142.332; //[Luxes]
desplegar();
}
if (volts>=0.508 && volts<0.80100){
Lux=21362.38717*volts*volts-34789.30338*volts+15160.103; //[Luxes]
desplegar();
}
if (volts>=0.3095 && volts<0.508){
Lux=168651.5198*volts*volts-171296.9684*volts+46592.88; //[Luxes]
desplegar();
}
if (volts>=0 && volts<0.3095){
Lux=1356427*volts*volts-880125.3026*volts+152633.554; //[Luxes]
desplegar();
}
}}
como puedes
ver linealizamos mediante un polinomio de segundo orden de lagrange.
[glow=red,2,300]Suerte en tu proyecto[/glow]