|
|
View previous topic :: View next topic |
Author |
Message |
LEVENT
Joined: 04 May 2006 Posts: 16
|
:shock: |
Posted: Wed May 17, 2006 2:21 am |
|
|
hi everbody..
I want to control a motor.In my project,I could write a program like that : Code: |
void main()
{
int num1, num2, num3;
static byte value;
long move;
int16 cm;
char yon;
//--------------------------------------
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
//--------------------------------------
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1, 999, 1);
output_low(pin_d1);
.....
.....
printf("DISTANCE(cm) : \n\r" );
num1 = getch()&0x0F;
delay_ms(1);
num2 = getch()&0x0F;
delay_ms(1);
num3 = getch()&0x0F;
printf("INIT...\n\r");
cm = (num1*100) + (num2*10) + num3;
move = (long)cm*4;
printf("move = \n\r",move);
printf(" 0- BACKWARD \n\r");
printf(" 1- FORWARD \n\r");
yon = getch();
motor_sur(value, move, yon);
.....
.....
}
motor_sur(byte value, long move, char yon)
{
long count;
switch(yon)
{
case '0': //----motor forward------
output_low(pin_d0);
break;
case '1': //-----motor backward-------
output_high(pin_d0);
break;
}
setup_ccp1(ccp_pwm);
value = read_adc();
set_pwm1_duty(value);
while(count <= move) //move motor
{
if(input(pin_a4)); // if pulse change low to high
{
count =count + 1;
while(input(pin_a4)); // while pulse is high
delay_us(1);
while(!input(pin_a4)); //while pulse is low
delay_us(1);
}
setup_ccp1(ccp_pwm);
value = read_adc();
set_pwm1_duty(value);
}
setup_ccp1(ccp_off);
}
|
As you see,The motor's distance controlled by user which distance (in cm) can send by RS232 (for exmp. 200cm)...And I read pulses from motor and control the motor's running time. This program is not run perfectly...Because the move value is not correct and I can't write this number on the pc screen with printf() function.I can't accept 3digit number at one times.Can anybody help me for this problems?? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 17, 2006 10:12 am |
|
|
Quote: |
int num1, num2, num3;
static byte value;
long move;
int16 cm;
char yon; |
It looks like you're trying to experiment with CCS data types by using
one of each different type. You shouldn't do that, because it makes the
program confusing to read. In CCS, the data types of "byte", "int", "int8"
and "char" are all the same. They are all 8-bit unsigned integers. Get
rid of all the different declarations for 8-bit values and just use one of
them. I suggest using "int8". Also, "int16" is the same as "long". Both
are unsigned 16-bit integers.
Quote: | cm = (num1*100) + (num2*10) + num3; |
In the code above, all of your "num" values are only 8 bits, but
the first two intermediate results are 16-bit values. So your code
will cause the MSB of each intermediate result to be discarded.
You can fix this by declaring the "num" variables as int16, or you
can cast them to 16-bit values in the equation. Example:
Code: | cm = ((int16)num1*100) + ((int16)num2*10) + (int16)num3; |
|
|
|
Ttelmah Guest
|
|
Posted: Wed May 17, 2006 10:18 am |
|
|
As a seperate 'comment', perhaps also post with a title that reflects your problem. I hadn't looked at this post thinking 'junk', and I suspect quite a few of the other peple who post replies have done the same. It was only when I saw that a reply had been posted by one of the respected posters here (PCM programmer), that I bothered to look...
Best Wishes |
|
|
|
|
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
|