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

RS232 control

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



Joined: 13 Feb 2014
Posts: 2

View user's profile Send private message

RS232 control
PostPosted: Mon Mar 03, 2014 3:16 pm     Reply with quote

Hello,
I have the following problem and needs help.

I have written the code below to control LEDs on the specified ports. However, I have to hit character 3 times before LED turns ON.
what am I not doing correctly?

#include <18F2520.h>
#device adc=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC //High speed osc with HW enabled 4X PLL
#FUSES NOBROWNOUT //No brownout reset
#FUSES LVP //Low voltage prgming
#FUSES NOXINST //Extended mode disabled (Legacy mode)
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#include <stdio.h>

void main()
{
while(true){
if(kbhit()){

if(getc()=='a')
{
output_high(PIN_B7);
printf("led1 on\r\n");

}
if(getc()=='b')
{
output_low(PIN_B7);
printf("led1 Off\r\n");


}

if(getc()=='c')
{
output_high(PIN_B6);
printf("led2 on\r\n");


}
if(getc()=='d')
{
output_low(PIN_B6);
printf("led2 off\r\n");



}}

}
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Mon Mar 03, 2014 3:37 pm     Reply with quote

1) Not using the code buttons....
2) Correct the remarks. These mislead both us, and you. For instance, you are not using the PLL.
3) Get rid of the LVP fuse. Use NOLVP. 99% of systems do not use low voltage programming. With LVP enabled, unless the pins that handle this are correctly resistor biased to disable the programming, the chip will _not_ run properly.
4) Add 'ERRORS' to your RS232 setup. Repeat 50*, "when using the hardware UART, you _must_ have the ERRORS parameter, unless _you_ handle errors yourself".
5) _Think_. Each 'getc', gets a character. Each one will only respond to the one character specified, and throw the character away. So to get to (say) the third one, three earlier characters will need to be received. You need to read the character _once_, and then do all the tests on this one character :
Code:

   int8 char_rcvd;
   if(kbhit())
   {
      char_rcvd=getc();
      if(char_rcvd=='a')
      {
          output_high(PIN_B7);
          printf("led1 on\r\n");
      }
      if(char_rcvd=='b')
      {
      //etc....

Also, realise you can code this as a 'switch'.

The last is the main reason it is not working, but the former ones are all 'waiting to catch you later'.
blixxman



Joined: 13 Feb 2014
Posts: 2

View user's profile Send private message

PostPosted: Mon Mar 03, 2014 5:45 pm     Reply with quote

Thanks Ttelmah...it works
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