char old = 0;
void Encoder(void)
{
int new = 0 ;
int value = 0 ;
new = PORTE ; //// encoder pin here PORTE PIN 6 ,PIN7
value = new ^ old ;
if (value & 0x40)
{
if(new & 0x40)
{
mStepper_B_Enb(); // STEPPER ENABLE
if(new & 0x80)
{
mStepper_B_For(); // STEPPER FORWARD
mStepper_B_Fourth();
}
else
{
mStepper_B_Rev(); // STEPPER REVERSE
mStepper_B_Fourth();
}
mStepper_B_Off();
Delay_us(25);
mStepper_B_On(); // STEPPER ON
}
}
old = new ;
return ;
}
main()
{
while(1)
{
Encoder();
}
}
char old = 0;
void Encoder(void)
{
int new = 0 ;
int value = 0 ;
new = PORTG ; //// encoder pin here PORTG PIN 2 ,PIN3
value = new ^ old ;
if (value & 0x04)
{
if(new & 0x04)
{
mStepper_B_Enb(); // STEPPER ENABLE
if(new & 0x08)
{
mStepper_B_For(); // STEPPER FORWARD
mStepper_B_Fourth();
}
else
{
mStepper_B_Rev(); // STEPPER REVERSE
mStepper_B_Fourth();
}
mStepper_B_Off();
Delay_us(25);
mStepper_B_On(); // STEPPER ON
}
}
old = new ;
return ;
}
main()
{
while(1)
{
Encoder();
}
}
mStepper_B_For();
mStepper_B_Rev();
mStepper_B_Fourth();
what these functions do?
post the codes of these functions.
#define S_2_Pulse BIT_10
#define S_2_Dir BIT_9
#define S_2_Enb BIT_8
#define S_2_MS1 BIT_11
#define S_2_MS2 BIT_12
#define S_2_Opto BIT_13
#define mInitStepperB() (mPORTBSetPinsDigitalOut(S_2_Pulse | S_2_Dir | S_2_Enb | S_2_MS1 | S_2_MS2),\
mPORTBSetPinsDigitalIn(S_2_Opto),\
mPORTBClearBits(S_2_Pulse | S_2_Dir | S_2_Enb))
#define mStepper_B_On() (LATBSET = S_2_Pulse)
#define mStepper_B_Off() (LATBCLR = S_2_Pulse)
#define mStepper_B_For() (LATBCLR = S_2_Dir)
#define mStepper_B_Rev() (LATBSET = S_2_Dir)
#define mStepper_B_Enb() (LATBCLR = S_2_Enb)
#define mStepper_B_Dis() (LATBSET = S_2_Enb)
#define mStepper_B_Half() (LATBCLR = S_2_MS1, LATBSET = S_2_MS2)
#define mStepper_B_Fourth() (LATBSET = S_2_MS1, LATBCLR = S_2_MS2)
#define mStepper_B_Eighth() (LATBSET = S_2_MS1, LATBSET = S_2_MS2)
#pragma config FPLLMUL = MUL_20 // PLL Multiplier
#pragma config FPLLIDIV = DIV_2 // PLL Input Divider
#pragma config FPLLODIV = DIV_1 // PLL Output Divider
#pragma config FPBDIV = DIV_1 // Peripheral Clock divisor
#pragma config FSRSSEL = PRIORITY_7 // SRS Select
#pragma config FWDTEN = OFF // Watchdog Timer
#pragma config WDTPS = PS1 // Watchdog Timer Postscale
#pragma config FCKSM = CSDCMD // Clock Switching & Fail Safe Clock Monitor
#pragma config OSCIOFNC = OFF // CLKO Enable
#pragma config POSCMOD = HS // Primary Oscillator
#pragma config IESO = OFF // Internal/External Switch-over
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable
#pragma config FNOSC = PRIPLL // Oscillator Selection
#pragma config CP = OFF // Code Protect
#pragma config BWP = OFF // Boot Flash Write Protect
#pragma config PWP = OFF // Program Flash Write Protect
#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select
#pragma config DEBUG = OFF // Debugger Disabled for Starter Kit
hello horace1 thanks for your reply , but i also have declare RG2 AND RG3 only input pin of encoder .
i have not configure any output pin of RG2 AND RG3.
so what's the problem ?
int old = 0;
void Encoder(void)
{
int new = 0 ;
int value = 0 ;
new = PORTG ; //// encoder pin here PORTG PIN 2 ,PIN3
value = new ^ old ;
if (value & 0x0080)
{
if(new & 0x0080)
{
mStepper_B_Enb(); // STEPPER ENABLE
if(new & 0x0100)
{
mStepper_B_For(); // STEPPER FORWARD
mStepper_B_Fourth();
}
else
{
mStepper_B_Rev(); // STEPPER REVERSE
mStepper_B_Fourth();
}
mStepper_B_Off();
Delay_us(25);
mStepper_B_On(); // STEPPER ON
}
}
old = new ;
return ;
}
main()
{
while(1)
{
Encoder();
}
}
Code C - [expand] | |
1 2 3 4 5 6 7 8 9 10 11 12 | main() { if( PORTG & 0x04) LED1 = 1; else LED1 = 0; if( PORTG & 0x08) LED2 = 1; else LED2 = 0; } |
#include <plib.h>
void mInitMicro(void);
void TimerandUartInit(void);
void PressureEncoder(void);
#ifndef OVERRIDE_CONFIG_BITS
#pragma config FPLLMUL = MUL_20 // PLL Multiplier
#pragma config FPLLIDIV = DIV_2 // PLL Input Divider
#pragma config FPLLODIV = DIV_1 // PLL Output Divider
#pragma config FPBDIV = DIV_1 // Peripheral Clock divisor
#pragma config FSRSSEL = PRIORITY_7 // SRS Select
#pragma config FWDTEN = OFF // Watchdog Timer
#pragma config WDTPS = PS1 // Watchdog Timer Postscale
#pragma config FCKSM = CSDCMD // Clock Switching & Fail Safe Clock Monitor
#pragma config OSCIOFNC = OFF // CLKO Enable
#pragma config POSCMOD = HS // Primary Oscillator
#pragma config IESO = OFF // Internal/External Switch-over
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable
#pragma config FNOSC = PRIPLL // Oscillator Selection
#pragma config CP = OFF // Code Protect
#pragma config BWP = OFF // Boot Flash Write Protect
#pragma config PWP = OFF // Program Flash Write Protect
#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select
#pragma config DEBUG = ON // Debugger Disabled for Starter Kit
#endif
/****Stepper B*********************************************/
#define SYS_FREQ (80000000L)
#define S_2_Pulse BIT_10
#define S_2_Dir BIT_9
#define S_2_Enb BIT_8
#define S_2_MS1 BIT_11
#define S_2_MS2 BIT_12
#define S_2_Opto BIT_13
#define mInitStepperB() (mPORTBSetPinsDigitalOut(S_2_Pulse | S_2_Dir | S_2_Enb | S_2_MS1 | S_2_MS2),\
mPORTBSetPinsDigitalIn(S_2_Opto),\
mPORTBClearBits(S_2_Pulse | S_2_Dir | S_2_Enb))
#define mStepper_B_On() (LATBSET = S_2_Pulse)
#define mStepper_B_Off() (LATBCLR = S_2_Pulse)
#define mStepper_B_For() (LATBCLR = S_2_Dir)
#define mStepper_B_Rev() (LATBSET = S_2_Dir)
#define mStepper_B_Enb() (LATBCLR = S_2_Enb)
#define mStepper_B_Dis() (LATBSET = S_2_Enb)
#define mStepper_B_Full() (LATBCLR = S_2_MS1, LATBCLR = S_2_MS2)
#define mStepper_B_Half() (LATBCLR = S_2_MS1, LATBSET = S_2_MS2)
#define mStepper_B_Fourth() (LATBSET = S_2_MS1, LATBCLR = S_2_MS2)
#define mStepper_B_Eighth() (LATBSET = S_2_MS1, LATBSET = S_2_MS2)
#define GetOpto_B_Status() PORTBbits.RB13; //S_2_Opto Sense Pin
#define Manual 0
void Delay_us(UINT16 Us)
{
Us *= 10;
while(Us != 0)
{
Us--;
}
}
void MsDelay(unsigned int mS)
{
unsigned int j = 0;
for(mS; mS > 0; mS--)
{
for(j = 0; j < 3200; j++)
{
asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");
asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");
asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");
asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");
asm("NOP");
}
}
}
int old1 = 0;
BYTE AutoStatus = 0;
int main(void)
{
mInitStepperB();
TimerandUartInit() ;
mInitMicro() ;
INTEnableSystemMultiVectoredInt();
MsDelay(10);
while(1)
{
if (AutoStatus == Manual)
{
PressureEncoder();
}
};
return 0;
}
void PressureEncoder(void)
{
int new1 = 0 ;
int value1 = 0 ;
new1 = PORTE ;
value1 = new1 ^ old1 ;
if (value1 & 0x40)
{
if(new1 & 0x40)
{
mStepper_B_Enb();
if(new1 & 0x80)
{
mStepper_B_For();
mStepper_B_Fourth();
}
else
{
mStepper_B_Rev();
mStepper_B_Fourth();
}
mStepper_B_Off();
Delay_us(200);
mStepper_B_On();
}
}
old1 = new1 ;
return ;
}
//void PressureEncoder(void)
//{
// int new1 = 0 ;
// int value1 = 0 ;
// new1 = PORTG ;
// value1 = new1 ^ old1 ;
// if (value1 & 0x0080)
// {
// if(new1 & 0x0080)
// {
// mStepper_B_Enb();
// if(new1 & 0x0100)
// {
// mStepper_B_For();
// mStepper_B_Fourth();
//
// }
// else
// {
// mStepper_B_Rev();
// mStepper_B_Fourth();
// }
// mStepper_B_Off();
// Delay_us(200);
// mStepper_B_On();
//
// }
// }
// old1 = new1 ;
// return ;
//}
void TimerandUartInit(void)
{
int pbClk;
pbClk=SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
OpenTimer4(T4_ON | T4_PS_1_4, 0xFFFF); //Pressure Motor
ConfigIntTimer4(T4_INT_ON | T4_INT_PRIOR_2);
EnableIntT4;
}
void mInitMicro(void)
{
DDPCONbits.JTAGEN = 0; //JTAG debugger off
mStepper_B_Dis(); //Pressure Stepper Disable
mStepper_B_For();
mStepper_B_On();
mStepper_B_Half();
}
void __ISR(_TIMER_4_VECTOR, ipl2) PressureTimerIntHandler(void)
{
if (AutoStatus == Manual)
{
PressureEncoder();
}
mT4ClearIntFlag();
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?