View previous topic :: View next topic |
Author |
Message |
capella
Joined: 07 Feb 2009 Posts: 38
|
sending data to pc from pic problem |
Posted: Sat May 01, 2010 12:20 pm |
|
|
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
|
|
Posted: Sat May 01, 2010 10:41 pm |
|
|
So you want everything to only execute one time ?
Then remove the while() loop. Remove this:
And also remove the closing brace at the end.
Then, ideally, you would put this at the very end of the program
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
|
|
Posted: Thu May 13, 2010 1:16 am |
|
|
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
|
|
Posted: Thu May 13, 2010 1:26 am |
|
|
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
|
|
Posted: Thu May 13, 2010 2:16 am |
|
|
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
|
|
Posted: Fri May 14, 2010 12:21 am |
|
|
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
|
|
Posted: Fri May 14, 2010 1:44 am |
|
|
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;
}
|
|
|
|
|