Logo Search packages:      
Sourcecode: ktechlab version File versions  Download package

void PIC14::Skeypad ( const Variable pinMap  ) 

Read the value of the keypad to the working register.

Parameters:
name The variable giving the pin configuration of the keypad.

Definition at line 1016 of file pic14.cpp.

References Microbe::alias(), Code::append(), Code::instruction(), Variable::name(), PortPin::pin(), PortPin::port(), Variable::portPinList(), Code::queueLabel(), Sdelay(), Code::Subroutine, and Variable::type().

{
      // pinMap = 4 rows, n columns
      
      assert( pinMap.type() == Variable::keypadType );
      assert( pinMap.portPinList().size() >= 7 ); // 4 rows, at least 3 columns
      
      QString subName = QString("__wait_read_keypad_%1").arg( pinMap.name() );
      QString waitName = QString("__wait_keypad_%1").arg( pinMap.name() );
      QString readName = QString("__read_keypad_%1").arg( pinMap.name() );
      
      m_pCode->append( new Instr_call( subName ) );
      
      if ( m_pCode->instruction( subName ) )
            return;
      
      //BEGIN Wait until read subroutine
      m_pCode->queueLabel( subName, Code::Subroutine );
      
      // Read current key (if any) from keypad and save to temporary variable
      m_pCode->append( new Instr_call( readName ), Code::Subroutine );
      m_pCode->append( new Instr_movwf( "__m" ), Code::Subroutine );
      
      // Test if any key was pressed; if not, then start again
//    std::cout << "mb->alias(\"Keypad_None\")="<<mb->alias("Keypad_None") << std::endl;
      m_pCode->append( new Instr_sublw( mb->alias("Keypad_None").toInt( 0, 0 ) ), Code::Subroutine );
      m_pCode->append( new Instr_btfsc( "STATUS","Z" ), Code::Subroutine );
      m_pCode->append( new Instr_goto( subName ), Code::Subroutine );
      m_pCode->append( new Instr_goto( waitName ), Code::Subroutine );
      //END Wait until read subroutine
      
      
      //BEGIN Wait until released subroutine
      m_pCode->queueLabel( waitName, Code::Subroutine );
      
      Sdelay( 10000, Code::Subroutine ); // 10 milliseconds for debouncing
      
      // Key was pressed; now we wait until the key is released again
      m_pCode->append( new Instr_call( readName ), Code::Subroutine );
      m_pCode->append( new Instr_sublw( mb->alias("Keypad_None").toInt( 0, 0 ) ), Code::Subroutine );
      m_pCode->append( new Instr_btfss( "STATUS","Z" ), Code::Subroutine );
      m_pCode->append( new Instr_goto( waitName ), Code::Subroutine );
      m_pCode->append( new Instr_movf( "__m", 0 ), Code::Subroutine );
      m_pCode->append( new Instr_return(), Code::Subroutine );
      //END Wait until released subroutine
      
      
      if ( m_pCode->instruction( readName ) )
            return;
      
      //BEGIN Read current value of keypad subroutine
      m_pCode->queueLabel( readName, Code::Subroutine );
      
      // Make the four row lines low
      for ( unsigned row = 0; row < 4; ++ row )
      {
            PortPin rowPin = pinMap.portPinList()[row];
            m_pCode->append( new Instr_bcf( rowPin.port(), QString::number( rowPin.pin() ) ), Code::Subroutine );
      }
      
      // Test each row in turn
      for ( unsigned row = 0; row < 4; ++ row )
      {
            // Make the high low
            PortPin rowPin = pinMap.portPinList()[row];
            m_pCode->append( new Instr_bsf( rowPin.port(), QString::number( rowPin.pin() ) ), Code::Subroutine );
            
            for ( unsigned col = 0; col < 3; ++ col )
            {
                  PortPin colPin = pinMap.portPinList()[4+col];
                  m_pCode->append( new Instr_btfsc( colPin.port(), QString::number( colPin.pin() ) ), Code::Subroutine );
                  m_pCode->append( new Instr_retlw( mb->alias( QString("Keypad_%1_%2").arg(row+1).arg(col+1) ).toInt( 0, 0 ) ), Code::Subroutine );
            }
            
            // Make the low again
            m_pCode->append( new Instr_bcf( rowPin.port(), QString::number( rowPin.pin() ) ), Code::Subroutine );
      }
      
      // No key was pressed
      m_pCode->append( new Instr_retlw( mb->alias("Keypad_None").toInt( 0, 0 ) ), Code::Subroutine );
      //END Read current value of keypad subroutine
}


Generated by  Doxygen 1.6.0   Back to index