|
|
View previous topic :: View next topic |
Author |
Message |
nukeman
Joined: 04 Jan 2010 Posts: 3
|
PIC16F684 - problems using pins as inputs |
Posted: Sat Jan 16, 2010 12:38 pm |
|
|
First off, I'm completely new to PIC microcontrollers as of a couple weeks ago and I've started out with PIC16F684. My current goal is to get my chip to take input from some pushbuttons and display some reasonable change in output to some LEDs. If I get that working, I'll be attempting to drive a KTM-S1201 display and use an HS1101 humidity sensor, both of which are also currently wired up to the PIC. But these goals seem to be a little more difficult, so I'll start with the simpler stuff.
My PIC gets stable Vdd at 3.3V. I've connected some buttons to pins A3, A4, and A5, wired up so that the low is 0.3V and high is 3.0V, which I think should work. They're currently wired so that pressing a button makes the input go low. I've also connected some LEDs to A0 and C5, which I have succeeded in turning on and off as I like, but only with pure code and not by involving any inputs.
Here is some sample code, which I think should turn C5 high if A5 is high. Otherwise, C5 should just blink.
Code: | #INCLUDE <16F684.h>
#USE DELAY (CLOCK=4000000)
#FUSES XT,NOWDT,NOPROTECT,PUT
void main() {
while (TRUE)
{
if(input(PIN_A5))
{
output_high(PIN_C5);
}
else
{
output_high(PIN_C5);
delay_ms(100);
output_low(PIN_C5);
delay_ms(100);
}
delay_ms(50);
}
} |
Running this code as it is, the LED will blink all day if I leave it running, regardless of the current state of A5. So pushing the button connected to A5 does absolutely nothing. It can be noted that with the circuit wired the way it is, LED should not blink unless the button is pressed, but it blinks from the beginning.
I've tried many variations of this theme, some of them examples taken from online that use interrupts, but most without any use of interrupts. The reason I've added the blinking in this example I'm sharing is because of instances of peculiar behavior I get from the circuit, which are obvious when the blinking is written into the code.
The first is that if I use this exact code and press the button connected to A3 (which it seems should do nothing at all with this code), the flashing of the LED will stop until I stop pressing the button. I believe this indicates that a low on pin A3 is stopping the clock of the circuit or causing a continuous interrupt while the button is pressed, but I have no idea why it would do either. I realize that this pin is used in programming the PIC, but it would seem that it should be possible to use it as a standard I/O pin like any other pin of port A. Perhaps I need to do something in order to use it as a plain old I/O pin?
The second is that if I change "input(PIN_A5)" to "!input(PIN_A5)", the light will never blink, regardless of whether I'm pressing any button. This would seem to indicate that the PIC is only initially checking the state of A5 and ignoring any changes afterward, but I've tried powering up the circuit with the original example while pressing the button and in this case the light will blink. I have to assume that there's an internal assumption about the state of A5 and input on the pin simply isn't ever having any impact on the operation of the PIC.
I currently have two PIC16F684 chips on-hand. I've tried both with variations of the above code and they both do the exact same things.
What I think is that I'm probably missing some line of code that I specifically need in order to to use for the PIC16F684 in this manner. Perhaps it's related to fuses or perhaps I need to configure port A in some way before using the pins as inputs. Whatever it is, I haven't been able to find the information I need in searching around for it online and I didn't see anything in the documentation for the chip. What I'm doing seems to me to be along the lines of what's usually done to get the desired behavior from other models of PIC chip, so where this is going wrong is a mystery to me for now. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jan 16, 2010 1:02 pm |
|
|
Quote: |
#INCLUDE <16F684.h>
#USE DELAY (CLOCK=4000000)
#FUSES XT,NOWDT,NOPROTECT,PUT
void main() {
while (TRUE)
{
if(input(PIN_A5))
|
Pin A5 is the OSC1 pin. It connects to one of the external crystal's pins.
You are using XT mode, which implies you're using a crystal. You can't
use pin A5 for a crystal and as an input pin at the same time.
I suggest that you disconnect the crystal circuit (if you have one) and
use the internal oscillator instead. Change the XT fuse to this: INTRC_IO
If you still have problems, then post your compiler version. |
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
Re: PIC16F684 - problems using pins as inputs |
Posted: Sat Jan 16, 2010 3:14 pm |
|
|
nukeman wrote: | I've connected some buttons to pins A3, A4, and A5, wired up so that the low is 0.3V and high is 3.0V, which I think should work. They're currently wired so that pressing a button makes the input go low.
Code: |
if(input(PIN_A5))
{
output_high(PIN_C5);
}
} |
|
Consider what has PCM programmer wrote and change you input statement to :
Notice the ! in front of input |
|
|
nukeman
Joined: 04 Jan 2010 Posts: 3
|
|
Posted: Sat Jan 16, 2010 5:18 pm |
|
|
PCM programmer, thank you, that was most of my problem. Though, I'm not sure why the chip was working the way it was. It was somehow still working at 4 MHz so I thought I had the clock set up right. It was just refusing to work with any external inputs. The other issue, the one with A3, was apparently caused by my programmer setting MCLR, which I've now corrected.
bungee-, I think I addressed what you were saying in what I said. I was just trying to get the program to do something reasonable based on input from the buttons, which it would not before. I don't particularly care at this stage if pressing the button makes the lights blink or stop blinking, so long as I know it's for a good reason. Thank you though.
Now I guess it's time to play with interrupts again. Maybe they'll work now that that I'm not trying to use XT. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|