• PicŪ Basic


  • 3 Digit LED Rev counter Speedo

    I recently purchased a car with a speedo in Km/hr. I needed a display of MPH, and I didn't want (in fact wasn't possible) to convert, or change the Km/hr speedo. The speedo is electronic and a "clean" square wave output of speed sensor was available. The following routine counts the pulses over a given period and load a variable with the result. using the "DIG" command 3 variables are load with each digit. a BCD representation of each digit is out on a port and, "latched" into 3 4511's display driver/decoder, at the appropiate time. I used the 16F676 as it has only 14 pins, an internal 4Mhz osc, and internal MCLR. The main problem I had was "setting" up the "676" as it defaults to analogue inputs unless certain registers are set. I will be the first to admit this is not best piece of code written, but it works for me. Can be used equally well for a Digital Rev counter.

    DEVICE 16F676
    DIM COUN AS WORD
    DIM D1
    DIM D2
    DIM D3

    TRISA = %00000000 'setting up outputs
    ADCON1 = %00000000
    ANSEL = %00000000
    CMCON = %00000111
    TRISC = %00000000
    COUN =0
    INPUT PORTA.3
    OUTPUT PORTA.0
    OUTPUT PORTA.1
    OUTPUT PORTA.2
    OUTPUT PORTA.4
    OUTPUT PORTC
    PORTA.4 = 0
    PORTA.5 = 0
    MAIN:
    COUN = COUNTER PORTA.3,520 'tweak the "520" time value to calibrate display
    COUN = COUN * 1 'scaling if you need it
    COUN = COUN / 1 'more scaling if you need it
    IF COUN > 999 THEN COUN = 0 ' set to 0 if over count

    D1 = DIG COUN,0 'LSD
    D2 = DIG COUN,1
    D3 = DIG COUN,2 'MSD

    PORTA.0=1
    PORTA.1=1
    PORTA.2=1
    DIGIT1:
    IF D1=0 THEN GOSUB ZERO 'Subroutines to set BCD for LSD digit
    IF D1=1 THEN GOSUB ONE
    IF D1=2 THEN GOSUB TWO
    IF D1=3 THEN GOSUB THREE
    IF D1=4 THEN GOSUB FOUR
    IF D1=5 THEN GOSUB FIVE
    IF D1=6 THEN GOSUB SIX
    IF D1=7 THEN GOSUB SEVEN
    IF D1=8 THEN GOSUB EIGHT
    IF D1=9 THEN GOSUB NINE
    PORTA.0=1 'Pulsing latch input to 4511 LSD
    PORTA.0=0
    DELAYUS 10
    PORTA.0=1

    PORTA.0=1
    PORTA.1=1
    PORTA.2=1
    DIGIT2:
    IF D2=0 AND D3=0 THEN GOSUB BLANK 'Blanking leading zeros
    IF D2=0 AND D3>0 THEN GOSUB ZERO
    IF D2=1 THEN GOSUB ONE 'Middle digit gosub to output BCD
    IF D2=2 THEN GOSUB TWO
    IF D2=3 THEN GOSUB THREE
    IF D2=4 THEN GOSUB FOUR
    IF D2=5 THEN GOSUB FIVE
    IF D2=6 THEN GOSUB SIX
    IF D2=7 THEN GOSUB SEVEN
    IF D2=8 THEN GOSUB EIGHT
    IF D2=9 THEN GOSUB NINE
    PORTA.1=1 'Pulse middle digit 4511 latch pin
    PORTA.1=0
    DELAYUS 10
    PORTA.1=1

    PORTA.0=1
    PORTA.1=1
    PORTA.2=1
    DIGIT3:
    IF D3=0 THEN GOSUB BLANK 'Leading zero blanking
    IF D3=1 THEN GOSUB ONE 'MSD BCD gosubs
    IF D3=2 THEN GOSUB TWO
    IF D3=3 THEN GOSUB THREE
    IF D3=4 THEN GOSUB FOUR
    IF D3=5 THEN GOSUB FIVE
    IF D3=6 THEN GOSUB SIX
    IF D3=7 THEN GOSUB SEVEN
    IF D3=8 THEN GOSUB EIGHT
    IF D3=9 THEN GOSUB NINE
    PORTA.2=1 'Pulse MSD 4511 lactch line
    PORTA.2=0
    DELAYUS 10
    PORTA.2=1

    PORTA.0=1
    PORTA.1=1
    PORTA.2=1
    IF COUN >99 THEN 'Output for buzzer if speed goes over 100 MPH :-)
    PORTA.4=1
    ELSE
    PORTA.4 = 0
    ENDIF
    GOTO MAIN

    'DISPLAY DIGITS ON 7 SEGMENT LED DISPLAY CONNECTED TO PORT B (BCD)
    ONE:
    PORTC= %00000001 '1
    RETURN
    TWO:
    PORTC= %00000010 '2
    RETURN
    THREE:
    PORTC= %00000011 '3
    RETURN
    FOUR:
    PORTC= %00000100 '4
    RETURN
    FIVE:
    PORTC= %00000101 '5
    RETURN
    SIX:
    PORTC= %00000110 '6
    RETURN
    SEVEN:
    PORTC= %00000111 '7
    RETURN
    EIGHT:
    PORTC= %00001000 '8
    RETURN
    NINE:
    PORTC= %00001001 '9
    RETURN
    ZERO:
    PORTC= %00000000 '0
    RETURN
    BLANK:
    PORTC= %00001011 'NUL ' using unspecified "BCD" word to blank display w
    RETURN
    contributed by Rob EDWARDS