Hi Kiki,
I've rewritten you code to fix a lot of the problems.
1) The first thing I noticed, was that in all your IF routines, the only thing that changed was the state of the four inputs. So I made them into a case statement.
2) The second was the never ending loop in the coin input, now there's a timer on it, in mS
3) The next timer was how long Portd.0 was on for, this also is in the interrupt routine. again, in mS
4) since we have a 1mS interrupt, might as well use it to do your 500mS delay.
See how that goes, I can simulate it, and it seems to work but I can't in a chip. Really, The whole program doesn't seem to do much other than turn on Portd.0 for three different periods. depending on the four inputs combination, and even then Nilai is duplicated twice for each value, 3,4 & 5.
Anyhow, give it a try.
Regards
Dean
[code:1:41822806a9]
$regfile = "m8def.dat"
$crystal = 4000000
'$sim
Dim Nilai As Byte
Dim Tempinput As Byte
Dim Waitcounter As Word
Dim Onesecond As Word
Config Pinb.0 = Input 'this pin set 1coin
Config Pinb.1 = Input 'this pin set 2coin
Config Pinb.2 = Input 'this pin set time to output
Config Pinb.3 = Input 'this pin set time to ouput
Config Pinb.4 = Input 'this pin is wait coin coming in
Config Portd.0 = Output
Coinip Alias Portb.4
WhaterverOutput alias as portd.4
'One second interrupt @ 4Mhz clock
Const Timer1reload = 62500
config timer1=timer,prescale = 64
load timer1 , Timer1Reload
on ovf1 Timer1_isr
enable timer1
Enable Interrupts
Start Timer1
'Set Internal pullups on inputs 0 to 4 even though bits 0-34 are inputs
Portb = &B00011111
Onesecond = 1000
Do
Nilai = 0
'Get ALL of portb
Tempinput = Pinb
' Get rid of the high bits & keep 0 - 3 bits
Tempinput = Tempinput And &B00001111
'Compare them against the wanted bit mask & set Nilai to the correct value
Select Case Tempinput
Case &B00001010
Nilai = 3
Case &B00000110
Nilai = 4
Case &B00000010
Nilai = 5
Case &B00001001
Nilai = 3
Case &B00000101
Nilai = 4
Case &B00000010
Nilai = 5
Case Else
'None of the above
Goto Nogo
End Select
'Wait 10 seconds (10000mS)
Waitcounter = 10000 'Set maximum wait time for the coin (this can go up to 65535mS or 65 seconds)
'If no coins in that time, wait counter counts down to zero and the program exits the loop.
'Wait for Coin(low) or End of Wait Time
While Coinip = 1
If Coinip = 0 Then Goto Here
If Waitcounter = 0 Then Goto Nogo
Wend
Here:
'Must have got a coin to have gotten here.
'Since we have a one mS timer, might as well use it. Wait for 500mS (1/2 Second)
Waitcounter = 500
While Waitcounter > 0
Wend
'Portd output ON. Nilai is already set in the case statement.
Set WhaterverOutput 'Portd.0
'Wait here until Portd.4 is OFF again
Do
Loop Until Portd.0 = 0
Nogo:
Loop
End
Timer1_isr:
'Comes In Here Every 1ms
Load Timer1 , Timer1reload
'It does 1000 of these, = 1 second
If Onesecond > 0 Then
Decr Onesecond
Else
'It does this bit every one second
'Reload the one second counter
Onesecond = 1000
'Do number of counts before turning Portd"
If Nilai > 0 Then
'Still NOT zero so
Decr Nilai
Else
'Portd Output OFF
Reset WhaterverOutput 'Turn Portd.0 OFF
End If
Toggle Portd.6
End If
If Waitcounter > 0 Then
Decr Waitcounter
End If
Return
[/code:1:41822806a9]
↧