duvida como ler um sinal onda quadrada com o pic 16f628a

Software e Hardware para uC PIC

Moderadores: andre_luis, 51, guest2003, Renie

duvida como ler um sinal onda quadrada com o pic 16f628a

Mensagempor diogo_18sp » 24 Jul 2009 17:31

ola boa tarde, a duvida que tenho e que preciso ler o tempo de um sinal de onda quadrada este sinal varia sua frequencia e depois comparar este sinal para realizar uma determinada funçao preciso de uma ajuda como fazer este programa, a linguagem que uso eo assembly eo pic 16f628a.
obrigado
diogo_18sp
Byte
 
Mensagens: 117
Registrado em: 14 Jan 2008 18:12
Localização: Amparo ,SP

Mensagempor Djalma Toledo Rodrigues » 24 Jul 2009 20:39

Qual a largura mínima / máxima desse Pulso ?
Avatar do usuário
Djalma Toledo Rodrigues
Dword
 
Mensagens: 2334
Registrado em: 03 Ago 2008 13:22

Mensagempor diogo_18sp » 25 Jul 2009 13:03

Djalma Toledo Rodrigues escreveu:Qual a largura mínima / máxima desse Pulso ?

a largura maxima e de 10 ms ea largura minima 1 ms
diogo_18sp
Byte
 
Mensagens: 117
Registrado em: 14 Jan 2008 18:12
Localização: Amparo ,SP

Mensagempor tcpipchip » 25 Jul 2009 14:58

A forma mais rapida...é utilizando os CCP1 e CCP2, com timers de 16 bits...com AUTORELOAD...
Avatar do usuário
tcpipchip
Dword
 
Mensagens: 6560
Registrado em: 11 Out 2006 22:32
Localização: TCPIPCHIPizinho!

Mensagempor Djalma Toledo Rodrigues » 26 Jul 2009 17:48

Exato Captura CCP1 CCP2

Mas, se estiver na velocidade máx. de Clock dará para executar 5 000 Instruções em 1 ms
Tempo suficente para contar o Pulso de 1 ms até sem o uso de Interupção
a desvantagem é que o uC ficará ocupado nessa tarefa

Uma terceira alternativa é usar a Interrupção Externa
Interrompe na bordade subida e na borda de descida alternadamente
Nesse intervalo conta a largura do Pulso diretamente ou através do Timer.

A precisão em ambos os casos não será tão elevada quanto aos CCPs
.
Avatar do usuário
Djalma Toledo Rodrigues
Dword
 
Mensagens: 2334
Registrado em: 03 Ago 2008 13:22

Mensagempor _blackmore_ » 26 Jul 2009 17:53

tcpipchip

A forma mais rapida...é utilizando os CCP1 e CCP2, com timers de 16 bits...com AUTORELOAD.

bixo ... tu poderia postar um fragmento de código que possa ser implementado essa leitura?

abrax!
_blackmore_
Dword
 
Mensagens: 1397
Registrado em: 28 Set 2008 13:26

Mensagempor tcpipchip » 27 Jul 2009 10:21

Se possivel...usa a série 18...

#include <16F877.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=2000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

// This global variable holds the time interval
// between two consecutive rising edges of the
// input signal.
int16 isr_ccp_delta;

// When a rising edge occurs on the input signal,
// the CCP1 will 'capture' the value of Timer1
// at that moment. Shortly after that, a CCP1
// interrupt is generated and the following isr
// is called. In the isr, we read the 'captured'
// value of Timer1. We then subtract from it the
// Timer1 value that we 'captured' in the previous
// interrupt. The result is the time interval
// between two rising edges of the input signal.
// This time interval is then converted to a frequency
// value by code in main().
#int_ccp1
void ccp1_isr(void)
{
int16 current_ccp;
static int16 old_ccp = 0;

// Read the 16-bit hardware CCP1 register
current_ccp = CCP_1; // From 16F877.H file

// Calculate the time interval between the
// previous rising edge of the input waveform
// and the current rising edge. Put the result
// in a global variable, which can be read by
// code in main().
isr_ccp_delta = current_ccp - old_ccp;

// Save the current ccp value for the next pass.
old_ccp = current_ccp;
}


//=======================
void main()
{
int8 duty;
int8 pr2_value;
int16 current_ccp_delta;
int16 frequency;


// Setup the ADC so we can read a value from 0 to 255
// as we turn a trimpot which is connected to pin A0.
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_8); // Divisor for 4 MHz crystal
set_adc_channel(0);
delay_us(20);

// The CCP2 will be used in PWM mode to output a test
// signal that will be connected to the CCP1 pin.
// The test signal can be varied from 244 Hz to 2016 Hz
// by turning the trimpot on pin A0.
setup_ccp2(CCP_PWM);

// Setup Timer1 and CCP1 for Capture mode so that
// we can measure the input signal's frequency.
// The input signal comes from the CCP2 pin, which
// is connected to the CCP1 pin with a wire.
set_timer1(0);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
setup_ccp1(CCP_CAPTURE_RE);

// Clear the CCP1 interrupt flag before we enable
// CCP1 interrupts, so that we don't get an unwanted
// immediate interrupt (which might happen).
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);


while(1)
{

// Read the A/D value from the trimpot.
// This is a value from 0 to 255. We limit
// it to a minimum of 30 for the purposes
// of this test program. This will limit the
// test signal to 2016 Hz maximum frequency.
pr2_value = read_adc();
if(pr2_value < 30)
pr2_value = 30;

// Configure CCP2 to put out a rectangular
// waveform with approximately a 50% duty
// cycle. The setting of the trimpot controls
// the frequency of this waveform.
setup_timer_2(T2_DIV_BY_16, pr2_value, 1);
duty = pr2_value / 2;
set_pwm2_duty(duty);


// Now calculate the frequency.

// Get a local copy of the latest ccp delta from the isr.
// We have to disable interrupts when we read a global
// isr variable that is larger than a single byte.
disable_interrupts(GLOBAL);
current_ccp_delta = isr_ccp_delta;
enable_interrupts(GLOBAL);

// To calculate the frequency of the input signal,
// we take the number of clocks that occurred
// between two consecutive edges of the input signal,
// and divide that value into the number of Timer1
// clocks per second. Since we're using a 4 MHz
// crystal, the Timer1 clock is 1 MHz (Timer1 runs
// at the instruction cycle rate, which is 1/4 of the
// crystal frequency). For example, suppose the
// the input waveform has a frequency of 244 Hz.
// 244 Hz has a period of about 4098 usec.
// Timer1 is clocked at 1 MHz, so between two
// consecutive rising edges of the input signal,
// it will count up by 4098 clocks. To find the
// frequency, we divide 4098 into the number of
// clocks that occur in 1 second, which is 1000000.
// This gives 1000000 / 4098 = 244 Hz.

frequency = (int16)(1000000L / current_ccp_delta);

// Display the calculated frequency.
printf("%lu Hz\n\r", frequency);

delay_ms(500);
}

}
Avatar do usuário
tcpipchip
Dword
 
Mensagens: 6560
Registrado em: 11 Out 2006 22:32
Localização: TCPIPCHIPizinho!

Mensagempor diogo_18sp » 27 Jul 2009 23:12

entao obrigado pela ajuda so que agora nao estou conseguindo configurar os modulos ccp do pic 16f628a para ler esta frequencia.
diogo_18sp
Byte
 
Mensagens: 117
Registrado em: 14 Jan 2008 18:12
Localização: Amparo ,SP


Voltar para PIC

Quem está online

Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante

x