EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Problem with CCP Modules

Started by Sanjyot Katti June 27, 2003
Hello Everybody,

I am using PIC 16F73 micro which has 2 CCP
modules.
The hardwrae is basically a motor controller + data
acqusition card. CCP1 module I have configured for
capture mode to measure frequency. CCP2 module is
configured for PWM mode. I have to use both the CCP
module simutaneously.
But it seems that whenever I start my frequency
measurement my PWM operation is stopped.

I have used ISR's for both. My code is in C using
Hitech C Cross compiler. My hardware is Ok becoz if I
generate only PWM or measure frequency only I get the
desired output. When I tried combining both the codes
I am able to measure only frequency correctly but no
PWM. I have referred the data sheet for PIC 16F73. It
says both CCP modules are independent.

Any suggestions ????

Sanjyot
________________________________________________________________________
Send free SMS using the Yahoo! Messenger. Go to http://in.mobile.yahoo.com/new/pc/





The CCP modules are independent and it would not appear that your use
would not have conflicts. As you are using interrupts, are you sure
that you process both interrupts in the routine, not just 1 or the
other?

In section 8.2 of the 16F73 datasheet there is a reference to other
literature re: the use of the CCP modules.

You could post your code, perhaps someone could take a look

--- In , Sanjyot Katti <sanjyotkatti@y...>
wrote:
> Hello Everybody,
>
> I am using PIC 16F73 micro which has 2 CCP
> modules.
> The hardwrae is basically a motor controller + data
> acqusition card. CCP1 module I have configured for
> capture mode to measure frequency. CCP2 module is
> configured for PWM mode. I have to use both the CCP
> module simutaneously.
> But it seems that whenever I start my frequency
> measurement my PWM operation is stopped.
>
> I have used ISR's for both. My code is in C using
> Hitech C Cross compiler. My hardware is Ok becoz if I
> generate only PWM or measure frequency only I get the
> desired output. When I tried combining both the codes
> I am able to measure only frequency correctly but no
> PWM. I have referred the data sheet for PIC 16F73. It
> says both CCP modules are independent.
>
> Any suggestions ????
>
> Sanjyot >
>
______________________________________________________________________
__
> Send free SMS using the Yahoo! Messenger. Go to
http://in.mobile.yahoo.com/new/pc/




I must have been brain dead this morning - why would you have an ISR
for the PWM. Basically, you set the frequency during setup and
change the pulse width whenever you want. It could be inside an ISR
for some other device but the PWM itself doesn't use interrupts. --- In , "rtstofer" <rstofer@p...> wrote:
>
> The CCP modules are independent and it would not appear that your
use
> would not have conflicts. As you are using interrupts, are you
sure
> that you process both interrupts in the routine, not just 1 or the
> other?
>
> In section 8.2 of the 16F73 datasheet there is a reference to other
> literature re: the use of the CCP modules.
>
> You could post your code, perhaps someone could take a look
>
> --- In , Sanjyot Katti <sanjyotkatti@y...>
> wrote:
> > Hello Everybody,
> >
> > I am using PIC 16F73 micro which has 2 CCP
> > modules.
> > The hardwrae is basically a motor controller + data
> > acqusition card. CCP1 module I have configured for
> > capture mode to measure frequency. CCP2 module is
> > configured for PWM mode. I have to use both the CCP
> > module simutaneously.
> > But it seems that whenever I start my frequency
> > measurement my PWM operation is stopped.
> >
> > I have used ISR's for both. My code is in C using
> > Hitech C Cross compiler. My hardware is Ok becoz if I
> > generate only PWM or measure frequency only I get the
> > desired output. When I tried combining both the codes
> > I am able to measure only frequency correctly but no
> > PWM. I have referred the data sheet for PIC 16F73. It
> > says both CCP modules are independent.
> >
> > Any suggestions ????
> >
> > Sanjyot
> >
> >
> >
> >
>
______________________________________________________________________
> __
> > Send free SMS using the Yahoo! Messenger. Go to
> http://in.mobile.yahoo.com/new/pc/





Dear Friend,

Thank you for your reply ... but still my problem
persists. As for your point on ISR, I am using ISR for
PWM just to clear the TMR2IF flag. that's all.
I am not using CCP1IF interrupt for capture.

Basically there are 2 things :

1) I have configured CCP2 module for PWM mode to
generate a PWM for 1 KHz on RC1 pin (configured as
output). This configuration I have done at the start
of my program.

2) At the same time I am measuring an incoming square
wave on RC2 pin. For this I configure CCP1 module for
capture mode. I call a function (frequency) in which
after configuration I detect the time period between 2
rising edges on RC2 and return to my main function.

Problem : At start I get PWM till I enter my frequency
measurement function. After I return from this
function
my PWM wave vanishes from RC1 pin. Even after
reconfiguring CCP2 for PWM I can't generate a PWM on
RC1.

I am sending my code as attachment .. I have tried to
describe most of the operation in it.

If you can suggest anything ... it will be of great
help ...

Thanks for your help,

Sanjyot

--- rtstofer <> wrote: >
> I must have been brain dead this morning - why would
> you have an ISR
> for the PWM. Basically, you set the frequency
> during setup and
> change the pulse width whenever you want. It could
> be inside an ISR
> for some other device but the PWM itself doesn't use
> interrupts. > --- In , "rtstofer"
> <rstofer@p...> wrote:
> >
> > The CCP modules are independent and it would not
> appear that your
> use
> > would not have conflicts. As you are using
> interrupts, are you
> sure
> > that you process both interrupts in the routine,
> not just 1 or the
> > other?
> >
> > In section 8.2 of the 16F73 datasheet there is a
> reference to other
> > literature re: the use of the CCP modules.
> >
> > You could post your code, perhaps someone could
> take a look
> >
> > --- In , Sanjyot Katti
> <sanjyotkatti@y...>
> > wrote:
> > > Hello Everybody,
> > >
> > > I am using PIC 16F73 micro which has 2 CCP
> > > modules.
> > > The hardwrae is basically a motor controller +
> data
> > > acqusition card. CCP1 module I have configured
> for
> > > capture mode to measure frequency. CCP2 module
> is
> > > configured for PWM mode. I have to use both the
> CCP
> > > module simutaneously.
> > > But it seems that whenever I start my frequency
> > > measurement my PWM operation is stopped.
> > >
> > > I have used ISR's for both. My code is in C
> using
> > > Hitech C Cross compiler. My hardware is Ok becoz
> if I
> > > generate only PWM or measure frequency only I
> get the
> > > desired output. When I tried combining both the
> codes
> > > I am able to measure only frequency correctly
> but no
> > > PWM. I have referred the data sheet for PIC
> 16F73. It
> > > says both CCP modules are independent.
> > >
> > > Any suggestions ????
> > >
> > > Sanjyot
> > >
> > >
> > >
> > >
> >
>
______________________________________________________________________
> > __
> > > Send free SMS using the Yahoo! Messenger. Go to
> > http://in.mobile.yahoo.com/new/pc/ > ------------------------ Yahoo! Groups Sponsor
>
> to unsubscribe, go to http://www.yahoogroups.com and
> follow the instructions
>
> ">http://docs.yahoo.com/info/terms/

________________________________________________________________________
Send free SMS using the Yahoo! Messenger. Go to http://in.mobile.yahoo.com/new/pc/


/*

Program Code for Data acquistion and motor control

Measure parameters :

1) Input Voltage
2) Ouput Voltage
3) Output Current
4) Output Frequency

Project Description :

The first 3 parameters are given as 5V AC signal to the hardware board form
5-0-5 transformers. These signals are then rectified to equivalent DC voltage
and given to ADC pins RA0-RA2 of PIC 16F73. Now 5V AC signal of Output Voltage
is convert onboard into square wave and given to RC2 pin for frequency measurement. If the output voltage signal deviates from fixed voltage say 230V.
Only then I am supposed to generate a PWM signal. CCP1 module is used in Capture Mode (RC2 configured as Input)
CCP2 module is used for PWM generation (RC1 configured as Output)

PWM Frequency = 1 KHz. Time Period = 1 millisecond

Incoming Output Frequency on RC2 pin is 45 to 55 Hz.

Crystal Used = 11.0592 MHZ. (To get 1 KHz PWM)

I have to measure the incoming frequency on RC2 pin using CCP1 module in
capture mode. After which I am supposed to display the frequency value on a LCD.

Along with it I have to monitor the incoming signals on my Analog pins.
If the output voltage appearing on my RA1 pin varies from its previously
Set Voltage = 230V (say), only then I am supposed to generate a PWM to
drive a motor.

I am using ISR for PWM generation because it is more critical in my case.
In ISR all I am doing is clearing TMR2IF flag since I want a continuous PWM
till my incoming Output voltage signal returns to set value.

Though frequency measurement is not very critical I have to measure it for
display on LCD. So I have not used ISR for it. I just call frequency function
which will capture the incoming signal and set global variable which holds
the current Output Frequency value. This value is then displayed on LCD. Problem :

During my initial code writing I wrote both the code for PWM generation and
frequency measurement separately. Both worked fine when programmed independently.

But when I integrated both I could measure only frequency, but my PWM generation was
stopped.

What I did is simple just configured CCP2 module for PWM generation
at the start of program. Then initialized my LCD. Called my frequency measurement
function which configures CCP1 module for capture mode. Observed Result :

At start immediately when after configuration for PWM, I get the PWM output.
But the moment it goes into my frequency measurement function my PWM generation
is stopped. After this I cannot generate PWM at all.

I tried reconfiguring my CCP2 module for PWM once again after I return from
frequency function. But still I don't get the PWM.

The moment I configure CCP1 for capture my PWM using CCP2 vanishes ?????

I tried using ISR for Capture (check CCP1IF) and TMR2IF for PWM ...
but with same result.

I programmed PIC16F873 with same code but still the same result.

Note :
I have not listed the Lcd initialization and display code below because
it is irrelevant and is working fine.

Only the code having problem is listed.

*/
// Include files

#include <pic.h // Declarations

static bit TRISPWMWAVE@(unsigned)&TRISC*8+1; // TRISC pin1
static bit TRISFREQ@(unsigned)&TRISC*8+2; // TRISC pin2

// Variable declarations

bit flag_value_captured = 0; // Main Program begins

main()
{

// Configure CCP2 module for PWM

TMR2 = 0x00; // Clear Timer2
PR2 = 0xAB; // PWM Period is set (PWM PERIOD of 1 millisecond)
CCPR2L = 0x56; // Duty Cycle is 50% of PWM Period

CCPR2H = 0x00;

INTCON=0xC0; // Enable GIE and PEIE bits

PIE1 = 0x00; // Clear PIE and PIR registers
PIR1 = 0x00; TMR2IE = 1; // Enable Timer2 Overfow interrupt TRISPWMWAVE = 0; // Configure RC1 as Output pin

T2CON = 0x03; // Configure Timer2 control register for 1 : 16 prescale

CCP2CON = 0x0C; // Configure CCP2CON for PWM mode

TMR2ON=1; // Start Timer2 // LCD initialization routine
// Code not listed while()
{

// Frequency measurement function using capture

frequency();

// Frequency value displayed on LCD
// Code not listed // Delay ... wait for some time
// Code not listed
// Continue

} // while ends }
// Interupt Service Routine

void interrupt isr()
{
unsigned long int icnt; // Check for TMR2IF interrupt

if(TMR2IF)
{
TMR2IF = 0;
} return;
} // THIS FUNCTION IS TO CALCULATE FREQUENCY

void frequency(void)
{
unsigned int freqLcount, freqHcount;
unsigned int time_out; // Variable time_out is used as a software counter to come out
// of frequency function if no square wave (frequency) signal appears
// on RC2 pin for Capture operation.

time_out = 5000;

// Global flag indicating value captured

flag_value_captured = 0; // freqLcount and freqHcount registers hold the captured CCPR1L and CCPR1H register values
// Cleared at the start

freqLcount = 0;
freqHcount = 0; // Clear Timer1 and CCPR1 registers

TMR1H=0;
TMR1L=0;

CCPR1L=0;
CCPR1H=0; // Configure RC2 (PORTC pin 1) as input

TRISFREQ = 1;

// Disable CCP1IE interrupt

CCP1IE=0;

// Clear CCP1IF interrupt flag

CCP1IF=0;

// Configure Timer1 with prescale value of 1:8

T1CON00111100;

// Configure CCP1CON for every rising edge

CCP1CON00000101; // Start timer1 for capture

TMR1ON=1; // This loop is used to detect the rising edge of incoming Square or pulse waveform
// During this wait timeout variable is decremented
// This is required since if there is no incoming signal the control will remain in
// frequency function. So

while(CCP1IF==0)
{
time_out--;

if(time_out < 2) // If no signal or rising edge detected
break; // exit loop
else
continue;

} // Stop timer1

TMR1ON=0;

// Clear Timer1 L and H registers

TMR1H=0;
TMR1L=0;

// Clear CCP1IF flag and restart Timer1 to detect next rising edge

if(CCP1IF)
{
CCP1IF=0;
TMR1ON = 1;
} // Reload the time_out register

time_out = 5000; // Detect the next rising edge to get the total time period between
// two successive rising edges

while(CCP1IF==0)
{

time_out--;

if(time_out < 2)
break;
else
continue;
}

// Clear CCP1IF flag and store the values captured in user defined registers

if(CCP1IF)
{
TMR1ON=0; // Stop timer1
CCP1IF=0

freqLcount = CCPR1L; // Load Captured values in user defined registers
freqHcount = CCPR1H;

flag_value_captured = 1; // Flag to indicate value captured

} // Code for converting time period into frequency
// It is basically combinning the lower and higher bytes into an integer variable
// The performing the calculations using :

// Frequency = 1 / Timeperiod (Total 16 bit captured value)

// Return from function

return;
}




Read carefully section 6.5 of the manual - you were turning pin C1
into part of an oscillator circuit. Also check section 8.1.2 because
the capture only works in synchronous mode.

Change:
//**T1CON00111100;
To:
T1CON00110000;

I didn't debug any further than getting the PWM output to stay alive
after T1CON was changed.

I learn a great deal from these problems - I probably would have been
caught by this sooner or later. Thanks!
--- In , Sanjyot Katti <sanjyotkatti@y...>
wrote:
> Dear Friend,
>
> Thank you for your reply ... but still my problem
> persists. As for your point on ISR, I am using ISR for
> PWM just to clear the TMR2IF flag. that's all.
> I am not using CCP1IF interrupt for capture.
>
> Basically there are 2 things :
>
> 1) I have configured CCP2 module for PWM mode to
> generate a PWM for 1 KHz on RC1 pin (configured as
> output). This configuration I have done at the start
> of my program.
>
> 2) At the same time I am measuring an incoming square
> wave on RC2 pin. For this I configure CCP1 module for
> capture mode. I call a function (frequency) in which
> after configuration I detect the time period between 2
> rising edges on RC2 and return to my main function.
>
> Problem : At start I get PWM till I enter my frequency
> measurement function. After I return from this
> function
> my PWM wave vanishes from RC1 pin. Even after
> reconfiguring CCP2 for PWM I can't generate a PWM on
> RC1.
>
> I am sending my code as attachment .. I have tried to
> describe most of the operation in it.
>
> If you can suggest anything ... it will be of great
> help ...
>
> Thanks for your help,
>
> Sanjyot
>
> --- rtstofer <rstofer@p...> wrote: >
> > I must have been brain dead this morning - why would
> > you have an ISR
> > for the PWM. Basically, you set the frequency
> > during setup and
> > change the pulse width whenever you want. It could
> > be inside an ISR
> > for some other device but the PWM itself doesn't use
> > interrupts.
> >
> >
> > --- In , "rtstofer"
> > <rstofer@p...> wrote:
> > >
> > > The CCP modules are independent and it would not
> > appear that your
> > use
> > > would not have conflicts. As you are using
> > interrupts, are you
> > sure
> > > that you process both interrupts in the routine,
> > not just 1 or the
> > > other?
> > >
> > > In section 8.2 of the 16F73 datasheet there is a
> > reference to other
> > > literature re: the use of the CCP modules.
> > >
> > > You could post your code, perhaps someone could
> > take a look
> > >
> > > --- In , Sanjyot Katti
> > <sanjyotkatti@y...>
> > > wrote:
> > > > Hello Everybody,
> > > >
> > > > I am using PIC 16F73 micro which has 2 CCP
> > > > modules.
> > > > The hardwrae is basically a motor controller +
> > data
> > > > acqusition card. CCP1 module I have configured
> > for
> > > > capture mode to measure frequency. CCP2 module
> > is
> > > > configured for PWM mode. I have to use both the
> > CCP
> > > > module simutaneously.
> > > > But it seems that whenever I start my frequency
> > > > measurement my PWM operation is stopped.
> > > >
> > > > I have used ISR's for both. My code is in C
> > using
> > > > Hitech C Cross compiler. My hardware is Ok becoz
> > if I
> > > > generate only PWM or measure frequency only I
> > get the
> > > > desired output. When I tried combining both the
> > codes
> > > > I am able to measure only frequency correctly
> > but no
> > > > PWM. I have referred the data sheet for PIC
> > 16F73. It
> > > > says both CCP modules are independent.
> > > >
> > > > Any suggestions ????
> > > >
> > > > Sanjyot
> > > >
> > > >
> > > >
> > > >
> > >
> >
>
______________________________________________________________________
> > > __
> > > > Send free SMS using the Yahoo! Messenger. Go to
> > > http://in.mobile.yahoo.com/new/pc/
> >
> >
> > ------------------------ Yahoo! Groups Sponsor
> >
> > to unsubscribe, go to http://www.yahoogroups.com and
> > follow the instructions
> >
> > ">http://docs.yahoo.com/info/terms/
> >
>
______________________________________________________________________
__
> Send free SMS using the Yahoo! Messenger. Go to
http://in.mobile.yahoo.com/new/pc/
> /*
>
> Program Code for Data acquistion and motor control
>
> Measure parameters :
>
> 1) Input Voltage
> 2) Ouput Voltage
> 3) Output Current
> 4) Output Frequency
>
> Project Description :
>
> The first 3 parameters are given as 5V AC signal to
the hardware board form
> 5-0-5 transformers. These signals are then rectified
to equivalent DC voltage
> and given to ADC pins RA0-RA2 of PIC 16F73. Now 5V AC
signal of Output Voltage
> is convert onboard into square wave and given to RC2
pin for frequency measurement.
>
>
> If the output voltage signal deviates from fixed
voltage say 230V.
> Only then I am supposed to generate a PWM signal.

>
>
> CCP1 module is used in Capture Mode (RC2 configured
as Input)
> CCP2 module is used for PWM generation (RC1
configured as Output)
>
> PWM Frequency = 1 KHz. Time Period = 1 millisecond
>
> Incoming Output Frequency on RC2 pin is 45 to 55 Hz.
>
> Crystal Used = 11.0592 MHZ. (To get 1 KHz PWM)
>
> I have to measure the incoming frequency on RC2 pin
using CCP1 module in
> capture mode. After which I am supposed to display
the frequency value on a LCD.
>
> Along with it I have to monitor the incoming signals
on my Analog pins.
> If the output voltage appearing on my RA1 pin varies
from its previously
> Set Voltage = 230V (say), only then I am supposed to
generate a PWM to
> drive a motor.
>
> I am using ISR for PWM generation because it is more
critical in my case.
> In ISR all I am doing is clearing TMR2IF flag since I
want a continuous PWM
> till my incoming Output voltage signal returns to set
value.
>
> Though frequency measurement is not very critical I
have to measure it for
> display on LCD. So I have not used ISR for it. I just
call frequency function
> which will capture the incoming signal and set global
variable which holds
> the current Output Frequency value. This value is
then displayed on LCD.
>
>
> Problem :
>
> During my initial code writing I wrote both the code
for PWM generation and
> frequency measurement separately. Both worked fine
when programmed independently.
>
> But when I integrated both I could measure only
frequency, but my PWM generation was
> stopped.
>
> What I did is simple just configured CCP2 module for
PWM generation
> at the start of program. Then initialized my LCD.
Called my frequency measurement
> function which configures CCP1 module for capture
mode.
>
>
> Observed Result :
>
> At start immediately when after configuration for
PWM, I get the PWM output.
> But the moment it goes into my frequency measurement
function my PWM generation
> is stopped. After this I cannot generate PWM at all.
>
> I tried reconfiguring my CCP2 module for PWM once
again after I return from
> frequency function. But still I don't get the PWM.
>
> The moment I configure CCP1 for capture my PWM using
CCP2 vanishes ?????
>
> I tried using ISR for Capture (check CCP1IF) and
TMR2IF for PWM ...
> but with same result.
>
> I programmed PIC16F873 with same code but still the
same result.
>
> Note :
> I have not listed the Lcd initialization and display
code below because
> it is irrelevant and is working fine.
>
> Only the code having problem is listed.
>
> */ >
> // Include files
>
> #include <pic.h > // Declarations
>
> static bit TRISPWMWAVE@(unsigned)&TRISC*8+1; // TRISC pin1
> static bit TRISFREQ@(unsigned)&TRISC*8+2; // TRISC pin2
>
> // Variable declarations
>
> bit flag_value_captured = 0; > // Main Program begins
>
> main()
> {
>
> // Configure CCP2 module for PWM
>
> TMR2 = 0x00; // Clear Timer2
> PR2 = 0xAB; // PWM Period is set (PWM PERIOD of 1
millisecond)
> CCPR2L = 0x56; // Duty Cycle is 50% of PWM Period
>
> CCPR2H = 0x00;
>
> INTCON=0xC0; // Enable GIE and PEIE bits
>
> PIE1 = 0x00; // Clear PIE and PIR registers
> PIR1 = 0x00; > TMR2IE = 1; // Enable Timer2 Overfow interrupt > TRISPWMWAVE = 0; // Configure RC1 as Output pin
>
> T2CON = 0x03; // Configure Timer2 control register
for 1 : 16 prescale
>
> CCP2CON = 0x0C; // Configure CCP2CON for PWM mode
>
> TMR2ON=1; // Start Timer2 > // LCD initialization routine
> // Code not listed > while()
> {
>
> // Frequency measurement function using capture
>
> frequency();
>
> // Frequency value displayed on LCD
> // Code not listed > // Delay ... wait for some time
> // Code not listed
> // Continue
>
> } // while ends > } >
> // Interupt Service Routine
>
> void interrupt isr()
> {
> unsigned long int icnt; > // Check for TMR2IF interrupt
>
> if(TMR2IF)
> {
> TMR2IF = 0;
> } > return;
> } > // THIS FUNCTION IS TO CALCULATE FREQUENCY
>
> void frequency(void)
> {
> unsigned int freqLcount, freqHcount;
> unsigned int time_out; > // Variable time_out is used as a software counter to come
out
> // of frequency function if no square wave (frequency) signal
appears
> // on RC2 pin for Capture operation.
>
> time_out = 5000;
>
> // Global flag indicating value captured
>
> flag_value_captured = 0; > // freqLcount and freqHcount registers hold the captured
CCPR1L and CCPR1H register values
> // Cleared at the start
>
> freqLcount = 0;
> freqHcount = 0; > // Clear Timer1 and CCPR1 registers
>
> TMR1H=0;
> TMR1L=0;
>
> CCPR1L=0;
> CCPR1H=0; > // Configure RC2 (PORTC pin 1) as input
>
> TRISFREQ = 1;
>
> // Disable CCP1IE interrupt
>
> CCP1IE=0;
>
> // Clear CCP1IF interrupt flag
>
> CCP1IF=0;
>
> // Configure Timer1 with prescale value of 1:8
>
> T1CON00111100;
>
> // Configure CCP1CON for every rising edge
>
> CCP1CON00000101; > // Start timer1 for capture
>
> TMR1ON=1; > // This loop is used to detect the rising edge of incoming
Square or pulse waveform
> // During this wait timeout variable is decremented
> // This is required since if there is no incoming signal the
control will remain in
> // frequency function. So
>
> while(CCP1IF==0)
> {
> time_out--;
>
> if(time_out < 2) // If no signal or
rising edge detected
> break; // exit loop
> else
> continue;
>
> } > // Stop timer1
>
> TMR1ON=0;
>
> // Clear Timer1 L and H registers
>
> TMR1H=0;
> TMR1L=0;
>
> // Clear CCP1IF flag and restart Timer1 to detect next rising
edge
>
> if(CCP1IF)
> {
> CCP1IF=0;
> TMR1ON = 1;
> } > // Reload the time_out register
>
> time_out = 5000; > // Detect the next rising edge to get the total time period
between
> // two successive rising edges
>
> while(CCP1IF==0)
> {
>
> time_out--;
>
> if(time_out < 2)
> break;
> else
> continue;
> }
>
> // Clear CCP1IF flag and store the values captured in user
defined registers
>
> if(CCP1IF)
> {
> TMR1ON=0; // Stop timer1
> CCP1IF=0
>
> freqLcount = CCPR1L; // Load
Captured values in user defined registers
> freqHcount = CCPR1H;
>
> flag_value_captured = 1; // Flag to indicate
value captured
>
> } > // Code for converting time period into frequency
> // It is basically combinning the lower and higher bytes into
an integer variable
> // The performing the calculations using :
>
> // Frequency = 1 / Timeperiod (Total 16 bit captured
value)
>
> // Return from function
>
> return;
> }






Section 3.3 of the datasheet advises against using bit operations on
TRISC. This doesn't seem to be an issue but it might be something to
look out for. --- In , "rtstofer" <rstofer@p...> wrote:
>
> Read carefully section 6.5 of the manual - you were turning pin C1
> into part of an oscillator circuit. Also check section 8.1.2
because
> the capture only works in synchronous mode.
>
> Change:
> //**T1CON00111100;
> To:
> T1CON00110000;
>
> I didn't debug any further than getting the PWM output to stay
alive
> after T1CON was changed.
>
> I learn a great deal from these problems - I probably would have
been
> caught by this sooner or later. Thanks! >
> --- In , Sanjyot Katti <sanjyotkatti@y...>
> wrote:
> > Dear Friend,
> >
> > Thank you for your reply ... but still my problem
> > persists. As for your point on ISR, I am using ISR for
> > PWM just to clear the TMR2IF flag. that's all.
> > I am not using CCP1IF interrupt for capture.
> >
> > Basically there are 2 things :
> >
> > 1) I have configured CCP2 module for PWM mode to
> > generate a PWM for 1 KHz on RC1 pin (configured as
> > output). This configuration I have done at the start
> > of my program.
> >
> > 2) At the same time I am measuring an incoming square
> > wave on RC2 pin. For this I configure CCP1 module for
> > capture mode. I call a function (frequency) in which
> > after configuration I detect the time period between 2
> > rising edges on RC2 and return to my main function.
> >
> > Problem : At start I get PWM till I enter my frequency
> > measurement function. After I return from this
> > function
> > my PWM wave vanishes from RC1 pin. Even after
> > reconfiguring CCP2 for PWM I can't generate a PWM on
> > RC1.
> >
> > I am sending my code as attachment .. I have tried to
> > describe most of the operation in it.
> >
> > If you can suggest anything ... it will be of great
> > help ...
> >
> > Thanks for your help,
> >
> > Sanjyot
> >
> > --- rtstofer <rstofer@p...> wrote: >
> > > I must have been brain dead this morning - why would
> > > you have an ISR
> > > for the PWM. Basically, you set the frequency
> > > during setup and
> > > change the pulse width whenever you want. It could
> > > be inside an ISR
> > > for some other device but the PWM itself doesn't use
> > > interrupts.
> > >
> > >
> > > --- In , "rtstofer"
> > > <rstofer@p...> wrote:
> > > >
> > > > The CCP modules are independent and it would not
> > > appear that your
> > > use
> > > > would not have conflicts. As you are using
> > > interrupts, are you
> > > sure
> > > > that you process both interrupts in the routine,
> > > not just 1 or the
> > > > other?
> > > >
> > > > In section 8.2 of the 16F73 datasheet there is a
> > > reference to other
> > > > literature re: the use of the CCP modules.
> > > >
> > > > You could post your code, perhaps someone could
> > > take a look
> > > >
> > > > --- In , Sanjyot Katti
> > > <sanjyotkatti@y...>
> > > > wrote:
> > > > > Hello Everybody,
> > > > >
> > > > > I am using PIC 16F73 micro which has 2 CCP
> > > > > modules.
> > > > > The hardwrae is basically a motor controller +
> > > data
> > > > > acqusition card. CCP1 module I have configured
> > > for
> > > > > capture mode to measure frequency. CCP2 module
> > > is
> > > > > configured for PWM mode. I have to use both the
> > > CCP
> > > > > module simutaneously.
> > > > > But it seems that whenever I start my frequency
> > > > > measurement my PWM operation is stopped.
> > > > >
> > > > > I have used ISR's for both. My code is in C
> > > using
> > > > > Hitech C Cross compiler. My hardware is Ok becoz
> > > if I
> > > > > generate only PWM or measure frequency only I
> > > get the
> > > > > desired output. When I tried combining both the
> > > codes
> > > > > I am able to measure only frequency correctly
> > > but no
> > > > > PWM. I have referred the data sheet for PIC
> > > 16F73. It
> > > > > says both CCP modules are independent.
> > > > >
> > > > > Any suggestions ????
> > > > >
> > > > > Sanjyot
> > > > >
> > > > >
> > > > >
> > > > >
> > > >
> > >
> >
>
______________________________________________________________________
> > > > __
> > > > > Send free SMS using the Yahoo! Messenger. Go to
> > > > http://in.mobile.yahoo.com/new/pc/
> > >
> > >
> > > ------------------------ Yahoo! Groups Sponsor
> > >
> > > to unsubscribe, go to http://www.yahoogroups.com and
> > > follow the instructions
> > >
> > > ">http://docs.yahoo.com/info/terms/
> > >
> > >
> >
> >
>
______________________________________________________________________
> __
> > Send free SMS using the Yahoo! Messenger. Go to
> http://in.mobile.yahoo.com/new/pc/
> > /*
> >
> > Program Code for Data acquistion and motor control
> >
> > Measure parameters :
> >
> > 1) Input Voltage
> > 2) Ouput Voltage
> > 3) Output Current
> > 4) Output Frequency
> >
> > Project Description :
> >
> > The first 3 parameters are given as 5V AC signal to
> the hardware board form
> > 5-0-5 transformers. These signals are then rectified
> to equivalent DC voltage
> > and given to ADC pins RA0-RA2 of PIC 16F73. Now 5V AC
> signal of Output Voltage
> > is convert onboard into square wave and given to RC2
> pin for frequency measurement.
> >
> >
> > If the output voltage signal deviates from fixed
> voltage say 230V.
> > Only then I am supposed to generate a PWM signal.
>
> >
> >
> > CCP1 module is used in Capture Mode (RC2 configured
> as Input)
> > CCP2 module is used for PWM generation (RC1
> configured as Output)
> >
> > PWM Frequency = 1 KHz. Time Period = 1 millisecond
> >
> > Incoming Output Frequency on RC2 pin is 45 to 55 Hz.
> >
> > Crystal Used = 11.0592 MHZ. (To get 1 KHz PWM)
> >
> > I have to measure the incoming frequency on RC2 pin
> using CCP1 module in
> > capture mode. After which I am supposed to display
> the frequency value on a LCD.
> >
> > Along with it I have to monitor the incoming signals
> on my Analog pins.
> > If the output voltage appearing on my RA1 pin varies
> from its previously
> > Set Voltage = 230V (say), only then I am supposed to
> generate a PWM to
> > drive a motor.
> >
> > I am using ISR for PWM generation because it is more
> critical in my case.
> > In ISR all I am doing is clearing TMR2IF flag since I
> want a continuous PWM
> > till my incoming Output voltage signal returns to set
> value.
> >
> > Though frequency measurement is not very critical I
> have to measure it for
> > display on LCD. So I have not used ISR for it. I just
> call frequency function
> > which will capture the incoming signal and set global
> variable which holds
> > the current Output Frequency value. This value is
> then displayed on LCD.
> >
> >
> > Problem :
> >
> > During my initial code writing I wrote both the code
> for PWM generation and
> > frequency measurement separately. Both worked fine
> when programmed independently.
> >
> > But when I integrated both I could measure only
> frequency, but my PWM generation was
> > stopped.
> >
> > What I did is simple just configured CCP2 module for
> PWM generation
> > at the start of program. Then initialized my LCD.
> Called my frequency measurement
> > function which configures CCP1 module for capture
> mode.
> >
> >
> > Observed Result :
> >
> > At start immediately when after configuration for
> PWM, I get the PWM output.
> > But the moment it goes into my frequency measurement
> function my PWM generation
> > is stopped. After this I cannot generate PWM at all.
> >
> > I tried reconfiguring my CCP2 module for PWM once
> again after I return from
> > frequency function. But still I don't get the PWM.
> >
> > The moment I configure CCP1 for capture my PWM using
> CCP2 vanishes ?????
> >
> > I tried using ISR for Capture (check CCP1IF) and
> TMR2IF for PWM ...
> > but with same result.
> >
> > I programmed PIC16F873 with same code but still the
> same result.
> >
> > Note :
> > I have not listed the Lcd initialization and display
> code below because
> > it is irrelevant and is working fine.
> >
> > Only the code having problem is listed.
> >
> > */
> >
> >
> >
> > // Include files
> >
> > #include <pic.h>
> >
> >
> > // Declarations
> >
> > static bit TRISPWMWAVE@(unsigned)&TRISC*8+1; // TRISC pin1
> > static bit TRISFREQ@(unsigned)&TRISC*8+2; // TRISC pin2
> >
> > // Variable declarations
> >
> > bit flag_value_captured = 0;
> >
> >
> > // Main Program begins
> >
> > main()
> > {
> >
> > // Configure CCP2 module for PWM
> >
> > TMR2 = 0x00; // Clear Timer2
> > PR2 = 0xAB; // PWM Period is set (PWM
PERIOD of 1
> millisecond)
> > CCPR2L = 0x56; // Duty Cycle is 50% of PWM
Period
> >
> > CCPR2H = 0x00;
> >
> > INTCON=0xC0; // Enable GIE and PEIE bits
> >
> > PIE1 = 0x00; // Clear PIE and PIR registers
> > PIR1 = 0x00;
> >
> >
> > TMR2IE = 1; // Enable Timer2 Overfow interrupt
> >
> >
> > TRISPWMWAVE = 0; // Configure RC1 as Output pin
> >
> > T2CON = 0x03; // Configure Timer2 control
register
> for 1 : 16 prescale
> >
> > CCP2CON = 0x0C; // Configure CCP2CON for PWM mode
> >
> > TMR2ON=1; // Start Timer2
> >
> >
> > // LCD initialization routine
> > // Code not listed
> >
> >
> > while()
> > {
> >
> > // Frequency measurement function using capture
> >
> > frequency();
> >
> > // Frequency value displayed on LCD
> > // Code not listed
> >
> >
> > // Delay ... wait for some time
> > // Code not listed
> > // Continue
> >
> > } // while ends
> >
> >
> > }
> >
> >
> >
> > // Interupt Service Routine
> >
> > void interrupt isr()
> > {
> > unsigned long int icnt;
> >
> >
> > // Check for TMR2IF interrupt
> >
> > if(TMR2IF)
> > {
> > TMR2IF = 0;
> > }
> >
> >
> > return;
> > }
> >
> >
> > // THIS FUNCTION IS TO CALCULATE FREQUENCY
> >
> > void frequency(void)
> > {
> > unsigned int freqLcount, freqHcount;
> > unsigned int time_out;
> >
> >
> > // Variable time_out is used as a software counter to come
> out
> > // of frequency function if no square wave (frequency) signal
> appears
> > // on RC2 pin for Capture operation.
> >
> > time_out = 5000;
> >
> > // Global flag indicating value captured
> >
> > flag_value_captured = 0;
> >
> >
> > // freqLcount and freqHcount registers hold the captured
> CCPR1L and CCPR1H register values
> > // Cleared at the start
> >
> > freqLcount = 0;
> > freqHcount = 0;
> >
> >
> > // Clear Timer1 and CCPR1 registers
> >
> > TMR1H=0;
> > TMR1L=0;
> >
> > CCPR1L=0;
> > CCPR1H=0;
> >
> >
> > // Configure RC2 (PORTC pin 1) as input
> >
> > TRISFREQ = 1;
> >
> > // Disable CCP1IE interrupt
> >
> > CCP1IE=0;
> >
> > // Clear CCP1IF interrupt flag
> >
> > CCP1IF=0;
> >
> > // Configure Timer1 with prescale value of 1:8
> >
> > T1CON00111100;
> >
> > // Configure CCP1CON for every rising edge
> >
> > CCP1CON00000101;
> >
> >
> > // Start timer1 for capture
> >
> > TMR1ON=1;
> >
> >
> > // This loop is used to detect the rising edge of incoming
> Square or pulse waveform
> > // During this wait timeout variable is decremented
> > // This is required since if there is no incoming signal the
> control will remain in
> > // frequency function. So
> >
> > while(CCP1IF==0)
> > {
> > time_out--;
> >
> > if(time_out < 2) // If no signal or
> rising edge detected
> > break; // exit loop
> > else
> > continue;
> >
> > }
> >
> >
> > // Stop timer1
> >
> > TMR1ON=0;
> >
> > // Clear Timer1 L and H registers
> >
> > TMR1H=0;
> > TMR1L=0;
> >
> > // Clear CCP1IF flag and restart Timer1 to detect next rising
> edge
> >
> > if(CCP1IF)
> > {
> > CCP1IF=0;
> > TMR1ON = 1;
> > }
> >
> >
> > // Reload the time_out register
> >
> > time_out = 5000;
> >
> >
> > // Detect the next rising edge to get the total time period
> between
> > // two successive rising edges
> >
> > while(CCP1IF==0)
> > {
> >
> > time_out--;
> >
> > if(time_out < 2)
> > break;
> > else
> > continue;
> > }
> >
> > // Clear CCP1IF flag and store the values captured in user
> defined registers
> >
> > if(CCP1IF)
> > {
> > TMR1ON=0; // Stop timer1
> > CCP1IF=0
> >
> > freqLcount = CCPR1L; // Load
> Captured values in user defined registers
> > freqHcount = CCPR1H;
> >
> > flag_value_captured = 1; // Flag to indicate
> value captured
> >
> > }
> >
> >
> > // Code for converting time period into frequency
> > // It is basically combinning the lower and higher bytes into
> an integer variable
> > // The performing the calculations using :
> >
> > // Frequency = 1 / Timeperiod (Total 16 bit captured
> value)
> >
> >
> >
> >
> > // Return from function
> >
> > return;
> > }






All of the section numbers referenced in my prior 2 responses refer
to the 16F877 datasheet - sections 3.3, 6.5 & 8.1.2.

For the 16F73 the sections are 4.3, 6.5, 8.3.2. --- In , "rtstofer" <rstofer@p...> wrote:
>
> Section 3.3 of the datasheet advises against using bit operations
on
> TRISC. This doesn't seem to be an issue but it might be something
to
> look out for. > --- In , "rtstofer" <rstofer@p...> wrote:
> >
> > Read carefully section 6.5 of the manual - you were turning pin
C1
> > into part of an oscillator circuit. Also check section 8.1.2
> because
> > the capture only works in synchronous mode.
> >
> > Change:
> > //**T1CON00111100;
> > To:
> > T1CON00110000;
> >
> > I didn't debug any further than getting the PWM output to stay
> alive
> > after T1CON was changed.
> >
> > I learn a great deal from these problems - I probably would have
> been
> > caught by this sooner or later. Thanks!
> >
> >
> >
> > --- In , Sanjyot Katti <sanjyotkatti@y...>
> > wrote:
> > > Dear Friend,
> > >
> > > Thank you for your reply ... but still my problem
> > > persists. As for your point on ISR, I am using ISR for
> > > PWM just to clear the TMR2IF flag. that's all.
> > > I am not using CCP1IF interrupt for capture.
> > >
> > > Basically there are 2 things :
> > >
> > > 1) I have configured CCP2 module for PWM mode to
> > > generate a PWM for 1 KHz on RC1 pin (configured as
> > > output). This configuration I have done at the start
> > > of my program.
> > >
> > > 2) At the same time I am measuring an incoming square
> > > wave on RC2 pin. For this I configure CCP1 module for
> > > capture mode. I call a function (frequency) in which
> > > after configuration I detect the time period between 2
> > > rising edges on RC2 and return to my main function.
> > >
> > > Problem : At start I get PWM till I enter my frequency
> > > measurement function. After I return from this
> > > function
> > > my PWM wave vanishes from RC1 pin. Even after
> > > reconfiguring CCP2 for PWM I can't generate a PWM on
> > > RC1.
> > >
> > > I am sending my code as attachment .. I have tried to
> > > describe most of the operation in it.
> > >
> > > If you can suggest anything ... it will be of great
> > > help ...
> > >
> > > Thanks for your help,
> > >
> > > Sanjyot
> > >
> > > --- rtstofer <rstofer@p...> wrote: >
> > > > I must have been brain dead this morning - why would
> > > > you have an ISR
> > > > for the PWM. Basically, you set the frequency
> > > > during setup and
> > > > change the pulse width whenever you want. It could
> > > > be inside an ISR
> > > > for some other device but the PWM itself doesn't use
> > > > interrupts.
> > > >
> > > >
> > > > --- In , "rtstofer"
> > > > <rstofer@p...> wrote:
> > > > >
> > > > > The CCP modules are independent and it would not
> > > > appear that your
> > > > use
> > > > > would not have conflicts. As you are using
> > > > interrupts, are you
> > > > sure
> > > > > that you process both interrupts in the routine,
> > > > not just 1 or the
> > > > > other?
> > > > >
> > > > > In section 8.2 of the 16F73 datasheet there is a
> > > > reference to other
> > > > > literature re: the use of the CCP modules.
> > > > >
> > > > > You could post your code, perhaps someone could
> > > > take a look
> > > > >
> > > > > --- In , Sanjyot Katti
> > > > <sanjyotkatti@y...>
> > > > > wrote:
> > > > > > Hello Everybody,
> > > > > >
> > > > > > I am using PIC 16F73 micro which has 2 CCP
> > > > > > modules.
> > > > > > The hardwrae is basically a motor controller +
> > > > data
> > > > > > acqusition card. CCP1 module I have configured
> > > > for
> > > > > > capture mode to measure frequency. CCP2 module
> > > > is
> > > > > > configured for PWM mode. I have to use both the
> > > > CCP
> > > > > > module simutaneously.
> > > > > > But it seems that whenever I start my frequency
> > > > > > measurement my PWM operation is stopped.
> > > > > >
> > > > > > I have used ISR's for both. My code is in C
> > > > using
> > > > > > Hitech C Cross compiler. My hardware is Ok becoz
> > > > if I
> > > > > > generate only PWM or measure frequency only I
> > > > get the
> > > > > > desired output. When I tried combining both the
> > > > codes
> > > > > > I am able to measure only frequency correctly
> > > > but no
> > > > > > PWM. I have referred the data sheet for PIC
> > > > 16F73. It
> > > > > > says both CCP modules are independent.
> > > > > >
> > > > > > Any suggestions ????
> > > > > >
> > > > > > Sanjyot
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>
______________________________________________________________________
> > > > > __
> > > > > > Send free SMS using the Yahoo! Messenger. Go to
> > > > > http://in.mobile.yahoo.com/new/pc/
> > > >
> > > >
> > > > ------------------------ Yahoo! Groups Sponsor
> > > >
> > > > to unsubscribe, go to http://www.yahoogroups.com and
> > > > follow the instructions
> > > >
> > > > ">http://docs.yahoo.com/info/terms/
> > > >
> > > >
> > >
> > >
> >
>
______________________________________________________________________
> > __
> > > Send free SMS using the Yahoo! Messenger. Go to
> > http://in.mobile.yahoo.com/new/pc/
> > > /*
> > >
> > > Program Code for Data acquistion and motor control
> > >
> > > Measure parameters :
> > >
> > > 1) Input Voltage
> > > 2) Ouput Voltage
> > > 3) Output Current
> > > 4) Output Frequency
> > >
> > > Project Description :
> > >
> > > The first 3 parameters are given as 5V AC signal to
> > the hardware board form
> > > 5-0-5 transformers. These signals are then rectified
> > to equivalent DC voltage
> > > and given to ADC pins RA0-RA2 of PIC 16F73. Now 5V AC
> > signal of Output Voltage
> > > is convert onboard into square wave and given to RC2
> > pin for frequency measurement.
> > >
> > >
> > > If the output voltage signal deviates from fixed
> > voltage say 230V.
> > > Only then I am supposed to generate a PWM signal.
> >
> > >
> > >
> > > CCP1 module is used in Capture Mode (RC2 configured
> > as Input)
> > > CCP2 module is used for PWM generation (RC1
> > configured as Output)
> > >
> > > PWM Frequency = 1 KHz. Time Period = 1 millisecond
> > >
> > > Incoming Output Frequency on RC2 pin is 45 to 55 Hz.
> > >
> > > Crystal Used = 11.0592 MHZ. (To get 1 KHz PWM)
> > >
> > > I have to measure the incoming frequency on RC2 pin
> > using CCP1 module in
> > > capture mode. After which I am supposed to display
> > the frequency value on a LCD.
> > >
> > > Along with it I have to monitor the incoming signals
> > on my Analog pins.
> > > If the output voltage appearing on my RA1 pin varies
> > from its previously
> > > Set Voltage = 230V (say), only then I am supposed to
> > generate a PWM to
> > > drive a motor.
> > >
> > > I am using ISR for PWM generation because it is more
> > critical in my case.
> > > In ISR all I am doing is clearing TMR2IF flag since I
> > want a continuous PWM
> > > till my incoming Output voltage signal returns to set
> > value.
> > >
> > > Though frequency measurement is not very critical I
> > have to measure it for
> > > display on LCD. So I have not used ISR for it. I just
> > call frequency function
> > > which will capture the incoming signal and set global
> > variable which holds
> > > the current Output Frequency value. This value is
> > then displayed on LCD.
> > >
> > >
> > > Problem :
> > >
> > > During my initial code writing I wrote both the code
> > for PWM generation and
> > > frequency measurement separately. Both worked fine
> > when programmed independently.
> > >
> > > But when I integrated both I could measure only
> > frequency, but my PWM generation was
> > > stopped.
> > >
> > > What I did is simple just configured CCP2 module for
> > PWM generation
> > > at the start of program. Then initialized my LCD.
> > Called my frequency measurement
> > > function which configures CCP1 module for capture
> > mode.
> > >
> > >
> > > Observed Result :
> > >
> > > At start immediately when after configuration for
> > PWM, I get the PWM output.
> > > But the moment it goes into my frequency measurement
> > function my PWM generation
> > > is stopped. After this I cannot generate PWM at all.
> > >
> > > I tried reconfiguring my CCP2 module for PWM once
> > again after I return from
> > > frequency function. But still I don't get the PWM.
> > >
> > > The moment I configure CCP1 for capture my PWM using
> > CCP2 vanishes ?????
> > >
> > > I tried using ISR for Capture (check CCP1IF) and
> > TMR2IF for PWM ...
> > > but with same result.
> > >
> > > I programmed PIC16F873 with same code but still the
> > same result.
> > >
> > > Note :
> > > I have not listed the Lcd initialization and display
> > code below because
> > > it is irrelevant and is working fine.
> > >
> > > Only the code having problem is listed.
> > >
> > > */
> > >
> > >
> > >
> > > // Include files
> > >
> > > #include <pic.h>
> > >
> > >
> > > // Declarations
> > >
> > > static bit TRISPWMWAVE@(unsigned)&TRISC*8+1; // TRISC pin1
> > > static bit TRISFREQ@(unsigned)&TRISC*8+2; // TRISC pin2
> > >
> > > // Variable declarations
> > >
> > > bit flag_value_captured = 0;
> > >
> > >
> > > // Main Program begins
> > >
> > > main()
> > > {
> > >
> > > // Configure CCP2 module for PWM
> > >
> > > TMR2 = 0x00; // Clear Timer2
> > > PR2 = 0xAB; // PWM Period is set (PWM
> PERIOD of 1
> > millisecond)
> > > CCPR2L = 0x56; // Duty Cycle is 50% of PWM
> Period
> > >
> > > CCPR2H = 0x00;
> > >
> > > INTCON=0xC0; // Enable GIE and PEIE bits
> > >
> > > PIE1 = 0x00; // Clear PIE and PIR registers
> > > PIR1 = 0x00;
> > >
> > >
> > > TMR2IE = 1; // Enable Timer2 Overfow interrupt
> > >
> > >
> > > TRISPWMWAVE = 0; // Configure RC1 as Output pin
> > >
> > > T2CON = 0x03; // Configure Timer2 control
> register
> > for 1 : 16 prescale
> > >
> > > CCP2CON = 0x0C; // Configure CCP2CON for PWM mode
> > >
> > > TMR2ON=1; // Start Timer2
> > >
> > >
> > > // LCD initialization routine
> > > // Code not listed
> > >
> > >
> > > while()
> > > {
> > >
> > > // Frequency measurement function using capture
> > >
> > > frequency();
> > >
> > > // Frequency value displayed on LCD
> > > // Code not listed
> > >
> > >
> > > // Delay ... wait for some time
> > > // Code not listed
> > > // Continue
> > >
> > > } // while ends
> > >
> > >
> > > }
> > >
> > >
> > >
> > > // Interupt Service Routine
> > >
> > > void interrupt isr()
> > > {
> > > unsigned long int icnt;
> > >
> > >
> > > // Check for TMR2IF interrupt
> > >
> > > if(TMR2IF)
> > > {
> > > TMR2IF = 0;
> > > }
> > >
> > >
> > > return;
> > > }
> > >
> > >
> > > // THIS FUNCTION IS TO CALCULATE FREQUENCY
> > >
> > > void frequency(void)
> > > {
> > > unsigned int freqLcount, freqHcount;
> > > unsigned int time_out;
> > >
> > >
> > > // Variable time_out is used as a software counter to come
> > out
> > > // of frequency function if no square wave (frequency) signal
> > appears
> > > // on RC2 pin for Capture operation.
> > >
> > > time_out = 5000;
> > >
> > > // Global flag indicating value captured
> > >
> > > flag_value_captured = 0;
> > >
> > >
> > > // freqLcount and freqHcount registers hold the captured
> > CCPR1L and CCPR1H register values
> > > // Cleared at the start
> > >
> > > freqLcount = 0;
> > > freqHcount = 0;
> > >
> > >
> > > // Clear Timer1 and CCPR1 registers
> > >
> > > TMR1H=0;
> > > TMR1L=0;
> > >
> > > CCPR1L=0;
> > > CCPR1H=0;
> > >
> > >
> > > // Configure RC2 (PORTC pin 1) as input
> > >
> > > TRISFREQ = 1;
> > >
> > > // Disable CCP1IE interrupt
> > >
> > > CCP1IE=0;
> > >
> > > // Clear CCP1IF interrupt flag
> > >
> > > CCP1IF=0;
> > >
> > > // Configure Timer1 with prescale value of 1:8
> > >
> > > T1CON00111100;
> > >
> > > // Configure CCP1CON for every rising edge
> > >
> > > CCP1CON00000101;
> > >
> > >
> > > // Start timer1 for capture
> > >
> > > TMR1ON=1;
> > >
> > >
> > > // This loop is used to detect the rising edge of incoming
> > Square or pulse waveform
> > > // During this wait timeout variable is decremented
> > > // This is required since if there is no incoming signal the
> > control will remain in
> > > // frequency function. So
> > >
> > > while(CCP1IF==0)
> > > {
> > > time_out--;
> > >
> > > if(time_out < 2) // If no signal or
> > rising edge detected
> > > break; // exit loop
> > > else
> > > continue;
> > >
> > > }
> > >
> > >
> > > // Stop timer1
> > >
> > > TMR1ON=0;
> > >
> > > // Clear Timer1 L and H registers
> > >
> > > TMR1H=0;
> > > TMR1L=0;
> > >
> > > // Clear CCP1IF flag and restart Timer1 to detect next rising
> > edge
> > >
> > > if(CCP1IF)
> > > {
> > > CCP1IF=0;
> > > TMR1ON = 1;
> > > }
> > >
> > >
> > > // Reload the time_out register
> > >
> > > time_out = 5000;
> > >
> > >
> > > // Detect the next rising edge to get the total time period
> > between
> > > // two successive rising edges
> > >
> > > while(CCP1IF==0)
> > > {
> > >
> > > time_out--;
> > >
> > > if(time_out < 2)
> > > break;
> > > else
> > > continue;
> > > }
> > >
> > > // Clear CCP1IF flag and store the values captured in user
> > defined registers
> > >
> > > if(CCP1IF)
> > > {
> > > TMR1ON=0; // Stop timer1
> > > CCP1IF=0
> > >
> > > freqLcount = CCPR1L; // Load
> > Captured values in user defined registers
> > > freqHcount = CCPR1H;
> > >
> > > flag_value_captured = 1; // Flag to
indicate
> > value captured
> > >
> > > }
> > >
> > >
> > > // Code for converting time period into frequency
> > > // It is basically combinning the lower and higher bytes into
> > an integer variable
> > > // The performing the calculations using :
> > >
> > > // Frequency = 1 / Timeperiod (Total 16 bit captured
> > value)
> > >
> > >
> > >
> > >
> > > // Return from function
> > >
> > > return;
> > > }





The 2024 Embedded Online Conference