+ Reply to Thread
Results 1 to 6 of 6

Thread: little help with my program please - simple count loop

  1. #1
    kud0s Newbie
    Join Date
    Feb 2005
    Posts
    8

    Default little help with my program please - simple count loop

    hi there


    here is what im trying to do:
    a simple program that is acivated from a simple switch on an input pin on RA7 on the PIC, then the pic outputs a set number of pulses on anuther output pin in my case RA0. then when it has done this it outputs a simple finish pulse on RA1 pin.

    Program:

    here is my program to date, i have it working but im alittle lost on putting a simple count down or up loop into it so i can set it to putput for a set number of pulses as such.

    any help would be grate

    program is as follows

    Code:
    ;indexer program - pules's to index with start and finish pules
    	LIST	p=16F628A		;tell assembler what chip we are using
    	include "P16F628A.inc"		;include the defaults for the chip
    	__config 0x3F18			;sets the configuration settings (oscillator type etc.)
    
    ;defines
    PULPORT	Equ	PORTA			;set constant PULPORT = 'PORTA'
    TRIPORT	Equ	PORTA			;set constant TRIPORT = 'PORTA'
    PORT	Equ	TRISA			;set constant for TRIS register
    TRIG1	Equ	7
    PULSE	Equ	0
    FIN	Equ	1
    
    ;end defines
    
    	cblock 	0x20 			;start of general purpose registers
    		count1 			;used in delay routine
    		counta 			;used in delay routine 
    		countb 			;used in delay routine
    		count2
    	endc
    
    
    	org	0x0000			;org sets the origin, 0x0000 for the 16F628,
    					;this is where the program starts running	
    	movlw	0x07
    	movwf	CMCON			;turn comparators off (make it like a 16F84)
    
       	bsf 	STATUS,		RP0	;select bank 1
       	movlw 	b'10000000'		;set PortA 7 input rest outputs
       	movwf 	PORT			;set portA
    	bcf	STATUS,		RP0	;select bank 0
    	clrf	PULPORT			;set all outputs low
    
    ;main program
    
    loop	btfss	TRIPORT, TRIG1
    		call	TRIG			;call 
    		goto	loop
    
    
    ;loops
    
    TRIG	
    ;	movlw	d'10'			;pules output for steps
    ;lp	movwf	count2
    	bsf	PULPORT, PULSE		;output 1 for pulse RA0
    	call 	delay			;call delay
    	bcf	PULPORT, PULSE		;turn off RA0
    	call	delay
    ;	decfsz	count2,	f
    ;	goto	lp
    	bsf	PULPORT, FIN		;turn on RA1 for finish pulse
    	call	delay			;call delay
    	bcf	PULPORT, FIN		;turn off RA1 for Finihs
    	retlw	0x00
    
    delay	movlw	d'150'			;delay 250 ms (4 MHz clock)
    	movwf	count1
    d1	movlw	0xC7			;delay 1mS
    	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
    
    once again thanks 8)
    Attached Files


  2. #2
    kud0s Newbie
    Join Date
    Feb 2005
    Posts
    8

    Default

    forgot to say

    the lines in the TRIG loops that i commented out with ; was wht i had a play with to do a loop for a set ammoutn which did not work, it seems to get stuck in that loop!!

    lines
    ; movlw d'10' ;pules output for steps
    ;lp movwf count2

    and
    ; decfsz count2, f
    ; goto lp

    does any one know why?

  3. #3
    williB Good williB Good
    Join Date
    Aug 2004
    Location
    New Jersey
    Posts
    2,226

    Default

    countdown loop complements of Mike ..

    Code:
            movlw   d'33'           ; use 33 (20 mhz) or 13 (8 mhz)   |B0 
            movwf   TEMP            ;                                 |B0 
    ACQ     decfsz  TEMP,F          ; wait 20 usec  to acquire        |B0
            goto    ACQ
    

  4. #4
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,754

    Default

    Quote Originally Posted by kud0s
    forgot to say

    the lines in the TRIG loops that i commented out with ; was wht i had a play with to do a loop for a set ammoutn which did not work, it seems to get stuck in that loop!!

    lines
    ; movlw d'10' ;pules output for steps
    ;lp movwf count2

    and
    ; decfsz count2, f
    ; goto lp

    does any one know why?
    As williB's example shows, you are jumping back one line too far, so 'count2' continually gets reloaded, and can never reduce to zero to end the loop.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  5. #5
    kud0s Newbie
    Join Date
    Feb 2005
    Posts
    8

    Default

    thanks guys

    top stuff fully sorted it now time to refine it up

  6. #6
    mstechca Bad
    Join Date
    Mar 2003
    Location
    Why do you care?
    Posts
    1,598

    Default Re: little help with my program please - simple count loop

    Quote Originally Posted by kud0s
    hi there


    here is what im trying to do:
    a simple program that is acivated from a simple switch on an input pin on RA7 on the PIC, then the pic outputs a set number of pulses on anuther output pin in my case RA0. then when it has done this it outputs a simple finish pulse on RA1 pin...
    Dont even bother using a microcontroller.

    Use a 4060 binary counter. Your button will activate the reset function on the chip, and connect the device that receives the pulses to Q0 since every odd number in binary has a 1 at the end of it. Now you will get pulses. Once the desired bit pattern is received, force the clock to stop.

    Here is an example:

    If you want the chip to pulse 4 times, connect the chip as follows:
    connect Q0 to output
    connect Q3 to the input of a NOR or an OR gate.
    connect your clock to the other input of the same gate.
    The output of the gate is connected to the clock pin on the counter.

    As soon as the chip is initialized, Q0 will be pulsed 4 times and that's it.
    When the reset pin is activated, the process starts over.

    Here's how the counter does it:

    at initialization:

    ....0000
    ....0001
    ....0010
    ....0011
    ....0100
    ....0101
    ....0110
    ....0111
    ....1000
    ....1000
    ....1000

    Notice how the right most column switches between 1 and 0? this is the best place to take the pulse at normal clock speed. For a pulse half the clock speed, connect the output to Q1 instead. At the end, the binary result 1000 remains true until the reset is activated. Why? because a "1" is applied to the gate and the output of the gate is fixed when a "1" is applied to at least one input. If bit 4 or Q3 wasn't activated, then the output is determined by the clock, and depending on the gate, it could be inversed.
    -=: The best low-priced components to troubleshoot with are the speaker and the LED :=-

+ Reply to Thread

Tags for this Thread