CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

[SOLVED] Query all states of I/O pins...

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

[SOLVED] Query all states of I/O pins...
PostPosted: Fri Dec 11, 2015 5:02 pm     Reply with quote

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: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Dec 11, 2015 7:53 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 11, 2015 8:01 pm     Reply with quote

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: 19552

View user's profile Send private message

PostPosted: Sat Dec 12, 2015 2:22 am     Reply with quote

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: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Dec 12, 2015 5:50 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Dec 12, 2015 8:22 am     Reply with quote

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: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Dec 12, 2015 11:07 am     Reply with quote

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: 19552

View user's profile Send private message

PostPosted: Sat Dec 12, 2015 11:23 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Dec 12, 2015 12:02 pm     Reply with quote

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 Smile


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: 19552

View user's profile Send private message

PostPosted: Sat Dec 12, 2015 3:25 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Dec 12, 2015 3:30 pm     Reply with quote

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 Smile

Case closed Very Happy
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group