Post by tegwyntwmffat on Jun 20, 2019 11:54:49 GMT
Hello!
I've got pins 8 and 9 tied up with other processes and so want to use pins 2 and 3 instead.
I've got the following code working on 8 and 9 with an AMT102 encoder but how do I adapt it for 2 and 3?
Thanks!
I've got pins 8 and 9 tied up with other processes and so want to use pins 2 and 3 instead.
I've got the following code working on 8 and 9 with an AMT102 encoder but how do I adapt it for 2 and 3?
Thanks!
/*** Don't worry, the normal Arduino setup() and loop() are below this block! ***/
#include "IfxGpt12_reg.h"
/* LMU uninitialised data */
StartOfUninitialised_LMURam_Variables
/* Put your LMU RAM fast access variables that have no initial values here e.g. uint32 LMU_var; */
EndOfUninitialised_LMURam_Variables
/* LMU uninitialised data */
StartOfInitialised_LMURam_Variables
/* Put your LMU RAM fast access variables that have an initial value here e.g. uint32 LMU_var_init = 1; */
EndOfInitialised_LMURam_Variables
/* If you do not care where variables end up, declare them here! */
volatile uint8 Port00_Sample = 0;
uint32 volatile PinIntFunc2_var = 0;
uint32 volatile PinIntFunc3_var = 0;
bool volatile Direction;
uint16 volatile EncoderCount;
void ReadPort00(void);
void PinIntFunc2(void);
void PinIntFunc3(void);
void QuadEncoderOut(void);
void IncrEnc_Init(void);
/*** Core 0 ***/
void setup() {
// put your setup code for core 0 here, to run once:
/* Call a function every 100us */
CreateTimerInterrupt(ContinuousTimerInterrupt, 10000, ReadPort00);
IncrEnc_Init();
}
/* Setup incremental (quad) encoder input on D8 Inc_A, D9, Inc_B, D51 (zero position index) */
void IncrEnc_Init(void)
{
/* Clear Einit protection */
IfxScuWdt_clearCpuEndinit(IfxScuWdt_getCpuWatchdogPassword());
/* If GTM is disabled then enable it */
while(GPT120_CLC.B.DISS == 1u)
{
GPT120_CLC.B.DISR = 0u;
}
/* Set Einit protection */
IfxScuWdt_setCpuEndinit(IfxScuWdt_getCpuWatchdogPassword());
/* Set P2.6/7/8 to input mode */
P02_IOCR4.B.PC6 = 0x00u; /* Input mode IOP_U_A, ShieldBuddy pin D8 */
P02_IOCR4.B.PC7 = 0x00u; /* Input mode IOP_U_B, ShieldBuddy pin D9 */
P02_IOCR8.B.PC8 = 0x00u; /* Input mode T4IN, , ShieldBuddy pin D51 */
/* Configure pin inputs to GPT12 */
GPT120_PISEL.U = 0x00u; /* Set all pin input options to default (T3INA, T3EUDA, T4INA) */
/* Set up GPT120 Encoder Input Mode */
GPT120_T3CON.U = 0x0000u; /* Default configuration */
GPT120_T3CON.B.BPS1 = 0x00u; /* T3 runs at 2.5MHz */
GPT120_T3CON.B.T3M = 0x7u; /* Interrupt on edge detect mode (not used) */
GPT120_T3CON.B.T3UD = 1u;
GPT120_T3CON.B.T3I = 3u; /* Any transition (rising or falling edge) on any Tx input (TxIN or TxEUD). */
GPT120_T4CON.B.CLRT3EN = 1u; /* Reset T3 using mechanical zero position input (T4IN, P2.8) */
/* Start encoder position monitoring */
GPT120_T3CON.B.T3R = 1u ;
}
void loop() {
// put your main code for core 0 here, to run repeatedly:
Direction = GPT120_T3CON.B.T3RDIR;
/* Get position count from Timer3 */
EncoderCount = (uint16)GPT120_T3.U;
}
/* Entered when pin D2 changes state */
void PinIntFunc2(void)
{
PinIntFunc2_var++;
}
/* Entered when pin D2 changes state */
void PinIntFunc3(void)
{
PinIntFunc3_var++;
}
/* Entered every 100us */
void ReadPort00(void)
{
/* Read directly Port00 bit 0 to 7. This is J406 PIN25 to PIN39 */
Port00_Sample = (uint8)P00_IN.U;
}
/*** Core 1 ***/
/* CPU1 Uninitialised Data */
StartOfUninitialised_CPU1_Variables
/* Put your CPU1 fast access variables that have no initial values here e.g. uint32 CPU1_var; */
EndOfUninitialised_CPU1_Variables
/* CPU1 Initialised Data */
StartOfInitialised_CPU1_Variables
/* Put your CPU1 fast access variables that have an initial value here e.g. uint32 CPU1_var_init = 1; */
uint32 LEDcount = 0;
uint32 Speed = 4;
uint32 QuadState = 0;
uint32 ChannelA = 0;
uint32 ChannelB = 0;
EndOfInitialised_CPU1_Variables
/** Use core 1 to simulate an encoder ***/
void setup1() {
// put your setup code for core 1 here, to run once:
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(13,OUTPUT); // LED
// Period = 1000000u => 10ms period
CreateTimerInterrupt0_Core1(ContinuousTimerInterrupt, 100000u , QuadEncoderOut);
}
void loop1() {
// put your main code for core 1 here, to run repeatedly:
}
/* Simulate a quad encoder on D2 and D3 */
void QuadEncoderOut(void)
{
switch(QuadState)
{
case 0:
digitalWrite(13, (LEDcount & 0x01));
LEDcount++;
digitalWrite(2,1);
QuadState = 1;
break;
case 1:
digitalWrite(3,1);
QuadState = 2;
break;
case 2:
digitalWrite(2,0);
QuadState = 3;
break;
case 3:
digitalWrite(3,0);
QuadState = 0;
break;
}
}
/*** Core 2 ***/
/* CPU2 Uninitialised Data */
StartOfUninitialised_CPU2_Variables
/* Put your CPU2 fast access variables that have no initial values here e.g. uint32 CPU2_var; */
EndOfUninitialised_CPU2_Variables
/* CPU2 Initialised Data */
StartOfInitialised_CPU2_Variables
/* Put your CPU2 fast access variables that have an initial value here e.g. uint32 CPU2_var_init = 1; */
EndOfInitialised_CPU2_Variables
void setup2() {
// put your setup code for core 2 here, to run once:
}
void loop2() {
// put your main code for core 2 here, to run repeatedly:
}