Microcontroller Simulator Task 8 - Electronics Tutorials

Open the Simulator in a new tab or window.

Click the "Gray" button and select Port C.

Gray Code

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

Topics:

01-nop-jmp   02-LEDs   03-Heater   03-Tlight   04-SevSeg   05-Delay   06-H-Bridge   07-Interrupts   08-Gray   09-Stepper   doc   hex-num   index  

Flash:

sim  

PDF:

Microcontroller Simulator  

DOC:

Microcontroller Simulator   Microcontroller Simulator.   Microcontroller with Harvard Architecture  

Images:

architecture   assem-H-Bridge   assem-Heater-FC   assem-Heater   assem-Stepper   assem-TLIGHT-grid   Gray   pinout   sim-leds   sim-sev-seg-design   sim-sev-seg   sim-traffic  

 

Privacy Policy, © Copyright, Contact Information and Disclaimers

Site highly rated by Schoolzone.co.uk

Valid XHTML 1.0 Transitional

Valid CSS!