|
Open the Simulator in a new tab or window.
Click the "Gray" button and select Port C.

Copy and paste this example code.
Assemble and run the code with a clock frequency of 15Hz or more.
Lower clock rates might allow the mechanism to overshoot before the code detects its position.
Please answer the related reviseOmatic questions.
- 0x3F is copied to TRISC. This configures the PORTC inputs and outputs.
- 0x3F corresponds to OO I I I I I I where O is an output and I is an input.
- If bit 7 of PORTC is high, the motor drives the mechanism left.
- If bit 6 of PORTC is high, the motor drives the mechanism right.
- Bit 0 is the least significant bit of the Gray code pattern.
- Bit 5 is the most significant bit of the Gray code pattern.
- ANDW 0X3F This is a bit mask used to ignore bits 6 and 7
- SUBW 0X0F If W contains 0x3F, this subtraction leaves zero in W and the Z Flag is set.
- JPZ will jump, only if the Z Flag is set.
TASK:
Alter the code so the mechanism goes apporximately to the half way mark and back.
Add code to make the mechanism reverse if it reaches the left or right hand ends of the scale.
; ================================
; ===== GRAY CODE CONTROLLER =====
; ===== CONNECT TO PORTC =========
; ================================
MOVW 0X3F
MOVWR TRISC
FORWARD:
MOVW 0X40 ; Set bit 6
MOVWR PORTC
F_REP:
MOVRW PORTC
ANDW 0X3F
SUBW 0X0F
JPZ BACKWARD
JMP F_REP
BACKWARD:
MOVW 0X80 ; Set bit 7
MOVWR PORTC
B_REP:
MOVRW PORTC
ANDW 0X3F
SUBW 0X03
JPZ FORWARD
JMP B_REP
|