+ Reply to Thread
Results 1 to 14 of 14

Thread: Measuring Vehicle Speed using C167 microcontroller

  1. #1
    hc8th Newbie
    Join Date
    Jul 2009
    Posts
    7

    Exclamation Measuring Vehicle Speed using C167 microcontroller

    Hi all,

    I'm new to this forum , I'm currently working on one of my projects in which I'm using a micrcontroller (Infineon C167) to capture the output signal from a speed sensor, this sensor is producing a digital pulse (not PWM) depending on the changes of frequency, e.g. 10Hz = 4mph. I managed to simulate the speed sensor output and the response frequency is 0 to 135 Hz representing from 0mph to 60mph. But I'm struggling to derive an algorithm using C language for the microcontroller to calculate the speed, can anyone help please? plus can I also calculate the RPM with a known speed?


  2. #2
    marcbarker Good marcbarker Good marcbarker Good
    Join Date
    Jul 2009
    Location
    London, UK
    Posts
    728

    Default

    Sometimes in development, it's easier to solve a problem in hardware instead of in software, especially when an end product becomes urgent. Later on in production, the software can be done, the hardware can be linked out on the PCB, and the parts ommitted.

    Here's an example hardware solution, if you get the pulse, differentiate it, trigger a monostable, then average the output with an R & C. The resulting DC voltage is proportional to roadspeed, can be fed into the infineon's adc input. I'll let someone else do a schematic if this is an option you'd like to try.

    Otherwise in software, you'll have to get your head round the infineon's counter/timer, interrupt service routine triggered by the input frequency, and read the period from the counter/timer. Me, I'd do a prototype in hardware because I'm lazy, then let a softy come up with the equivalent, so I can re-route the PCB minus the hardware I did, or leave it in if the softy is too busy that week.

    "I think, therefore I toast" ( Mr. T. Toaster, artificial life form, kitchen appliance & Philosopher)

  3. #3
    hc8th Newbie
    Join Date
    Jul 2009
    Posts
    7

    Default

    Thanks for the reply Marc,

    The idea is good but I 'm only allow to work on the software path, I played around with the timer counter of the c167 and try to get the results on some LEDs just to test to see if it works or not but the result was not as expected.... so i guess the algorithm i m working on isnt correct, and wud like know if theres any better way to work it out in terms of software

  4. #4
    MikeMl Excellent MikeMl Excellent MikeMl Excellent MikeMl Excellent MikeMl Excellent MikeMl Excellent MikeMl Excellent
    Join Date
    Mar 2009
    Location
    Utah
    Posts
    2,852

    Default

    The most important question to ask is how long are you willing to wait for the Speed Reading? Another way of asking this is how long an interval are you willing to count pulses for? If you want a new answer once a minute, that is one thing. If you need a new speed reading once a second, that is another.

    You need to look up "frequency measurement" vs "period measurement"

  5. #5
    marcbarker Good marcbarker Good marcbarker Good
    Join Date
    Jul 2009
    Location
    London, UK
    Posts
    728

    Default

    OK, a S/W solution :-)

    Take in the SS pulse into a suitable c167 IO.

    Config a counter/timer to measure the period with.

    Config this IO to trigger an ISR, by a ChangeOfState the input.

    At the front end of the (triggered) ISR, if it's been a H>L, then return from I. If it's a L>H, then read off the period counter into a suitable RAM register and reset timer, before RTFI.

    In your exec loop (or whatever you do) you'll find the period in the RAM register. If you want a frequency, obviously you'll have to do math with it.

    Hope this helps.

    "I think, therefore I toast" ( Mr. T. Toaster, artificial life form, kitchen appliance & Philosopher)

  6. #6
    hc8th Newbie
    Join Date
    Jul 2009
    Posts
    7

    Default

    thanks Mike and Marc for the reply, I had already set up the microcontroller to my requirement in order to capture the incoming signal from SS but my problem is the maths bit, I'm circulating around try to convert those captured signals into relative speed.....

  7. #7
    MikeMl Excellent MikeMl Excellent MikeMl Excellent MikeMl Excellent MikeMl Excellent MikeMl Excellent MikeMl Excellent
    Join Date
    Mar 2009
    Location
    Utah
    Posts
    2,852

    Default

    Speed is just "Distance traveled" per "unit time"

    Suppose the circumference of the (rear?) driven wheel is 4 ft. Then one revolution of the driven wheel means 4 ft of travel.

    Suppose the rear-end gear ratio is 3:1, meaning three revolutions of the drive shaft for one revolution of the rear wheel.

    Suppose your speed pick-up is two pulses per drive-shaft revolution.

    Suppose the integration time you use in your uc is one second.

    Now suppose you see 27 pulses in one second.

    27 pulses/sec × 1DSrev/2pulses × 1Wrev/3DSrev × 4ft/1Wrev

    =(27 × 4)/(2 × 3) ft/sec = 18 ft/sec

    To convert to miles per hour:

    18 ft/sec × 3600sec/1hr × 1 mile/5280ft = (18 ×3600)/5280 mi/hr
    = 12.27mi/hr.

    Note that with an integration time of only 1 sec, your resolution is such that you can only capture 26, 27, or 28 pulse in one second near 12mph. That is kinda coarse. That is what elicited my look up "period measurement" comment in the earlier post.

  8. #8
    hc8th Newbie
    Join Date
    Jul 2009
    Posts
    7

    Default

    thanks v much Mike, I aint really that good at C programming and by the way can i get the rpm as well jst from the signal that i have with the SS? cheers^^
    Last edited by hc8th; 13th July 2009 at 10:33 PM.

  9. #9
    marcbarker Good marcbarker Good marcbarker Good
    Join Date
    Jul 2009
    Location
    London, UK
    Posts
    728

    Default

    "Period measurement" seems the natural method for this one. Measuring it as a frequency by comparison, Mike has done an analysis on it.

    We already know speed sensor = 0 to 135 Hz representing from 0mph to 60mph.

    I believe OP has already managed to capture (in a register) the time period, e.g. between each rising edge of the SS. From there on it's math. I don't do C myself, never got into it, I'm assembler code.

    The way I plan out math functions in microcontroller software, is to do it on MS Excel, trying out the math on a spreadsheet (example attached). Once it's working nicely, I code it on the microcontroller and it always works first time.
    Attached Files

    "I think, therefore I toast" ( Mr. T. Toaster, artificial life form, kitchen appliance & Philosopher)

  10. #10
    hc8th Newbie
    Join Date
    Jul 2009
    Posts
    7

    Default

    thanks again Marc, really appreciate it, not a bad idea to test it on excel first, however i still have another question with the uc C167, to set the timer counter, i tried adjusting the prescaler but seems its not matching the SS frequency?? I tried resolution of /8, /16....but still the captured results seem change too quickly? I'm detecting the rising edge of the incoming pulses from SS.....

  11. #11
    marcbarker Good marcbarker Good marcbarker Good
    Join Date
    Jul 2009
    Location
    London, UK
    Posts
    728

    Default

    I notice you use the word 'frequency' a bit more than I'd expect. You are measuring the period between SS rising edges aren't you?

    "I think, therefore I toast" ( Mr. T. Toaster, artificial life form, kitchen appliance & Philosopher)

  12. #12
    hc8th Newbie
    Join Date
    Jul 2009
    Posts
    7

    Default

    what I'm trying to do is to match the frequency of uc with the incoming SS frequency, I'm detecting the rising edge and just measuring the freq of the SS, so if uc detected lets say 5Hz, that will corresponds to 3mph, not sure if this is a gd idea cos at da mo, i m jst using a look-up table rather than writing a algorithm which i guess is less accurate........

  13. #13
    marcbarker Good marcbarker Good marcbarker Good
    Join Date
    Jul 2009
    Location
    London, UK
    Posts
    728

    Default

    Oh, are you making an local oscillator inside the uc track the SS signal? That's a bit ambitious of you...

    ... Nah, just count the timer ticks that occur, between incoming SS pulses. Then you have the period (1 / Hz)

    "I think, therefore I toast" ( Mr. T. Toaster, artificial life form, kitchen appliance & Philosopher)

  14. #14
    hc8th Newbie
    Join Date
    Jul 2009
    Posts
    7

    Default

    yer i tried few methods but seems not working so i try this simpler method and also i m not that familiar with uc

+ Reply to Thread

Similar Threads

  1. measuring microcontroller perfomance
    By ahmedragia21 in forum General Electronics Chat
    Replies: 2
    Latest: 7th July 2008, 04:36 PM
  2. Measuring the Speed of Light Using Infrared Pulses
    By Moneer81 in forum Electronic Projects Design/Ideas/Reviews
    Replies: 11
    Latest: 11th March 2008, 05:06 AM
  3. Measuring Motor Speed
    By Haidy in forum Micro Controllers
    Replies: 6
    Latest: 25th December 2006, 07:11 PM
  4. Connection of Siemens c167 microcontroller to peripherals.
    By Roslan in forum Electronic Projects Design/Ideas/Reviews
    Replies: 3
    Latest: 23rd March 2004, 06:52 PM
  5. measuring baseball bat speed
    By alan in forum Electronic Projects Design/Ideas/Reviews
    Replies: 2
    Latest: 10th January 2003, 04:06 PM

Tags for this Thread