Previous

KeyPad Driver Code:

/***************************************** KEYPAD features *******************************************/
 void OSKeyPadInit(void)
 { 
 OSCreateTask(OSKeyPadDebounce, DebounceTime, NoKeyPadMessage, KEYSTATE_NoPush);  
 }  
char OSKeyPadDebounce(void) //debounce state machine
   { 
   if(OSGetState()== KEYSTATE_NoPush)
   { KEYPAD_KeyPress=OSKeyPad_Read(); 
   if(KEYPAD_KeyPress!=16)
   { 
   OSSendMess(0, KEYSTATE_StillPressed);
   OSSetState(KEYSTATE_MaybePush); 
   }
   else OSSetState( KEYSTATE_NoPush); 
   } 
   if(OSGetState()== KEYSTATE_MaybePush) { 
   
   if(KEYPAD_KeyPress==OSKeyPad_Read() && OSGetMess(0)== KEYSTATE_StillPressed)
   OSSetState( KEYSTATE_Pushed); 
   else 
   {
   // OSSendMess(0, KEYSTATE_error);
   OSSetState( KEYSTATE_NoPush); 
   
   }
   }
   if(OSGetState()== KEYSTATE_Pushed)
   {
   if(KEYPAD_KeyPress==OSKeyPad_Read() && OSGetMess(0)== KEYSTATE_StillPressed)
   OSSetState( KEYSTATE_Pushed);
   else 
   OSSetState( KEYSTATE_DebouncePress);
   
   }
   if(OSGetState()== KEYSTATE_DebouncePress)
   {
   // OSSendMess(1,1); 
   
   /* if(KEYPAD_letter[KEYPAD_KeyPress]=='D')
   { 
   OSKeyPadClearBuffer();
   }*/
   OSKeyPadWriteBuffer(KEYPAD_letter[KEYPAD_KeyPress]);
 OSSetState( KEYSTATE_NoPush);
   } 
   
   return 0; 
   }
   
   char OSKeyPad_Read(void) //reads the keypad
   { 
   char temp; 
   
   //get lower nibble
   DDRC = 0x0f;
   PORTC = 0xf0; 
   delay_us(5);
   KEYPAD_Key = PINC;
   
   //get upper nibble
   DDRC = 0xf0;
   PORTC = 0x0f; 
   delay_us(5);
   KEYPAD_Key = KEYPAD_Key | PINC;
   
   //find matching keycode in keytbl
   if (KEYPAD_Key != 0xff)
   {
   for (KEYPAD_butnum=0; KEYPAD_butnum<KEYPAD_maxkeys; KEYPAD_butnum++)
   { 
   if (KEYPAD_keytbl[KEYPAD_butnum]==KEYPAD_Key)
   { 
   temp=KEYPAD_butnum;
   break; 
   }
   }
   if (KEYPAD_butnum==KEYPAD_maxkeys) KEYPAD_butnum=0;
   else KEYPAD_butnum++; //adjust by one to make range 1-16
   } 
   else 
   {
   temp = 16;
   KEYPAD_butnum=0; 
   }
   return temp;
   } //end main
   /*void OSKeyPadWriteBuffer(char Key)
   { //send the value if the enter key'A' is pressed or when the buffer is full
   if((Key=='A' && KEYPAD_KeyFlag==1) || (KEYPAD_index ==KEYPAD_BUFFERSIZE))
   {
   
   KEYPAD_KeyFlag=0; 
   KEYPAD_Key_Ready=1; //string ready
   KEYPAD_index = 0; //reset index count
   } 
   else if(Key=='A' && KEYPAD_KeyFlag==0)
   {
   // printf("\r\n"); 
   PORTB=~0b10101010;
   // OSUARTTransmitBytes(empty, 20);
   // printf("\r\n"); 
   }
   else
   {
   KEYPAD_Buffer[KEYPAD_index]=Key; 
   // OSUARTTransmitBytes(buffer, 5); 
   // OSUARTTransmitByte(Key); 
   // printf("\r\n");
   KEYPAD_index++;
   KEYPAD_KeyFlag=1;
   } 
   
   } */
   void OSKeyPadWriteBuffer(char Key)
   { //send the value if the enter key'A' is pressed or when the buffer is full
   if(KEYPAD_index == (OSNumCharInput-1)) 
   {
   
   KEYPAD_KeyFlag=0; 
   KEYPAD_Key_Ready=1; //string ready
   KEYPAD_index = 0; //reset index count
   } 
   
   else
   {
   KEYPAD_Buffer[KEYPAD_index]=Key; 
   // OSUARTTransmitBytes(buffer, 5); 
   // OSUARTTransmitByte(Key); 
   // printf("\r\n");
   KEYPAD_index++;
   KEYPAD_KeyFlag=1;
   } 
   
   } 
   /*
   void OSKeyPadClearBuffer()
   { 
   while(KEYPAD_index!=0)
   {
   KEYPAD_Buffer[KEYPAD_index--]=0;
   
   }
   KEYPAD_KeyFlag=0;
   
   } 
   */ 
   void OSGetKeyReady(unsigned char x)
   {
   if(OSKeyLock==0) //if no one is using the keypad 
   {
   OSNumCharInput=x; 
   OSKeyLock=OSCurrentTask; //set lock so no one can use it 
   OSKeyCommand=0;
   }
   else OSKeyCommand=1; //some other task is currently using the keypad right now,    OSGetKeyCommand is off until the key unlocks
 
}
   unsigned int OSGetKeyCommand(char *data)
   { 
   if(OSKeyCommand==0)
   {
   int i;
   if(KEYPAD_Key_Ready==1)
   {
   
   for(i=0;i<OSNumCharInput;i++)
   {
   data[i]=KEYPAD_Buffer[i];
   KEYPAD_Buffer[i]=0;
   }
   data[i]=0; //terminate to make string
   KEYPAD_Key_Ready=0; 
   OSKeyLock = 0; //unlocks the KeyPad for someone else
 return 1;
   }
   
   }
   else return 0;
   
   }

Previous