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

math problem?
Goto page Previous  1, 2
 
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

PostPosted: Tue Jun 22, 2010 7:23 am     Reply with quote

You are right, if(b==1) make something also if(b==2) ,if(b==3) ,if(b==4)
makes something. You are right, I changed as if(buf[0]==1), if(buf[0]==2) , if(buf[0]==3) and also if(buf[0]==4) but result is same. Now pic takes first value, for example 55 pic answers is 20 also 92 - > 36. Yes you are right. I thinks a lot, I don't find anything so I feel to ask question. If you don't want to answer Ttelmah to me , this is your choice.

Best regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19790

View user's profile Send private message

PostPosted: Tue Jun 22, 2010 8:26 am     Reply with quote

Have a look at 'input.c'. There are routines in here to get a string, and also ones to get a number. Look at what they do...

Best Wishes
capella



Joined: 07 Feb 2009
Posts: 38

View user's profile Send private message

PostPosted: Wed Jun 23, 2010 2:50 am     Reply with quote

I think I have to write my code completly to what I want for my problem understanding correctly..
Code:

#include <18f452.H>

#device adc=10

#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#INCLUDE <stdlib.h>
//======================================
void main()
{
 

int32 ilk_pot_deger,kendi_degeri,ikinci_deger,ucuncu_deger,dorduncu_deger,ikinci_kendi_deger,ucuncu_kendi_deger,dorduncu_kendi_deger;
char buf[3],m[3];
int32 c;
int1 flag = false;
int pwm_duty=0,first=30,b,b1,b2,b3;

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);

setup_ccp2(CCP_PWM);       
setup_timer_2(T2_DIV_BY_16, 127, 1);

set_pwm2_duty(0);

while(true)

{

if (kbhit())

buf[0]=getch();

 buf[0]=getch();
 
 b1 = atoi(buf[0]);
 
 buf[1] = getch();
 
 b2 = atoi(buf[1]);
 
 buf[2] = getch();
 
 b3 = atoi(buf[2]);
 
 
  b = b1*100 + b2*10 + b3; */
 //if (!kbhit());
 
////////////////////////////////////////////////

delay_ms(200);
c =(b*4);
ilk_pot_deger = c;
//printf(" %lu ",ilk_pot_deger);
 
ikinci_deger = ilk_pot_deger+100;
ucuncu_deger = ilk_pot_deger+200;
dorduncu_deger = ilk_pot_deger+300;


if (buf[0]=='a')
{
//  set_pwm2_duty(0);
 

  delay_ms(100);
  kendi_degeri = read_adc();
  printf(" %lu ",ilk_pot_deger);

if ((ilk_pot_deger>kendi_degeri)&&(ilk_pot_deger-kendi_degeri<=60)&&(ilk_pot_deger-kendi_degeri>20))
  {
 
  output_low(pin_d2);
  output_low(pin_d3);
  output_high(pin_d2);
   while(first>10)
   {
   first = first-10;
   delay_ms(10);
    set_pwm2_duty(first);
   
    break;
   }
    set_pwm2_duty(first);
   
 }
 
 
 if ((ilk_pot_deger>kendi_degeri)&&(ilk_pot_deger-kendi_degeri>60))
  {
 
   output_low(pin_d2);
  output_low(pin_d3);
  output_high(pin_d2);
 
while (first<120)
  {
 
   first=first+10;
   delay_ms(10);
   
  set_pwm2_duty(first);
 
  break;
  }
 
 
 set_pwm2_duty(first);
 
 }
 
 
 if ((ilk_pot_deger-kendi_degeri<=20)||(kendi_degeri-ilk_pot_deger<=20))
 {
 set_pwm2_duty(0);
 delay_ms(50);
 output_low(pin_d3);
 output_low(pin_d2);
 
 if (!flag)
  {
    putc('r');
    flag = true;
    first=32;
    delay_ms(100);
 }
 
 }

  else

flag = false; 
///////////////////////
if ((ilk_pot_deger<kendi_degeri)&&(kendi_degeri-ilk_pot_deger<=60)&&(kendi_degeri-ilk_pot_deger>20))
  {
 
   output_low(pin_d2);
   output_low(pin_d3);
   output_high(pin_d3);
   while (first>10)
   {
   first=first-10;
   delay_ms(10);
    set_pwm2_duty(first);
   
    break;
   }
    set_pwm2_duty(first);
   
   }
 
if ((ilk_pot_deger<kendi_degeri)&&(kendi_degeri-ilk_pot_deger>60))
  {
  output_low(pin_d3);
  output_low(pin_d2);
  output_high(pin_d3);
   
while (first<120)
  {
 
   first=first+10;
   delay_ms(10);
   
  set_pwm2_duty(first);
 
  break;
  }
  set_pwm2_duty(first);
 
   }

}

/////////////////////////2.deger//////////////////////////////

if (buf[0]=='b')
{
   //set_pwm2_duty(16);
   delay_ms(100);
   ikinci_kendi_deger = read_adc();
     
if ((ikinci_deger>ikinci_kendi_deger)&&(ikinci_deger-ikinci_kendi_deger<=60)&&(ikinci_deger-ikinci_kendi_deger>20))
  {
 output_low(pin_d2);
  output_low(pin_d3);
  output_high(pin_d2);
   while (first>10)
   {
   first = first-10;
   delay_ms(10);
   pwm_duty=first;
    set_pwm2_duty(pwm_duty);
   
    break;
   }
    set_pwm2_duty(first);
   
  }

/////////////////////////////////////////////////// 
 if ((ikinci_deger>ikinci_kendi_deger)&&(ikinci_deger-ikinci_kendi_deger>60))
  {
 
  output_high(pin_d2);
  output_low(pin_d3);
  output_high(pin_d2);
 
while (first<120)
  {
 
   first=first+10;
    delay_ms(10);
   
  pwm_duty =first;
   set_pwm2_duty(pwm_duty);
 
 break;
  }
   
 set_pwm2_duty(first);

}
 ////////////////////////////////////////////////////////////
 if ((ikinci_deger-ikinci_kendi_deger<=20)||(ikinci_kendi_deger-ikinci_deger<=20))
 {
set_pwm2_duty(0);
 delay_ms(50);
 output_low(pin_d3);
 output_low(pin_d2);
 
 if (!flag)
  {
    putc('r');
    flag = true;
    first=30;
    delay_ms(100);
  }
 
 }
  else

flag = false; 
 //////////////////////-------------------------------------------------------/////////////////////////////////////
 if ((ikinci_deger<ikinci_kendi_deger)&&(ikinci_kendi_deger-ikinci_deger<=60)&&(ikinci_kendi_deger-ikinci_deger>20))
  {
   output_low(pin_d2);
   output_low(pin_d3);
   output_high(pin_d3);
   while (first>10)
   {
   first=first-10;
   delay_ms(10);
    set_pwm2_duty(first);
   
    break;
   }
    set_pwm2_duty(first);
   
  }
 
  if ((ikinci_deger<ikinci_kendi_deger)&&(ikinci_kendi_deger-ikinci_deger>60))
  {
  output_low(pin_d3);
  output_low(pin_d2);
  output_high(pin_d3);
 
  while (first<120)
  {
 
   first=first+10;
   delay_ms(10);
   set_pwm2_duty(first);
   
   break;
  }
  set_pwm2_duty(first);
 
  }

}
 //////////////////////////////3. deger////////////////////////////////////
 if (buf[0]=='c')
{
   
   delay_ms(100);
   ucuncu_kendi_deger = read_adc();
   
if ((ucuncu_deger>ucuncu_kendi_deger)&&(ucuncu_deger-ucuncu_kendi_deger<=60)&&(ucuncu_deger-ucuncu_kendi_deger>20))

 {
  output_low(pin_d2);
  output_low(pin_d3);
  output_high(pin_d2);
   while (first>10)
   {
   first = first-10;
   delay_ms(10);
    set_pwm2_duty(first);
   
    break;
   }
    set_pwm2_duty(first);
   
 }
 
 if ((ucuncu_deger>ucuncu_kendi_deger)&&(ucuncu_deger-ucuncu_kendi_deger>60))
 
  {
   output_low(pin_d2);
   output_low(pin_d3);
   output_high(pin_d2);
 
while (first<120)
  {
 
   first=first+10;
   delay_ms(10);
   
  pwm_duty =first;
  set_pwm2_duty(pwm_duty);
 
  break;
  }
 
 set_pwm2_duty(first);
   
  }
 if ((ucuncu_deger-ucuncu_kendi_deger<=20)||(ucuncu_kendi_deger-ucuncu_deger<=20))
 {
 set_pwm2_duty(0);
 
  output_low(pin_d3);
 output_low(pin_d2);

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

flag = false;

 if ((ucuncu_deger<ucuncu_kendi_deger)&&(ucuncu_kendi_deger-ucuncu_deger<=60)&&(ucuncu_kendi_deger-ucuncu_deger>20))
  {
   output_low(pin_d2);
   output_low(pin_d3);
   output_high(pin_d3);
   while (first>10)
   {
   first=first-10;
   delay_ms(10);
    set_pwm2_duty(first);
   
    break;
   }
    set_pwm2_duty(first);
   
  }
 
  if ((ucuncu_deger<ucuncu_kendi_deger)&&(ucuncu_kendi_deger-ucuncu_deger>60))
  {
 //  set_pwm2_duty(0);
    output_low(pin_d3);
    output_low(pin_d2);
    output_high(pin_d3);
 
  while (first<120)
  {
 
   first=first+10;
   delay_ms(10);
   
 
  set_pwm2_duty(first);
 
  break;
  }
  set_pwm2_duty(first);
 
  }
 
}
//////////////////////////4. deger/////////////////////////////////////////

if (buf[0]=='d')
{
 
   //set_pwm2_duty(0);
   delay_ms(100);
   dorduncu_kendi_deger = read_adc();
       
if ((dorduncu_deger>dorduncu_kendi_deger)&&(dorduncu_deger-dorduncu_kendi_deger<=60)&&(dorduncu_deger-dorduncu_kendi_deger>20))

  {
   output_low(pin_d2);
   output_low(pin_d3);
   output_high(pin_d2);
   while (first>10)
   {
   first=first-10;
   delay_ms(10);
    set_pwm2_duty(first);
   
    break;
   }
    set_pwm2_duty(first);
   
  }
 
  if ((dorduncu_deger>dorduncu_kendi_deger)&&(dorduncu_deger-dorduncu_kendi_deger>60))
 
  {
 
  output_low(pin_d2);
  output_low(pin_d3);
   output_high(pin_d2);
 
while (first<120)
  {
 
   first=first+10;
   delay_ms(10);
   
     
  set_pwm2_duty(first);
 
  break;
  }
   
 set_pwm2_duty(first);
 
  }
 
 if ((dorduncu_deger-dorduncu_kendi_deger<=20)||(dorduncu_kendi_deger-dorduncu_deger<=20))
 {
 
 output_low(pin_d3);
 output_low(pin_d2);

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

flag = false;

 if ((dorduncu_deger<dorduncu_kendi_deger)&&(dorduncu_kendi_deger-dorduncu_deger<=60)&&(dorduncu_kendi_deger-dorduncu_deger>20))
 
  {
    output_low(pin_d2);
   output_low(pin_d3);
   output_high(pin_d3);
   while (first>10)
   {
    first=first-10;
    delay_ms(10);
    set_pwm2_duty(first);
   
    break;
   }
    set_pwm2_duty(first);
       
   }
 
  if ((dorduncu_deger<dorduncu_kendi_deger)&&(dorduncu_kendi_deger-dorduncu_deger>60))
 
  {
   output_low(pin_d3);
   output_low(pin_d2);
   output_high(pin_d3);
 
   while (first<120)
   {
 
    first=first+10;
    delay_ms(10);
   
    set_pwm2_duty(first);
   
    break;
   }
    set_pwm2_duty(first);
   
  }
 
}

 }



firstly I want to take three-digit numbers later I want to equal ilk_pot_deger this three-digit numbers what I take numbers. then I want to take action that according to a,b,c,d ...
pwm is working no problem
pic takes a,b,c,d from pc no problem
just problem is can not take three digit numbers(for example 120, 130, 140)

best regards
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Jun 23, 2010 3:56 am     Reply with quote

You are still not understanding what a C string is. You also have an error in your code, you also require other inputs other than a number such as the letter 'a'.

Code:

char buf[3],m[3];
buf is NOT big enough for a 3 digit input, you need to allocate enough space for the 3 digits and the null terminating char!

char buf[4], m[3];


This next bit is all wrong, If you indent your code properly you will see that the if only covers the next line, you are missing braces!
Code:

if (kbhit())
  buf[0]=getch();

buf[0]=getch();
b1 = atoi(buf[0]);
buf[1] = getch();
b2 = atoi(buf[1]);
buf[2] = getch();
b3 = atoi(buf[2]);


Also, this code does not account for the fact that you also require the letters 'a', 'b', 'c' and 'd' to be pressed

Your easiest option is:-
Code:

buf[0] = 0; // This is to prevent certain routines to be continually run later on in your code (unless you expect them to be)

if (kbhit())
{
  buf[0] = getc();
  if ((buf[0] >= '0') || (buf[0] <= '9'))
  {
    buf[1] = getc();
    buf[2] = getc();
    buf[3] = '\0';
    b = atoi(buf);
  }
}


There are problems with this, you MUST enter 3 digits once you press the first digit. So if you only wanted to enter 24 you would have to enter 024.

If anything else is entered other than the last 2 digits then atoi will give incorrect value.

I would also look at the following to see if there is an improvement in the assembly created other than using atoi.
Code:

char buf[3];


buf[0] = 0;

if (kbhit())
{
  buf[0] = getc();
  if ((buf[0] >= '0') || (buf[0] <= '9'))
  {
    buf[1] = getc();
    buf[2] = getc();
    b = ((buf[0] - '0') * 100) + ((buf[1] - '0') * 10) + (buf[2] - '0');
  }
}


But then again I would do it totally differently Smile requiring enter to be pressed when entering the number and not stopping the code from running while it is being entered!
capella



Joined: 07 Feb 2009
Posts: 38

View user's profile Send private message

PostPosted: Wed Jun 23, 2010 6:27 am     Reply with quote

thanks your suggestion I tried what you said.
but can you please explain your last sentence with more details. So I can understand it my language is not good enough to understand your sentence.

But I apply your suggestion as that
Code:

while(true)

{
buf[0] = 0;
if (kbhit())
{
  buf[0] = getch();
  if ((buf[0] >= '0') || (buf[0] <= '9'))
  {
    buf[1] = getch();
    buf[2] = getch();
    b = ((buf[0] - '0') * 100) + ((buf[1] - '0') * 10) + (buf[2] - '0'); 
    delay_ms(100);
   c =b;
  ilk_pot_deger = c;
 
  ikinci_deger = ilk_pot_deger+100;
  ucuncu_deger = ilk_pot_deger+200;
  dorduncu_deger = ilk_pot_deger+300;

  }
}


if (buf[0]=='a')
{

when I send 129,417 like this and then I send a,b,c,d .It is not working code. What I do wrong???
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 23, 2010 7:11 pm     Reply with quote

What is the overall purpose of your project ? Tell us what external
devices are controlled by the PIC.

Describe the user interface. Is it a terminal window on your PC,
(such as Hyperterminal or TeraTerm) in which you type numbers
and commands ?

Give a full description of the list of commands that you want to
be able to type into the terminal (and then be sent to the PIC board).
Tell us what each command does. Make a list and describe each
command. Example:

a: Starts the program running.
b: Stops the program temporarily.
c: Tells the program to accept new numbers, which will be typed in next.

Those may not be your real commands. Tell us what the real commands
are, in a list, just like I have shown above.

Tell us if you only press the single letter 'a' (for example) as a command,
or do you press 'a', and also press the Enter key after it ?

Explain if you type in some values initially, before your main code
starts running (such as some setup numbers). Or, are all numbers
to be typed in while the program is running ?

Tell us if the program must keep running while it checks for new
commands, or is it allowed for the program to stop for several seconds
(or longer) while a new number or command is typed in and sent ?

Quote each one of the above paragraphs separately in your reply, and
answer below each one. That way, it will be easier for us to understand
your replies.
capella



Joined: 07 Feb 2009
Posts: 38

View user's profile Send private message

PostPosted: Thu Jun 24, 2010 12:19 am     Reply with quote

PCM programmer wrote:
What is the overall purpose of your project ? Tell us what external
devices are controlled by the PIC.

Overall purpose is that I used pc program what I do. My program sends
string via comport to pic. Firstly pc program send where the pic begins to work. For this I purpose to send three digit value where the pic start with pc program via comport. So pic is connected a potentiometer. Then I send a position with pc what is pic contains (It can be four different positions). Then pic analyzed position and it begin to use pwm signal to 24 volt dc motor. Important thing motor play with potentiometer together. So motor's speed start stop is adjusted by potentiometer.

PCM programmer wrote:

Describe the user interface. Is it a terminal window on your PC,
(such as Hyperterminal or TeraTerm) in which you type numbers
and commands ?

I used pc program when I click button my program. It just send string. In reality I won't use hyperterminal and like this. But I can use a little time for test.

PCM programmer wrote:

Give a full description of the list of commands that you want to
be able to type into the terminal (and then be sent to the PIC board).
Tell us what each command does. Make a list and describe each
command. Example:

a: Starts the program running.
b: Stops the program temporarily.
c: Tells the program to accept new numbers, which will be typed in next.

Those may not be your real commands. Tell us what the real commands
are, in a list, just like I have shown above.

Tell us if you only press the single letter 'a' (for example) as a command,
or do you press 'a', and also press the Enter key after it ?

Explain if you type in some values initially, before your main code
starts running (such as some setup numbers). Or, are all numbers
to be typed in while the program is running ?

I just send string. Firstly I send three digit numbers but it sends as a string. A short time later for example 2 seconds later, I just send single a and then single b and then single c and then single d and finished. So motor will go 4 different position according to three digit number,

PCM programmer wrote:

Tell us if the program must keep running while it checks for new
commands, or is it allowed for the program to stop for several seconds
(or longer) while a new number or command is typed in and sent ?

When I started the last my program. When I send three digit numbers to pic. Pic do anything later I try to hyperterminal program. Firstly I send a number and then I pressed single 'a'. Pic do anything and I press single 'a' many times. Pic says 0.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Jun 24, 2010 1:49 am     Reply with quote

LOL, just spotted an error in my code Smile

if ((buf[0] >= '0') || (buf[0] <= '9'))

should be

if ((buf[0] >= '0') && (buf[0] <= '9'))

otherwise it will allways be true.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Jun 24, 2010 1:54 am     Reply with quote

I have also noted you use the value 417 this would mean you either use atol or my routine for getting the number BUT you need to make b at least an in16.

int16 b;

b = (buf[0] * 100) + (buf[1] * 10) + buf[0];
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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