View previous topic :: View next topic |
Author |
Message |
Guest
|
Declaring and sensing the state of input pin |
Posted: Wed Feb 20, 2008 11:59 am |
|
|
I am very confused why my pic isnt detecting the state of my pins,here is the code:
/////////////////////////////////////////////////////////////
void main()
{
char rf_id[11];
int i;
int rs_state;
input(pin_b0);
rs_state = input_state(pin_b0);
do
{
if(rs_state)
output_high(pin_a1);
else
output_low(pin_a1);
}while(1);
}
////////////////////////////////////////////////////////////////////////////////
What i inteded here was if portB pin 0 is 1 then portA pin 1 will go high or
else it stays output low. |
|
|
ECACE
Joined: 24 Jul 2006 Posts: 94
|
|
Posted: Wed Feb 20, 2008 12:19 pm |
|
|
Need more information. How did you setup the port? Did you use fastio? Also, please include your compiler version.
Your line: input(pin_b0); does nothing. _________________ A HW Engineer 'trying' to do SW !!! Run!!! |
|
|
Guest
|
|
Posted: Wed Feb 20, 2008 12:25 pm |
|
|
I didnt setup anything like the one you said the fast io,do we need to setup it?
I used input_state() right away. my compiler version is v4 |
|
|
ECACE
Joined: 24 Jul 2006 Posts: 94
|
|
Posted: Wed Feb 20, 2008 12:52 pm |
|
|
Not sure what happens if you don't define it. I typically use fast_io, then set_tris_X as my I/o's don't change. Once it is in input, it is always an input, same for outputs. Take a look at the code below, mod it to your processor and the like and see if it works. If you're using the IDE, try using the project wizard and see what it produces. I used it for this one, so some of the things don't really apply, but shouldn't hurt.
Code: | #include <16F877A.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#use delay(clock=20000000)
#use standard_IO(A)
#use standard_IO(B)
#define OUTA1 PIN_A1 //Output of A1
#define INB0 PIN_B0 //Input of B0
void main()
{
char rf_id[11];
int i, rs_state;
while(1)
{
rs_state = input_state(INB0);
if(rs_state)
output_high(OUTA1);
else
output_low(OUTA1);
}
}
void setup_PIC()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
} |
If I were using the fast_io, I would have something like:
Code: | #use fast_io(A);
#use fast_IO(B);
#use fast_IO(C);
..
..
..
..
..
<inside setup_PIC>
set_tris_a(0b11011100); //7,6,4,3,2=Inputs 5,1,0 = Outputs
set_tris_b(0b01010000); //6,4=Inputs 7,5,3,2,1,0 = Outputs
set_tris_c(0b01000000); //6=Input 7,5,4,3,2,1,0 = Outputs |
_________________ A HW Engineer 'trying' to do SW !!! Run!!! |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Feb 20, 2008 1:28 pm |
|
|
The compiler's default is "standard I/O" so not declaring fixed or fast I/O is not the problem. You have these lines: Code: |
input(pin_b0);
rs_state = input_state(pin_b0); |
Input(pin_b0) should return the state of b0, but you don't do anything with it.
Input_state() is not a standard CCS function or a C function. Where is it defined and what do you expect it to do?
Why not use: Code: |
rs_state = input(pin_b0);
|
If you put this inside your do-while loop it will continue to check each time through the loop. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
ECACE
Joined: 24 Jul 2006 Posts: 94
|
|
Posted: Wed Feb 20, 2008 1:33 pm |
|
|
Page 162 of the CCS manual:
INPUT_STATE( )
Syntax:
value = input_state(pin)
Parameters:
pin to read. Pins are defined in the devices .h file. The actual value is a bit address. For example, port a (byte 5) bit 3 would have a value of 5*8+3 or 43. This is defined as follows: #define PIN_A3 43.
Returns:
Bit specifying whether pin is high or low. A 1 indicates the pin is high and a 0 indicates it is low.
Function:
This function reads the level of a pin without changing the direction of the pin as INPUT() does.
Availability:
All devices.
Requires:
Nothing
level = input_state(pin_A3);
Examples:
printf("level: %d",level);
Example Files:
None
Also See:
input(), set_tris_x(), output_low(), output_high() _________________ A HW Engineer 'trying' to do SW !!! Run!!! |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Feb 20, 2008 3:18 pm |
|
|
Well you learn something new every day. I never ran into that function. It is not in the older (3.242) compiler I am using today.
So my best guess why his code doesn't work is that his input test should be inside the do-while loop. We will have to wait for him to give us more info. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
ECACE
Joined: 24 Jul 2006 Posts: 94
|
|
Posted: Wed Feb 20, 2008 3:56 pm |
|
|
That's my guess too. As his code is now, it will do the test one time on power up, then hang in the while(1). Like you said, need more info. _________________ A HW Engineer 'trying' to do SW !!! Run!!! |
|
|
crukid88
Joined: 04 Oct 2007 Posts: 14
|
|
Posted: Fri Feb 22, 2008 12:40 am |
|
|
I figured it out,you guys were right it has something to do with the do-while loop..tnx |
|
|
|