I'm new to PICs and working with a simple circuit that should read two momentary switches and illuminate one of two LEDs depending on which switch is pressed. I'm trying to look for the input on ports A.0 and A.1 and have the LEDs connected to ports B.0 and B.1. I can get one of the ports to read the state change and one LED to come on but the other doesn't work. I have tried different combinations of input and output ports but still never get both to work. I thought I was crazy because the one LED that does come on does not match the switch I assigned. I did check to make sure I was seeing a state change on the correct input pin yet the output port assigned to respond to that switch is not the one to come on. The other LED either doesn't light at all or comes on but with reduced brightness almost as if it's being PWM'd. I pulled everything apart and the last time I tried my code, it was different again in that the LED I was able to switch on now stays on after the switch is released but turns off when pressed again. I'm not sure whether to be happy it does something, albeit wrong, or scream.
The programming board I am using is the Velleman 8048 with the 16F627 (non-A). I have been able to blink LEDs, make chase lights, port sound out, etc. so something is working but not what I'm trying to do with the switches and lights.
I did find a couple of comments online about having to set the inputs as digital but don't how to do that from within GCB or if that's even the problem. I have no idea if there is a problem with hardware or if it's just something I have done wrong. My board provides four momentary switches connected to ports A.0 - A.3 and six LEDs connected to ports B.0 - B.5. I've considered writing to the IC and building the circuit on a proto-board trying input/output ports other than the ones I have tried.
Any ideas would be appreciated as I'm completely new to these.
TIA for any help
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I found the schematic so try this.
Remove the apostrophe from the #chip line. I put it there to show up properly in the forum.
'switches.gcb
'Monitor two switches and light LED when pressed.
'Setup for Demo-Shield
'A0 switch controls B0 LED
'A1 switch control B1 LED
;Chip Settings
'#chip 16F627,4
DIR PORTA.0 in 'Set A0 switch pin to input
DIR PORTA.1 in 'Set A1 switch pin to input
DIR PORTB.0 out 'Set B0 LED pin to output
DIR PORTB.1 out 'Set B1 LED pin to output
Start:
If PORTA.0=1 Then
set PORTB.0 on 'B0 LED on
Set PORTB.1 off 'B1 LED off
End If
If PORTA.1=1 Then
Set PORTB.1 on 'B1 LED on
set PORTB.0 off 'B0 LED off
End If
Goto Start
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm new to this also, but what you are doing should be pretty simple.
I've checked a switch lots of times, the only thing you have to do is set the port to input. You will probably have to debounce a noisy switch if you are checking it's value at a high rate. Also set a variable when the switch is first pushed and don't respond again until you find the switch off at least once. If you just set the output port on each time you check the switch, GCBasic has the annoying habit of turning the port off first and then back on, which could give you your dimmed light condition.
The best thing you could do at this point would be to make a stripped down version and post your code for someone to take a look at, then the guesses would be much smaller in number :)
as a quickie, i would try something like this untested code (I'm not at home right now)
#chip 16LF1939, 4 'chip I'm currently using with clock of 4 Mhz
#config OSC=INTOSC, CLKOUTEN=OFF,
Dir PORTA in ' all the buttons
Dir PORTB out ' All pins in Portb set to output
ANSELD = 0 ' needed for this chip, not done automatically by GCBasic
sw1 =0 'switch1 is open
sw2 =0 'switch2 is open
Do
If porta.0=1 Then 'if switch is pushed
If sw1=0 Then 'changed to on
sw1=1
portb.0=1
End If
' could add debounce here with wait statement
Else ' switch is open
If sw1=1 Then 'changed to off
sw1=0
portb.0=0
EndIf
End If
If porta.1=1 Then 'if switch is pushed
If sw2=0 Then 'changed to on
sw2=1
portb.1=1
End If
Else ' switch is open
If sw2=1 Then 'changed to off
sw2=0
portb.1=0
EndIf
End If
loop
Last edit: Jim giordano 2013-06-13
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I tried it on another code sample and then previewed it multiple times. I found all I needed to do was put one space in front of any # symbol and then #define, #chip and #include all displayed properly.
Such a simple solution.
Thanks Jim.
Without space:
chip PIC16F886, 4
define LED PORTB.0
include <chipino.h>
With one space in front:
#chip PIC16F886, 4
#define LED PORTB.0
#include <chipino.h>
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
My apologies for not responding sooner but I've been out of town.
Thanks for the input and suggestions. I'm excited to give the code suggestions a try. I don't have my old pc or programmer with me. I should be able to try the code by Monday evening. I only have one old laptop left that has a real serial port on it and decided not to drag it along.
I didn't originally post my code but it's short and will do so when I get back.
Greg
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well, back home albeit a day late and I finally had a chance to get back to the programmer.
My code was very close to the code Chuck posted. There wasn't anything there that appeared functionally different than mine but I entered it as listed to see if there would be a difference. The results were the same as what I had seen before. There's something odd going on here. The programmer was a kit so I thought it would be a good idea to look at the signal paths just to confirm I had what I should and nothing I shouldn't. It all looked good with switches operating properly and voltage levels at the inputs as they should be. I still have odd things happening.
If I use just one input and one output I can press a switch and an LED lights. It's still not right though. I picked AN0 and RB0 as input and output. AN0 is physically connected to SW1 yet RB0 only goes high when I press AN1 (SW2). If I recode to use another input and/or output, other things happen or nothing at all happens. In addition, there is no combination of inputs/outputs I can code that will result in two switches controlling two LEDs. These are the same results I had which lead me to post for help.
I'm going to try to find time to code for one and two LED/switches and then put the 627 on a proto-board and see if there is some issue with the programmer board. I'm hoping for a better outcome although the programming board is pretty straight forward and I haven't found any problems. I'll post my results.
Greg
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I finally put everything on the protoboard. The short story is nothing changed.
I had the same result as I had while trying it on the programmer board. I did try several other things to try to decide what the problem was or, just maybe, wasn't. I had already done blinking lights, chase lights, sound out, etc. successfully and, with a few more experiments, realize anything limited to outputs seems to follow what the code tells it to do. Two inputs has not worked at all as one LED will stay on even if I tell it to never come on. I tried to use only "B" ports for input and output with the same failing results.
Using just one switch input gets me as close to success as I have gotten but even then it's not correct. SW1 on my programmer is hard-wired to RA0 and with the code written to read that switch it still activates the assigned output port only when SW2 (hard-wired to RA1) is pressed. At this point I'm baffled.
It functions or malfunctions in the same manner on the programmer or protoboard, programs operate as expected using only output commands and the results are not limited to just one IC (I purchased more) I'm now starting to wonder if this is in some way related to GCBASIC. I didn't realize until I returned from my trip that I had not installed the update to GCB so I'm still using the 2007 version. Anyone think this could account for the odd behavior? When I started using this old laptop the CDROM got as far as transferring GCB and the programmer s/w before quitting. I have no other way to transfer the update until I find my other old laptop or spare CDROM unit. A step forward and three back...
Greg
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The old level software can definitely be a factor.
There have been a lot of fixes since 2007.
I assume the old laptop can't download?
Does the old laptop have a USB port?
Can you use a USB Flash Drive?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Were the new chips also the non-A version of the 16F627?
Would you post the .asm file produced by GCBasic so we can see if it's compiling correctly?
Can you download the code from the chip after it's programmed to see if it's getting set correctly?
With these three things, we should be able to find the problem.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Chuck:
The old laptop has no USB and, apparently, is on it's last leg. I have another of the same model which is in better shape and would allow me to use USB via PCMCIA but haven't remembered where it is since the last move. It also probably has the better CDROM and floppy unit with it.
Jim:
The original 627 chip was a non-A but what I ordered was a few 628A chips which gave the same results. The programming s/w will read the chip after programming but I honestly don't think I would know whether it was correct or not. My last dealings with machine or assembly were almost 30 years ago. What form would I want to offer the .asm file in? Can it be attached here in some way or would it need to be a text listing?
As my wife says, I seem to be just one clip-lead short of a working project once again :)
Greg
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
What value resistors are you using for the pull ups/pull downs on the buttons, and for current limiting on the LEDs? That's sometimes something that causes problems if the button resistors are too large or the LED resistors are too small.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sorry Anobium, I watched the video without sound up properly the first time, then deleted my original comment about the video showing strange behaviour. Must remember to engage brain before posting sometimes! The video does show everything working fine, and 10k pull ups with 470R current limiting are good.
I'm trying to think of anything that could possibly result in the problems that Greg W is having. I can't think of anything that could be a compiler problem, even the 2007 version should be able to blink some LEDs with a 16F627. One of the first PICs I used with GCBASIC was a 16F628A back in 2006. Greg, are you sure that you have the pull down resistors on the buttons attached correctly?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Send me a note and I will send you the hex and asm etc.
Check you circuit. Check you have 10k pull ups etc. If you have questions on this, I can send you a great document on basic connectivity.
Try the hex and asm.
Let us all know the results.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Greg-
Looks like you're getting lots of help from people much better qualified than myself now, so I'm going to bow out. But in answer to your question, yes, you can just cut and paste the contents of the .asm file here. Nearly every problem I've had with GCBasic has been solved by looking at the asm and seeing something unexpected. Good luck. If I can be of any further assistance, feel free to ask.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm new to PICs and working with a simple circuit that should read two momentary switches and illuminate one of two LEDs depending on which switch is pressed. I'm trying to look for the input on ports A.0 and A.1 and have the LEDs connected to ports B.0 and B.1. I can get one of the ports to read the state change and one LED to come on but the other doesn't work. I have tried different combinations of input and output ports but still never get both to work. I thought I was crazy because the one LED that does come on does not match the switch I assigned. I did check to make sure I was seeing a state change on the correct input pin yet the output port assigned to respond to that switch is not the one to come on. The other LED either doesn't light at all or comes on but with reduced brightness almost as if it's being PWM'd. I pulled everything apart and the last time I tried my code, it was different again in that the LED I was able to switch on now stays on after the switch is released but turns off when pressed again. I'm not sure whether to be happy it does something, albeit wrong, or scream.
The programming board I am using is the Velleman 8048 with the 16F627 (non-A). I have been able to blink LEDs, make chase lights, port sound out, etc. so something is working but not what I'm trying to do with the switches and lights.
I did find a couple of comments online about having to set the inputs as digital but don't how to do that from within GCB or if that's even the problem. I have no idea if there is a problem with hardware or if it's just something I have done wrong. My board provides four momentary switches connected to ports A.0 - A.3 and six LEDs connected to ports B.0 - B.5. I've considered writing to the IC and building the circuit on a proto-board trying input/output ports other than the ones I have tried.
Any ideas would be appreciated as I'm completely new to these.
TIA for any help
I found the schematic so try this.
Remove the apostrophe from the #chip line. I put it there to show up properly in the forum.
'switches.gcb
'Monitor two switches and light LED when pressed.
'Setup for Demo-Shield
'A0 switch controls B0 LED
'A1 switch control B1 LED
;Chip Settings
'#chip 16F627,4
DIR PORTA.0 in 'Set A0 switch pin to input
DIR PORTA.1 in 'Set A1 switch pin to input
DIR PORTB.0 out 'Set B0 LED pin to output
DIR PORTB.1 out 'Set B1 LED pin to output
Start:
If PORTA.0=1 Then
set PORTB.0 on 'B0 LED on
Set PORTB.1 off 'B1 LED off
End If
If PORTA.1=1 Then
Set PORTB.1 on 'B1 LED on
set PORTB.0 off 'B0 LED off
End If
Goto Start
I'm new to this also, but what you are doing should be pretty simple.
I've checked a switch lots of times, the only thing you have to do is set the port to input. You will probably have to debounce a noisy switch if you are checking it's value at a high rate. Also set a variable when the switch is first pushed and don't respond again until you find the switch off at least once. If you just set the output port on each time you check the switch, GCBasic has the annoying habit of turning the port off first and then back on, which could give you your dimmed light condition.
The best thing you could do at this point would be to make a stripped down version and post your code for someone to take a look at, then the guesses would be much smaller in number :)
as a quickie, i would try something like this untested code (I'm not at home right now)
Last edit: Jim giordano 2013-06-13
what a mess it made of my code. didn't look at all like that in the preview :( sorry. Think I fixed it.
(Why did they change how to format code?)
Last edit: Jim giordano 2013-06-13
It looks better.
What is the secret to make the #chip line to look proper?
I like the preview feature but I don't like the new formatting.
put at least 4 spaces in front of every line
I tried it on another code sample and then previewed it multiple times. I found all I needed to do was put one space in front of any # symbol and then #define, #chip and #include all displayed properly.
Such a simple solution.
Thanks Jim.
Without space:
chip PIC16F886, 4
define LED PORTB.0
include <chipino.h>
With one space in front:
#chip PIC16F886, 4
#define LED PORTB.0
#include <chipino.h>
I wonder what happened to Greg W
I was wondering that as well.
Did the samples work?
Did he figure it out?
I assume its fixed or he would have posted more.
My apologies for not responding sooner but I've been out of town.
Thanks for the input and suggestions. I'm excited to give the code suggestions a try. I don't have my old pc or programmer with me. I should be able to try the code by Monday evening. I only have one old laptop left that has a real serial port on it and decided not to drag it along.
I didn't originally post my code but it's short and will do so when I get back.
Greg
No problem. I'm on vacation myself, which is why I gave you untested code :)
Well, back home albeit a day late and I finally had a chance to get back to the programmer.
My code was very close to the code Chuck posted. There wasn't anything there that appeared functionally different than mine but I entered it as listed to see if there would be a difference. The results were the same as what I had seen before. There's something odd going on here. The programmer was a kit so I thought it would be a good idea to look at the signal paths just to confirm I had what I should and nothing I shouldn't. It all looked good with switches operating properly and voltage levels at the inputs as they should be. I still have odd things happening.
If I use just one input and one output I can press a switch and an LED lights. It's still not right though. I picked AN0 and RB0 as input and output. AN0 is physically connected to SW1 yet RB0 only goes high when I press AN1 (SW2). If I recode to use another input and/or output, other things happen or nothing at all happens. In addition, there is no combination of inputs/outputs I can code that will result in two switches controlling two LEDs. These are the same results I had which lead me to post for help.
I'm going to try to find time to code for one and two LED/switches and then put the 627 on a proto-board and see if there is some issue with the programmer board. I'm hoping for a better outcome although the programming board is pretty straight forward and I haven't found any problems. I'll post my results.
Greg
I finally put everything on the protoboard. The short story is nothing changed.
I had the same result as I had while trying it on the programmer board. I did try several other things to try to decide what the problem was or, just maybe, wasn't. I had already done blinking lights, chase lights, sound out, etc. successfully and, with a few more experiments, realize anything limited to outputs seems to follow what the code tells it to do. Two inputs has not worked at all as one LED will stay on even if I tell it to never come on. I tried to use only "B" ports for input and output with the same failing results.
Using just one switch input gets me as close to success as I have gotten but even then it's not correct. SW1 on my programmer is hard-wired to RA0 and with the code written to read that switch it still activates the assigned output port only when SW2 (hard-wired to RA1) is pressed. At this point I'm baffled.
It functions or malfunctions in the same manner on the programmer or protoboard, programs operate as expected using only output commands and the results are not limited to just one IC (I purchased more) I'm now starting to wonder if this is in some way related to GCBASIC. I didn't realize until I returned from my trip that I had not installed the update to GCB so I'm still using the 2007 version. Anyone think this could account for the odd behavior? When I started using this old laptop the CDROM got as far as transferring GCB and the programmer s/w before quitting. I have no other way to transfer the update until I find my other old laptop or spare CDROM unit. A step forward and three back...
Greg
The old level software can definitely be a factor.
There have been a lot of fixes since 2007.
I assume the old laptop can't download?
Does the old laptop have a USB port?
Can you use a USB Flash Drive?
Were the new chips also the non-A version of the 16F627?
Would you post the .asm file produced by GCBasic so we can see if it's compiling correctly?
Can you download the code from the chip after it's programmed to see if it's getting set correctly?
With these three things, we should be able to find the problem.
I feel your frustration... try this code. Using @Chuck code as the basis, this works on my emulator and on my test kit.
Good luck.
'switches.gcb
'Monitor two switches and light LED when pressed.
'Setup for Demo-Shield
'A0 switch controls B0 LED
'A1 switch control B1 LED
;Chip Settings
#chip 16F627,4
#config INTRC_OSC_NOCLKOUT, MCLRE_OFF
DIR PORTA.0 in 'Set A0 switch pin to input
DIR PORTA.1 in 'Set A1 switch pin to input
DIR PORTB.0 out 'Set B0 LED pin to output
DIR PORTB.1 out 'Set B1 LED pin to output
do
If PORTA.0=0 Then
set PORTB.0 on 'B0 LED on
else
set PORTB.0 off
end If
If PORTA.1=0 Then
Set PORTB.1 on
else
set PORTB.1 off
End If
loop
I think your issue the correct testing of the ports and the appropriate assignment when you make the tests.
I have just tested this on the same chip. All works ok here.
Any issues then post again and we can help.
Anobium
Chuck:
The old laptop has no USB and, apparently, is on it's last leg. I have another of the same model which is in better shape and would allow me to use USB via PCMCIA but haven't remembered where it is since the last move. It also probably has the better CDROM and floppy unit with it.
Jim:
The original 627 chip was a non-A but what I ordered was a few 628A chips which gave the same results. The programming s/w will read the chip after programming but I honestly don't think I would know whether it was correct or not. My last dealings with machine or assembly were almost 30 years ago. What form would I want to offer the .asm file in? Can it be attached here in some way or would it need to be a text listing?
As my wife says, I seem to be just one clip-lead short of a working project once again :)
Greg
I just tried emailing you the files directly... no luck.
See attached.
I cannot post any more responses.... send me a personal message and I will provide you access to my FTP site to get the code, asm and hex files.
Last edit: Anobium 2013-06-22
What value resistors are you using for the pull ups/pull downs on the buttons, and for current limiting on the LEDs? That's sometimes something that causes problems if the button resistors are too large or the LED resistors are too small.
Hugh, there is no issue. The video shows the solution working as expected. I am trying to show that using the code show above at https://sourceforge.net/p/gcbasic/discussion/579126/thread/3ddb6389/#a243 that the all is good.
And, 10k pull ups with 470r LED current limiting.
Anobium
Sorry Anobium, I watched the video without sound up properly the first time, then deleted my original comment about the video showing strange behaviour. Must remember to engage brain before posting sometimes! The video does show everything working fine, and 10k pull ups with 470R current limiting are good.
I'm trying to think of anything that could possibly result in the problems that Greg W is having. I can't think of anything that could be a compiler problem, even the 2007 version should be able to blink some LEDs with a 16F627. One of the first PICs I used with GCBASIC was a 16F628A back in 2006. Greg, are you sure that you have the pull down resistors on the buttons attached correctly?
@Hugh. :-), and I cannot post attachments....
@Greg.
Send me a note and I will send you the hex and asm etc.
Check you circuit. Check you have 10k pull ups etc. If you have questions on this, I can send you a great document on basic connectivity.
Try the hex and asm.
Let us all know the results.
Greg W.
Can you just post a picture of your breadboard circuit and a schematic?
We need the whole picture to help.
Anobium
I didn't know Real Pic Simulator existed until I saw your video. Looks like a great tool. Thanks.
Greg-
Looks like you're getting lots of help from people much better qualified than myself now, so I'm going to bow out. But in answer to your question, yes, you can just cut and paste the contents of the .asm file here. Nearly every problem I've had with GCBasic has been solved by looking at the asm and seeing something unexpected. Good luck. If I can be of any further assistance, feel free to ask.