Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

strange problem during frequency measurement.

Status
Not open for further replies.

hemnath

Advanced Member level 3
Advanced Member level 3
Joined
Jun 24, 2012
Messages
702
Helped
61
Reputation
120
Reaction score
57
Trophy points
1,308
Location
Chennai
Visit site
Activity points
6,589
PIC18F2520, CCS C compiler.

I'm measuring the frequency and depending on the frequency, LED output changes. When i comment the below 2 lines in the program, frequency reads correctly. But when i uncomment those lines, it reads wrong frequency. But i want to read and write the eeprom data. Is it possible? below program fails to read the correct frequency during reading and writing eeprom? Please help.

HTML:
flag1 = read_eeprom(FLAG_ADDRESS);    
write_eeprom(FLAG_ADDRESS,flag);

HTML:
#include "18f2520.h"
#fuses H4, NOWDT               
#use delay(clock=16000000)         // 4Mhz crystal with PLL enabled. 4Mhz * 4 => 16Mhz 

#define     output_pin    PIN_A4
#define  	LED  		  PIN_C4

#define FLAG_ADDRESS 		10
#define FLAG_STATUS_ADDRESS     20

int8 old_f, f = 0;
int16 temp_data; 
int32 local;
int8 HIGH;

int8 flag1;

int16 isr_ccp_value;
int16 current_ccp_value; 
int32 frequency; 
int flag;

#int_ccp1
void ccp1_isr(void)
{
	int16 current_ccp;
	static int16 old_ccp = 0;
	f++; 	
	current_ccp = CCP_1;   // Read the 16-bit hardware CCP1 register 
	
	isr_ccp_value = current_ccp - old_ccp;       
	                              
	old_ccp = current_ccp;        // Save the current ccp value for the next pass.

}

void main()
{
set_tris_a(0x00);
set_tris_c(0x00);

set_timer1(0);                               //   Timer value to 0
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);    //   Timer1 internal & 1:1 Prescaler
setup_ccp1(CCP_CAPTURE_RE);                  //   Looks for Rising edge

clear_interrupt(INT_CCP1);                  //   disables the CCP1 interrupt
enable_interrupts(INT_CCP1);                //   enables the CCP1 Interupt
enable_interrupts(GLOBAL);                  //   Enables global interrupt

HIGH = 1;

while(1)
{   
current_ccp_value = isr_ccp_value;         
enable_interrupts(GLOBAL);           		 //   enable interrupts

	while(old_f == f);
	old_f = f;
	if(f < 64)
	   {

	       do
	       {
 	         temp_data=current_ccp_value;
	       } while (current_ccp_value!=temp_data);
	       local += temp_data;   
	   }
	else
	   {

    	disable_interrupts(GLOBAL);						
		frequency = (int16)((640 * 393822) / (local));       	

	if((frequency < 2000))     
	{
		output_high(LED);
		output_high(output_pin);                           
		flag = 1;                                                  
	}

	flag1 = read_eeprom(FLAG_ADDRESS);                // read the value in eeprom address - flag
	
	if((frequency > 2000) && (frequency < 3000) && (flag1 == 1))   
	{
		output_high(LED);
		output_high(output_pin);                          
		flag = 1;                                                      
	}

	if((frequency > 3000))       
	{
		output_low(LED);
		output_low(output_pin);                          
		flag = 0;                                                      
	}

	if((frequency > 2000) && (frequency < 3000) && (flag == 0))  
	{
		output_low(LED);
		output_low(output_pin);
		flag = 0;                                        
	} 

    write_eeprom(FLAG_ADDRESS,flag); 	
	local = f=old_f=0;	
	}
}
}

Thanks in advance :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top