EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Measuring PWM /w input capture ECT

Started by jeffguilfoil December 26, 2002
Okay I've tossed this together into a stand alone program. I messed
around with your code/my code quiet a bit, so I'll give you the
simplist version with just your code. I was hoping PWM0 or PWM1
would produce the signal for IC3. ACCD would hold the a count of
time. I get 0000 in ACCD when I put a breakpoint in NOP. I don't
understand why this isn't working!

Thanks for the continued help.

ORG RAM_START

bset TSCR1,%10000000 ;TEN=1 - Enable timer
bset TIE,%1000 ;Enable input capture 3
bset TFLG1,%1000 ;Clear input capture 3 interrupt
movb #%10000000,TCTL4 ;Capture on Falling edge

BSET DDRP,%00000011
movb #%00000000,PWME ;All channels disabled
movb #%00000000,PWMPOL ;Low during duty cycle
movb #%00000000,PWMCLK ;Clock SA & Clock SB
movb #%01110111,PWMPRCLK ;Clock A = Bus Clock / 128
movb #%00000000,PWMCAE ;channels in Left Aligned Output Mode
movb #%00001100,PWMCTL ;No concatenation
movb #%00000000,PWMSCLA ;Clock SA = Clock A / ( 2 * 256)

movb #%11111111,PWMPER0

movb #%11111000,PWMDTY0

bset PWME,%00000011 ;enable PWM0 and PWM1

ldd TC3
movb #%00001000,TFLG1 ;Clear input capture 3 interrupt
NOP
NOP
NOP
--- In , "zeta_alpha2002 <zeta_alpha2002@y...>"
<zeta_alpha2002@y...> wrote:
> This one works for me. I used PWM1 output and IC3 to measure
PWM1.
> I got 1/(32640 counts * 125E-9)=~245hz. I verified result with
> frequency counter.
>
> Var2 is the result of the difference between 1st and 2nd capture of
> falling edges. Note that if the frequency is doubled then there
> would be a problem with overflow, so some readjustment is required
> for your apps. Hope this one would help - good luck.
>
> bset TSCR1,%10000000 ;TEN=1 - Enable timer
> bset TIE,%1000 ;Enable input capture 3
> bset TFLG1,%1000 ;Clear input capture 3
> interrupt
> movb #%10000000,TCTL4 ;Capture on Falling edge
>
> clr capFlag
>
> PWMInit
> bset DDRP,PWME0+PWME1 ;Bit 1,0 = o/p
>
> movb #%00000000, PWME ;All channels disabled
> movb #%00000000, PWMPOL ;Low during duty cycle
> movb #%00000000, PWMCLK ;Clock SA & Clock SB
> movb #%01110111, PWMPRCLK ;Clock A = Bus Clock / 128,
> Clock B = Bus Clock / 128
> movb #%00000000, PWMCAE ;All channels operate in Left
> Aligned Output Mode
> movb #%00001100, PWMCTL ;No concatenation
> movb #%00000000, PWMSCLA ;Clock SA = Clock A / ( 2 *
> 256)
> movb #%00000000, PWMSCLB ;Clock SB = Clock B / ( 2 *
> 256)
>
> movb #%11111111, PWMPER0
> movb #%11111111, PWMPER1
>
> movb #%11111000, PWMDTY0
> movb #%10000000, PWMDTY1
>
> bset PWME,PWME0+PWME1 ;enable PWM0 and PWM1
>
> rts
>
> ;-------------------------------
--
> ---------
> IC3Int
> ldd TC3
> pshd ;Save momentarily
>
> ldaa capFlag ;Check if 1st or 2nd capture
> bne SecondCap ;If flag is set then it is
> 2nd falling edge
>
> puld
> std Var1 ;1st Capture
> movb #1,capFlag
> bra ICex ;Wait until next capture
>
> SecondCap
> puld
> subd Var1 ;Subtract from 1st Capture
> std Var2 ;Result in Var2
> clr capFlag
>
> ICex
> movb #%00001000,TFLG1 ;Clear input capture 3
> interrupt
> rti > --- In , "jeffguilfoil <jeffguilfoil@y...>"
> <jeffguilfoil@y...> wrote:
> > --- In , "zeta_alpha2002
> <zeta_alpha2002@y...>"
> > <zeta_alpha2002@y...> wrote:
> > > > ECT. I do not understand why it is not working. I've got a
> > HCS12
> > >
> > > Your PWM regs are not initialized correctly, specially the
> > PWMPER0.
> > > This one works for me, change the clock for higher freq.
> >
> > Thanks for the reply.
> >
> > I tried the settings you suggested, it's still not working. If I
> do
> > 62500hz/256 = 122hz output (PWMCLK=$03 SA clock, PWMPRCLK=$0F SA
=
> > 8mhz/128, PWMSCLA=$00 SAb500hz/2, PWMPER0=$0F SA2500/256) and
> > select input capture of 8mhz/256 = 32500hz sample. The 16-bit
> input
> > capture register never fills and the timer always overflows.
> >
> > I've tested the PWM output on it's slowest setting with a
> multimeter
> > and it is working. Feeding the PWM output into the input capture
> pin
> > always results in timer overflow.
> >
> > Any ideas?
> > Thanks again,
> > Jeff





> ORG RAM_START

Wait a minute...Are your testing the codes in RAM? The settings I
wrote are using interrupts, so if your testing it in RAM how is IC3
interrupt being vectored?

I was just curious why you were having no luck so I wrote quickie
code to see why not and burn it into flash. Cannot seem to duplicate
your problem. I could only see an overflow if the frequency is less
than 245/22Hz because the counts I was getting was 32640. If you
double 32640 then it would be 65280 which is close to $FFFF. I used
an Adapt9S12DP256 with 16Mhz crystal. Pll not enabled.

Email me at and I will give you the routine
I used. You will have to burn it into flash though. The results is
dump out of SCI0 at 9600,8,N,1. Open a terminal window and press R
for the program to start dumping the result.

Are you perchance using D-BUG to test your code? If you are then
change the value at address $3E68 to the value of the IC3 interrupt
address routine.


Jeff,

I have a calculation of duty cycle using an A4 with and ADXL202
accelerometer device. These devices have a fixed period variable duty output
relative to applied force, maybe this code will help:

/* tc3 capture state values */
#define CAP_VALID ((char) (0x00)) /* made measurement, take it */
#define CAP_INVALID ((char) (0xff)) /* making measurement, do not disturb
*/
#define CAP_NON ((char) (0x01)) /* no measurement made */
#define CAP_NOSIG ((char) (0x02)) /* no signal received */

struct Capture
{
unsigned int lead;
unsigned int period;
unsigned int high;
char result;
} Accel;

/* int_tc3() -- here each time edge capture from accelerometer (vert) occurs
*/
void int_tc3(void)
{
if ((PORTT & 0x08) != 0)
{
/*
* Input bit is high
*/
Accel.period = TC3 - Accel.lead;
Accel.result = CAP_VALID; /* valid */
/* set leading edge point to current capture */
Accel.lead = TC3;
}
else
/*
* Input bit is low...
*/
/* we know leading edge point, calculate high time. Wait for next capture
for result */
Accel.high = TC3 - Accel.lead;
Accel.result = CAP_INVALID; /* invalid */
/* reset the interrupt flag */
TFLG1=0x08;
}

Your main routine can read the value for duty using the this typical call to
the following routine:
(long)duty=dutyval(Accel.high, Accel.period, 10000)

/* dutyval -- calculates the duty cycle (high/period) scaled by places */
long dutyval(high, period, places)
int high;
int period;
int places;
{
long h;
long p;
long r;

h=(long) high * (long) places;
p=(long) period;
r=h/p;

if (r < 0L || r > (long) (100*places))
return (-1);
else
return (r);
}

The timer initialization will typically need:

CLKCTL = 0x00; /* timer MCLK=4MHz */
TIOS = 0x00; /* All input captures */
TSCR = 0x80; /* enable timer */
TCTL4 = 0xc0; /* Timer 3 input capture, both edges */
TMSK1 = 0x08; /* TC3 enabled */
TMSK2 = 0x81; /* TOI enabled, prescale div 2 */

Hope this helps,

Jonathan Masters
----- Original Message -----
From: <>
To: <>
Sent: Friday, January 03, 2003 10:17 AM
Subject: [68HC12] Re: Measuring PWM /w input capture ECT (Please help) > I don't have code working yet, that's why I posted here. :) Judging
> from the way the CAS signal looks I would guess that it's mag pickup
> or something. A pulse is created every crankshaft rotation figure
> 7000RPM ~= 117hz. I'm reluctant to do the A/D method because my
> sample periods will be pretty short and I think doing it with assured
> accuracy is pretty much the input capture method. I'll give it a try
> if/when I rule out the input capture. Input capture should work, but
> I can't get it to! I starting to feel like a real idiot. :)
>
> --- In , "Stephen Mac Neil" <macneil@d...>
> wrote:
> > Do you have any code that can do that, I would be very interested
> in getting
> > the sub routine and examining that. Is that like a mag pickup input
> to the
> > unit, this crank sensor. What would be the sampling rate of the a/d
> what is
> > the frequency ranges of the input signal? I am working on something
> similar
> > but don't know if its the same or not.
> >
> > Stephen Mac Neil
> > -----Original Message-----
> > From: jeffguilfoil <jeffguilfoil@y...>
> > [mailto:jeffguilfoil@y...]
> > Sent: Tuesday, December 31, 2002 7:12 PM
> > To:
> > Subject: [68HC12] Re: Measuring PWM /w input capture ECT (Please
> help)
> >
> >
> > --- In , BobGardner@a... wrote:
> > > Hi Jeff. Why not just run the pwm into an rc lo pass filter, and
> > run that
> > > into an a/d input? The a/d reads the avg voltage of the pwn
> > waveform from the
> > > cap. This is exactly what you want, right?
> > >
> >
> > Thanks for the reply.
> >
> > Yes, I suppose the results using your technique are what I'm
> looking
> > for. I will be using a signal from a automobile crank angle
> sensor,
> > so the length of the highs changes perportionally with the length
> of
> > the lows. Do you see any problems with your suggested approach?
> The
> > only issue I see is what if the start of the sample doesn't land
> on
> > the same part of the pulse wave and the sample time is rather
> short
> > the value might quiet different. Thoughts?
> >
> > Jeff
> >
> >
> >
> >
> >
> >
> >
> > --------------------
> >
> >
> >
> > ">http://docs.yahoo.com/info/terms/





I'm not exactly sure why, but I got my code to work!

Sincere thanks to zeta_alpha2002, Jonathan Masters and Bob Gardner
for the help.



Are you sure you want to measure PWM? A crank or cam sensor will have a
constant pulse width, the frequency varies with engine RPM.

Andrei



I thought it would be a input capture averaging the frequency of a couple of
samples to determine the frequency of the engine at any particular time.

Stephen
-----Original Message-----
From: Andrei Chichak [mailto:]
Sent: Tuesday, January 07, 2003 6:07 PM
To:
Subject: Re: [68HC12] Re: Measuring PWM /w input capture ECT - Got it
working! Are you sure you want to measure PWM? A crank or cam sensor will have a
constant pulse width, the frequency varies with engine RPM.

Andrei

--------------------



I appologize for the late reply.

I'm measuring the time between when the pulse goes high. ie. |-|_|
That time changes when the RPM changed on the CAS. Also, the peak |-
| and vally |_| time changes perportionally with RPM. I took an
Oscilloscope to a CAS to be 100% sure that occurs.

I hope I'm not making an incredibly daft mistake, so please correct
me if I'm wrong. :)

--- In , Andrei Chichak <acpmiedm@t...> wrote:
> Are you sure you want to measure PWM? A crank or cam sensor will
have a
> constant pulse width, the frequency varies with engine RPM.
>
> Andrei





I'm just wondering why you would do this type of timing rather than doing
an RPM count and calculate the times?

If you are trying to determine the positions to synchronize injection or
ignition pulses, I'm not sure you are on the right track at all. There are
quite a few web sites around dealing with injection/ignition that you might
want to look at. Start at www.diy-efi.org. Motorola, I seem to remember,
has an app note on doing injection using their modular counter time module
(CTM) if my memory is not failing. THere has been articles published by
Circuit Cellar www.circuitcellar.com that have these systems well
described, and Al Lipper has a couple of systems written up that use 8051s.

Andrei

At 02:24 AM 1/18/2003 +0000, you wrote:
>I appologize for the late reply.
>
>I'm measuring the time between when the pulse goes high. ie. |-|_|
>That time changes when the RPM changed on the CAS. Also, the peak |-
>| and vally |_| time changes perportionally with RPM. I took an
>Oscilloscope to a CAS to be 100% sure that occurs.
>
>I hope I'm not making an incredibly daft mistake, so please correct
>me if I'm wrong. :)
>
>--- In , Andrei Chichak <acpmiedm@t...> wrote:
> > Are you sure you want to measure PWM? A crank or cam sensor will
>have a
> > constant pulse width, the frequency varies with engine RPM.
> >
> > Andrei >
>-------------------- >
>">http://docs.yahoo.com/info/terms/





"modular counter time module (CTM)"

You are probably thinking of the TPU (Time Processing Unit) present on all
MC68332 MCUs and many other Motorola MCUs of the modular MCU family.

These little jewels have specific timer functions built in that detect
crankshaft position and calculate the firing angle for ignition and fuel
injection timing.

The '332 was designed specifically for engine management, but is also a
wonderful little general purpose MCU with lots and lots of horsepower.

Bob Smith

--- Avoid computer viruses, Practice safe hex ---

-- Specializing in small, cost effective
embedded control systems -- Robert L. (Bob) Smith
Smith Machine Works, Inc.
9900 Lumlay Road
Richmond, VA 23236 804/745-1065
----- Original Message -----
From: "Andrei Chichak" <>
To: <>
Sent: Monday, January 20, 2003 4:53 PM
Subject: Re: [68HC12] Re: Measuring PWM /w input capture ECT - Got it
working! > I'm just wondering why you would do this type of timing rather than doing
> an RPM count and calculate the times?
>
> If you are trying to determine the positions to synchronize injection or
> ignition pulses, I'm not sure you are on the right track at all. There are
> quite a few web sites around dealing with injection/ignition that you
might
> want to look at. Start at www.diy-efi.org. Motorola, I seem to remember,
> has an app note on doing injection using their modular counter time module
> (CTM) if my memory is not failing. THere has been articles published by
> Circuit Cellar www.circuitcellar.com that have these systems well
> described, and Al Lipper has a couple of systems written up that use
8051s.
>
> Andrei
>
> At 02:24 AM 1/18/2003 +0000, you wrote:
> >I appologize for the late reply.
> >
> >I'm measuring the time between when the pulse goes high. ie. |-|_|
> >That time changes when the RPM changed on the CAS. Also, the peak |-
> >| and vally |_| time changes perportionally with RPM. I took an
> >Oscilloscope to a CAS to be 100% sure that occurs.
> >
> >I hope I'm not making an incredibly daft mistake, so please correct
> >me if I'm wrong. :)
> >
> >--- In , Andrei Chichak <acpmiedm@t...> wrote:
> > > Are you sure you want to measure PWM? A crank or cam sensor will
> >have a
> > > constant pulse width, the frequency varies with engine RPM.
> > >
> > > Andrei
> >
> >
> >
> >--------------------
> >
> >
> >
> >">http://docs.yahoo.com/info/terms/ >
> -------------------- >
> ">http://docs.yahoo.com/info/terms/ >





I have a couple of the EFI332 boards assembled and use '332s in my daily
job, but I have seen app notes from a couple of manufacturers on doing
injection/ignition without using the TPU. One was from Seimens and the
other was for one of Moto's smaller processors (not the HC16 w/TPU), but do
you think I can find it??? NOOOOooo..

http://www.infineon.com/cmc_upload/migrated_files/document_files/Application_Notes/ap163502.pdf

Motorola also has some app notes for the HC08 as well.

Andrei

At 10:12 PM 1/20/2003 -0500, you wrote:
>"modular counter time module (CTM)"
>
>You are probably thinking of the TPU (Time Processing Unit) present on all
>MC68332 MCUs and many other Motorola MCUs of the modular MCU family.
>
>These little jewels have specific timer functions built in that detect
>crankshaft position and calculate the firing angle for ignition and fuel
>injection timing.
>
>The '332 was designed specifically for engine management, but is also a
>wonderful little general purpose MCU with lots and lots of horsepower.
>
> Bob Smith
>
>--- Avoid computer viruses, Practice safe hex ---
>
>-- Specializing in small, cost effective
> embedded control systems -- >Robert L. (Bob) Smith
>Smith Machine Works, Inc.
>9900 Lumlay Road
>Richmond, VA 23236 804/745-1065 >
>----- Original Message -----
>From: "Andrei Chichak" <>
>To: <>
>Sent: Monday, January 20, 2003 4:53 PM
>Subject: Re: [68HC12] Re: Measuring PWM /w input capture ECT - Got it
>working! > > I'm just wondering why you would do this type of timing rather than doing
> > an RPM count and calculate the times?
> >
> > If you are trying to determine the positions to synchronize injection or
> > ignition pulses, I'm not sure you are on the right track at all. There are
> > quite a few web sites around dealing with injection/ignition that you
>might
> > want to look at. Start at www.diy-efi.org. Motorola, I seem to remember,
> > has an app note on doing injection using their modular counter time module
> > (CTM) if my memory is not failing. THere has been articles published by
> > Circuit Cellar www.circuitcellar.com that have these systems well
> > described, and Al Lipper has a couple of systems written up that use
>8051s.
> >
> > Andrei
> >
> > At 02:24 AM 1/18/2003 +0000, you wrote:
> > >I appologize for the late reply.
> > >
> > >I'm measuring the time between when the pulse goes high. ie. |-|_|
> > >That time changes when the RPM changed on the CAS. Also, the peak |-
> > >| and vally |_| time changes perportionally with RPM. I took an
> > >Oscilloscope to a CAS to be 100% sure that occurs.
> > >
> > >I hope I'm not making an incredibly daft mistake, so please correct
> > >me if I'm wrong. :)
> > >
> > >--- In , Andrei Chichak <acpmiedm@t...> wrote:
> > > > Are you sure you want to measure PWM? A crank or cam sensor will
> > >have a
> > > > constant pulse width, the frequency varies with engine RPM.
> > > >
> > > > Andrei
> > >
> > >
> > >
> > >--------------------
> > >
> > >
> > >
> > >">http://docs.yahoo.com/info/terms/
> >
> >
> >
> > --------------------
> >
> >
> >
> > ">http://docs.yahoo.com/info/terms/
> >
> >
> >
>-------------------- >
>">http://docs.yahoo.com/info/terms/





The 2024 Embedded Online Conference