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

Need help with ADC and quadrature encoder code

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



Joined: 10 Apr 2013
Posts: 1

View user's profile Send private message

Need help with ADC and quadrature encoder code
PostPosted: Wed Apr 10, 2013 3:06 pm     Reply with quote

Hi I have a project I'm working which requires very low frequency <1hz.
I have two photosensors which output analog into the input pins of a 16f1937 and these sensors will be controlling the motor I have. I have an Hbridge controlling the motor.

I have limited knowledge of C and CCS, and all the examples of ADC I've found are for much higher frequencies, so I'm not sure how to approach using such a low frequency.

For the encoder I was thinking about using this code posted by Ttelmah and modifying it to my needs. But I need to solve the ADC problem first.

Code:
signed int32 position;
#INT_RB
void quad(void) {
   static int old;
   static int new;
   static int value;
   //Here I have an edge on one of the quadrature inputs
   new=portb;
   /*Now I have to decode the quadrature changes. There are four
   possibilities:
   I can have a rising or falling edge, on each of the two inputs. I have to
   look at the state of the other bit, and increment/decrement according to
   this. /*
   value=new^old;
   //'value', now has the bit set, which has changed
   if (value & 0x10) {
      //Here the low bit has changed
      if (new & 0x10) {
         //Here a rising edge on A
         if (new & 0x20) --position;
         else ++position;
      }
      else {
         //Here a falling edge on A
         if (new & 0x20) ++position;
         else --position;
      }
   }
   else {
      //Here the high bit (B) must have changed
      if (new & 0x20) {
         //Here a rising edge on B
         if (new & 0x10) ++position;
         else --position;
      }
      else {
         //Here a falling edge on B
         if (new & 0x10) --position;
         else ++position;
      }
   }
   old=new;
}


My project is basically a baby swing but for adults. So I need the encoder to change the direction of the motor after a certain distance is traveled.
I think this program determines the direction the motor is spinning by looking at the sign of position(??). I was thinking of adding interrupts to check the value of position and changing the direction if position is above a certain value.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Apr 10, 2013 7:19 pm     Reply with quote

You can always set the ADC to sample as slow as you want.

Just because it takes some number of microseconds to get the sample doesn't mean you have to sample as fast as it can between samples.

I've written apps that sample once a minute.

Your code doesn't reflect anything that does A/D sampling but rather only a portB interrupt routine.

Remember to read the PIC101 Sticky at the top of this forum.


Cheers,

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
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