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 support@ccsinfo.com

getc() problem

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



Joined: 24 Jun 2004
Posts: 285

View user's profile Send private message

getc() problem
PostPosted: Wed Oct 13, 2004 1:38 pm     Reply with quote

I have made a small program
Code:


#if defined(__PCM__)
#include <16F819.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, parity=N, xmit=PIN_B7, rcv=PIN_B6, INVERT)

#include <math.h>

void main()
{
   int8 duty,i;
   char st=' ';
   int val1,val2,val3;
   float Total;
   int1 incr_decr;
   int mode; //lighting condition grabbing set
 //  setup_comparator(NC_NC_NC_NC);
   setup_port_a(AN0_AN1_AN3);
   setup_adc( ADC_CLOCK_INTERNAL );
   set_tris_b(0b10111011);   //set ccp1 pin as low as output ccp1 pin is b2
   setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
   setup_timer_2(T2_DIV_BY_1, 255, 1);
   duty=50;
   set_pwm1_duty(duty);
   delay_ms(10);
   incr_decr=1;

   while(1)
    {
     while(getc()=='a')
     {
     set_pwm1_duty(0);
     delay_ms(100);
       for(i=0;i<256;i++)
          {
          set_pwm1_duty(i);
          delay_ms(50);
          set_adc_channel( 0 );
          delay_us(100);
          val1 = Read_ADC();

          set_adc_channel( 1 );
           delay_us(100);
          val2 = Read_ADC();

          set_adc_channel( 3 );
          delay_us(100);
          val3 = Read_ADC();

          printf("%2u %2u %2u %2u\n\r",val1,val2,val3,i);
          st=' ';
         }
       }
      }
}


In this program, What I want is whenever I click on letter'a', it start loop, when I am not clicking on letter 'a', it will wait for me to click on the letter 'a' again. however, the program works like this way, when power is on, it waiting for me to click on letter 'a', after I click on letter on, the program start for-next loop, and keep loop again without waiting me to click on letter 'a'. please help to find what is the reaseon and how to correct it?
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Wed Oct 13, 2004 2:29 pm     Reply with quote

The 16F819 does´n have UART.
The reason of the endless loop is

while(getc()=='a') where once keyed "a", the while always is true.

Try this way:
Code:

while(1)
    {
     do
        {
          char_rcved = getc();
        }while(char_rcved !='a');
       
       set_pwm1_duty(0);
       delay_ms(100);
       for(i=0;i<256;i++)
          {
           set_pwm1_duty(i);
           delay_ms(50);
           set_adc_channel( 0 );
           delay_us(100);
           val1 = Read_ADC();

           set_adc_channel( 1 );
           delay_us(100);
           val2 = Read_ADC();

           set_adc_channel( 3 );
           delay_us(100);
           val3 = Read_ADC();

           printf("%2u %2u %2u %2u\n\r",val1,val2,val3,i);
           st=' ';
           char_rcved = 0;   // clear char_rcved to break the TRUE condition
                                     // inside the do while cycle
          } // for...

     } // while(1)
   


best wishes,

Humberto
dyeatman



Joined: 06 Sep 2003
Posts: 1929
Location: Norman, OK

View user's profile Send private message

one idea...
PostPosted: Wed Oct 13, 2004 3:37 pm     Reply with quote

Do you think an integer only goes from 0 to 255 might have something to do with it?

Code:

    for(i=0;i<256;i++)

 


Question
Guest








PostPosted: Wed Oct 13, 2004 4:28 pm     Reply with quote

I did as you advised, I still get the same result anyway.
Guest








PostPosted: Wed Oct 13, 2004 4:33 pm     Reply with quote

what do you mean by
Quote:

Do you think an integer only goes from 0 to 255 might have something to do with it?



do you thing I should go to a big loop, but I do not a bigger loop anyway?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Oct 13, 2004 5:19 pm     Reply with quote

Quote:
while(getc()=='a') where once keyed "a", the while always is true.

Wrong. It will wait for another press.

Quote:
Do you think an integer only goes from 0 to 255 might have something to do with it?


What he means is that i<256 will always be true since the next value after 255 is 0 for an 8bit int. Change it to i<255 or make i be a int16.
Guest








PostPosted: Wed Oct 13, 2004 6:34 pm     Reply with quote

Thank you, Dayman and mark is right
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