View previous topic :: View next topic |
Author |
Message |
Guest
|
Port status |
Posted: Mon Jul 24, 2006 7:07 am |
|
|
Hello everyone !
I had a look on the forum before asking but I've got to many replies and they were not dealing with my problem.
Actually, it's quite simple : I would like to know the state of each PIN of one Port. Let say I'm using pin B0 to B3 of PortB. Each pin is connected to a button. When you press the button, the pin status changes from high to low. I would like to know the state (high or low) of the 4 pins I've mentioned, without using interrupts, approximately like this :
Code: |
void main()
{
//port_b_pullups(TRUE);
setup_adc_ports(NO_ANALOGS|VSS_VDD); //Entr�es/Sorties en digital
setup_adc(ADC_OFF|ADC_TAD_MUL_0); //On n'utilise pas l'ADC
setup_spi(FALSE); //Pas de communication SPI
setup_wdt(WDT_OFF); //Pas de Watchdog timer
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,255,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_low_volt_detect(FALSE);
setup_oscillator(FALSE);
//Configuration des ports du microcontr�leur
SET_TRIS_A(0x80); //Configuration du port A : A7 en entr�e, le reste en sortie
SET_TRIS_B(0x0F); //Configuration du port B = �cran LCD et LEDS Tout en sortie
SET_TRIS_C(0x80); //Configuration du port C : C7 en entr�e, le reste en sortie
while(TRUE) //Infinite loop
{
//TEST
//Something like :
// if(PIN_B0==1) { ...}
// or
// if(output_bit(PIN_B0,1)==TRUE) { ]
// but none of this work.
}
}
|
I don't see why we could face difficulties knowing the pins' state.
Have a nice day all. |
|
|
birumher
Joined: 28 Apr 2004 Posts: 10
|
|
Posted: Mon Jul 24, 2006 7:33 am |
|
|
try
if (INPUT(PIN_B0) == 0)
{
.....
....
} |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Mon Jul 24, 2006 7:48 am |
|
|
Per the November 2005 PDF manual (your page numbers may be different, consult the index of your copy):
INPUT() page 119
INPUT_x() page 121
BIT_TEST() page 92 (with some "gymnastics")
#byte and #bit pages 34 and 35
Several example programs are cited on these pages for your reference. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
|
Posted: Mon Jul 24, 2006 7:56 am |
|
|
I have gotten used to not using CCS's functions for reading port pins' status.
Code: |
#byte port_b=0xf81 // on a picf18f458, 0xF81 is the address of PORT B
...
unsigned char x;
x = port_b; // this reads port_b
|
In assembly, one would read port b as:
movf PORTB,w
movwf TEMPX
If you stop relying on a compiler's functions, and instead try to understand the assembly, then writing it in C would be a no-brainer. |
|
|
AcquaVx
Joined: 02 Jun 2006 Posts: 6
|
|
Posted: Mon Jul 24, 2006 8:11 am |
|
|
Thank you for the advice. I've used birumher's method and it's working fine.
You're right MikeValencia, I'll try to keep it in mind.
A great afternoon. |
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
|
Posted: Mon Jul 24, 2006 9:56 am |
|
|
By the way, here is how you would code it using the #byte method:
Code: |
void main(void)
{
...
while (1)
{
if (PORT_B & 0x01) // Is PortB Bit 0 equal to '1'? 0000 0001
{
// do what you want here
}
}
}
|
If you want to test if it is equal to '0':
Code: |
if ( !(PORT_B & 0x01)) // Is PORTB Bit 0 equal to '0'? 0000 0010
|
Or if you want to test if PORTB Bit 1 is equal to '1'
Code: |
if (PORT_B & 0x02) // Is PORTB BIT 1 equal to '1'? 0000 0010
|
|
|
|
|