View previous topic :: View next topic |
Author |
Message |
Guest
|
input() function problem??? |
Posted: Mon Dec 13, 2004 7:19 am |
|
|
Hi,
I'm using a PIC16LF873A with CCS and I have to test an input level state in order to command a Regulator.
Here is a simple test for shutdown the regul :
while(input(ONOFF)==1);
output_low(REGUL);
The problem is that the ONOFF button is always high and the program goes out of the "while" and put REGUL down. Even if we have an high state level in ONOFF input.
ONOFF is RA4
REGUL is RA5
There is a pullup resistor on RA4.
here is the header :
#include <16F873A.h>
#device adc=8
#use delay(clock=7372800) //7372800
#fuses HS, NOWDT, NOPROTECT, NOLVP, NOBROWNOUT, PUT
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use fast_io(A)
#use standard_io(B)
#use standard_io(C)
#define LED0 PIN_A0
#define LED1 PIN_A1
#define ONOFF PIN_A4
#define REGUL PIN_A5
#define DATADS PIN_C4
And the main program :
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
output_low(LED1);
set_tris_a(0x10);
output_high(REGUL);
output_high(LED0);
delay_ms(2000);
while(input(ONOFF)==1);
output_low(REGUL);
If you have experienced that problem before... Thanks by advance for your help. |
|
|
YulL
Joined: 29 Sep 2004 Posts: 39 Location: Paris (France)
|
|
Posted: Mon Dec 13, 2004 7:21 am |
|
|
Sorry I have forgot to log on the forum
it's me! |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
Re: input() function problem??? |
Posted: Mon Dec 13, 2004 8:25 am |
|
|
Anonymous wrote: | while(input(ONOFF)==1);
output_low(REGUL);
If you have experienced that problem before... Thanks by advance for your help. |
Loose the ";" after the "while" statement. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
YulL
Joined: 29 Sep 2004 Posts: 39 Location: Paris (France)
|
|
Posted: Mon Dec 13, 2004 8:32 am |
|
|
the problem still remains... |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
Re: input() function problem??? |
Posted: Mon Dec 13, 2004 8:44 am |
|
|
rwyoung wrote: | Anonymous wrote: | while(input(ONOFF)==1);
output_low(REGUL);
If you have experienced that problem before... Thanks by advance for your help. |
Loose the ";" after the "while" statement. |
Do you want to stay in the while loop until ONOFF goes to 0, then drop REGUL? Is so the ";" stays where it is.
I would look closely at the assembly code for these two lines. It should be maybe ten lines of assy, not too hard to understand. If there are no clues there try using standard I/O and see it the behavior changes. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Dec 13, 2004 9:18 am |
|
|
Maybe you are getting noise on the line causing the problem. Debounce the input and see how it goes. |
|
|
YulL
Joined: 29 Sep 2004 Posts: 39 Location: Paris (France)
|
|
Posted: Mon Dec 13, 2004 9:24 am |
|
|
There is absolutely no noise and the button is not pressed at all!! |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Dec 13, 2004 10:44 am |
|
|
Try: Code: | While (1);
output_low(REGUL); |
If that fails then you have some interrupt or serious EMI problem.
Can you give us the part of the list file for the while and output_low lines? What version of the compiler are you using? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
YulL
Joined: 29 Sep 2004 Posts: 39 Location: Paris (France)
|
|
Posted: Mon Dec 13, 2004 11:15 am |
|
|
Thanks a lot for the reply :
.................... while (1);
0059: GOTO 059
.................... output_low(LED1);
005A: BSF 03.5
005B: BCF 05.1
005C: BCF 03.5
005D: BCF 05.1
.................... }
....................
005E: SLEEP
and
.................... while (input(ONOFF));
0059: BSF 03.5
005A: BSF 05.4
005B: BCF 03.5
005C: BTFSC 05.4
005D: GOTO 059
.................... output_low(LED1);
005E: BSF 03.5
005F: BCF 05.1
0060: BCF 03.5
0061: BCF 05.1
.................... }
....................
0062: SLEEP
Hope it helps |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Mon Dec 13, 2004 11:44 am |
|
|
Try this code. It should toggle the output when the input toggles.
Code: |
main()
{
while(1)
{
if(input(ONOFF))
{
output_low(REGUL);
}
else
{
output_high(REGUL);
}
}
}
|
Ronald |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Dec 13, 2004 12:17 pm |
|
|
What version of the compiler are you using? Here is what I got
Code: | .................... while(input(ONOFF)==1);
005B: MOVLW 00
005C: BTFSC 05.4
005D: MOVLW 01
005E: SUBLW 01
005F: BTFSC 03.2
0060: GOTO 05B
.................... output_low(REGUL);
0061: BCF 05.5
|
You have fast_io for port A but the compiler is still setting the tris!
I would do it like this
Code: | .................... while(input(ONOFF));
005B: BTFSC 05.4
005C: GOTO 05B
.................... output_low(REGUL);
005D: BCF 05.5
.................... } |
Regardless, your program should be working unless you are leaving something out like if your are using interrupts. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Dec 13, 2004 12:19 pm |
|
|
.................... while (input(ONOFF));
0059: BSF 03.5
005A: BSF 05.4
005B: BCF 03.5
005C: BTFSC 05.4
005D: GOTO 059
That looks odd to me. If 05.4 is the ONOFF pin, why is it being SET by the input command? And if it is set, why is the code ever skipping at 005C?
There appears to be wierdness here!
Does anyone else have any ideas? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah Guest
|
|
Posted: Mon Dec 13, 2004 12:27 pm |
|
|
SherpaDoug wrote: | .................... while (input(ONOFF));
0059: BSF 03.5
005A: BSF 05.4
005B: BCF 03.5
005C: BTFSC 05.4
005D: GOTO 059
That looks odd to me. If 05.4 is the ONOFF pin, why is it being SET by the input command? And if it is set, why is the code ever skipping at 005C?
There appears to be wierdness here!
Does anyone else have any ideas? |
This code is fine.
The code is switching banks. Setting the _TRIS_ bit to '1', to make the pin an input. Then switching to the bank to read the pin, and correctly testing it.
There is nothing wrong with the code as posted. The pin _must_ be being seen as going low.
Remember that pin A4, has a Schmidt input. It requires to be above 0.8Vdd, to see a 'high'. A small amount of noise, or a capacitive input that is taking some time to charge 'high' (because you are not using 'fast_io' or 'fixed_io', the pin is set as an output, till only a couple of machine cycles before it is read, and this could be pulling the pin low...).
Try selecting 'fixed_io' and defining the pins you want to be used as outputs on this port, or using 'fast_io', and setting the TRIS register yourself.
Best Wishes |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Mon Dec 13, 2004 1:03 pm |
|
|
We doesn´t know what compiler version is using YulL
Quote: |
This code is fine.
The code is switching banks. Setting the _TRIS_ bit to '1', to make the pin an input. Then switching to the bank to read the pin, and correctly testing it.
|
Yes but.. he defines:
#use fast_io(A) and then:
set_tris_a(0x10);
Why is setting the TRIS_BIT in the main if it was done before ?
Quote: |
.................... while (input(ONOFF));
0059: BSF 03.5
005A: BSF 05.4 // !!!! ???? Why here ?
005B: BCF 03.5
005C: BTFSC 05.4
005D: GOTO 059
|
I agree with rnielsen proposal -just to test- his code is very clean and generate the simplest available code.
Code: |
.................... while(1)
.................... {
.................... if(!input(ONOFF))
*
0019: BTFSC 05.4
001A: GOTO 01D
.................... {
.................... output_low(REGUL);
001B: BCF 05.5
.................... }
.................... else
001C: GOTO 01E
.................... {
.................... output_high(REGUL);
001D: BSF 05.5
.................... }
.................... }
001E: GOTO 019
.................... }
....................
|
Humberto[/quote] |
|
|
YulL
Joined: 29 Sep 2004 Posts: 39 Location: Paris (France)
|
|
Posted: Tue Dec 14, 2004 2:26 am |
|
|
Thanks for all the reply. The version of CCS that I am using is PCM 3.150.
Well when I replace the REGUL output by a LED output the problem still remains.
The code :
.................... while(1)
.................... {
.................... if(!input(ONOFF))
0059: BSF 03.5
005A: BSF 05.4
005B: BCF 03.5
005C: BTFSC 05.4
005D: GOTO 063
.................... {
.................... output_low(REGUL);
005E: BSF 03.5
005F: BCF 05.5
0060: BCF 03.5
0061: BCF 05.5
.................... }
.................... else
0062: GOTO 067
.................... {
.................... output_high(REGUL);
0063: BSF 03.5
0064: BCF 05.5
0065: BCF 03.5
0066: BSF 05.5
.................... }
.................... }
works very well, and I have no problem.
In fact, I have to read a low state on ONOFF (when I push the button, it- drives the input low) and then put REGUL to a low state in ordor to power down the unit. When I release the button, the regulator put the power supply down, so the PIC shuts down.
It's strange that even if I'm doing nothing, sometimes it detects a low level state. I'm going to change the port input (RA2 or some sorts) in order to see if it is better or not.
|
|
|
|