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.

HELP Duty Cycle With PIC24F

Status
Not open for further replies.

bubble_d

Junior Member level 3
Joined
Sep 12, 2012
Messages
25
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,483
So Im reading a 100Hz frequency from an MEMSIC 2125 and I need to find the duty cycle which i will later use to find the angle.

For now I am just trying to find the Ton = Rising - Falling.

Here is my Code.

I am supposed to get a duty of about 0.6 but when I run the code and look at the watch I find the diffrence between the rising and falling to be about 3500 which results in 0.7 duty cycle which is wayyy to high
Ignore the capture 3 for now. Maybe im doing my math wrong? or maybe my code is wrong? I am unsure

Please help me! Someone!

Code:
#include <stdio.h>
#include <stdlib.h>
//#include "LCD.h"
#include <p24FJ128GA010.h>



_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & BKBUG_ON & ICS_PGx2 & FWDTEN_OFF)
_CONFIG2(IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_OFF &  FNOSC_PRIPLL & POSCMOD_HS)

  
#define DELAY 8712.5
#define DELAY2 500



unsigned int X_capture1; // second capture variable
unsigned int X_capture2; // second capture variable
unsigned int X_capture3; // second capture variable
unsigned int R1; // second capture variable
unsigned int F1; // second capture variable
unsigned int R2; // second capture variable
unsigned int Ton; // second capture variable
int count = 1;
void __attribute__((interrupt,no_auto_psv)) _IC1Interrupt(void);

void timer3init(void)

{

	TMR3 = 0;  //Clear the contents of Timer 3 Register
	T3CON = 0x8020;

}

void capr1init(void)

{

	IC1CON = 0x0003;

}



void clearcap(void)

{

IC1CON = 0; //clear contents of Input Capture 1

}
	



int main(void)

{	
	

	IPC0bits.IC1IP = 1; //level 1 priority
	IFS0bits.IC1IF = 0; //clear  the IC1 interrupt status flag 
	IEC0bits.IC1IE = 1;  // Enable IC1 interrupts
	
	TRISDbits.TRISD8 = 1;				//Set D8 to input mode

	timer3init();
	clearcap();
	capr1init();
	
	

while(1){

if(R1>F1){

Ton = (65535 - R1)+ F1;
}else{

Ton = F1 - R1;
}

}
}




void __attribute__((interrupt,no_auto_psv)) _IC1Interrupt(void)

{
	IC1CON = 0x0001;
	

	X_capture2 = X_capture1;
	X_capture1 = IC1BUF;
	
	

count++;


if (count == 2){
		
	R1	=	X_capture2;
	F1	=	X_capture1;
		
	count = 1;

}


IFS0bits.IC1IF = 0; //Reset Timer1 interrupt flag and Return from ISR	
}
 

for pwm you want to use timer 2. at which freq do you generate the signal?
 

for pwm you want to use timer 2. at which freq do you generate the signal?

Alright, I will switch the timer, but out of curiosity... Why does it need Timer 2 not timer 3? is that just standard?

Also the frequency coming out of the MEMSIC is 100Hz or 10 ms.

Thank you for your help,
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top