PDA

View Full Version : 16F628A - Stops if release power switch.



dene12
- 9th February 2009, 19:36
I have a led chaser based on 16F628A, all A's and B's as outputs [12 leds].
The sequence starts and... stops if i release the push button which power it, but the PIC can't keep power for itself after releasing the switch.
I want it to execute the sequence after a short push of a button.

What is to be done in the asm file ??

Thanks in advance.

mister_e
- 9th February 2009, 19:58
change line 14 of your code...

Does it work?

Oh well, if we had code and schematic we could have found the problem even in ASM in a Melabs PICBasic Pro forum :rolleyes:

16F628A
- 10th February 2009, 01:53
Overdone sarcasm aside, you really do need to post the salient details if you expect any sort of helpful answer.

dene12
- 10th February 2009, 05:02
Sorry, i thought it can be simply added to whatever code...

Here is my messy code. I couldn't have done it better. Any help would be very welcome, i needed 12-15 days of study of this pic&mpasm&icprog&stuff just to get to this level, i'm not blind yet but not far... Hope not to make an attack because of so much sitting, omg.

It only gives some warnings, no errors and it works;

So, the problems are;
-1. the code does not allow the PIC to power itself and the sequence stops when i release the pushbutton that powers it. Need to fix that or i will need to sacrifice an output [12 leds] to power an electromagnetic relay etc, etc. I need it to "play" the sequence only once, after a "short touch" of a push button [not retaining]. At this moment, it plays the seq. for 8 times, don't know why... althoug no looping is specified...
- 2. i need to "group" 2 or more delays in a shorter way; ex. "call delay 10x"... Actually, the code contains much longer delays.
- 3. code is horrible, need help to optimize it, correct it and maybe simplifying it, without loosing the ability to preciselly&independently control al the leds.

Thanks for your kindness, i hope my english is good enough, i'm in Romania
.


LIST p=16F628 ;tell assembler what chip we are using
include "P16F628.inc" ;include the defaults for the chip
__config 0x3D18 ;config settings (osc. type etc.)
cblock 0x20 ;start of general purpose registers
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
endc
LEDA0 Equ 0 ;set constant LED = 0
LEDPORTA0 Equ PORTA ;set constant LEDPORT = 'PORTA'
LEDA1 Equ 1 ;set constant LED = 1
LEDPORTA1 Equ PORTA ;set constant LEDPORT = 'PORTA'
LEDA2 Equ 2 ;set constant LED = 2
LEDPORTA2 Equ PORTA ;set constant LEDP0RT = 'PORTA'
LEDA3 Equ 3 ;set constant LED = 3
LEDPORTA3 Equ PORTA ;set constant LEDPORT = 'PORTA'
LEDA4 Equ 4 ;set constant LED = 4
LEDPORTA4 Equ PORTA ;set constant LEDPORT = 'PORTA'
LEDA5 Equ 5 ;set constant LED = 5
LEDPORTA5 Equ PORTA ;set constant LEDPORT = 'PORTA'
LEDB0 Equ 0 ;set constant LED = 0
LEDPORTB0 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB1 Equ 1 ;set constant LED = 1
LEDPORTB1 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB2 Equ 2 ;set constant LED = 2
LEDPORTB2 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB3 Equ 3 ;set constant LED = 3
LEDPORTB3 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB4 Equ 4 ;set constant LED = 4
LEDPORTB4 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB5 Equ 5 ;set constant LED = 5
LEDPORTB5 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB6 Equ 6 ;set constant LED = 6
LEDPORTB6 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB7 Equ 7 ;set constant LED = 7
LEDPORTB7 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDPORT Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDTRIS Equ TRISB ;set constant for TRIS register
org 0x0000 ;org sets the origin, 0x0000 for the 16F628,
;this is where the program starts running LEDTRIS Equ TRISB ;set constant for TRIS register
movlw 0x07
movwf CMCON ;turn comparators off (make it like a 16F84)
bsf STATUS, RP0 ;select bank 1
clrf TRISA ;set PortA all outputs
movlw b'00000000' ;set PortB all outputs
movwf LEDTRIS
bcf STATUS, RP0 ;select bank 0
;--------------------------------------------------------------------------------
Loop
BSF LEDPORTA0, LEDA0 ; RA0 ON
call Delay
call Delay
call Delay
call Delay
call Delay
call Delay
call Delay
movlw b'11111111'
movwf LEDPORT
call Delay
call Delay
call Delay
call Delay
movlw b'00000000'
movwf LEDPORT
call Delay
call Delay
call Delay
call Delay
movlw b'11111111'
movwf LEDPORT
call Delay
call Delay
call Delay
call Delay

call Delay
movlw b'00000000'
movwf LEDPORT
call Delay
call Delay
call Delay
call Delay
movlw b'11111111'
movwf LEDPORT
call Delay
call Delay
call Delay
call Delay
movlw b'00000000'
movwf LEDPORT
call Delay
call Delay
call Delay
call Delay
movlw b'11111111'
movwf LEDPORT
call Delay
call Delay
call Delay

call Delay
movlw b'00000000'
movwf LEDPORT
call Delay
call Delay
movlw b'00010000'
movwf LEDPORT
call Delay
movlw b'00001000'
movwf LEDPORT
call Delay
movlw b'00010000'
movwf LEDPORT
call Delay
movlw b'00000000'
movwf LEDPORT
call Delay
call Delay
bcf LEDPORTA0, LEDA0 ; RA0 off
;-------------------------------------------------------------------------------------

Delay movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0

decfsz count1 ,f
goto d1
retlw 0x00
end

Jerson
- 10th February 2009, 10:22
I'm not sure if this is what you're looking for. I think there should be an END statement just above the Delay routine or a GOTO loop if you want to repeat the sequence.



movwf LEDPORT
call Delay
call Delay
bcf LEDPORTA0, LEDA0 ; RA0 off

END ; if you want to stop right here
GOTO loop ; if you want to repeat some section of code. Ofcourse you have to
; put the label 'loop' where you want it.
;-------------------------------------------------------------------------------------

Delay movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1

16F628A
- 10th February 2009, 14:58
So far, so good. We need a schematic to solve problem #1, however.

Acetronics2
- 10th February 2009, 15:05
I have a led chaser based on 16F628A, all A's and B's as outputs [12 leds].
The sequence starts and... stops if i release the push button which power it, but the PIC can't keep power for itself after releasing the switch.
I want it to execute the sequence after a short push of a button.

What is to be done in the asm file ??

Thanks in advance.


a working solution :

Keep the chip under power but in sleep mode ... you cut the power ... circuit resets and execute the chasing sequence.

Then falls asleep.

trick used for mini Led Torch lamps.



Alain

Bruce
- 10th February 2009, 16:30
Alain,

Weird you would say that - see attached...;o}

dene12,

See the attached example for one way. Note this doesn't help for your power button. The PIC needs power to operate, so just power it all the time, and give the attached example a whirl. Place a pull-up resistor on /MCLR. When you press your button it should ground the /MCLR pin.

Note: You'll get faster responses for assembly code on the Microchip forum. If you need to tweak the example attached, visit http://forum.microchip.com/

We normally stick to PBP BASIC around here...;o}

Acetronics2
- 10th February 2009, 16:46
Hi, Bruce

Some times ago on this Forum, we had been working on a 12F683 project for Argentina ... for "PEU" some moderator of the "CandlePower Forum".

http://www.candlepowerforums.com/vb/showthread.php?p=2410929

His little shop is worth the look ...


BTW. This "config" really works great ...

Alain

dene12
- 10th February 2009, 17:04
Jerson, it's not working.
Really, the last 15 lines of my sequence are crucial. I knew that before your message, but i also tried your solution and of course it did not want to take it, it gave 30 errors ! ... are crucial, don't know FOR WHAT, lol, but are crucial, haha. OMG i'm finished

Here is my schematics;

http://www.picbasic.co.uk/forum/attachment.php?attachmentid=3189&stc=1&d=1234285240


Thanks everybody.
Let's see now, it's a very simple circuit. I need to solve problem 1, at least.

Bruce
- 10th February 2009, 17:05
Hi Alain,

Really cool looking flashlights, but I'm not sure I would spend $180 on one..;o}

Edit: Dene12,

If you're using an external oscillator, and the "A" version, you'll need the modified version.

Note your original config had it set for internal osc.

dene12
- 10th February 2009, 17:38
Bruce, nice, thank you very much.
Very nice.
Just nice, from assembling without errors and warnings to the sequence and
finally grouping the delays with " execute delay this number of times". Cool.
Today i love electronics.

I'll wait for some more answers to see if problem 1 will be completely solved, although grounding MCLR and that "sleep mode until /MCLR reset"..., almost solve the problem, looks very good ALREADY.

Thanks again, Bruce.

dene12
- 10th February 2009, 20:47
Bruce, chaser2.asm makes the MCLR pin ssso sensible it turn the A0 led ON by just hovering the hand over the pic at about 15 cm... The circuit has transformed into a kind of "human bioelectric field detector" effective up to 15 cm
This happened after i attached a wire to MCLR pin. The wire is now "in the air" and was intended to connect the pin to a PCB switch, but only by soldering it to MCLR pin it made everything uncontrolable...
Before soldering the wire, the sequence was able to be started by only touching the MCRL pin with a metal, without even being conected to the GND.

I only have here now some variable resistors 0-3K... you said something about a pull-up resistor, which i don't even imagine its shape... and function.
Can you help ?
This is the code chaser2;



LIST p=16F628A ;tell assembler what chip we are using
include "P16F628A.inc" ;include the defaults for the chip

;__config 0x3D18 ;config settings (osc. type etc.)

; similar to above with /MCLR enabled (for reset), XT osc (not internal). power-up timer on,
; and now you know at a glance what your config settings are.

__CONFIG _CP_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _LVP_OFF & _XT_OSC

cblock 0x20 ;start of general purpose registers
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
times ;number of times to execute Delay loop
endc

LEDA0 Equ 0 ;set constant LED = 0
LEDPORTA0 Equ PORTA ;set constant LEDPORT = 'PORTA'
LEDA1 Equ 1 ;set constant LED = 1
LEDPORTA1 Equ PORTA ;set constant LEDPORT = 'PORTA'
LEDA2 Equ 2 ;set constant LED = 2
LEDPORTA2 Equ PORTA ;set constant LEDP0RT = 'PORTA'
LEDA3 Equ 3 ;set constant LED = 3
LEDPORTA3 Equ PORTA ;set constant LEDPORT = 'PORTA'
LEDA4 Equ 4 ;set constant LED = 4
LEDPORTA4 Equ PORTA ;set constant LEDPORT = 'PORTA'

; NOTE: Removed below because RA5 = INPUT only on 16F628
; LEDA5 Equ 5 ;set constant LED = 5
; LEDPORTA5 Equ PORTA ;set constant LEDPORT = 'PORTA'

LEDB0 Equ 0 ;set constant LED = 0
LEDPORTB0 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB1 Equ 1 ;set constant LED = 1
LEDPORTB1 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB2 Equ 2 ;set constant LED = 2
LEDPORTB2 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB3 Equ 3 ;set constant LED = 3
LEDPORTB3 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB4 Equ 4 ;set constant LED = 4
LEDPORTB4 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB5 Equ 5 ;set constant LED = 5
LEDPORTB5 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB6 Equ 6 ;set constant LED = 6
LEDPORTB6 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDB7 Equ 7 ;set constant LED = 7
LEDPORTB7 Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDPORT Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDTRIS Equ TRISB ;set constant for TRIS register

org 0x0000 ;org 0 for the 16F628

banksel PORTB ;select bank 0
movlw 0x07
movwf CMCON ;turn comparators off (make it like a 16F84)
clrf PORTB ;clear all port latches before setting TRIS regs
clrf PORTA

banksel TRISA ;select bank 1
movlw b'00100000'
movwf TRISA ;set PortA outputs, RA5 = /MCLR input
clrf TRISB ;set PortB all outputs

banksel PORTB ;select bank 0

;----------------------------------------------------------------------

Loop
BSF LEDPORTA0, LEDA0 ; RA0 ON
movlw d'4' ; execute delay this number of times
movwf times
call Delay

movlw b'11111111'
movwf LEDPORT
movlw d'4' ; execute delay this number of times
movwf times
call Delay

movlw b'00000000'
movwf LEDPORT
movlw d'4'
movwf times
call Delay

movlw b'11111111'
movwf LEDPORT
movlw d'4'
movwf times
call Delay

movlw b'00000000'
movwf LEDPORT
movlw d'4'
movwf times
call Delay

movlw b'11111111'
movwf LEDPORT
movlw d'4'
movwf times
call Delay

movlw b'00000000'
movwf LEDPORT
movlw d'4'
movwf times
call Delay

movlw b'11111111'
movwf LEDPORT
movlw d'4'
movwf times
call Delay

movlw b'00000000'
movwf LEDPORT
movlw d'4'
movwf times
call Delay

movlw b'00010000'
movwf LEDPORT
movlw d'1'
movwf times
call Delay

movlw b'00001000'
movwf LEDPORT
movlw d'10'
movwf times
call Delay

movlw b'00010000'
movwf LEDPORT
movlw d'1'
movwf times
call Delay

movlw b'00000000'
movwf LEDPORT
movlw d'4'
movwf times
call Delay
bcf LEDPORTA0,LEDA0 ; RA0 off

; code above executes once, then PIC enters low-power sleep mode until /MCLR reset.

SleepLoop
SLEEP ; enter low power sleep
GOTO SleepLoop ; go back to sleep if it wakes up
;-------------------------------------------------------------------------------------

Delay
movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1

d1
movlw 0xC7
movwf counta
movlw 0x01
movwf countb

Delay_0
decfsz counta,f
goto $+2
decfsz countb,f
goto Delay_0
decfsz count1,f
goto d1
decfsz times ; execute delay this number of times
goto Delay

retlw 0x00

end

mister_e
- 10th February 2009, 21:25
A pull-up resistor is just a simple resistor connected between a pin (MCLR) and Vdd.
http://www.seattlerobotics.org/encoder/mar97/basics.html

Everything bellow 22K would work. Usually 10K for me (in many case).

dene12
- 12th February 2009, 19:22
It's totally uncontrollable, even with the pull-up resistor.
Then, I don't want 2 switches, there is nobody there to reset the pin.
The sequence must start and play with just a push&release, very short and then must standby and wait another push-start instead reseting.

Acetronics2
- 13th February 2009, 09:31
Too bad,

We had a working solution ... but with PBP.

Really sorry not to be able to help you further.

Alain

dene12
- 14th February 2009, 07:57
Too bad,

We had a working solution ... but with PBP.

Really sorry not to be able to help you further.

Alain

Alain, omg. I went back to your post with the working solution and i finally understood what you said... That post was posted before Bruce gave me the codes #1&#2 and at that time i was unable to think clearly.
Ace, thanks for coming back, this time i did it; a NC push button on Vdd powering the chip and that was it. omg it was easy.
Merci, Alain, thanks for coming back, omg.

Thanks everybody subject closed; this configuration is good for a half million applications, even for tech processes which are not so complex.