Trouble with inputs 16f628A

Status
Not open for further replies.

KMD

New Member
Well, I got a programmer and a few 16f628a's and I've been playing around with them for a couple days now. I've been making LED's blink, small motors move, and made a small piezo scream which scared the cat. All in all, with the exception of a few hours of frustration trying to get a program downloaded to the pic, it's been a lot of fun.

But now I have run into a snag, I'm trying to get my pic to react to input and it's just not working. I'm not sure if it's a programming problem or a circuit problem.

Here's the code. All I'm trying to do is get a motor to start spinning as soon as power is applied and then stop when a button is pressed.
Code:
main
     bsf	     STATUS,RP0
     movlw	     b'010' 
     movwf	     TRISA ;this should make pin 18 an input, correct?
     bcf	     STATUS,RP0

LOOP
     movlw	     b'101' 
     movwf	     PORTA ;this seems to work, pin 17 and 1 become outputs
     btfss	     PORTA,1 ;should send the program to hold when pin 18 gets voltage
     goto	     LOOP
     goto 	     HOLD

HOLD
     movlw	     b'000' 
     movwf	     PORTA ;turns off pins 17 and 1
     goto	     HOLD ;should just keep it looping forever, motor should stay off

If it looks like this should work, why can't I get the motor to shut down? All I should need to do is send a few volts to pin 18?

Thanks for any help.
 
If you're using binary numbers I would advise you to enter all eight bits, otherwise it's VERY confusing - I'm not sure how the assembler treats them?.

Try checking my PIC tutorials, I have key routines there - plus the required hardware circuits - you don't mention how you have the switches connected?, you should use either pull up, or pull down, resistors. My tutorials use pull up resistors, which is the most common method.
 
KMD,

I guess it's a good thing you're running into this now so that you learn about it. PORTB is a generic I/O port that most people use first. PORTA (the one you're trying to use) shares operation with analog comparators. So to use PORTA as digital I/O you have to turn off the comparators.

Check out pages 53 and 54 of the datasheet. You have to set the CMCON register to 0x07.

And go through Nigel's tutorials. You will learn a lot.

Mike
 
Please read The Sticky: Newcommers, please read! (PIC regarded) Upd. 4
 
Thanks guys, when I run into a problem like this, the most important thing to me is understanding why my approach didn't work. Based on what guys have told me, I think I get that now.
 
input/outputs

hi again





main
bsf STATUS,RP0
movlw b'010'
movwf TRISA ;this should make pin 18 an input, correct?
bcf STATUS,RP0

LOOP
movlw b'101'
movwf PORTA ;this seems to work, pin 17 and 1 become outputs

these line you are telling the pic 3 things to make porta,0 high,porta,1 low porta,2 high.
but you have setup porta,1 as a input so nothing will happen to this pin.

also you should have a pulldown resistor (10k or so)on porta,1 and your switch to pos supply.

you can give your input a name of your own.
i mainly use the standered inputs are sw1/sw2/sw3 ex.outputs are LD1/LD2/LD3 ex.
PUT THESE IN YOU INPUT/OUTPUT VERIABLES LIKE SO.

SW1 EQU H'0' ;SW1 IS CONNECTED TO PORTA,0
SW2 EQU H'1' ;SW2 IS CONNECTED TO PORTA,1

YOURS WOULD BE

SW1 EQU H'1' SW1 IS CONNECTED TO PORTA,1



hope thats helped


btfss PORTA,1 ;should send the program to hold when pin 18 gets voltage
goto LOOP
goto HOLD

HOLD
movlw b'000'
movwf PORTA ;turns off pins 17 and 1
goto HOLD ;should just keep it looping forever, motor should stay off
 
Thanks, I'm quickly becoming familiar with the pull down and pull up resistor concept. All of this micro controller stuff is so new, every day It seems like I obtain a whole new level of understanding, I'm really having a lot of fun with this.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…