Hi,
I have a new problem. I am using 5 switches connected to a PIC. One of them is the START button, and the others are options (A,B,C and D) to a question that appears on LCD. Sometimes everything works fine, but some other times the system assumes that I pressed C option, when I simply pressed START button. Please note that START button and option buttons are in different ports, and that the C switch isn't connected in the board yet.
Should this be a code error ? Or can this be a debounce problem ? You can find below the code of the first PIC.
Regards,
Pedro Cardoso
TRISA = %11111 '5 pins as inputs
TRISB = %00000101 'bits 0 and 2 as inputs
ton var Byte ‘ Stepper time on
toff var Byte ‘ Stepper time off
j var Word
tpd var Word ‘ Number of turns per day given by the stepper
sentido var Bit ‘ Direction of rotation
n_passos var Byte ‘ Number of steps
max_passos var Byte ‘ Maximum number of steps
start_stop var Byte ‘start/stop flag
valor_enviar var byte ‘ Value to send to 2nd pic
start_stop = 0
max_passos = 200 ‘ Stepper with 200 steps
PORTB = %00000000
PORTA = %00000
' Wait for start button to be pressed
loop_inicio:
If PORTB.2 = 0 Then
Goto loop_inicio
Endif
TRISB = %00000001
portb=%00000000
' Start transmitted to second pic
serout2 portb.1,16780,["S"]
'Choose na option (A,B,C or D)
loop1:
'Wait till a button is pressed
If PORTA.0 = 1 Then
ton = 1 ' Provisorio
toff = 0 ' Provisorio
tpd = 600
valor_enviar=1
serout2 portb.1,16780,["B",valor_enviar]
Else
If PORTA.1 = 1 Then
ton = 1 ' Provisorio
toff = 1 ' Provisorio
tpd = 800
valor_enviar=2
serout2 portb.1,16780,["B",valor_enviar]
Else
If PORTA.2 = 1 Then
ton = 3
toff = 3
tpd = 1000
valor_enviar=3
serout2 portb.1,16780,["B",valor_enviar]
Else
If PORTA.3 = 1 Then
' ton = 42
' toff = 18
tpd = 1200
valor_enviar=4
serout2 portb.1,16780,["B",valor_enviar]
Else
Goto loop1
Endif
Endif
Endif
Endif
portb.1=0
portb.2=0
portb.3=0
'Os botões, a partir de agora, ficam inactivos excepto o do sentido
TRISA = %10000
ton_seg = ton ' Provisorio
toff_seg = toff ' Provisorio
n_passos = 0
' Rotation direction (CW or CCW)
If PORTA.4 = 1 Then
sentido = 1
Else
sentido = 0
Endif
loop2:
If sentido = 1 Then
Gosub motor_run1
Else
Gosub motor_run2
Endif
ton_seg = ton_seg - 1
If ton_seg = 0 Then
If toff_seg <> 0 Then
PORTB = %00000000 'Stop stepper
For j = 1 To toff_seg
pause 5 ' Provisorio
Next j
Endif
Endif
Goto loop2
End
motor_run1:
PORTB = %00110000
'mr1 -> loop motor run1
mr1:
'While PORTB <> %1001000
'WaitMs 250
pause 1 ' Sentido de rotação escolhido pelo 5º switch
'Calcula os passos dados pelo motor
If n_passos <> max_passos Then
n_passos = n_passos + 1
Else
n_passos = 0
Endif
If PORTB = %10010000 Then
PORTB = %00110000
Else
PORTB=PORTB << 1
'Se houver carry põe o bit menos significativo a 1 (RB4)
If PORTB = %10000000 Then PORTB = %10010000
Endif
If PORTB <> %10010000 Then Goto mr1
pause 1
'Wend
Return
motor_run2:
PORTB = %10010000
mr2:
pause 1
'Calculates number of steps
If n_passos <> max_passos Then
n_passos = n_passos + 1
Else
n_passos = 0
Endif
If PORTB = %10010000 Then
PORTB = %11000000
Else
If PORTB = %00110000 Then
PORTB = %10010000
Else
PORTB =PORTB>>1
Endif
Endif
If PORTB <> %00110000 Then Goto mr2
pause 1
Return
'1 minute delay
delay_1min:
i var Byte
For i = 1 To 60
pause 1000
Next i
Return