#INCLUDE<plib.h>
#define mLED1on() (LATESET = BIT_4)
#define mLED1off() ( LATECLR= BIT_4)
#define mLED2on() (LATESET = BIT_3)
#define mLED2off() (LATECLR = BIT_3)
int oldvalue = 0;
void InitializePorts(void)
{
TRISEBits.TRISE6 = 1 ; // make as a input for encoder
TRISEBits.TRISE7 = 1 ; // make as a input for encoder
TRISEBits.TRISE3 = 0 ; // make as an output for led1
TRISEBits.TRISE4 = 0 ; // make as an output for led2
return;
}
void InitializeLed(void)
{
mLED1off();
mLED2off();
return;
}
void Encoder (void)
{
int newvalue = 0 ;
int value = 0 ;
newvalue = PORTE ; // read the current port status
value = newvalue ^ oldvalue ; // flag each port bit that has changed
if(value & 0x40)
{
if (newvalue & 0x40)
{
if (newvalue & 0x80)
{
mLED1on() ;
}
else
{
mLED2on() ;
}
Delay(20);
}
}
oldvalue = newvalue; // save current port status
return;
}
void main(void)
{
InitializePorts();
InitializeLed();
while(1)
{
Encoder();
}
}