PDA

View Full Version : LED Blinking while main does something else



dwright
- 1st June 2015, 00:14
Hello, I am new to PIC programming and the forum so please be gentle. I was hoping someone could offer some suggestions on a program. I am using a PIC16f628 to check for a button press (this all works) and I have one pin set for High for a steady LED. I am trying to also have a second LED blink continuously while the main program keeps looping (checking for button presses. I am trying to use the blink LED:

main:
High LED5 ' Turn on LED connected to PORTB.0
Pause 1000 ' Delay for .5 seconds
Low LED5 ' Turn off LED connected to PORTB.0
Pause 1000


readbutton1:

Pause 30
Button bt1,0,40,0,B1,0,readbutton2
Toggle LED1 ' Toggle LED if pressed

readbutton2:
Button button2,0,40,0,B2,0,readbutton1
Toggle LED2 ' Toggle LED if pressed
goto readbutton1

If I have it return to main, then it blinks but the button checks are erratic. I am trying to get away from having a 555 timer in the circuit just to provide a blinking LED and was hoping something SIMPLE would work.
Any advice would be greatly appreciated.

Demon
- 1st June 2015, 00:20
Darrel's blinky led is what you need.

Robert

dwright
- 1st June 2015, 01:32
Thank You Robert, I will give that a try

dwright
- 1st June 2015, 03:23
Hello Robert,

When compiling the Blinky code I get these ASM errors, using PBP3 (MPLABX IDE) with PIC16f628. Any ideas on the errors?

7847

richard
- 1st June 2015, 06:55
as the error message suggests , add these lines to your code

wsave VAR BYTE $70 SYSTEM ' location for W if in bank0
wsave1 VAR BYTE $A0 SYSTEM ' location for W if in bank1
wsave2 VAR BYTE $120 SYSTEM ' location for W if in bank2

LinkMTech
- 1st June 2015, 16:04
I've never used the Button command before but here's something simple to try.
You'll probably need to modify your readbutton routines so it returns to the Main loop after testing for button activity.



x VAR BYTE

main:
High LED5 ' Turn on LED connected to PORTB.0

FOR x = 0 TO 250 ' Loop here for 1 second while monitoring
PAUSE 4
GOSUB readbutton1
GOSUB readbutton2
NEXT x

Low LED5 ' Turn off LED connected to PORTB.0

FOR x = 0 TO 250 ' Loop here for 1 second while monitoring
PAUSE 4
GOSUB readbutton1
GOSUB readbutton2
NEXT x

GOTO main


readbutton1:
Button bt1,0,40,0,B1,0,readbutton2 ' main instead of redabutton2?
Toggle LED1 ' Toggle LED if pressed
RETURN

readbutton2:
Button button2,0,40,0,B2,0,readbutton1 ' main instead of redabutton1?
Toggle LED2 ' Toggle LED if pressed
RETURN

END

Acetronics2
- 1st June 2015, 19:34
just read the "DT interrupts" file comments header....

explanation is there !

Alain

dwright
- 2nd June 2015, 03:12
Richard, thanks for pointing out what was clearly staring me in the face. I will also give LinkMTech version a try. I really appreciate the help.

Thanks,
Don

flotulopex
- 22nd June 2015, 10:39
I sometimes like to use "old fashion" systems like "autonomous" blinking leds like, i.e., these ones here (http://www.kingbrightusa.com/category.asp?catalog_name=LED&category_name=KCBlinking+LED&Page=1); there are many others - just google around.

So when you need blinking led, just set the respective PORT and do what ever you need to and when done, reset the PORT ;-)

No interrupts, no subroutines, just two lines of code.....

Ioannis
- 22nd June 2015, 11:57
But if you do it the software way, you can be sure that te program is running and did not crash.

Ioannis

flotulopex
- 22nd June 2015, 12:00
You're right Ioannis ;)

dwright
- 5th July 2015, 21:55
OK, I finally had some time to get back to it. However the code compiles fine but I don't get any output on B1. If anyone can point me to what Im missing please do. I am using a 16f628. Attached is the PBP3 code that complies. Thank You in advance.

wsave VAR BYTE $70 SYSTEM
LED1 VAR PORTB.1


INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts



CMCON = 7
TRISB = 0
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

T1CON = $31 ; Prescaler = 8, TMR1ON
@ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts

Main:
PAUSE 1
GOTO Main

'---[TMR1 - interrupt handler]--------------------------------------------------
ToggleLED1:
TOGGLE LED1
@ INT_RETURN

richard
- 6th July 2015, 08:26
If anyone can point me to what Im missing please do.

no. 1 putting code in code tags
notice how all the necessary indenting is now lost


wsave VAR BYTE $70 SYSTEM
LED1 VAR PORTB.1


INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts



CMCON = 7
TRISB = 0
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

T1CON = $31 ; Prescaler = 8, TMR1ON
@ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts

Main:
PAUSE 1
GOTO Main

'---[TMR1 - interrupt handler]--------------------------------------------------
ToggleLED1:
TOGGLE LED1
@ INT_RETURN

no 2. posting complete code

the "configuration word/s" are a vital part of pic code , having to speculate makes diagnosis a hit and miss affair

no 3. posting a sketch/description of your setup
led1 is on what pin? , what resistor? . is mclr high ? , what vcc ? , xtal /ext clock/intosc ?