PIC16F684 Programming in Assembly

Thread Starter

Tomahawk640

Joined Feb 20, 2009
20
For every time I've done it, the pin has read low (just staying stuck in the loop) with code I gave you. When trying a different code (back where I said "...it was setting bit 0 of PORTA but I couldn't find where..", the program skipped the loop since the bit was set already. I have not had the case where it would sometimes be set and sometimes not though. Unfortunately, the project this is for is designed for very limited control by the user, hence a switch going from the pin to ground can't be used in the final design, not to mention that this is 1 of 6 PIC to motor setups.

So using a wire to apply voltage to a pin as an input can create problems? How else could you provide an input? As for the possible bouncing problem, I plan to put a debounce section in the program.
 

n9352527

Joined Oct 14, 2005
1,198
Using only a wire means that the input pin is left floating when the wire is not connected. There's no telling what is the value of floating input pin when read. At the very least, you need to connect a resistor, either to ground or Vcc, depending on the default value that you need. Or enable the internal weak pull-up, if there is any. Check the datasheet to see if that RA0 on that PIC has an internal pull-up.

With internal pull-up the default value will be 1, and you need to touch the pin with a grounded wire to provide 0.

You can use a switch during development and debugging only, that'll make your life easier.

Can you post the complete source code?
 

Thread Starter

Tomahawk640

Joined Feb 20, 2009
20
Oh OK I see. I'll try that setup the next time I get a chance. Here's the complete program:

LIST R=DEC
INCLUDE "p16f684.inc"
__CONFIG _FCMEN_OFF & _IESO_OFF & _BOD_OFF & _CPD_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO

; Variables
CBLOCK 0x20
Dlay, i, INNER, OUTER, OUTER2
ENDC

PAGE
; Mainline

org 0

nop ; For ICD Debug
CLRF INNER
CLRF OUTER

;*******Start of input requirement for PIC*******

STATUS equ 03h ;Address of the STATUS register
TRISA equ 85h ;Address of the tristate register for port A
PORTA equ 05h ;Address of Port A
;****Set up the port****
movlw b'111'
movwf CMCON0
bsf STATUS,5 ;Switch to Bank 1
movlw 01h ;Set the Port A pins:
movwf TRISA ;bit 1to output, bit 0 to input.
bcf STATUS,5 ;Switch back to Bank 0

Delayloop4:
Delayloop5:
decfsz INNER, 1
goto Delayloop5
decfsz OUTER, 1
goto Delayloop4




Start:
btfss PORTA, 0 ;skip next line if bit 0 is set in PORTA
goto Start

;*******end of input requirement*******

movlw 0
movwf OUTER
movwf OUTER2
movlw 1 << 2 ; Start with Bit 2 Active
movwf PORTC
movlw 7 ; Turn off Comparators
movwf CMCON0
bsf STATUS, RP0 ; Execute out of Bank 1
clrf ANSEL ^ 0x080 ; All Bits are Digital
movlw 1 << 2 ; Start with Bit 2 Active
movwf PORTC
movlw 7 ; Turn off Comparators
movwf CMCON0
bsf STATUS, RP0 ; Execute out of Bank 1
clrf ANSEL ^ 0x080 ; All Bits are Digital
movlw b'000011' ; RC5:RC2 are Outputs
movwf TRISC ^ 0x080
bcf STATUS, RP0 ; Return Execution to Bank 0

clrf i

movlw 100 ;Number of steps in cw direction
movwf INNER
ccwLoop:
call ccwstep
decfsz INNER, 1
goto ccwLoop

Delayloop3: ;This section delays the transition between the two directions by 50 seconds.
Delayloop:
Delayloop2:
decfsz INNER, 1
goto Delayloop2
decfsz OUTER, 1
goto Delayloop
decfsz OUTER2,1
goto Delayloop3


movlw 100 ;Number of steps in cw direction
movwf INNER
cwLoop:
call cwstep
decfsz INNER, 1
goto cwLoop
goto stopmotor

ccwstep: ; Return Here for Next Value
;movlw HIGH ((250000 / 5) + 256);250ms
movlw HIGH ((100000 / 5) + 256);100ms
movwf Dlay
;movlw LOW ((250000 / 5) + 256);250ms
movlw LOW ((100000 / 5) + 256);100ms
addlw -1 ; add -1 to w
btfsc STATUS,Z ; skip on no carry at digit Z
decfsz Dlay, f
goto $ - 3

movf i, w
call SwitchRead
movwf PORTC

incf i, f ; i=(i+1) % 8;
bcf i, 2

return

cwstep: ; Return Here for Next Value
;movlw HIGH ((250000 / 5) + 256);250ms
movlw HIGH ((100000 / 5) + 256);100ms
movwf Dlay
;movlw LOW ((250000 / 5) + 256);250ms
movlw LOW ((100000 / 5) + 256);100ms
addlw -1 ; add -1 to w
btfsc STATUS,Z ; skip on no carry at digit Z
decfsz Dlay, f
goto $ - 3

movf i, w
call SwitchRead2
movwf PORTC

incf i, f ; i=(i+1) % 8;
bcf i, 2

return

stopmotor:
goto stopmotor


SwitchRead:
addwf PCL, f
dt b'010100', b'011000', b'101000', b'100100' ;ccw
return

SwitchRead2:
addwf PCL, f
dt b'010100', b'100100', b'101000', b'011000' ;cw
return
end
 

thatoneguy

Joined Feb 19, 2009
6,359
Two questions:

1) Why the delay at the very start?
2) Bank switching - Two lines are commented switch to bank 1, though no switch to bank zero is between them.
 

Thread Starter

Tomahawk640

Joined Feb 20, 2009
20
The delay was suggested earlier in the thread. As for the the part that's switching it to bank 1 twice, I'm not sure but it works. It's the part before it, the input part, that I'm more concerned about. Do you have any suggestions in that regard?
 

thatoneguy

Joined Feb 19, 2009
6,359
I set up all the ports at the start of a program, so if I change things, the configuration is there. Also, no extra bank switching later on in the code to confuse me.

Things like interrupt enable, disable comparators, disable adcon, etc etc.
 

Thread Starter

Tomahawk640

Joined Feb 20, 2009
20
OK, right now I'm just trying to frankenstein this thing together, I'll worry about organization later. Any thoughts as to how to get the input requirement to work?
 

Thread Starter

Tomahawk640

Joined Feb 20, 2009
20
Here's something interesting...if I comment out the Start loop, the motor will not run. Something before the loop is messing it up.
 

n9352527

Joined Oct 14, 2005
1,198
That's strange. I can't see any code that's affected or affect that start loop.

The only thing that I can suggest is to start a new project, and reorganise those codes and copy them part by part to the new project, and testing those parts thoroughly.
 

Thread Starter

Tomahawk640

Joined Feb 20, 2009
20
Well, I was able to get one of my professors to lend me a hand by digging up a program that was similar to mine but controlled LEDs. I copied and pasted the beginning and it worked! I'll post it for future reference.

start BCF STATUS,RP0 ; Select Bank 0
;
CLRF PORTA ; Initialize PORTA (to all zeros)
;
MOVLW 7 ; Load w with 7
MOVWF CMCON0 ; Load CMCON0 with 7
; Turns off comparators
;
BSF STATUS,RP0 ; Select Bank 1
;
CLRF ANSEL ; Shut off ADC (digital I/O)
;
MOVLW b'001111' ; Load w – RA4 and RA5 outputs
MOVWF TRISA ; copy w to TRIS PORTA
;
BCF STATUS,RP0 ; Select Bank 0

Loop BTFSC PORTA,3 ; bit 3 low?
GOTO Loop ; No



Thanks for all your help guys!
 

Thread Starter

Tomahawk640

Joined Feb 20, 2009
20
Got a new problem with the same project. When powered down, the motors move freely; they are not fixed at all. As you could see in my program, the number of steps the motor takes is predetermined. When the motor is powered it is locked into place wherever it was before it was powered. There is no "home" position. However, I need one. I have absolutely no idea how to pull this off. I'm hoping you guys might. If the motor starts turning in the wrong starting position it can damage the project by forcing what the motor is carrying beyond where it needs to stop.
 

Thread Starter

Tomahawk640

Joined Feb 20, 2009
20
I think I found a way around, but found out I never really fixed the problem of setting the correct pins in PORTA to be inputs, it only worked before cause I used RA3 which cannot be an output. I've tried using the given program in the datasheet without much luck. Has anyone successfully done this before? The project is due May 13th. Any more help you guys can provide would be appreciated.
 
Top