Please help with 18F252 multiplexing in mikrobasic

Status
Not open for further replies.

peterzatko

New Member
I need help with multiplexing 3 digit - 7segment display.I use pic 18F252.This code must count from 0 to 999 - every second and number display on 3 digit 7 segment display.
I have a problem with my code in delay because when i using in my board them numbers blinking very fast (non every second).Whats wrong in my code.I use externall HS 8MHz oscillator.THx

program xxxx


dim shifter, portb_index as byte ' Variables shifter and portd_index are of byte type


Dim decim as byte
Dim num as byte
Dim dat as byte
Dim fir as byte
Dim sec as byte
Dim q as byte '
Dim b as byte
Dim g as byte
Dim h as integer
Dim i as byte
Dim j as byte
Dim suma as byte
Dim k as byte
Dim l as integer

main:
PORTA = %00000000
TRISA = %00010000 '
PORTB = %00000000
TRISB = %10000000
PORTC = %00000000
TRISC = %11111111
ADCON1 = 7
PORTB = portb_index

Startstop:
OUT1 = 1 'Out for led
OUT2 = 0 Out for led
PORTA.0 =1 'Out for transistor NPN (ON first 7 segment)
PORTB = $3F 'display 0 on first displaz
If SWT2 = 0 then 'Switch buttton
delay_ms(185)
h = 0
j = 0
i = 0
k = 0
goto Counter
End If
goto Startstop

Counter:

IF h = 999 then h = 0
end if
inc(h)
i = h/100
If h<100 then
j = h/10
else
j = (h/10) mod 10
end if
k = (h/10) mod 1


IF k = 0 then PORTB = $3F
else If k = 1 then k = $06
else If k = 2 then k = $5B
else If k = 3 then k = $4F
else If k = 4 then k = $66
else If k = 5 then k = $6D
else If k = 6 then k = $7D
else If k = 7 then k = $07
else If k = 8 then k = $7F
else If k = 9 then k = $6F
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If

IF j = 0 then j = $3F
else If j = 1 then j = $06
else If j = 2 then j = $5B
else If j = 3 then j = $4F
else If j = 4 then j = $66
else If j = 5 then j = $6D
else If j = 6 then j = $7D
else If j = 7 then j = $07
else If j = 8 then j = $7F
else If j = 9 then j = $6F
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If

IF i = 0 then i = $3F
else If i = 1 then i = $06
else If i = 2 then i = $5B
else If i = 3 then i = $4F
else If i = 4 then i= $66
else If i = 5 then i = $6D
else If i = 6 then i = $7D
else If i = 7 then i = $07
else If i = 8 then i = $7F
else If i = 9 then i = $6F
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If

display:
PORTB = k
PORTA.0 = 1
PORTA.1 = 0
PORTA.2 = 0
delay_ms(7)
PORTB = j
PORTA.1 = 1
PORTA.0 = 0
PORTA.2 = 0
delay_ms(7)
PORTB = i
PORTA.2 = 1
PORTA.0 = 0
PORTA.1 = 0
delay_ms(7)

goto Counter
 
Last edited:
Segments are with commond cathode
PORTA.0,1,2 are outputs for NPN transitors which supply displays to ground
 
I'm not a PIC programmer, but...
1. Comments would help
2. Are there too many ENDIF's. That's just a question.
3. Is the clock osc set right for the delay routine to work?
4. 185 mS is pretty small
5. Remember to implement inter-digit blanking. Before you go to the next digit, you have to turn off the current one and you may have to introduce a "tiny" delay when going to the next digit. Once the previous digit is off, you can set up the segments. Possibly delay a tiny bit and then enable the new digit.
6.Is the h/10 and h/100 integer or floating divides?
7. Simplify your program and don't count yet.
8. Not sure your going about it right. You could have a variable for each digit. A routine that would convert the digits to which segment.
Think it through. A routine called DISPLAY(digit, segments()) and another that would be called SEGMENTS(digit, segments())

Segments could be an integer that each bit represents a segment. You could also have RDP and LDP as flags)

So, you can "build" on your program.
Display a single digit using segments only.
Display all 4 digits as different.
Work on conversion from numbers to segments
Then work on the counting stuff.

Think the program through. Break into pieces. Test what you don't understand.

If I just listened to you explain your program. You'll find the error. I used this technique a lot when "helping" people. So, I encourage you to "play computer".
Test your timing, by blinking a segment, on and off time the same.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…