- EasyPIC Fusion v7 Pro
- MikroPascal Compiler
- A quadrature AB encoder
In my pursuit of using the VTFT, quadrature AB encoder I ran into the following quirks / pitfalls.
- How do I go about correcting the timing scale for the generic Delay functions (Delay_ms, Delay_us, etc...). I noticed that the timing scale was off when casually using the Delay_ms(X) function. I am aware that for the timer interrupt there is a calculator for the the TMR1 register values to correct against different clock frequencies. Do I need to do anything in particular to do this same thing for the built-in library delay functions?
- I downloaded a package from LibStock in the hopes to have starting point when using the quadrature AB encoder. I found the following code:
Code: Select all
unit Rotary_Encoder; // interface var Rotary_Encoder_A : sbit; external; sfr; // to be defined in the using program Rotary_Encoder_B : sbit; external; sfr; // to be defined in the using program // Rotary encoder output: // Rotary_Direction: byte; // output result, to be reset to zero by the using program { 0: no rotation 1: left rotation 2: right rotation } Procedure Init_Rotary_Encoder; // To be called once before calling Check_Rotary_Encoder procedure Check_Rotary_Encoder; // To be called every 1 millisecond. The result is in variable Rotary_Direction, see above implementation var Rotary_State : byte; { 0: both lines are 0 1: both lines are 1 2: both lines differ } Procedure Init_Rotary_Encoder; begin Rotary_Direction := 0; // no rotation detected Rotary_State := 2; // for safety end; procedure Check_Rotary_Encoder; var Rotary_Val : byte; begin Rotary_Val := Rotary_Encoder_A + (Rotary_Encoder_B shl 1); // bit 0 = A, bit 1 = B case Rotary_val of 0, 3: // both A and B are the same (0,0 or 1,1) begin Rotary_State := Rotary_Val.0; end else // A and B differ, A changed or B changed... begin if (Rotary_State <> 2) then // no detecion ongoing begin if Rotary_Val.0 <> Rotary_State // A changed? then Rotary_Direction := 1 // clockwise rotation, A changed before B else Rotary_Direction := 2; // Counter clockwise rotation, B Changed before A Rotary_State := 2; end; end; end; end; end.
In addition to the previous implementation, the following example is given:However, I get an expected "end" but "begin" found error.Code: Select all
unit Rotary_Encoder_Example; uses Rotary_Encoder; var Counter: byte; // Rotary Encoder connections to the PIC Rotary_Encoder_A : sbit at RG6_bit; Rotary_Encoder_B : sbit at RG7_bit; //------------------------------------------------------------------------------ //Timer1 //Prescaler 1:8; PR1 Preload = 25000; Actual Interrupt Time = 1 ms procedure InitTimer1; begin T1CON := 0x8010; T1IP0_bit := 1; T1IP1_bit := 1; T1IP2_bit := 1; T1IF_bit := 0; T1IE_bit := 1; PR1 := 25000; TMR1 := 0 end; procedure HW_Init; // Minimal Hardware initialisation for Debug begin TRISG6_bit := 1; // make input TRISG7_bit := 1; // make input // Initialise the rotary encoder detection Counter := 0; Init_Rotary_Encoder; // initialise timer InitTimer1; // Turn on all Interrupt handling in general (last thing to be done) TMR1 := 1; // Timer1 Interrupt enable INTCON.PEIE := 1; // Peripheral Interrupts enable INTCON.GIE := 1; // Interrupts global einschalten end; //------------------------------------------------------------------------------ // Interrupt procedure //------------------------------------------------------------------------------ procedure Timer1Interrupt(); iv IVT_TIMER_1; ilevel 7; ics ICS_SRS; begin T1IF_bit := 0; begin Check_Rotary_Encoder; // check the rotary encoder inputs end; end; begin // Init Hardware for minimal functionality (debugging) HW_Init; while (True) do begin // Rotary encoder if Rotary_Direction > 0 then begin case Rotary_Direction of 1: // inc(Counter); 2: // dec(Counter); end; byteToStr(Counter, Strng); Rotary_Direction := 0; // reset to zero after usage end; end; // while true ... end;
Error Image: [img] [/img]
If I place a
above the InitTimer1 proceedure then the compiler stops complaining, but I am not sure why I would need andend.
above the InitTimer1 proceedure.end.
Any help would be appreciated!
Thanks in advanced,
ckelley