View previous topic :: View next topic |
Author |
Message |
eng/ IBRAHIM
Joined: 14 Aug 2007 Posts: 31
|
16f887 not work |
Posted: Sun Jul 12, 2015 2:37 pm |
|
|
i use this program by 16f877 and it play good
but when change to 16f887 not work.
Code: |
int32 isr_ccp_delta,current_ccp,current_ccp_delta,frequency;
#int_ccp1
void ccp1_isr(void)
{
static int32 old_ccp = 0;
current_ccp = CCP_1;
isr_ccp_delta = current_ccp - old_ccp;
old_ccp = current_ccp;
}
//************************************
void main()
{
delay_us(20);
set_timer1(0);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
setup_ccp1(CCP_CAPTURE_RE);
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 = (2000000 / current_ccp_delta);
output_d(frequency);
delay_ms(500);
}
} |
|
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun Jul 12, 2015 2:42 pm |
|
|
You have been here long enough to know we need to see the setup
section of the program (CPU include, Fuses, Delay etc.) and the compiler version!
Also, when you say it doesn't work, what does that mean? What does it do? _________________ Google and Forum Search are some of your best tools!!!! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Sun Jul 12, 2015 3:41 pm |
|
|
One item....
this....
output_d(frequency);
isn't going to sent a 32 bit data to an 8 bit port properly.....
Jay |
|
|
eng/ IBRAHIM
Joined: 14 Aug 2007 Posts: 31
|
|
Posted: Sun Jul 12, 2015 6:20 pm |
|
|
the output still zero.at any frequency.
but when use 16f877 the output is reading input frequency
version 4.014
Code: |
#include <16F887.h>
#device PIC16F887
#list
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOPUT //No Power Up Timer
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPD //No EE protection
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NODEBUG //No Debug mode for ICD
#FUSES BORV40 //Brownout reset at 4.0V
#FUSES NOWRT //Program memory not write protected
#use delay(clock=8000000) |
|
|
|
eng/ IBRAHIM
Joined: 14 Aug 2007 Posts: 31
|
|
Posted: Mon Jul 13, 2015 2:13 pm |
|
|
when change some lines as below.
the output is reading frequency input correct.
but when freq is not in range 40Hz to 250Hz . the output is incorrect
what is error
input freq rang is 20 to 1000Hz
Code: |
int16 isr_ccp_delta,current_ccp,frequency;
#int_CCP2
void CCP2_isr(void)
{
static int32 old_ccp = 0;
current_ccp = CCP_2;
isr_ccp_delta = current_ccp - old_ccp;
old_ccp = current_ccp;
output_high(PIN_e0);
}
//************************************
void main()
{
delay_us(20);
while(1)
{
setup_ccp2(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1 ); // Start timer 1
enable_interrupts(INT_CCP2); // Setup interrupt on rise edge
enable_interrupts(GLOBAL);
frequency = (2000000 / isr_ccp_delta);
output_d(frequency);
delay_ms(5);
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Mon Jul 13, 2015 2:54 pm |
|
|
You are running out of counts.
You need to use int32 values, and increment the high byte of the current_ccp in a timer1 interrupt, |
|
|
eng/ IBRAHIM
Joined: 14 Aug 2007 Posts: 31
|
|
Posted: Mon Jul 13, 2015 5:12 pm |
|
|
OK run good
now at start freq is zero or no signal output is zero. then freq is vary and output is reading the freq
but when freq return zero or no signal . output not zero and still at last value of freq
how make output is zero if freq return zero?? |
|
|
guy
Joined: 21 Oct 2005 Posts: 297
|
|
Posted: Sat Jul 18, 2015 9:06 am |
|
|
Add a flag in the interrupt, and in the main loop wait for the flag.
That makes the output change only when there is an interrupt.
Then, while waiting for this flag check TIMER_1 if time between edges is more than 20Hz / 50ms. If so, set the output value to 0. |
|
|
|