View previous topic :: View next topic |
Author |
Message |
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
[SOLVED] Query all states of I/O pins... |
Posted: Fri Dec 11, 2015 5:02 pm |
|
|
Hi,
Is a better way to know pins status than input_state(PIN_A1)...input_state(PIN_A2)...etc
I want to make a small debug code that write all the current states of I/O pins on UART.
thanks! _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
Last edited by ELCouz on Sat Dec 12, 2015 3:31 pm; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9252 Location: Greensville,Ontario
|
|
Posted: Fri Dec 11, 2015 7:53 pm |
|
|
My first try would be to dump a listing of a small program to see how CCS does it per pin then decide if you could go 'port by port' with a similar strategy.
Currently trying how to repair a busted 40' truss and put the rockshaft onto my #1 tractor at the same time....so PIC time is very very little...
sigh |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 11, 2015 8:01 pm |
|
|
CCS doesn't have an input_state_x() function. You can make one for
each port as shown below. Or you can skip the function and just read
directly from each Portx register.
Code: |
// This function reads Port B without changing the TRIS.
int8 input_state_b(void)
{
#byte PortB = getenv("SFR:PORTB")
return(PortB);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19559
|
|
Posted: Sat Dec 12, 2015 2:22 am |
|
|
Add (of course), that if you have fast_io enabled, then the CCS input_b function, will give the input state of the whole port..... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9252 Location: Greensville,Ontario
|
|
Posted: Sat Dec 12, 2015 5:50 am |
|
|
hmm my mind is elsewhere..... I was thinking he needed to know which pins were set for inputs vs outputs.
Jay |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Sat Dec 12, 2015 8:22 am |
|
|
Thanks guys!
What about partial query of a port?
Let's say I have some defines like this:
Code: |
#define LED1 PIN_A0
#define LED2 PIN_A6
#define LED3 PIN_B2
#define LED4 PIN_C3
//and I want to query the status of those pins.
for (i=1;i<=4;++i)
{
printf("%u\r\n",input_state(LEDS+i));
}
// how its possible to cycle through LEDS1 to LEDS4 defines?
|
I tried google-ing "increment define loop" or "increment variable name" of course it only showed how to i++ a variable value on CCS.
Thanks again guys! _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9252 Location: Greensville,Ontario
|
|
Posted: Sat Dec 12, 2015 11:07 am |
|
|
you're making it far to complicated,go back to KISS principle....
just create a function that contains...
//
printf("%u\r\n",input_state(LED1));
printf("%u\r\n",input_state(LED2));
printf("%u\r\n",input_state(LED3));
printf("%u\r\n",input_state(LED4));
//
.....
far simpler to implement,
easier to understand,
quick to edit to add a pin or two...
probably FASTER than any 'higher level' coding as well....
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19559
|
|
Posted: Sat Dec 12, 2015 11:23 am |
|
|
and also worth understanding that the 'single pin' version is solved at compile time, and will only use one or two instructions. One involving using a variable, will have to split the variable up into port and pin number, then do indexed I/O to access the port, then use a variable to access the pin. Literally dozens of instructions..... |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Sat Dec 12, 2015 12:02 pm |
|
|
temtronic wrote: | you're making it far to complicated,go back to KISS principle....
just create a function that contains...
//
printf("%u\r\n",input_state(LED1));
printf("%u\r\n",input_state(LED2));
printf("%u\r\n",input_state(LED3));
printf("%u\r\n",input_state(LED4));
//
.....
far simpler to implement,
easier to understand,
quick to edit to add a pin or two...
probably FASTER than any 'higher level' coding as well....
Jay |
It was just a quick example with leds to understand... but I have more than 30 readings to take and they are not sequential (spanned across multiple ports)!
I just wanted to see if there was a cleaner way than copy pasting 30 lines of repeatable code
Thanks for the clarification guys! _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19559
|
|
Posted: Sat Dec 12, 2015 3:25 pm |
|
|
As a comment though, think again.
If you are reading various bits from multiple ports, just read all the ports directly, using code like PCM_programmer has given, and store/print these. Even the biggest chips only have a handful of ports.... |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Sat Dec 12, 2015 3:30 pm |
|
|
Ttelmah wrote: | As a comment though, think again.
If you are reading various bits from multiple ports, just read all the ports directly, using code like PCM_programmer has given, and store/print these. Even the biggest chips only have a handful of ports.... |
I misread the PCM programmer comment in code
Quote: | This function reads Port B without changing the TRIS. |
I was afraid it will change the state of the pins (like input)...
Turns out its just fine!
Sorry my bad! Thanks all of you guys
Case closed _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
|