 		}
 	}
 
+	//turn off all rows first
 	for ( int rowid = 4; rowid < 16; rowid ++ )
 	{
-		//turn off all rows first
-		PORTA = 0xFF;  PORTC = 0xFF;
 		//turn on the current row
 		if (rowid < 8)
 		{
-			PORTA &= ~(0x1 << rowid);
+			PORTA = ~(0x1 << rowid);
 		}
 		else
 		{
-			PORTC &= ~(0x1 << (16 - rowid) );
-		}
-		//digitalWrite( rowid + 22, LOW );
+			PORTC = ~(0x1 << (15 - rowid) );
+		}
+		//we must have such a delay so the digital pin output can go LOW steadily, 
+		//without this delay, the row PIN will not 100% at LOW during yet,
+		//so check the first column pin's value will return incorrect result.
+		delayMicroseconds(3);
+
 		byte colResult[16];
 		//pin 38, PD7
 		colResult[0] = (PIND & B10000000)== 0 ? 1 : 0;

		for ( int colid = 0; colid < 16; colid ++ )
		{
			if ( colResult[ colid ] == 1 )
				//if ( digitalRead( colid + 38 ) == HIGH )
			{
				joyReport.button[ rowid * 2 + colid / 8 ] |= (0x1 << ( colid % 8 ));
			}

	//loopTime = millis();

	//scan rotary encoder

	PORTC = 0xFF;
	for ( int rowid = 0; rowid < 4; rowid ++ )
	{
		//turn on the current row
		PORTA = ~( B00000001 << rowid);

		//we must have such a delay so the digital pin output can go LOW steadily, 
		//without this delay, the row PIN will not 100% at LOW during yet,
		//so check the first column pin's value will return incorrect result.
		delayMicroseconds(3);

		byte colResult[16];

			//pin 38, PD7
			colResult[0] = (PIND & B10000000 )== 0 ? 1 : 0;
			//pin 39, PG2
			colResult[1] = (PING & B00000100 )== 0 ? 1 : 0;
			//pin 40, PG1
			colResult[2] = (PING & B00000010 )== 0 ? 1 : 0;
			//pin 41, PG0
			colResult[3] = (PING & B00000001 )== 0 ? 1 : 0;

			//pin 42, PL7
			colResult[4] = (PINL & B10000000 )== 0 ? 1 : 0;
			//pin 43, PL6
			colResult[5] = (PINL & B01000000 )== 0 ? 1 : 0;
			//pin 44, PL5
			colResult[6] = (PINL & B00100000 )== 0 ? 1 : 0;
			//pin 45, PL4
			colResult[7] = (PINL & B00010000 )== 0 ? 1 : 0;

			//pin 46, PL3
			colResult[8] = (PINL & B00001000 )== 0 ? 1 : 0;
			//pin 47, PL2
			colResult[9] = (PINL & B00000100 )== 0 ? 1 : 0;
			//pin 48, PL1
			colResult[10] =( PINL & B00000010)== 0 ? 1 : 0;
			//pin 49, PL0
			colResult[11] =( PINL & B00000001)== 0 ? 1 : 0;

			//pin 50, PB3
			colResult[12] =( PINB & B00001000)== 0 ? 1 : 0;
			//pin 51, PB2
			colResult[13] =( PINB & B00000100)== 0 ? 1 : 0;
			//pin 52, PB1
			colResult[14] =( PINB & B00000010)== 0 ? 1 : 0;
			//pin 53, PB0
			colResult[15] =( PINB & B00000001)== 0 ? 1 : 0;


		for ( int colid = 0; colid < 16; colid += 2 )
		{
			byte encoderValue = colResult[colid] << 1 | colResult[colid + 1];
			//byte encoderValue = digitalRead(colid+38) << 1 | digitalRead(colid + 39);
			byte encoderId = rowid * 8 + colid / 2;
			//int encoderState = encoderTable[ prevEncoderValue[ encoderId ] << 2 | encoderValue ];

