1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Speedometer

Discussion in 'Microcontrollers' started by Kiko, Mar 8, 2007.

  1. gramo

    gramo New Member

    Joined:
    Oct 2, 2006
    Messages:
    1,221
    Likes:
    23
    Location:
    Newcastle, Australia
    All finished, heres the code;

    Code (text):
    Device 16F876
    Declare XTAL 20

    Config XT_OSC , WDT_OFF , PWRTE_ON , BODEN_OFF , LVP_OFF , CPD_OFF, CP_OFF , DEBUG_OFF

    Declare LCD_TYPE 0
    Declare LCD_DTPIN PORTB.4
    Declare LCD_ENPIN PORTB.3
    Declare LCD_RSPIN PORTB.2
    Declare LCD_INTERFACE 4
    Declare LCD_LINES 2

    Dim KMH As Float
    Dim Temp_Float As Float
    Dim KMHT As DWord
    Dim KMH_Last As DWord

    Dim Result As Word
    Dim Total As Float
    Dim Last_Result As Float
    Dim X As Byte

    Symbol _1_Min = 1.243
    Symbol _1_Max = 2.355

    Symbol _2_Min = 2.356
    Symbol _2_Max = 3.194

    Symbol _3_Min = 3.195
    Symbol _3_Max = 3.709

    Symbol _4_Min = 3.710
    Symbol _4_Max = 4.307

    Symbol _5_Min = 4.308
    Symbol _5_Max = 4.674

    Symbol _6_Min = 4.675
    Symbol _6_Max = 4.810

    Symbol _N_Min = 4.811
    Symbol _N_Max = 5.245

    TRISC.0 = 1

    DelayMS 1000
    Cls

    Print At 1,5, "KM/Hr"
    KMHT = 0

    TRISA.0 = 1
    ADCON1 = %10000000          ' Set analogue input, Vref is Vdd

    Start:

        Result = 0
        Total = 0
        X = 0
       
        Repeat
       
              Result = ADIN 0
              Result = Result * 2
              Total = Total + Result
             
              Inc X
       
        Until X = 15
       
        Total = Total / 15
       
        Total = Total * 5 / 1023
       
        Select Total
               Case _1_Min To _1_Max
                    Print At 2, 1, "1st Gear ", DEC3 Total, "V"
               Case _2_Min To _2_Max
                    Print At 2, 1, "2nd Gear ", DEC3 Total, "V"
               Case _3_Min To _3_Max
                    Print At 2, 1, "3rd Gear ", DEC3 Total, "V"
               Case _4_Min To _4_Max
                    Print At 2, 1, "4th Gear ", DEC3 Total, "V"
               Case _5_Min To _5_Max
                    Print At 2, 1, "5th Gear ", DEC3 Total, "V"
               Case _6_Min To _6_Max
                    Print At 2, 1, "6th Gear ", DEC3 Total, "V"
               Case _N_Min To _N_Max
                    Print At 2, 1, "Neutral  ", DEC3 Total, "V"
               Case Else
                    Print At 2, 1, "Unknown  ", DEC3 Total, "V"
        EndSelect
                   
            KMHT = Counter PORTC.0, 250
       
        If KMHT = 0 Then Goto Display
       
        Temp_Float = KMHT
        Temp_Float = Temp_Float * 4 * 7.2
            KMH =  Temp_Float / 1000
        KMHT = KMH

    Display:
       
        If KMH_Last <> KMHT Then
                Print At 1, 1, DEC3 KMHT
            KMH_Last = KMHT
        EndIf
       
        GoTo Start
    You can modify the Symbols Containing the Min and Max voltage values that are allowed for each gear.

    The program can probably be tweaked - eg I left the volt display on there for debugging and you can remove it once you feel happy you know all of the volages are correct.

    Watch the circuit in action here
    The SIM appears laggy as there are 7 Signal gererators in use. It will be much faster in real time.
     
  2. gramo

    gramo New Member

    Joined:
    Oct 2, 2006
    Messages:
    1,221
    Likes:
    23
    Location:
    Newcastle, Australia
    You might be asking why on earth I used a voltage divider in the circuit - its because in Neutral, the were voltages over 5V, and PIC's don’t like that!

    So to keep everything to operating standards, the voltage divider is just halving the sample voltage; eg, 5.5V from the gear selector output will now be sampled as 2.25V by the PIC. The program scales it back up mathematically :)
     
  3. Kiko

    Kiko New Member

    Joined:
    Apr 24, 2004
    Messages:
    79
    Likes:
    1
    Location:
    Brazil
    Looks great for me, I'll try to understand what you did and run it on the bench and then on the bike. But I can tell you that for some reason that I don't know yet the speedo when I install on the bike have a strange behavior, I mean, the speed readings are not accurated, but it's on the bench when I simulate the speed with signal generator, tomorrow if my wife permit, I'll troubleshoot my bike's voltage regulator and generator.

    I apreciate you help indeed and thank you very much

    ps: If you have any more ideas please let me know.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Kiko

    Kiko New Member

    Joined:
    Apr 24, 2004
    Messages:
    79
    Likes:
    1
    Location:
    Brazil

    OK I understand and I have it in my circuit, thank you.
     
  6. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,016
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    Does the gear voltage vary with engine RPM. If so, the voltage may be relative to the battery voltage and you may be able to fix it by setting the ADC reference voltage to a division of the battery voltage.

    BTW, have you changed to a schmitt trigger input and if you have, did it work?

    Mike.
     
    • Like Like x 1
  7. gramo

    gramo New Member

    Joined:
    Oct 2, 2006
    Messages:
    1,221
    Likes:
    23
    Location:
    Newcastle, Australia
    I used PORTC.0 as the RPM sensor, all of the Pins have a built in schmitt trigger on PORTC, so I hope that helps him out
     
  8. Kiko

    Kiko New Member

    Joined:
    Apr 24, 2004
    Messages:
    79
    Likes:
    1
    Location:
    Brazil
    Unfortunately not and I have changed de input port to RC7 and still the same. I think I have some kind of noise that come from the generator because when the engine is not runnig or the circuit is on the bench it works ok. I didn't have time to try the gramo routine yet and to troubleshoot the generator.

    I will post the news later just for information. Thank you very much.
     
  9. Kiko

    Kiko New Member

    Joined:
    Apr 24, 2004
    Messages:
    79
    Likes:
    1
    Location:
    Brazil
    FYI attached is the circuit I'm using.

    I increased the value of C5 and C7 to try to filter any noise (didn't work).

    I've placed a 10k resistor (pull-down) on PORTC7 then I removed because it killed the signal to the original bike's speedometer.
     

    Attached Files:

  10. danci1973

    danci1973 New Member

    Joined:
    Nov 3, 2006
    Messages:
    7
    Likes:
    1
    I think your problem is in the ground - what ground did you use when you measured the voltages? I think you should use the black/white wire on the gear position switch connector, not just any ground on the bike.

    Attached is a picture of how I think the Suzuki gear position switch is wired.

    D.
     

    Attached Files:

    • Like Like x 1
  11. Kiko

    Kiko New Member

    Joined:
    Apr 24, 2004
    Messages:
    79
    Likes:
    1
    Location:
    Brazil
    How did you find the constants assigned below?

    Symbol _1_Min = 1.243
    Symbol _1_Max = 2.355

    Symbol _2_Min = 2.356
    Symbol _2_Max = 3.194

    Symbol _3_Min = 3.195
    Symbol _3_Max = 3.709

    Symbol _4_Min = 3.710
    Symbol _4_Max = 4.307

    Symbol _5_Min = 4.308
    Symbol _5_Max = 4.674

    Symbol _6_Min = 4.675
    Symbol _6_Max = 4.810

    Symbol _N_Min = 4.811
    Symbol _N_Max = 5.245
     
  12. Kiko

    Kiko New Member

    Joined:
    Apr 24, 2004
    Messages:
    79
    Likes:
    1
    Location:
    Brazil
    I'm taking the ground from somewhere in the instrument cluster.

    I'll try to change the ground...

    See attached the bike's diagram

    Thank you
     

    Attached Files:

  13. gramo

    gramo New Member

    Joined:
    Oct 2, 2006
    Messages:
    1,221
    Likes:
    23
    Location:
    Newcastle, Australia
    Heres your real life data:
    As they overlap, I had to find the "Middle" between each set. Take the First Gear for example:

    1st 1.243v - 2.586v
    2nd 2.124v - 3.210v

    Now the "Overlap" from 1st too 2nd is 2.586 minus 2.124 = 0.462V

    As I now know how much they overlap by, I simply divide this by 2 so I can split the overlap evenly 0.462 / 2 = 0.231

    And now add that value to the lower voltage 2.124 + 0.231 = 2.355

    There you have it - the "Middle" point for the overlap between gears :)

    I did however modify the gap between 2nd to 1st as it was not the same (too small) and figured it was a miss read
     
  14. Kiko

    Kiko New Member

    Joined:
    Apr 24, 2004
    Messages:
    79
    Likes:
    1
    Location:
    Brazil

    Allright, thank you
     
  15. gramo

    gramo New Member

    Joined:
    Oct 2, 2006
    Messages:
    1,221
    Likes:
    23
    Location:
    Newcastle, Australia
    I said i modified 2nd to 1st, I meant 2nd to 3rd
     
  16. Kiko

    Kiko New Member

    Joined:
    Apr 24, 2004
    Messages:
    79
    Likes:
    1
    Location:
    Brazil
    Ok I understood, but one more question my friend, in your simulation you told the voltages (sine wave) for each gear are 10khz from min to max, Why? and don't you think it's better to put a little delay between the readings for speed because as it's a digital reading any little change on the speed will be displayed on LCD but not on the original speedometer?

    Rgds
     
  17. gramo

    gramo New Member

    Joined:
    Oct 2, 2006
    Messages:
    1,221
    Likes:
    23
    Location:
    Newcastle, Australia
    I chose a frequency of 10K as a random "out there" number, to simulate noise from other devices on motorbikes circuits, it could be much less that.

    There is no delay in the routine - as I 'average' multiple samples with the following code


    Code (text):
        Result = 0
        Total = 0
        X = 0
       
        Repeat
       
              Result = ADIN 0
              Result = Result * 2
              Total = Total + Result
             
              Inc X
       
        Until X = 15
       
        Total = Total / 15
       
        Total = Total * 5 / 1023
    This way, I can average out and slew noise from my final result. Lets say for example my 15 readings were the following (The bike is in 1st gear):


    1.25, 1.0, 2.3, 2.7, 1.1, 1.5, 1.7, 1.77, 1.79, 1.2, 2.4, 2.45, 1.5, 1.6, 2.8

    The samples in bold were "out of range" and could have been produced from noise/spikes from else where in the motorbikes circrtry.

    But as I have taken 15 samples, I can now find the average.

    So all those numbers added together gives: 27.06
    The average is 27.06 / 15 = 1.804 Volts

    And this 'filters' out the noise - no need for a delay persay - the time that 15 samples takes is the delay. If you find that this speed is too fast, then put a simple delay in the repeat loop, eg

    Code (text):
        Result = 0
        Total = 0
        X = 0
       
        Repeat

    [B]       DelaymS 1[/B]
       
              Result = ADIN 0
              Result = Result * 2
              Total = Total + Result
             
              Inc X
       
        Until X = 15
       
        Total = Total / 15
       
        Total = Total * 5 / 1023
    And that will help generate a broader time scale for your samples
     
  18. danci1973

    danci1973 New Member

    Joined:
    Nov 3, 2006
    Messages:
    7
    Likes:
    1
    Looks similar to my bike's diagram. Does your bike have three wires to GPS (gear position switch)?

    The voltages from the gear position switch should be 'stable' and pretty much spot on as the ECU is using those to choose different ignition and/or fuel maps for first three gears (to prevent 'accidental' wheelies, I guess) and to enforce a speed limit in the top gear (on the GSXR-1000 and Hayabusa).

    D.
     
  19. Kiko

    Kiko New Member

    Joined:
    Apr 24, 2004
    Messages:
    79
    Likes:
    1
    Location:
    Brazil
    OK as we say down here in Brazil "living and learning", so can I do the same with speed code below (your code)?

    KMHT=Counter PORTC.7, 500 ' I increased the value of period from 250 to 500 but the reading still too sensitive
    If KMHT=0 Then Goto Display_speed
    Temp_Float=KMHT
    Temp_Float=(Temp_Float*2)*8 ' I round 7.2 to 8 which gave me a more accurate speed reading
    KMH=Temp_Float/10
    KMHT=KMH

    Display_speed:
    If KMH_Last<>KMHT Then
    Print At 1, 1, DEC3 KMHT
    KMH_Last=KMHT
    EndIf
    GoTo Start

    Thank you again for your valuable help
     
  20. Kiko

    Kiko New Member

    Joined:
    Apr 24, 2004
    Messages:
    79
    Likes:
    1
    Location:
    Brazil

    Yes sir, it has 3 wires and I took the signal from "P" wire near the ECU connector as you can see on the diagram. And as I post before I'm almost sure I have a GPS or generator or regulator problem, first because the voltages overlaps and second because on the bench the circuit works very nice and the only thing I can see strange on this bike is that it drinks a lot of fuel and it's fast, so it may be a indicative of GPS problem and because of that the ECU chose a higher gear (5th) to deal with. I don't know...

    I don't think the Suzuki TL1000 has a speed limit but I'm sure it has a RPM limit above 10500 RPM with load.

    Thank you
     
  21. Kiko

    Kiko New Member

    Joined:
    Apr 24, 2004
    Messages:
    79
    Likes:
    1
    Location:
    Brazil
    Just for information, I changed the ground of the circuit that I took from somewhere of the instrument cluster and now the circuit is very stable, so the problem was GROUND. The Gear indicator is working perfect... But not the speedometer that when I turn the ignition ON it shows me 10 Km/hr and in 30Km/h shows 60Km/hr and so on, with more speed the difference between analog and digital increase.

    I had to change the resistors of the voltage divider from 1k to 10k because with 1K the voltage on the lcd indicator drop about 1.5 Volts.
     

Share This Page