por lasfclestat » 20 Abr 2011 11:25
ola...
Estou tentando fazer um exemplo como segue a baixo, eu consigo gerar uma onda quadrada de periodo de 26ms, porém não estou conseguindo gerar a interrupção do CCP1, está faltando alguma coisa no codigo? Tambem estou tentando ler um sensor ultrasonico.
Muito Obrigado
- Código: Selecionar todos
#include <16F877A.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#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
#FUSES NOWRT //Program memory not write protected
#use delay(clock=20000000,RESTART_WDT)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
int8 capture_rising_edge;
int8 got_pulse_width;
int16 ccp_delta;
int flag = 0;
#INT_TIMER2
void TIMER2_isr(void) //timer2 interrupt
{
if (flag)
{
output_high(pin_c0);
flag = 0;
} else
{
flag = 1;
output_low(pin_c0);
}
clear_interrupt(int_timer2);// clear timer2 interrupt's flag
}
#int_ccp1
void ccp1_isr(void)
{
static int16 t1_rising_edge;
// If current interrupt is for rising edge.
if(capture_rising_edge)
{
setup_ccp1(CCP_CAPTURE_FE);
capture_rising_edge = FALSE;
t1_rising_edge = CCP_1;
}
else
{
setup_ccp1(CCP_CAPTURE_RE);
capture_rising_edge = TRUE;
ccp_delta = CCP_1 - t1_rising_edge;
got_pulse_width = TRUE;
}
}
//====================================
main()
{
int16 pulse_width_ms;
int16 local_ccp_delta;
got_pulse_width = FALSE;
capture_rising_edge = TRUE;
setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8 );
setup_timer_2(T2_DIV_BY_16,255,16);
enable_interrupts(INT_TIMER2);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
while(1)
{
if(got_pulse_width)
{
disable_interrupts(GLOBAL);
local_ccp_delta = ccp_delta;
enable_interrupts(GLOBAL);
pulse_width_ms = local_ccp_delta / 125;
printf("%lu ms \n\r", pulse_width_ms);
got_pulse_width = FALSE;
}
}
}
"Se existe algo como repousar em nome da verdade. Então vós e teus amados, qualquer que sejam as dificuldades, não devem ficar separados indefinidamente!"
Ferrari Caim Lestat