Hi,
I made this program, but the LED lights up, or goes out AFTER releasing the button,
not direct after pressing the button... Strange.
The button is between Ub and the input, and the input is with 1200 Ohm to ground.
Is there any reason for ?
'This code will wait until a button is pressed,'than it will invert an output.#chip 16F54, 1Dir PortA InDir PortB OutPortB = 0Do If PortA.0 = 1 then If PortB.0 = 0 then PortB.0 = 1 Else PortB.0 = 0 End if Wait 100 ms Wait until PortA.0 = 0 Wait 100 ms End ifLoop
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Would you be able to hand sketch a really simple diagram of your circuit? Show the 5v and 0v plus all devices and resistors. A device includes the switch.
This will really help us help you.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sounds like the button needs to be debounced. You can try waiting say 30 ms after Porta.0 = 1 and test condition again before proceeding. That is a pretty strong pulldown on the button, so shouldn't have to wait for port to go zero.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
'This code will wait until a button is pressed,'than it will invert an output.#chip 16F54, 1Dir PortA InDir PortB OutPortB = 0Do If PortA.0 = 1 thenWait100ms' <---------- does not change If PortB.0 = 0 then PortB.0 = 1 Else PortB.0 = 0 End if Wait 100 ms Wait until PortA.0 = 0 Wait 100 ms End ifLoop
Last edit: Eddylvi 2017-01-19
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The easiest way is to use the following: It will set the state of the LED to the state of the switch. #option volatile portb.0 ensures the output is set correctly. (I am sure everyone noticed this new capability in the release note last year.. :-) )
What I try to do is One push on the button is LED ON
Another push: LED GOES OUT
But the LED should change at the PUSH, not the release of the button.
And possible max 8 times on one chip, A.0 >> B.0.....A.7 >> B.7
Many thanks !
Eddy
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am going to assume we should do this without interrupts. Interrupts are for later.
There are about 20 very good demos in your demo folder that do exactly what you want. Please search your demo folder for LED demos.
And,
Some psuedo code with portb assumed to have 8 LEDs attached:
set portb to zero
set a_switch_flag to zero
Do a loop
if switch is on then...
if a_switch_flag is zero then
increment portb led array
set a_switch_flag to 1
else
set a_switch_flag to 0
endif
wait until the switch is off
endif
end the loop
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am really sorry.... honest !
But I still write the solution here, so that everyone knows what went wrong.
After that I grounded the input with 100 Ohm (!!!!) and the LED stil was blinking
as if that was programmed, (without touching the button) I could not believe that.
Than I found out that the button was connected to A.1 instead of A.0.
A.0 was open.... BUT I got (strange) reactions from pushing the button, so I was not
thinking of an error like this.
After changing the connection to pin 17 instead of 18 everything worked
as it should.
Again, very sorry, that this stupid error also have cost you all your time.
I still wrote it here, so that in future same errors can be recognised.
Kind regards,
Eddy
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This perspective comes from a GCB nube but a seasoned electronics forums member....
This issue would have been caught a long time ago if a clear photo of your project had been posted. Believe it or not "Duh" momements like you had happen all the time. ;-)
Sometimes it just takes another set of eyes.
Chris
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I made this program, but the LED lights up, or goes out AFTER releasing the button,
not direct after pressing the button... Strange.
The button is between Ub and the input, and the input is with 1200 Ohm to ground.
Is there any reason for ?
Hi Eddy,
Would you be able to hand sketch a really simple diagram of your circuit? Show the 5v and 0v plus all devices and resistors. A device includes the switch.
This will really help us help you.
Sounds like the button needs to be debounced. You can try waiting say 30 ms after Porta.0 = 1 and test condition again before proceeding. That is a pretty strong pulldown on the button, so shouldn't have to wait for port to go zero.
Thanks !
I make a diagram tomorrow, it is 0:42 now.
For the debouncing I used the two wait-s of 100 ms.
Zero wait time between test of button and toggle of PortB pin.
But it runs only one time before a wait time ? Ain't that logic enough than ?

Here the schematic
Last edit: Eddylvi 2017-01-19
@Eddy,
What outcome are trying to achieve?
is this code that part of a larger solution?
The easiest way is to use the following: It will set the state of the LED to the state of the switch. #option volatile portb.0 ensures the output is set correctly. (I am sure everyone noticed this new capability in the release note last year.. :-) )
What I try to do is One push on the button is LED ON
Another push: LED GOES OUT
But the LED should change at the PUSH, not the release of the button.
And possible max 8 times on one chip, A.0 >> B.0.....A.7 >> B.7
Many thanks !
Eddy
Now we can help. We understand the requirements. :-)
Before we solution. Have you used interrupts before?
I am going to assume we should do this without interrupts. Interrupts are for later.
There are about 20 very good demos in your demo folder that do exactly what you want. Please search your demo folder for LED demos.
And,
Some psuedo code with portb assumed to have 8 LEDs attached:
set portb to zero
set a_switch_flag to zero
The 16f54 is a baseline device, so no interrupt. Small stack, so use of subroutines is very limited.
Along the lines with what Anobium has suggested; Here is debounce routine that exits quickly. Code is not tested, but debounce method has.
Sorry, I was suddenly in hospital yesterday, so I could not answer.
I try to understand the new answers today.
Hello again,
I have not completely understood everything yet,
but I will try again with the examples.
Many thanks !
Eddy
Try this.... this is simplier.
I am really sorry.... honest !
But I still write the solution here, so that everyone knows what went wrong.
After that I grounded the input with 100 Ohm (!!!!) and the LED stil was blinking
as if that was programmed, (without touching the button) I could not believe that.
Than I found out that the button was connected to A.1 instead of A.0.
A.0 was open.... BUT I got (strange) reactions from pushing the button, so I was not
thinking of an error like this.
After changing the connection to pin 17 instead of 18 everything worked
as it should.
Again, very sorry, that this stupid error also have cost you all your time.
I still wrote it here, so that in future same errors can be recognised.
Kind regards,
Eddy
This perspective comes from a GCB nube but a seasoned electronics forums member....
This issue would have been caught a long time ago if a clear photo of your project had been posted. Believe it or not "Duh" momements like you had happen all the time. ;-)
Sometimes it just takes another set of eyes.
Chris