rd7l 0 Denunciar post Postado Junho 6, 2012 Boa noite. Este é meu primeiro programa em C e já estou há dias fazendo ele. Porém, preciso melhorar uma coisa: quero ler a porta analógica de um PIC ( read_adc() ) por 1 segundo e para cada vez que a porta for lida (dentro deste 1 segundo) incrementar um contador, depois imprimir este contador no LCD. Já li vários help sobre a função interrupt, mas não consegui entender a estrutura dela. Alguém poderia me ajduar? Segue me código: #include <16F877A.h> //PIC utilizado #device adc=10 #FUSES NOWDT //No Watch Dog Timer #FUSES XT //Clock <=4Mhz #FUSES PUT //Power Up Timer #FUSES NOPROTECT //Code not protected from reading #FUSES NOBROWNOUT //No brownout reset #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O #FUSES NOCPD //No EE protection #use delay(clock=4000000) #include <LCD.c> int16 ad, i, j, q, b_nulo = 511; float dif, tensao; signed int16 b, media, acumula; void main() { lcd_init(); setup_adc_ports(AN0); //Configura canal 0 analógico setup_adc(ADC_CLOCK_DIV_8); //De acordo com relógio interno. set_adc_channel(0); //Habilita canal 0 delay_us(20); //Espera um pouco, obrigatório! while(TRUE) { { for (i=0;i<10;i++) { ad = read_adc(); j++; dif = ad - b_nulo; acumula = acumula + dif; } { media = acumula/i; tensao = (media * 5.f ) / 1024.f; // Converte para tensão b = tensao / 0.0025; // Converte para Gauss acumula = 0; // Reinicia a variável acumula para mostrar no LCD e começar nova media. } printf (lcd_putc,"\f%1.2f Gauss" (float) j); if (b > 0) { printf (lcd_putc,"\nPolo Sul"); } if (b < 0) { printf (lcd_putc,"\nPolo Norte"); } if (b == 0) { printf (lcd_putc,"\nNulo"); } delay_ms (500); } } } Estou utiliznado o ccs como copilador. Compartilhar este post Link para o post Compartilhar em outros sites
GBecker 51 Denunciar post Postado Junho 6, 2012 rd7l, Peguei este código do próprio site da CCS, que pelo visto tem muitos exemplos lá. Pelo que entendi a função, ela já ja retorna o sinal analógico convertido para digital. É só pegar o retorno dela e sair trabalhando com os dados. ///////////////////////////////////////////////////////////////////////// //// EX_ADMM.C //// //// //// //// This program displays the min and max of 30 A/D samples over //// //// the RS-232 interface. The process is repeated forever. //// //// //// //// Configure the CCS prototype card as follows: //// //// Insert jumpers from: 11 to 17, 12 to 18 and 9 to 16 //// //// Use the #9 POT to vary the voltage. //// ///////////////////////////////////////////////////////////////////////// #include<16f877a.h> #fuses HS,NOLVP,NOWDT,PUT #use delay(clock=20000000) #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7) void main() { int i, value, min, max; printf("Sampling:"); setup_adc_ports( RA0_ANALOG ); setup_adc( ADC_CLOCK_INTERNAL ); set_adc_channel( 0 ); do { //Takes 30 samples from pin A0 min = 255; //and displays the min and max max = 0; //values for that 100ms period for(i = 0; i <= 30; ++i) { delay_ms(100); value = read_adc(); if(value < min) min = value; if(value > max) max = value; } printf("nrMin:%x MAX: %x", min, max); } while (TRUE); } Espero ter ajudado,FLW! :grin: Compartilhar este post Link para o post Compartilhar em outros sites