Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 CCP1CON = 0X05; T3CON = 0X00; T1CON = 0X00; TRISB = 0X00; TRISD = 0X00; TRISCbits.TRISD = 0X01; CCPR1L = 0; CCP1RH = 0; unsigned int hbyte, lbyte; void main(){ while(1){ TMR1H = 0; TMR1L = 0; PIR1bits.CCP1IF = 0; while(pir1BITS.CCP1IF == 0); T1CONbits.TMR1ON = 1; PIR1bits.CCP1IF = 0; while(PIR1bits.CCP1IF == 0); T1CONbits.TMR1ON = 0; lbyte = CCPR1H; hbyte = CCPR1H; //lbyte and hbyte gives the value of freq count. } }
#include "18f2520.h"
#include "f2420_regs.h"
#fuses INTRC
#use delay(clock=4000000)
#define RS PIN_A2
#define EN PIN_A1
void lcd_init();
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
unsigned int16 value;
unsigned int8 high_byte, low_byte;
void main()
{
lcd_init();
TRIS_C2 = 1; // PIN C2 configured as input for capture mode
TRISB = 0;
CCP1CON = 0b00000101; // Capture mode, every rising edge
CCPR1H = 0;
CCPR1L = 0;
T1CON = 0b11001000; // 16-Bit, 1:1 Prescale value
while(1)
{
TMR1H = 0;
TMR1L = 0;
CCP1IF = 0; // Interrupt flag is cleared
while(CCP1IF == 0); // waits till Interrupt flag gets cleared
TMR1ON = 1; // Timer1 ON
CCP1IF = 0; // Interrupt flag is cleared
while(CCP1IF == 0); // waits till Interrupt flag gets cleared
TMR1ON = 0; // Timer1 OFF
low_byte = CCPR1L;
high_byte = CCPR1H;
value = ((high_byte * 256) + low_byte)*60;
CCP1IE = 0;
CCP1IF = 0;
lcd_cmd(0x01);
lcd_cmd(0x80);
printf(lcd_data,"%lu HZ",value);
delay_ms(1000);
}
}
void lcd_init()
{
lcd_cmd(0x30); // Configure the LCD in 8-bit mode, 1 line and 5x7 font
//lcd_cmd(0x28);
lcd_cmd(0x0c); // display on and cursor off
lcd_cmd(0x01); // clear display screen
lcd_cmd(0x06); // increment cursor
lcd_cmd(0x80); // set cursor to 1st line
}
void lcd_cmd(unsigned char c)
{
output_b(c);
output_low(RS);
output_high(EN);
delay_ms(15);
output_low(EN);
}
void lcd_data(unsigned char z)
{
output_b(z);
output_high(RS);
output_high(EN);
delay_ms(15);
output_low(EN);
}
*/
Code C - [expand] 1 2 3 value = high_byte; value = value << 8; value = value | low_byte;
bit 1 TMR1CS: Timer1 Clock Source Select bit
1 = External clock from pin RC0/T1OSO/T13CKI (on the rising edge)
0 = Internal clock (FOSC/4)
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1); // Timer1 internal & 1:1 Prescaler
Hi,
It had happened with me before. Try to do it with capture Interrupt. Reading a high to next high or low to next low of the pulse. Also take care about the interrupt latency time?
Enjoy!
#int_ccp1
void ccp1_isr(void)
{
int16 current_ccp;
static int16 old_ccp = 0;
current_ccp = CCP_1; // ccp_1 holds the timer values
isr_ccp_delta = current_ccp - old_ccp; // difference between 2 rising edges
old_ccp = current_ccp;
}
//=======================
void main()
{
int16 current_ccp_delta;
int16 frequency;
lcd_init();
set_timer1(0);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1); // timer 1; prescaler 1:1
setup_ccp1(CCP_CAPTURE_RE); // rising edge
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
while(1)
{
disable_interrupts(GLOBAL);
current_ccp_delta = isr_ccp_delta;
enable_interrupts(GLOBAL);
frequency = (int16)((1000000) / current_ccp_delta);
lcd_cmd(0x01);
lcd_cmd(0x80);
printf(lcd_data,"%lu Hz", frequency);
delay_ms(500);
}
}
Before Asking this question have you tried to read about Interrupt latency. In one minute i have got several links that talk about it. Have a look at the following links.What do u mean by interrupt latency time?[/HTML]
http://www.ccsinfo.com/faq.php?page=functions_in_interrupts
http://en.wikipedia.org/wiki/Interrupt_latency
http://whatis.techtarget.com/definition/interrupt-latency
Have you tried to read about the CCS compiler manual? If no read this before you start working on any device. The experience guys say "Understand your compiler 1st".how to configure?[/HTML]
interrupt latency time is the time, which your isr consumes, in that time, your main program stops to being executed, this is called interrupt latency, you can minimise your latency by using higher clock rate and by optimising your isr code,
try to minimize your interrupt routine,
Interrupt latency is defined as the time from the interrupt event (the interrupt flag bit gets set) to
the time that the instruction at address 0004h starts execution (when that interrupt is enabled).
For synchronous interrupts (typically internal), the latency is 3TCY. ( PIC 16F )
For asynchronous interrupts (typically external), such as the INT or Port RB Change Interrupt,
the interrupt latency will be 3 - 3.75TCY (instruction cycles). The exact latency depends upon
when the interrupt event occurs in relation to the instruction cycle.
Time consume by ISR is not calculated
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?