

	/* Channel 0 init */
	CANPAGE = (0 << 4);		/* CHNB=0x00; select channel 0 */
	CANSTMOB  = 0x00;			/* reset channel status */
	CANCDMOB = 0x00;			/* reset control and dlc register */

	/* Channel 0: identifier = 11bits. CANIDT=0x123 */
	CANIDT1 = 0x24;
	CANIDT2 = 0x60;

	/* Channel 0: mask = 11bits. 0x7F0 */
	CANIDM1 = 0xFE;
	CANIDM2 &= ~0xE0;
	CANIDM4 = 0;

	/* Channel 0 configuration */
	CANIDT4 &=~0x04;           /* clear bit rtr in CANIDT4. */
	CANCDMOB |= DLC_MAX;       /* Reception 8 bytes.*/
	CANCDMOB |= CH_RxENA;      /* Reception enabled without buffer.*/


	/* Channel 1 init */
	CANPAGE = (1 << 4);		/* CHNB=0x01; select channel 1 */
	CANSTMOB  = 0x00;			/* reset channel status */
	CANCDMOB = CH_DISABLE;		/* reset control and dlc register */

	/* Channel 1: identifier = 11bits. CANIDT=0x123 */
	CANIDT1 = 0x24;
	CANIDT2 = 0x60;

	/* Channel 1: mask = 11bits. 0x7F0 */
	CANIDM1 = 0xFE;
	CANIDM2 &= ~0xE0;
	CANIDM4 = 0;
	
	/* interrupt configuration */
	CANIE2|=0x01;                       /* IECH0=1 */
	CANGIE = ((1<<ENRX)|(1<<ENIT));    /* Can_Rx & IT enable */
}



ISR(CANIT_vect) {

unsigned int id;			/* can_data index */

/* echo receive data on channel 0 reception */
 CANPAGE = (0 << 4);					 /* CHNB=0x00; select channel 0 */
 
 if((CANSTMOB & MSK_CANSTMOB_RxOk) == MSK_CANSTMOB_RxOk) {
 
 id = (((int)(CANIDT2))>>5) + (((int)(CANIDT1))<<3);       // V2.0 part A
 
 switch(id)
   {
     case(0x120): Stop_motor();    break; // Stop motor

     case(0x121):  Run_motor();     break; // Run motor

     case(0x122):  speed=(((int)(CANMSG))<<8);	
                   speed = speed + (int)(CANMSG);	//  set speed
                   break; 

     case(0x123): direction=CANMSG;  break; // set direction motor

     case(0x124):  if ( (CANIDT4 & 0x04) == 0x04)
                    {
                         CANPAGE = (1 << 4);   /* select chanel 1 */
                         CANIDT2 = (char)(id << 5);
                         CANIDT1 = (char)(id >> 3);
                         CANMSG = (char)(measured_speed >> 8);
                         CANMSG = (char)(measured_speed);
                         CANSTMOB = 0x00;
                         CANCDMOB = 0x02;			/* transmit 2 bytes */
                         CANCDMOB |= CH_TxENA;		/* emission enabled */
                         //CANEN2 |= (1 << 1);		/* channel 1 enable */
                         CANPAGE = (0 << 4);		// select chanel 0
                      }
                     break;                  // send back measured speed
   }
 }

 CANPAGE = (0 << 4);
 CANSTMOB=0x00;				/* reset channel 0 status */
 CANEN2 |= (1 << 0);		/* channel 0 enable */
 CANCDMOB = DLC_MAX;		/* receive 8 bytes */
 CANCDMOB |= CH_RxENA;		/* reception enable */
 CANGIT = CANGIT;			/* reset all flags */

}

//ISR(OVRIT_vect) {} 



