por RAguiar » 30 Jan 2011 09:49
Olá pessoal!
Seguinte, até o momento consegui fazer o seguinte, um conjunto de AMPOP amplifica e gera uma onda quadra para o zero cross de corrente e o mesmo para o zero cross de tensão, aplico à uma AND que por sua vez gera um terceiro sinal, sendo que o tempo em que fica em nivel logico 1 representa a defasagem...
...pois bem, estou tentanto medir o tempo em que o pulso fica em 1, mas o valor apresentado no display oscila e não tem nada à ver com o tempo em 1 (500uS)...
...já li umas 3 apostilas sobre o modulo CCP em C, as tres mostram o codigo abaixo, mas não dá certo, já fiz umas alterações, mas dá na mesma, valores estranhos e aleatórios são exibidos...
/*
-----------------------------------------------------
Projeto......: programando microcontrolador pic
Descrição....: captura a largura de um pulso no pino RB3
-----------------------------------------------------
*/
#include <16F877a.h>
#FUSES NOWDT,XT,PUT,NOPROTECT,BROWNOUT,NOLVP,NOCPD
#use fast_io(a)
#use fast_io(b)
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8)
#include "lcd.c"
//
#byte Rb = 0x06
#define trisb 0b00001010 // 1 input
//
#bit capturou = 0x0C.2 //CCP1IF - ver capítulo 3.12
#bit interrupcao = 0x8C.2 //CCP1IE
//
void main(){
int16 tempo;
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//captura pulso ascendente
setup_ccp1(CCP_CAPTURE_RE);
//
Set_tris_b( trisb ); //direção dos bits do portB
Rb = 0; //zera todos os bits
While( true ){
capturou = 0; //zera flag de captura
interrupcao= 0; //zera flag de interrupção
set_timer1(0); //zera o timer1 , incremento a cada 1us.
while( ! capturou ); //executa enquanto não capturou
tempo = ccp_1;//houve um evento em RB3 capturou o tempo
lcd_init();
lcd_putc ("\f");
printf (lcd_putc, "Tempo = %lu\r\n", tempo);
//printf(tempo (us) = %2X\n\r, tempo); //envia ao PC
delay_ms(200);
}
}
tentei esse:
#include <16f877a.h>
#use delay (clock=4000000)
#fuses XT, NOWDT, NOPUT, NOPROTECT, BROWNOUT
#byte porta = 0x05
#byte portb = 0x06
#use fast_io (a)
#use fast_io (b)
#byte porta = 0x05
#byte portb = 0x06
#byte Rb = 0x06
#define trisb 0b00001010 // 1 input
//
#bit capturou = 0x0C.2 // CCP1IE
#bit interrupcao = 0x8C.2 //CCP1IE
//
#include "lcd.c"
int16 tempo, tempo2, tempo3;
void main()
{
set_tris_a(0x00);
set_tris_b(0x00);
porta = 0x00;
portb = 0x00;
lcd_init();
lcd_putc("\fAGUARDE");
lcd_putc("\nINICIALIZANDO");
delay_ms (1000);
lcd_init();
setup_timer_0 (RTCC_INTERNAL | RTCC_DIV_1);
setup_timer_1 (T1_INTERNAL | T1_DIV_BY_1);
setup_timer_2 (T2_DISABLED, 0, 1);
setup_comparator (NC_NC_NC_NC);
setup_vref (FALSE); //captura pulso ascendente
setup_ccp1 (CCP_CAPTURE_RE);
setup_ccp2 (CCP_CAPTURE_FE);
Set_tris_b (trisb); //direção dos bits do port B
Rb = 0; //zera todos os bits
While (true)
{
capturou = 0; //zera flag de captura
interrupcao = 0; //zera flag de interrupção
set_timer1 (0); //zera o timer1, icremento a cada 1us
//while ( ! capturou );//executa enquanto não capturou
//{
//output_high (pin_d1);
//delay_ms (500);
//output_low (pin_d1);
//delay_ms (500);
//}
tempo = ccp_1 ; //houve um evento em RC2 capturou o tempo
tempo2 = ccp_2;
tempo3 = (ccp_1 - ccp_2);
delay_us (10);
lcd_init();
printf (lcd_putc, "Tempo = %lu\r\n", tempo3 );
delay_ms (100);
}
reset_cpu();
}
e esse exibe 530, e um valor alto alternadamente...