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 support@ccsinfo.com

How to #define a Pre-Processor directive?

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



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

How to #define a Pre-Processor directive?
PostPosted: Fri Dec 26, 2014 2:06 am     Reply with quote

How to I #define a Pre-Processor directive? I want this so that I can easily move devices between UART Ports.

I want something like this:
Code:

#define device2ISR INT_RDA3


#device2ISR
void myISR(){
 //...
 //...
}
dyeatman



Joined: 06 Sep 2003
Posts: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Dec 26, 2014 9:07 am     Reply with quote

...

Last edited by dyeatman on Fri Dec 26, 2014 4:29 pm; edited 3 times in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Fri Dec 26, 2014 9:31 am     Reply with quote

He wants to dynamically change things.

The only way I can think of to do this easily, would be with a 'hybrid' approach.
Problem is that you need to change so many things (not just the ISR, but the interrupt enable, and the character I/O functions)....

So, take advantage of the new compilers abilities to handle the ISR for you. Don't have an INT_RDAx or INT_TBEx, but use the buffer implementation in #USE_RS232, with interrupt support. Then you can just change this function to talk to whatever UART you want. It'll automatically shift the ISR's to suit.

Look here for the 'limitations' of this implementation (primary one if handling of buffer overflow), but it does seem to work correctly, provided this is avoided.

The only other way would be using #ifdef and a UART number, to deselect/select the required routines.
haxan7



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

PostPosted: Sat Dec 27, 2014 9:46 am     Reply with quote

Thanks for the replies.

I am doing it like following:
Code:

#if MY_DEVICE_STREAM==UART_PORT1
   #define MY_DEVICE_RDA INT_RDA
   #INT_RDA
#ELIF MY_DEVICE_STREAM==UART_PORT2
   #define MY_DEVICE_RDA INT_RDA2
   #INT_RDA2
#ELIF MY_DEVICE_STREAM==UART_PORT3
   #define MY_DEVICE_RDA INT_RDA3
   #INT_RDA3
#ELIF MY_DEVICE_STREAM==UART_PORT4
   #define MY_DEVICE_RDA INT_RDA4
   #INT_RDA4
#endif
void myISR(){
   char c = fgetc(MY_DEVICE_STREAM);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Sat Dec 27, 2014 10:24 am     Reply with quote

You still have to change the interrupt enable as well.

Your approach seems pointless, since you are not adding buffering (just one character). Why not use the internal buffering?. Easier, and safer

Just specify something like 8 characters of buffering in your #use RS232, and tell it to use interrupts, and get rid of your interrupt handler completely. Whenever kbhit says there is a character, something is in the buffer waiting for you.
haxan7



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

PostPosted: Sat Dec 27, 2014 10:32 am     Reply with quote

Ttelmah wrote:
You still have to change the interrupt enable as well.

Your approach seems pointless, since you are not adding buffering (just one character). Why not use the internal buffering?. Easier, and safer

Just specify something like 8 characters of buffering in your #use RS232, and tell it to use interrupts, and get rid of your interrupt handler completely. Whenever kbhit says there is a character, something is in the buffer waiting for you.


I can enable/disable the interrupts using MY_DEVICE_RDA, which I have defined above.

ISR in my post above is just an example, actual ISR is much complex.
temtronic



Joined: 01 Jul 2010
Posts: 9174
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Dec 27, 2014 11:03 am     Reply with quote

I'm lost trying to understand what you're trying to accomplish. It's an easy task to setup 4 ISRs for the 4 hardware UARTs your PIC has(very,very few have 4 UARTs !).What PIC are you using??
Using the CCS supplied ex_sisr.c example, it can be expanded to include all 4 .
Perhaps you can explain in better detail what you need to accomplish?
I'm thinking there's a far easier solution !!

jay
haxan7



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

PostPosted: Sat Dec 27, 2014 11:10 am     Reply with quote

temtronic wrote:
I'm lost trying to understand what you're trying to accomplish. It's an easy task to setup 4 ISRs for the 4 hardware UARTs your PIC has(very,very few have 4 UARTs !).What PIC are you using??
Using the CCS supplied ex_sisr.c example, it can be expanded to include all 4 .
Perhaps you can explain in better detail what you need to accomplish?
I'm thinking there's a far easier solution !!

jay


I have a bunch of devices, of which 4 will be connected to the uController.
ISR of each device is unique that looks for bit patterns and # of bits etc and sets a flag.
I wanted a way so that the devices can be easily selected on specified port.
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Sat Dec 27, 2014 11:20 am     Reply with quote

haxan7 wrote:
Ttelmah wrote:
You still have to change the interrupt enable as well.

Your approach seems pointless, since you are not adding buffering (just one character). Why not use the internal buffering?. Easier, and safer

Just specify something like 8 characters of buffering in your #use RS232, and tell it to use interrupts, and get rid of your interrupt handler completely. Whenever kbhit says there is a character, something is in the buffer waiting for you.


I can enable/disable the interrupts using MY_DEVICE_RDA, which I have defined above.

ISR in my post above is just an example, actual ISR is much complex.


Except for buffering, it shouldn't be.

Rule with ISR's is always get out ASAP. Simple buffering is all that should be done in a serial ISR.
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

PostPosted: Wed Dec 31, 2014 1:55 pm     Reply with quote

You could do this:

#define MY_DEVICE_STREAM1 UART_PORT4
#define MY_DEVICE_STREAM2 UART_PORT3

Code:

#if MY_DEVICE_STREAM1==UART_PORT1
   #INT_RDA
#ELIF MY_DEVICE_STREAM1==UART_PORT2
   #INT_RDA2
#ELIF MY_DEVICE_STREAM1==UART_PORT3
   #INT_RDA3
#ELIF MY_DEVICE_STREAM1==UART_PORT4
   #INT_RDA4
#endif
void RDA_STREAM1(){
   char c = fgetc(MY_DEVICE_STREAM1);
}


#if MY_DEVICE_STREAM2==UART_PORT1
   #INT_RDA
#ELIF MY_DEVICE_STREAM2==UART_PORT2
   #INT_RDA2
#ELIF MY_DEVICE_STREAM2==UART_PORT3
   #INT_RDA3
#ELIF MY_DEVICE_STREAM2==UART_PORT4
   #INT_RDA4
#endif
void RDA_STREAM2(){
   char c = fgetc(MY_DEVICE_STREAM2);
}

.. and so forth

temtronic



Joined: 01 Jul 2010
Posts: 9174
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Dec 31, 2014 2:10 pm     Reply with quote

re:
"
I have a bunch of devices, of which 4 will be connected to the uController.
ISR of each device is unique that looks for bit patterns and # of bits etc and sets a flag.
I wanted a way so that the devices can be easily selected on specified port.
"
...
Not to sure how you can accomplish that as hardware UARTS are setup for a fixed # of bits,baud,parity, etc. Even with 'streams' I don't see how you could detect unknown device #1 on serial input #3. Maybe I'm missing something?

Jay
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

PostPosted: Wed Dec 31, 2014 2:41 pm     Reply with quote

temtronic wrote:
re:
"
I have a bunch of devices, of which 4 will be connected to the uController.
ISR of each device is unique that looks for bit patterns and # of bits etc and sets a flag.
I wanted a way so that the devices can be easily selected on specified port.
"
...
Not to sure how you can accomplish that as hardware UARTS are setup for a fixed # of bits,baud,parity, etc. Even with 'streams' I don't see how you could detect unknown device #1 on serial input #3. Maybe I'm missing something?

Jay


I see what you mean. If you create a steam, only that function uses that stream. You would have to build all the different combinations of stream functions.

Can you not have a bunch of functions that all have different streams. Then call them to re-init the port and then use the new stream?

You can't pin_select after they have been assigned in CCS, but you can do it manually. So you can physically change the internal uart mapping. Evil or Very Mad
I'm not saying it's a good idea. It's just an idea.
it's not going to look pretty.
temtronic



Joined: 01 Jul 2010
Posts: 9174
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Dec 31, 2014 3:43 pm     Reply with quote

It'd be nice to know which PIC you're using as well as these 'devices' that get connected.
I have serious doubts you can attach a 'device' to the PIC and have the PIC figure out what the 'device' is unless you already know the 'datastream' coming from the device(which we don't). We don't know baud rates, bits per data, etc. all critical 'need to know' details.


jay
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

PostPosted: Wed Dec 31, 2014 7:13 pm     Reply with quote

temtronic wrote:
It'd be nice to know which PIC you're using as well as these 'devices' that get connected.
I have serious doubts you can attach a 'device' to the PIC and have the PIC figure out what the 'device' is unless you already know the 'datastream' coming from the device(which we don't). We don't know baud rates, bits per data, etc. all critical 'need to know' details.
jay


I'm using the PIC24FJ128GA306 connected to various serial devices.

Are you are talking about a plug and play serial host. May need a few bytes of free space to make that table. Shocked
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