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

defining software uart in function

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



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

defining software uart in function
PostPosted: Thu Apr 26, 2018 4:00 am     Reply with quote

Hello everyone,

I am trying to define my software uart with parameters. However, there is a problem

I wrote this

Code:

int stepmotorID = 6;

switch(stepmotorID)
{
     case 5: #use rs232(baud=9600, xmit=PIN_B2, stream=stepmodule) break;
     case 6: #use rs232(baud=9600, xmit=PIN_B3, stream=stepmodule) break;
}

putc('1',stepmodule);
               step_sayisi_l=200;
               step_sayisi_h=0;
               putc(step_sayisi_h,stepmodule);
               delay_ms(1);
               putc(step_sayisi_l,stepmodule);
               delay_ms(5); 



The problem is weird. When i define stepmotorID as 6 it works. I change it to 5 and it stops working.

I believe there is a problem with ";" since i dont use with #use rs232 line.

Can the software uart be defined in function? If i have to define it at the beginning, can i enable and disable it during my code. I want to use the pins as I/O when they stop communicating.

Thank you very much!
doguhanpala



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

PostPosted: Thu Apr 26, 2018 4:28 am     Reply with quote

now things got more interesting

I edited my code to this.

Code:
#include <18F4550.h>
#DEVICE ADC=10
#fuses HSPLL,NOWDT,MCLR,PROTECT,NOLVP,NODEBUG,NOBROWNOUT,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, UART,ERRORS,stream = bt)

void func();
void step();

int step_sayisi_l=200;
int step_sayisi_h=0;
int stepdriverport=0;

void main()
{
   func();   
}


void func()
{
        delay_ms(1000);
        stepdriverport=7;
        if (stepdriverport == 5)
        {
         #use rs232(baud=9600, xmit=PIN_B2, stream=stepmodule)   
         step();
        }
        else if (stepdriverport == 6){
         #use rs232(baud=9600, xmit=PIN_B3, stream=stepmodule) 
         step();
        }
        else if (stepdriverport == 7){
         #use rs232(baud=9600, xmit=PIN_B4, stream=stepmodule)
         step();     
        }     
        else if(stepdriverport==8)
        {
        #use rs232(baud=9600, xmit=PIN_B5, stream=stepmodule)     
        step();
        }
       

}

void step()
{
         putc('1',stepmodule);
         step_sayisi_l=200;
         step_sayisi_h=0;
         putc(step_sayisi_h,stepmodule);
         delay_ms(1);
         putc(step_sayisi_l,stepmodule);
         delay_ms(5);
}


when i call the step function it does not work. However when i edit code to this uglier version, it starts to work. what could be the reason? Any ideas?

Code:
if (stepdriverport == 5)
        {
         #use rs232(baud=9600, xmit=PIN_B2, stream=stepmodule)   
         putc('1',stepmodule);
         step_sayisi_l=200;
         step_sayisi_h=0;
         putc(step_sayisi_h,stepmodule);
         delay_ms(1);
         putc(step_sayisi_l,stepmodule);
         delay_ms(5);
        }
        else if (stepdriverport == 6){
         #use rs232(baud=9600, xmit=PIN_B3, stream=stepmodule) 
         putc('1',stepmodule);
         step_sayisi_l=200;
         step_sayisi_h=0;
         putc(step_sayisi_h,stepmodule);
         delay_ms(1);
         putc(step_sayisi_l,stepmodule);
         delay_ms(5);
        }
        else if (stepdriverport == 7){
         #use rs232(baud=9600, xmit=PIN_B4, stream=stepmodule)
         putc('1',stepmodule);
         step_sayisi_l=200;
         step_sayisi_h=0;
         putc(step_sayisi_h,stepmodule);
         delay_ms(1);
         putc(step_sayisi_l,stepmodule);
         delay_ms(5);
        }     
        else if(stepdriverport==8)
        {
        #use rs232(baud=9600, xmit=PIN_B5, stream=stepmodule)     
        putc('1',stepmodule);
         step_sayisi_l=200;
         step_sayisi_h=0;
         putc(step_sayisi_h,stepmodule);
         delay_ms(1);
         putc(step_sayisi_l,stepmodule);
         delay_ms(5);
        }



edit: i think i solved why this code works. the code goes into the if and after the #use rs232 line it sends the commands.

but in the first version, the code sees the last #use rs232 line which is

#use rs232(baud=9600, xmit=PIN_B5, stream=stepmodule)

since pin b5 is the software uart, pin b4 does not behave as uart. however i dont know how to disable the use rs232 lines that i dont use.
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Thu Apr 26, 2018 6:19 am     Reply with quote

You need to understand, #use is a _preprocessor_ directive, not code.

To switch UART's in the function you just use the stream name, and select this with your switch.
Code:

#use rs232(baud=9600, xmit=PIN_B2, stream=stepmoduleB2)
#use rs232(baud=9600, xmit=PIN_B3, stream=stepmoduleB3)


Then just select the stream name for fputc, between stepmoduleB2 and stepmoduleB3
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Thu Apr 26, 2018 6:34 am     Reply with quote

Explained here:

https://www.ccsinfo.com/faq.php?page=multiple_rs232
_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
doguhanpala



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

PostPosted: Thu Apr 26, 2018 7:47 am     Reply with quote

Ttelmah wrote:
You need to understand, #use is a _preprocessor_ directive, not code.

To switch UART's in the function you just use the stream name, and select this with your switch.
Code:

#use rs232(baud=9600, xmit=PIN_B2, stream=stepmoduleB2)
#use rs232(baud=9600, xmit=PIN_B3, stream=stepmoduleB3)


Then just select the stream name for fputc, between stepmoduleB2 and stepmoduleB3


thank you ttelmah

My intention was to choose the pin which will be used as softwareuart. As far as i understand you suggest i use multiple software uart and choose the which one i need. I have thought of it but did not want to because the code would be too long and i would have to write the putc functions for each uart.

I thought i could use a parameter to define uart.

I have one more question. I will use 6 software uarts for 6 pin. Also i want to use them as I/O's. Does defining a pin as uart affects usage as I/O ?

Thank you for your answer!

My best wishes

Doguhan
doguhanpala



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

PostPosted: Thu Apr 26, 2018 7:48 am     Reply with quote

Fusillade wrote:
Explained here:

https://www.ccsinfo.com/faq.php?page=multiple_rs232


thank you so much!

I did not want to use multiple uarts but it seems it is the only solution.
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Thu Apr 26, 2018 7:58 am     Reply with quote

I don't know if this will work, but try #inline for your function and check the code generated. It might generate the correct code under each #use statement
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Thu Apr 26, 2018 8:54 am     Reply with quote

On software UART's the pins can still be used for normal I/O.
You need to understand that there is different code for each software UART. The compiler uses explicit bit set/test operations on the specific pin, so the code can't be 'switched'. The amount of code involved though is tiny. You can just encapsulate the output into a function (my_putc for example), which automatically routes to the required UART, then use this to take the output you want to send.
doguhanpala



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

PostPosted: Sat Apr 28, 2018 3:23 am     Reply with quote

Ttelmah wrote:
On software UART's the pins can still be used for normal I/O.
You need to understand that there is different code for each software UART. The compiler uses explicit bit set/test operations on the specific pin, so the code can't be 'switched'. The amount of code involved though is tiny. You can just encapsulate the output into a function (my_putc for example), which automatically routes to the required UART, then use this to take the output you want to send.


Thank you Ttelmah. I solved my problem, the code now chooses which pin to use from multiple rs232.

I have a tiny question. I tried the rs232 pin as output. It works fine. I blinked a led. After that i could not manage to use that pin as uart again.

for example
Code:

output_high(pin_b5);
delay_ms(100);
output_low(pin_b5);
delay_ms(10000);//in this 10 seconds i remove my led and put my step driver module
putc('1',stepmoduledriverP5);

My guess is when the output is low the step module sees is as '0'. Is there a line of code that prepares the pin as uart again? I will put my module after that line of code.
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Sat Apr 28, 2018 12:51 pm     Reply with quote

Remember the serial expects the line to start 'high'. If you have set it low the first (start) bit won't be seen. It needs to be back high before the software UART can be used.
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