View previous topic :: View next topic |
Author |
Message |
saad
Joined: 07 Nov 2014 Posts: 2
|
QEI issue. |
Posted: Fri Nov 07, 2014 5:43 pm |
|
|
hi!
I am using two pic 18f2431 and using rs232 for communication between two pic.
I am controlling motor which is coupled with encoder and I am using qei interface, which is working fine.
What i have to do is to send a character from one pic when second pic receive the character motor will run and should stop from the feed back of encoder.
The character is sent and received and motor also starts running but will not stop from feedback.
Code: | #include <recieve.h>
int8 a;
int16 count;
int16 max_count;
int16 value;
void main()
{
setup_qei( QEI_MODE_X4_RESET_ON_MATCH | QEI_VELOCITY_MODE_DISABLED ,QEI_FILTER_ENABLE_QEB |QEI_FILTER_ENABLE_QEA |QEI_FILTER_DIV_1,3000);
while(TRUE)
{ count=qei_get_count(QEI_GET_POSITION_COUNT );
a=getch();
if(a=='f')
if(count<1600)
{output_high(IN1); output_low(IN2);}
else if (a=='r'){output_low(IN1);output_high(IN2);}
else if (a=='s'){output_low(IN1);output_low(IN2);}
else {output_low(IN1);output_low(IN2);}
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Sat Nov 08, 2014 2:04 am |
|
|
Lay the code out sensibly, and you may see what is causing the problem....
Think. what happens at the getch. What is the count value likely to be when this is reached. Will it ever reach the later tests?. |
|
|
saad
Joined: 07 Nov 2014 Posts: 2
|
|
Posted: Sat Nov 08, 2014 5:24 am |
|
|
when 'f' is recieved , the count<1600 coundition become true and motor starts running but when while loop stats second time the (a=='f') condition become false and it will not check the (count<1600) condition ...
i want something when (getch()=='f') become true...motor will run until the count<1600 become false. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Sat Nov 08, 2014 6:15 am |
|
|
just a couple of comments on your coding 'style'.
It'd be very helpful if you created functions for 'forward', 'reverse' and 'stopped'. You'll find it a LOT easier a day or week from now when you wonder what does '{output_low(IN1);output_high(IN2)' really do !
Also IN1 to me means an INput not an output direction.
CCS does allow long variable names and the better you can 'self describe' variables the easier your code is to understand.
And.. though you do not show your UART setup, be sure to include 'errors' in the USE RS232(...options...) ! Without it, any hardware UART will 'stall' do to overrun(too many incoming characters) condition.
You might like to read up on the 'switch' statement. Using it would 'clean up' your 'if..then.else...else..else' style of coding into an easier to read,decode and expand style.
hth
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Sat Nov 08, 2014 1:03 pm |
|
|
saad wrote: | when 'f' is recieved , the count<1600 coundition become true and motor starts running but when while loop stats second time the (a=='f') condition become false and it will not check the (count<1600) condition ...
i want something when (getch()=='f') become true...motor will run until the count<1600 become false. |
You are missing the point. count is read right at the start. getch, then sits and waits for a key, _and count does not update_.
You need to rethink your approach (and your coding style). The patterns for the pins, could be a #define, or a function, named to make 'what they do' obvious.
I used to have a guy who taught me, who said he would immediately reject any code, that was not either at least 50% comments, or where the names used were not themselves a description of what they did. |
|
|
|