SP.711: "Homework Two" ... Due Next Monday, 10/23 For next Monday, write C code to read a quadrature encoder plugged into pins RC4 and RC5. Your code should look for changes in position, figure out whether the motor is going forward or backwards, increment or decrement a "position" variable, and output this value to the RB0-RB7 pins so that your other PIC will be able to read it. Attached below are a review of quadrature encoding, and some sample code to solve a simpler form of the problem: How to keep track of position if the motor only goes in one direction (if you don't read through the sample code below, at least look at the first line, that begins "#byte", this shows you how to output data to all 8 RB pins at the same time, without having to write tons of little output commands) (ask us if you have any questions) **************************************************************** Quick review of quadrature encoding: The quadrature sensor unit on the motor outputs two 0 to 5 Volt square waves, 90 degrees out of phase. So if the motor is going "forward", the waveforms might look like this: --------------+ +------------- V1 | | +--------------------+ ----+ +--------------------+ V2 | | | +--------------------+ +-- And if going "in reverse", the waveforms might look like this: (these look similar, but the difference between forward and reverse is which waveform changes first from any given state) --------------+ +------------- V1 | | +--------------------+ +--------------------+ +--- V2 | | | ---+ +--------------------+ ******************************************************************* // sample position-reading code #byte position 0x06 // this is a special command that associates the variable "position" // with the RB0-RB7 pins... whenever you set position to equal some // value (from 0 to 255), the pins RB0 to RB7 will automatically // be set to the right bits to be a binary representation of this // value. If this is confusing, ask us, or don't worry, it just // works. #define encoder1 PIN_C4 void main() { short last_encoder_state = 0; // last time we read the encoder, it was this position = 128; // position can be from 0 to 255, and let's assume // we start somewhere in the middle while (1) { x = input(encoder1); if (x != last_encoder_state) { // the encoder has changed! we must have moved! last_encoder_state = x; position++; //shorthand for position = position + 1 } // otherwise, the encoder has not changed, so do nothing... } } // // note: one PROBLEM with the above code, that you may // want to think of a way to deal with in your code, // is that the encoders typically generate 100-500 // ticks per revolution of the motor, so the "position" // variable will quickly overflow and wrap around. // is there some way to only increment the position variable // every time (10,100,1000?) ticks go by? // -----------------------------------------------------------