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

#use direttive inside code

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







#use direttive inside code
PostPosted: Sat Jun 24, 2006 7:15 pm     Reply with quote

Hi people!

I am wondering which of the following code is the right and the best solution.
I'd like to use the UART port in two different settings depending on a pin state.
Here I show the three solution that I could think of, I don't even know if they are all ok...

1) Using a hardware UART and a software one:

#use rs232(stream=MID, baud=31250, xmit=PIN_C6, rcv=PIN_C7)

#use rs232(stream=COM, baud=9600, xmit=PIN_B1, rcv=PIN_B2)


then using fputc(0x90,MID) or fputc(0x90,COM) as I need the first or second configuration.

2) Using #use direcrive inside the code:

void main() {

int value=0;

int1 a,b,c;

setup_adc_ports(AN0);

setup_adc(ADC_CLOCK_INTERNAL);

set_adc_channel(0);

if ( input(PIN_B4)) {
midi=1;
#use rs232( baud=9600,xmit=PIN_C6, rcv=PIN_C7);
}
if (!input(PIN_B4)) {
midi=0;
#use rs232( baud=31250, xmit=PIN_C6, rcv=PIN_C7);
}
while (TRUE) {


.....
}

so that configuration of UART is done only once the program knows which settings will be used (depending on the state od PIN_B4


3) (i really think that this would not work) Using just une UART and two different settings for it with two different stream names:

#use rs232(stream=MID, baud=31250, xmit=PIN_C6, rcv=PIN_C7)

#use rs232(stream=COM, baud=9600, xmit=PIN_C6, rcv=PIN_C7)

then using fputc(0x90,MID) and fput(0x90,COM) as in 1st case but using just an UART


So that's all, which is the best (and the working one) ? Smile

thank you

mic
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Sat Jun 24, 2006 11:00 pm     Reply with quote

From the help file
Quote:

SET_UART_SPEED()


Syntax:
set_uart_speed (baud, [stream])



Parameters:
baud is a constant 100-115200 representing the number of bits per second. stream is an optional stream identifier.



Returns:
undefined



Function:
Changes the baud rate of the built-in hardware RS232 serial port at run-time.



Each bit in the value represents one pin. A 1 indicates the pin is input and a 0 indicates it is output.



Availability:
This function is only available on devices with a built in UART.



Requires
#use rs232



Examples:
// Set baud rate based on setting

// of pins B0 and B1



switch( input_b() & 3 ) {

case 0 : set_uart_speed(2400); break;

case 1 : set_uart_speed(4800); break;

case 2 : set_uart_speed(9600); break;

case 3 : set_uart_speed(19200); break;

}



Example Files:
loader.c


Ttelmah
Guest







PostPosted: Sun Jun 25, 2006 2:59 am     Reply with quote

Either version is 'right'. The 'multiple #use' form, was the _only_ version on older compilers. It can be a real 'pain', since it is very difficult at times to be sure which #use is currently in effect. 'Streams' (the ability to have multiple '#use' statements, accessed by name), were then added, and result in easier to understand/follow code, and a reduction is errors when coding.
I'd use streams, but either will work.

Best Wishes
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Sun Jun 25, 2006 5:47 am     Reply with quote

Ttelmah wrote:
Either version is 'right'. The 'multiple #use' form, was the _only_ version on older compilers. It can be a real 'pain', since it is very difficult at times to be sure which #use is currently in effect. 'Streams' (the ability to have multiple '#use' statements, accessed by name), were then added, and result in easier to understand/follow code, and a reduction is errors when coding.
I'd use streams, but either will work.

Best Wishes


Can you use streams with the same hardware pins? I wouldn't think so. Seems a bit much for the compiler to keep track of or a waste if the compiler had to setup the UART each time it was accessed. Anyways, it looked like the only difference was the speed. If so, then setting the baud rate is the way to go.
Ttelmah
Guest







PostPosted: Sun Jun 25, 2006 6:33 am     Reply with quote

No.
The stream solution, is right for the first, and second configuration in the original question, while the 'set_uart_speed' solution is correct for the third.

Best Wishes
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