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 support@ccsinfo.com

QEI issue.

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



Joined: 07 Nov 2014
Posts: 2

View user's profile Send private message

QEI issue.
PostPosted: Fri Nov 07, 2014 5:43 pm     Reply with quote

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: 19448

View user's profile Send private message

PostPosted: Sat Nov 08, 2014 2:04 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Nov 08, 2014 5:24 am     Reply with quote

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: 9204
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Nov 08, 2014 6:15 am     Reply with quote

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: 19448

View user's profile Send private message

PostPosted: Sat Nov 08, 2014 1:03 pm     Reply with quote

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.
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