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

How to receive 3 character from PC and send it to Output

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



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

How to receive 3 character from PC and send it to Output
PostPosted: Thu May 21, 2015 9:45 am     Reply with quote

Hi,

I am using rs232 (hardware on pic 18f452) to receive single character commands from a pc.
This works very well but I would like this to work with simple strings if possible so I can change my single character commands to 3 characters and the output can work when receive 3 characters.

Any suggestions on how to implement simple strings would be very welcome.
Code:

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <string.h>


#int_rb
void detect_rb_change() {


while(input(PIN_B0)) ;     

   delay_ms(25);               

}

void main()
{

    char  cmd ;

{

byte j;
for (j=1;j<=250;++j)
{
      byte i;
      for (i=1;i<=250;++i)

{

enable_interrupts(int_rb);
enable_interrupts(global);


      printf("\rPress 0,1,2,3,4,5  : %c \n",cmd);
     cmd=getc();
     
      if(cmd=='A')
         output_B(0b00000000);

      if(cmd=='B')
         output_B(0b00000001);

            if(cmd=='C')
        output_B(0b00000010);

}
}
}

}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu May 21, 2015 11:24 am     Reply with quote

Try the CCS sample folder.
(EX_STR.C for starters.)

Mike
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 21, 2015 11:30 am     Reply with quote

Also see:

Sample code to detect serial command strings:

From Ttelmah:
http://www.ccsinfo.com/forum/viewtopic.php?t=31144
From srhoar:
http://www.ccsinfo.com/forum/viewtopic.php?t=28159
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Thu May 21, 2015 1:37 pm     Reply with quote

and also get rid of the delay in the INT_RB handler.
This will mean that the serial _will_ at times have overloaded, and will never recover (since you don't have 'ERRORS' in the RS232 declaration.

If you need a delay to do something, then set a flag, and delay in the external code, with the interrupts left running.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri May 22, 2015 1:00 am     Reply with quote

Ttelmah wrote:
and also get rid of the delay in the INT_RB handler
Same applies to the while loop which loops as long as the button is pressed. Even a user pressing the button very short will take 100ms, that is very long for a processor to do nothing. Again, serial data will get lost.
The same effect can be achieved by setting the interrupt to activate on the other edge. Then, when you enter the interrupt again you know this is because the user let the button go.

Code:
byte j;
for (j=1;j<=250;++j)
{
      byte i;
      for (i=1;i<=250;++i)

{
This part of the code makes the program repeat itself. After 250 * 250 = 62,500 times the program will stop. Is that what you want? If yes, then using an int16 that runs from 1 to 62,500 is much clearer about your intentions.
My guess is you intend your program to loop forever, then using a while(true) command is the right choice.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Fri May 22, 2015 5:38 am     Reply with quote

Hi Ttelmah,
Thank you, i will use while(true) command
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