bubble_d
Junior Member level 3
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!
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
}