#include //Variables int ROTATION=2; //Encoder direction: 0=CCW, 1=CW, 2=STOP int1 PUSH=0; //Encoder switch status: 0=OFF, 1=ON int GREEN=0; //Counter for green LEDs int1 RED=0; //Counter for red LED #int_EXT void EXT_isr(void){ if(input(PIN_B2)==1)ROTATION=!input(PIN_B1); //If switch not pressed, else{ //detect the direction PUSH=1; //If switch is pressed, set it ROTATION=2; //and indicate no rotation } } void main(){ enable_interrupts(GLOBAL); //Global interrupt enable enable_interrupts(INT_EXT); //External interrupt enabled ext_int_edge(H_TO_L); //Detection on falling edge while(TRUE){ if((GREEN>=1)&&(ROTATION==0)){ //CCW rotation detected GREEN=GREEN-1; //Decrease counter (up to 0) ROTATION=2; //Back to rest position } if((GREEN<=14)&&(ROTATION==1)){ //CW rotation detected GREEN=GREEN+1; //Increase counter (up to 15) ROTATION=2; //Back to rest position } if(PUSH==1){ //Switch pushed RED=1-RED; //Toggle red LED status PUSH=0; //Back to open position } output_A(GREEN); //Output GREEN to green LEDs output_bit(PIN_B7,RED); //Output RED to red LED } }