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 and getc

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



Joined: 16 Jul 2008
Posts: 8
Location: Mexico

View user's profile Send private message

rs232 and getc
PostPosted: Wed Nov 05, 2008 4:57 am     Reply with quote

i can't work 'if"

if ((letra[5] == 'O') && (letra[4] == 'F')){
}................


Code:

#include <18F252.h>

#FUSES HS
#FUSES WDT
#FUSES WDT128
#FUSES NOLVP
#FUSES CPB
#FUSES PROTECT

#use delay(clock = 8000000)
#use rs232(baud = 9600, parity = N, xmit = PIN_C6, rcv = PIN_C7, bits = 8)


#define Led PIN_A0

char letra[7];

//*---------------------------------------------------------------------------*/
void main() {
   set_tris_a( 0b00000000 );
   set_tris_b( 0b01111111 );

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED, 0, 1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   
   output_high( Led );
   delay_ms( 1000 );
   printf ( "\n\r\START\n\r" );
   output_low( Led );
     
   while (true) {
      letra[6] = getc();
      for (i = 1; i < 6; ++i) letra[i] = letra[i + 1];
     
      if ((letra[5] == 'O') && (letra[4] == 'N') ) && (letra[3] == 'N') ) {
            printf("ON");
      };
      if ((letra[5] == 'O') && (letra[4] == 'F') ) && (letra[3] == 'F') ) {
            printf("OFF");
      };

   };
}


Last edited by mpardinho on Wed Nov 05, 2008 6:01 am; edited 1 time in total
Ttelmah
Guest







PostPosted: Wed Nov 05, 2008 5:32 am     Reply with quote

Of course it won't....
You are writing each character to the location 'letra[6]'. Getc, gets _one_ character.

Best Wishes
mpardinho



Joined: 16 Jul 2008
Posts: 8
Location: Mexico

View user's profile Send private message

PostPosted: Wed Nov 05, 2008 5:37 am     Reply with quote

i put caracter 2 in 1, 3 in 2
for (i = 1; i < 6; ++i) letra[i] = letra[i + 1];

i get 1 caracter in 6 and put i 5

if i test in proteus is 100% code works
tinley



Joined: 09 May 2006
Posts: 67

View user's profile Send private message Visit poster's website

PostPosted: Wed Nov 05, 2008 5:40 am     Reply with quote

if ((letra[5] == 'O') && (letra[4] == 'N') ) && (letra[3] == 'N') ) {
printf("ON);
};
if ((letra[5] == 'O') && (letra[4] == 'F') ) && (letra[3] == 'F') ) {
printf("OFF);
};

two many brackets in wrong places! Change to this:

if (letra[5] == 'O' && letra[4] == 'N' && letra[3] == 'N' ) {
printf("ON);
};
if (letra[5] == 'O' && letra[4] == 'F' && letra[3] == 'F' ) {
printf("OFF);
};
tinley



Joined: 09 May 2006
Posts: 67

View user's profile Send private message Visit poster's website

PostPosted: Wed Nov 05, 2008 5:43 am     Reply with quote

Oh, and the printf's are wrong too. Missing quotes, change:

printf("OFF);

to this:

printf("OFF");

and the same with ON
mpardinho



Joined: 16 Jul 2008
Posts: 8
Location: Mexico

View user's profile Send private message

PostPosted: Wed Nov 05, 2008 6:03 am     Reply with quote

i change code......i post without "
the problem is in if(

if(letra=='o' ...........

IN PROTEUS WORK
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Nov 05, 2008 6:05 am     Reply with quote

You must be sending "NO" and "FFO" ?

if you send "ON"

"O" ->
letra[6] = "O"

"N"->
letra[5] = letra[6] ('O')
letra[6] = "N"

so your check would be if ((letra[5] == 'O') && (letra[6] == 'N'))

You have extra ; at the end of your if braces aswell. Won't hurt but don't need them!
if (...)
{
} // <-- No ; required
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