View previous topic :: View next topic |
Author |
Message |
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
Port A as an Input not working |
Posted: Sun Apr 03, 2011 10:02 pm |
|
|
Hi,
I'm trying to get an input on port A and once there is an input i'm switching on the led...but some how it is not working on the hardware, but working on Proteus. The code is as follows:
Code: |
#include <16f1936.h>
#device ICD=TRUE
#fuses HS,NOLVP
#use delay (clock=40000000)
LED_ON()
{
output_high(PIN_B5);
output_high(PIN_B6);
output_high(PIN_B7);
delay_ms(1000);
output_low(Pin_B5);
output_low(Pin_B6);
output_low(Pin_B7);
delay_ms(1000);
}
void main()
{
while(1)
{
if(input(pin_A1)==1)
{
while(1)
{
LED_ON();
if(input(pin_A1)==0)
{
output_low(Pin_B7);
output_low(Pin_B5);
output_low(Pin_B6);
break;
}
}
}
}
}
|
It works on Pin B1 but not on A1.
I tried to disable the analog input, but i don't know if I was right.
please help.
Thank you,
Sid |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sun Apr 03, 2011 10:33 pm |
|
|
1: Proteus has been shown to be generally unreliable....
However, for you purposes, you might be ok. Always hold proteus in doubt.
Next, keep in mind, PortB has weak pullups that portA typically does not.
Is the thing controlling the state to your PortA pin totem pole? (can drive high and low) or does it just hold low?
If it does not provide a high-low state, you'll need a pull-up resistor on the pin you're using. 4.7K is a good start to test with... from there, adjust to your needs.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Sun Apr 03, 2011 10:43 pm |
|
|
thank bkamen
I did try to pull up the pin using 10K resistor, but it still did not work.
should I try using the 4.7K or is there any other problem.
Regards,
Sid |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 03, 2011 10:54 pm |
|
|
Quote: | #use delay (clock=40000000) |
The 16F1936 data sheet says the highest frequency of operation is
32 MHz in EC oscillator mode. The highest freq for HS mode is 20 MHz:
Quote: |
TABLE 29-1: CLOCK OSCILLATOR TIMING REQUIREMENTS
|
http://ww1.microchip.com/downloads/en/DeviceDoc/41364D.pdf
What is your compiler version ?
Post a description of the external circuit connected to pin A1. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sun Apr 03, 2011 10:57 pm |
|
|
Sid2286 wrote: | thank bkamen
I did try to pull up the pin using 10K resistor, but it still did not work.
should I try using the 4.7K or is there any other problem.
|
Well what is driving the PIC's input pin?
Not a user of proteus, I don't know if it matters, but you can't run that part at 40MHz. 32MHz is the max speed according to the datasheet.
I'm surprised the compiler doesn't complain.
Next, do you need the ICD=TRUE? (Does proteus require this? if you're simulating, you shouldn't. But again, I don't use proteus, so I don't know).
If you're debugging that's fine - can you confirm your program runs at all? (how about blinking the LED at startup to see if it does anything.)
Lastly, there is an "analog/digital" setting for PORTA pints (see datasheet and ANSELA) while the function input_pin()
The compiler probably sets that for you, but it's always good to double check.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sun Apr 03, 2011 11:13 pm |
|
|
PCM programmer wrote: | Quote: | #use delay (clock=40000000) |
The 16F1936 data sheet says the highest frequency of operation is
32 MHz in EC oscillator mode. The highest freq for HS mode is 20 MHz:
Quote: |
TABLE 29-1: CLOCK OSCILLATOR TIMING REQUIREMENTS
|
http://ww1.microchip.com/downloads/en/DeviceDoc/41364D.pdf
What is your compiler version ?
Post a description of the external circuit connected to pin A1. |
Although HS and a clock of 32M works in MPLAB's SIM...
I would drop back (for testing) to HS and a clock of 8MHz.
BTW, If I set this up and stimulate this in MPSIM using PCM 4.119, the flow works as written.
Also, if you want to use a 32MHz clock via external oscillator, the HS fused should be changed to ECL.
Cheers,
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sun Apr 03, 2011 11:17 pm |
|
|
Oh,
My working code looks like this:
Code: | #include <16f1936.h>
#device ICD=TRUE
#fuses HS,PLL,NOLVP
#use delay (clock=32M)
void LED_ON(void) {
output_high(PIN_B5);
output_high(PIN_B6);
output_high(PIN_B7);
delay_ms(1000);
output_low(Pin_B5);
output_low(Pin_B6);
output_low(Pin_B7);
delay_ms(1000);
}
void main() {
while(1) {
if(input(pin_A1)==1) {
while(1) {
LED_ON();
if(input(pin_A1)==0) {
output_low(Pin_B7);
output_low(Pin_B5);
output_low(Pin_B6);
break;
}
}
}
}
}
|
_________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Mon Apr 04, 2011 12:00 am |
|
|
Thanks PCM and bkamen
My hardware has nothing complicated.
I have used a comparator LM339 and the output is given to the Pin A1 of PIC16F139. Compiler version is 4.
Once the output of comparator goes high the LED should blink. moreover the output of the comparator has a pull up resistor of 3.3K(as per LM339 datasheet).
also as per bkamen, though port B has a weak internal pull ups, I never pulled them up using a program, but still it was working with pin_B1.
sid |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Mon Apr 04, 2011 5:09 am |
|
|
First, I assume a VDD of 5 volts.
LM339 must have pullup and 3k3 seems fine.Easy to check with DVM
Check the datasheet for other peripherals that use PortA and disable them.ADC typically is the defaulted use of I/O pins.
If you used MPLAB to compile code and burn PIC , did you set build configuation to 'release' from 'debug' AND recompile ? |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Mon Apr 04, 2011 5:58 am |
|
|
Yes I did that.. While using MPLAB! If i use B1 instead of A1 it works fine.. I don't know what else I need to add in the above code! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Mon Apr 04, 2011 6:18 am |
|
|
I just downloaded the datasheet for the 1936, seems Port A.1 is also used for analog,comparator and LCD uses !
Sounds like you haven't disabled those, to use that pin exclusively for digital I/O.
I'd check the listing to see that the proper bits in the correct registers are being set.If not it might be a compiler bug,in which case you may have to manually do the correct setup.The header file might be wrong,I don't have it or the PIC so I can't help further.
The fact the software works on PortB on real hardware seems to indicate a 'setup' or bit setting problem,the listig will show ! |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Mon Apr 04, 2011 6:28 am |
|
|
Thanks temtronics!
can u help me on how to disable the rest of the multiplexed applications except I/O. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Apr 04, 2011 6:37 am |
|
|
temtronic wrote: | I just downloaded the datasheet for the 1936, seems Port A.1 is also used for analog,comparator and LCD uses !
Sounds like you haven't disabled those, to use that pin exclusively for digital I/O.
I'd check the listing to see that the proper bits in the correct registers are being set.If not it might be a compiler bug,in which case you may have to manually do the correct setup.The header file might be wrong,I don't have it or the PIC so I can't help further.
The fact the software works on PortB on real hardware seems to indicate a 'setup' or bit setting problem,the listig will show ! |
I bet it's a proteus problem. I simulated it last night with MPSIM and it worked as expected.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Mon Apr 04, 2011 8:06 am |
|
|
The OP says it worked in hardware with PORTB, so I hope he's NOT using still in problematic Proteus !
If so, well, he's out of luck, I do NOT use ANY simulators.NONE simulate the real world,at least none of them that I've seen in the past 30+ years.
The OP should post his complete program and the setup error will probably show up real fast. |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Mon Apr 04, 2011 8:13 am |
|
|
Bkamen you are getting me all wrong! I wrote the program with A1 and simulated with proteus it worked and so I tried it on hardware where it did not work.
Now since it did not work I replaced A1 with B1 and it worked on hardware.. ..Strange! I guess there is some program issue. |
|
|
|