Boa tarde pessoal, preciso fazer um leitor de corrente e tensão com o pic 18f2550 e lcd.
Fiz o código, porém está muito instável, a leitura está muito ruim, não para, fica subindo e descendo,porém na entrada do pic não fica oscilando.
O sensor que uso é do tipo hall de 30a (acs 712). segue o código que fiz.
Se algum dos amigos puder me ajudar agradeço.
#include <18F2550.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES PROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES PUT //Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL12 //Divide By 12(48MHz oscillator input)
#FUSES CPUDIV4 //System Clock by 4
//#FUSES USBDIV //USB clock source comes from PLL divide by 2
//#FUSES VREGEN //USB voltage regulator enabled
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include <flexlcd7.c>
float v, a;
int16 pwm;
void main()
{
setup_adc_ports(AN0_TO_AN3|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DIV_BY_4,249,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(10);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
// TODO: USER CODE!!
lcd_init();
while(true){
//=========================================================================
// Tensão
//==========================================================================
set_adc_channel(0);
delay_us(10);
v=read_adc();
v=v*0.01528523949169110459433040078201;
printf("%fv \n ", v);
//==========================================================================
// Corrente
//==========================================================================
set_adc_channel(1);
delay_us(10);
a=read_adc();
a=a*0.004867585532746823069403714565;
a=a-2.5;
a=a/0.0799999999999999999999999999999999999;
printf("%fa\n ", a);
//==========================================================================
// PWM
//==========================================================================
set_adc_channel(2);
delay_us(10);
pwm=read_adc();
if(pwm<=9){pwm=10;}
if(pwm>=999){pwm=998;}
printf("%Lupwm\n\r ", pwm);
//==============================================================================
// LCD
//===================================================================================
lcd_gotoxy (1,1); //Ir para a coluna 5 na linha 1 do LCD.
// lcd_putc ("V=2.5"); //Escreve no LCD.
printf(lcd_putc,"A= %f",a);
lcd_gotoxy (1,2); // Ir para a coluna 5 da linha 2 do LCD.
//lcd_putc ("A=24"); //Escreve.
printf(lcd_putc,"V= %f",v);
// delay_ms (100); //Pausa de 100 milisegundos
// lcd_putc ("\f"); //Apaga o LCD.
// delay_ms (100);
set_pwm1_duty(pwm);
}
}