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

sending data to pc from pic problem

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



Joined: 07 Feb 2009
Posts: 38

View user's profile Send private message

sending data to pc from pic problem
PostPosted: Sat May 01, 2010 12:20 pm     Reply with quote

I want to send a char to pc from pic16f877A. I could. But while ilk_pot_deger equals kendi_degeri, pic are sending a char continuously.

Code:

#include <16F877A.H>
#device adc=8
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//======================================
void main()
{

int32 ilk_pot_deger,kendi_degeri,ikinci_deger,ucuncu_deger,dorduncu_deger,ikinci_kendi_deger,ucuncu_kendi_deger,dorduncu_kendi_deger;
char a;
a=getch();

setup_adc_ports(AN0); //TÜM ANALOG PORTLARI KULLANIMA AÇ
setup_adc(adc_clock_div_8);
set_adc_channel(0); //AN0 KANALINI AYARLA
delay_ms(100);
 
ilk_pot_deger = read_adc();
ikinci_deger = ilk_pot_deger+40;
ucuncu_deger = ilk_pot_deger+80;
dorduncu_deger = ilk_pot_deger+120;

while(true)
  {

 
if (a=='1')
{
   delay_ms(100);
   kendi_degeri = read_adc();
   
   
if (ilk_pot_deger>kendi_degeri)
  {
 delay_ms(200);
 output_high(pin_b7);
 output_high(pin_d0);

 }
 
 if (ilk_pot_deger==kendi_degeri)
 {
 delay_ms(100);
 output_low(pin_b7);
 output_low(pin_d0);
 output_low(pin_b6);
 output_low(pin_d1);
  putc('r');
 delay_ms(200);


 }
 if (ilk_pot_deger<kendi_degeri)
  {
 delay_ms(200);
 output_high(pin_b6);
 output_high(pin_d1);
 }
}



I want that pic must send to pc single r, not rrrrrrrrrr
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat May 01, 2010 10:41 pm     Reply with quote

So you want everything to only execute one time ?
Then remove the while() loop. Remove this:
Code:

while(true)
  {

And also remove the closing brace at the end.


Then, ideally, you would put this at the very end of the program
Code:
while(1);

to prevent the PIC from going to sleep. In your current program it
doesn't matter if the PIC goes to sleep, but you might add some new
code later, so it's best to put the while(1); statement at the end.
capella



Joined: 07 Feb 2009
Posts: 38

View user's profile Send private message

PostPosted: Thu May 13, 2010 1:16 am     Reply with quote

Thanks for your answer, but your suggestion behaviour is different than my request. I did what you say. When I changed my code, yes pic send a char to pc, but when I change potentiometer pic does not work. İt means your suggestion works pic for once. But I want that when I change potentiometer value pic works continuously not once.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu May 13, 2010 1:26 am     Reply with quote

You didn't give a clear specification of intended behaviour! If you want to act the program on change, oyu have to detect
a change, by comparing present and previous value. But the method must be expected not to work correctly without
applying a hysteresis, because ADC values can be varying by a few LSB with constant input. I'm sure you'll figure it out, if
you think about the problem seriously.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu May 13, 2010 2:16 am     Reply with quote

If you only want the putc('r'); to be output once then you can use a flag:-

Code:


int1 flag = false;
...

while(true)
{
...
...
if (ilk_pot_deger==kendi_degeri)
 {
 delay_ms(100);
 output_low(pin_b7);
 output_low(pin_d0);
 output_low(pin_b6);
 output_low(pin_d1);
  if (!flag)
  {
    putc('r');
    flag = true;
  }
 delay_ms(200);
 }
 else
   flag = false;  // Required reset so that 'r' is output the next time values become =

...
...
}


but as stated, the nature of ADC may mean your value is not stable enough for this to work reliably.
capella



Joined: 07 Feb 2009
Posts: 38

View user's profile Send private message

PostPosted: Fri May 14, 2010 12:21 am     Reply with quote

thanks a lot wayne_ ,yes your code is working normally...
I told missing things. I have second problem

Code:

void main()
{

int32 ilk_pot_deger,kendi_degeri,ikinci_deger,ucuncu_deger,dorduncu_deger,ikinci_kendi_deger,ucuncu_kendi_deger,dorduncu_kendi_deger;
char a;
int1 flag = false;
a=getch();

setup_adc_ports(AN0); //TÜM ANALOG PORTLARI KULLANIMA AÇ
setup_adc(adc_clock_div_8);
set_adc_channel(0); //AN0 KANALINI AYARLA
delay_ms(100);
 
ilk_pot_deger = read_adc();
ikinci_deger = ilk_pot_deger+10;
ucuncu_deger = ilk_pot_deger+20;
dorduncu_deger = ilk_pot_deger+30;


while(true)

{
 
if (a=='1')
{

   delay_ms(100);
   kendi_degeri = read_adc();
   
   
if (ilk_pot_deger>kendi_degeri)
  {
 // flag = false;
 delay_ms(200);
 output_high(pin_b7);
 output_high(pin_d0);
 delay_ms(100);
 
 }
 
 if (ilk_pot_deger==kendi_degeri)
 {
 delay_ms(100);
 output_low(pin_b7);
 output_low(pin_d0);
 output_low(pin_b6);
 output_low(pin_d1);

 
 if (!flag)
  {
    putc('r');
    flag = true;
    delay_ms(100);
  }
 

 }
  else
{
flag = false; 

}
}

 if (ilk_pot_deger<kendi_degeri)
  {
//  flag = false;
 delay_ms(200);
 output_high(pin_b6);
 output_high(pin_d1);
 delay_ms(100);
 }
 
}
/////////////////////////2.deger//////////////////////////////

if (a=='2')
{
   
   delay_ms(100);
  ikinci_kendi_deger = read_adc();
     
if (ikinci_deger>ikinci_kendi_deger)
  {
// flag = false;
 delay_ms(200);
 output_high(pin_b7);
 output_high(pin_d0);
 delay_ms(100);
 }
 
 if (ikinci_deger==ikinci_kendi_deger)
 {
 delay_ms(100);
 output_low(pin_b7);
 output_low(pin_d0);
 output_low(pin_b6);
 output_low(pin_d1);

 

 if (!flag)
  {
    putc('r');
    flag = true;
    delay_ms(100);
  }
 
else
  {
  flag = false;
  }
 
 }
 
 if (ikinci_deger<ikinci_kendi_deger)
  {
 
 delay_ms(200);
 output_high(pin_b6);
 output_high(pin_d1);
 delay_ms(100);
 }
 
}


my problem is when I press '2' or '1', program is just working in itself loop. It means program does not pass from 1's loop to 2's loop or from 2's loop to 1's loop.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri May 14, 2010 1:44 am     Reply with quote

Your problem is that you only read a char before entering the main loop

a=getch();

Once in the loop you do not read anymore key presses.

What you need to do is in the main loop, check for an available char and then read it.

Code:

while(true)
{
  if (kbhit)
    a = getch();

...


Also in your second '2' routine you are clearing the flag every time
ilk_pot_deger==kendi_degeri after you have output 'r' once so it will keep outputting it but at half the speed!
The flag = false should be done when the 2 values are not equal.

Code:

if (ikinci_deger==ikinci_kendi_deger)
{
  delay_ms(100);
  output_low(pin_b7);
  output_low(pin_d0);
  output_low(pin_b6);
  output_low(pin_d1);

  if (!flag)
  {
     putc('r');
     flag = true;
     delay_ms(100);
   }
}
else
{
  flag = false;
}
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