View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
How to receive 3 character from PC and send it to Output |
Posted: Thu May 21, 2015 9:45 am |
|
|
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
|
|
Posted: Thu May 21, 2015 11:24 am |
|
|
Try the CCS sample folder.
(EX_STR.C for starters.)
Mike |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Thu May 21, 2015 1:37 pm |
|
|
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
|
|
Posted: Fri May 22, 2015 1:00 am |
|
|
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
|
|
Posted: Fri May 22, 2015 5:38 am |
|
|
Hi Ttelmah,
Thank you, i will use while(true) command |
|
|
|