 // LCD module connections
sbit LCD_RS at RD4_bit;
sbit LCD_EN at RD5_bit;
sbit LCD_D4 at RD0_bit;
sbit LCD_D5 at RD1_bit;
sbit LCD_D6 at RD2_bit;
sbit LCD_D7 at RD3_bit;

sbit LCD_RS_Direction at TRISD4_bit;
sbit LCD_EN_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD3_bit;
#define REA TRISB0_bit         // Rotary encoder pin definition
#define REB TRISB1_bit
// End LCD module connections
unsigned char count;                //this variable will incremented or decremented on encoder rotation
void rotary(){
        unsigned char state;
        static unsigned char oldstate;  // this variable need to be static as it has to retain the value between calls
        Delay_ms(1);                                 //        delay for 1ms here for debounce
        state= REB<<1 | REA;                        //  combine the pin status and assign to state variable
        if(oldstate==0x0){
        if( state ==0x1)
                     {
                        count--;                                //decrement the count

                }else if( state == 0x2)
                {

                        count++;                                //decrement the count

                }

        }
        oldstate = state;                                // store the current state value to oldstate value this value will be used in next call

        PORTB = PORTB;

        switch(count){
        case 1:
        Init();
        Lcd_Out(1,1,"Menu 1") ;
        Lcd_Out(2,1,"Vodka") ;
        break;
        case 2:
        Init();
        Lcd_Out(1,1,"Menu 2") ;
        Lcd_Out(2,1,"Vodka2") ;
        break;
        case 3:
        Init();
        Lcd_Out(1,1,"Menu 3") ;
        Lcd_Out(2,1,"Vodka 3") ;
        break;
        case 4:
        Init();
        Lcd_Out(1,1,"Menu 4") ;
        Lcd_Out(2,1,"Vodka4") ;
        break;
        case 5:
        Init();
        Lcd_Out(1,1,"Menu 5") ;
        Lcd_Out(2,1,"Vodka 5") ;
        break;
        case 6:
        Init();
        Lcd_Out(1,1,"Menu 6") ;
        Lcd_Out(2,1,"Vodka6") ;
        break;
        }
}
void Init()
{
  Lcd_Init();       // RB7 plays Tone1
  Lcd_Cmd(_LCD_CLEAR);                           // Clear LCD
  Lcd_Cmd(_LCD_CURSOR_OFF);                      // Turn cursor off
}
void interrupt(void){
 if(INTCON.RBIF == 1){
 rotary();
 INTCON.RBIF = 0;
 }
           }
void Main()
{
  ANSEL  = 0;                                    // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                                  // Disable comparators
  C2ON_bit = 0;
  TRISB = 1;
  INTCON.RBIF = 0 ;
  INTCON.RBIE = 1;
  INTCON.GIE = 1;

  Lcd_Out(1,1,"Ludnica") ;
}