switches & LEDs ... my first attempt
Hi, a newbie to digital here. I'm having trouble getting a PIC16f628A to do more than one function at once with switches.
I have two switches and two LEDs. One switch operates one LED, the other switch operates the second LED.
I need each LED to operate independently of one another and together when their respective switches are activated. Right now, one switch interupts the others function. Here is my code:
Swstb var Porta.0 ' switchstb is on porta.0
Switchleft VAR PORTA.3 ' switchleft is on portb.0
LTS VAR PORTB.0 ' left portb.0
STB VAR PORTB.5 ' STB
TRISA.0 = 1 ' porta.0 is now an input
TRISA.3 = 1 ' Porta.3 is now an input
TRISB.0 = 0 ' Portb.0 is left
TRISB.5 = 0 ' Portb.5 is STB
Main:
IF Switchleft = 0 THEN left 'Pressing switch lights left led
if switchstb = 0 then strobe 'Pressing switch stobes led
Lts = 0 ' If switch is pressed, light the LED otherwise turn it OFF
stb = 0 ' If switch is pressed, strobe the LED otherwise turn it OFF
pause 83
GOTO Main
left:
LTS = 1 'Turn ON LTS
Pause 333 'Wait .333 seconds
LTS = 0 'Turn OFF LTS
Pause 333 'Wait .333 seconds
GOTO Main
strobe:
stb = 1 'Turn on strobe
pause 100 'wait for .1 seconds
stb = 0 'Turn off strobe
pause 100 'wait for .1 seconds
goto main
GOTO Main
Any help on this is greatly appreciated.
1 Attachment(s)
LED Flash Pattern Simulation
Hi,
OK. I've attached a program (in a ZIP file) that illustrates the LED flashing method that I described earlier.
It doesn't generate any PBP code, but it will give you a better idea of what is going on so that you can do it for yourself.
The 16 checkboxes at the bottom of the program represent the 16 bits (0 to 15) of the flash pattern word.
Checking or unchecking these boxes will change the "Flash Pattern Word Value" at the top.
NOTE:
I'm only displaying the "Flash Pattern Word Value" in binary format.
The little blue circle below the check boxes and the number in the "Current Pattern Bit Selected" text box indicate at what point in the flash pattern the LED (the big red box) is getting it's status from. Checked = ON, Unchecked = OFF.
Now, neither of the top 2 text boxes accept data input. They are simply indicators. However, the bottom 2 text boxes do accept input. These are the "Loop Delay (PAUSE)" text box and the "LED Update Count" text box. Any changes in these boxes will turn the box yellow. You will need to click the "Update Values" button to enter the changes you make here.
The "Loop Delay (PAUSE)" text box indicates the delay in milliseconds between each launch (i.e. a GOSUB) of the LED's status update routine. In PBP code, look at it as the delay between loops through "Main".
The "LED Update Count" is sort of a countdown between updates of the LED's status. It gets decremented each time the LED's status update routine is launched, and when it hit's zero the LED's flash pattern bit gets advanced by one.
The reason that I use a delay and a count is because a program is often doing several things. You may only want a 10ms delay between loops to handle debounce on switches. 10 milliseconds is way too short for 16 part LED flash though. You need a longer interval like 100ms. So, you end up using a count of ten with a loop delay of 10 milliseconds. Every ten milliseconds the switches are checked and the LED status update routine is launched. Within that routine the count gets decremented. If it's reached zero, the LED pattern bit is advanced and the count is reset to 10.
NOTE:
Due to some "VB issues", you can't make the loop delay value less than 100 milliseconds in the simulation. You can make it as small as you want on an actual PIC though.
Anyway, it will make much more sense to you when you run the program. Try it out by checking and unchecking the boxes. For example, checking the first 8 boxes and unchecking the last 8 boxes will produced a slow flashing pattern.
Enjoy!
Sorry for the long overdue reply ...
... I've been out of commission since August and just wanted to say thanks! Now that I'm back in the game, I'll try this out.
~ Dave
Sounds like a cool program................
Quote:
Originally Posted by picnaut
Hello,
Each time I pass through the loop I increment the flash-pattern word's bit counter by one and do the check again.
When the bit counter reaches 16 I reset it to 0 again (a word variable's bits are numbered 0 to 15).
For instance...
Main:
If Button1 = 0 Then
LED1Pattern = OPERATING_MODE
Else
LED1Pattern = STANDBY_MODE
EndIf
Pause 10
Gosub UpdateLEDState
Goto Main
:)
Hello picnaut,
I just found this post and I find it really interesting. I'd love to see your complete program if you could post it with all the defines and variables declared as I've never played with led's the way you explained it here but it sure sounds cool. I think I could learn a lot if I could take it apart and play with it.
Thanks
jessey