PS2 keyboard with LPC1317-48P

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

PS2 keyboard with LPC1317-48P

160 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by aamir ali on Thu Feb 21 02:15:35 MST 2013
I have to interface a PS2 keyboard with LPC1317.
P0.12 -data
P0.13 - clock
Pull up resistor value = 10K

Problem 1: LPC1317 is powered by 3.3V & keyboard is +5V. There is pull up between data & clcok with +5V.
But still I used GPIO of MCU,
whenever I have to write zero I use:

LPC_IOCON->TMS_PIO0_12 = 0x00000081;   //make it output         
LPC_GPIO->DIR[0] |= 0x00001000; 

// 
Quote: What problem can occur here as pin will sink now & its +5V.



Whenever I have to write 1 or relase line, I use

LPC_IOCON->TMS_PIO0_12 = 0x00000091;      // otherwise make it input
LPC_GPIO->DIR[0] &= 0xFFFFEFFF; 



Problem 2: Can i use OD drian mode of pins for that, as vdd is +5V. Pins which are not true open drain??


Problem 3:

Also check my code if I am doing it write:

uint32_t send_cmd(uint8_t temp, uint8_t par)
{
    uint32_t i;
    uint8_t temp2;
    
    LPC_IOCON->TDI_PIO0_11 = 0x00000089;
   
/* Step 1: relese both clock & data */
    LPC_IOCON->TMS_PIO0_12 = 0x00000091;      /* Data pin input wit pull up enabled  */
    LPC_GPIO->DIR[0] &= 0xFFFFEFFF;                    /*  Set pin as input */
    
    LPC_IOCON->TDO_PIO0_13 = 0x00000091;      /* Data pin input wit pull up enabled  */
    LPC_GPIO->DIR[0] &= 0xFFFFDFFF;           /*  Set pin as input */
    
/* Step 2: Bring the clock line low */    
    LPC_IOCON->TDO_PIO0_13 = 0x00000081;    
    LPC_GPIO->DIR[0] |= 0x00002000;  
    
/* Step 3: Wait 100 us */     
    wait(100);
    
/* Step 4: If data already low or clock high, then error PS2 busy */ 
    if( (LPC_GPIO->W0[12] == 0) || (LPC_GPIO->W0[13] != 0) )                   
    {
        return 1;                      
    }   
    
/* Step 5: Bring the Data line low */     
    LPC_IOCON->TMS_PIO0_12 = 0x00000081;            
    LPC_GPIO->DIR[0] |= 0x00001000;  

/* Step 6: Release the Clock line. */      
    LPC_IOCON->TDO_PIO0_13 = 0x00000091; 
    LPC_GPIO->DIR[0]  &= 0xFFFFDFFF;                //release clock line
    
/* Step 7: Wait for the device to bring the Clock line low. */     
    while(LPC_GPIO->W0[13] != 0);            // Wait for the device to bring the Clock line low
    
  /* Step 8: Write 8 bit data, LSB first */     
    for( i = 0 ; i < 8 ; i++ )
    {
        temp2 = 0x01 << i ;                     //send LSB
        temp2 = temp2 & temp;
        
        if(temp2 == 0)                                
        {                                           // if bit 0 then make data pin as output
            LPC_IOCON->TMS_PIO0_12 = 0x00000081;            
            LPC_GPIO->DIR[0] |= 0x00001000; 
        }   
        else
        {
            LPC_IOCON->TMS_PIO0_12 = 0x00000091;      // otherwise make it input
            LPC_GPIO->DIR[0] &= 0xFFFFEFFF; 
        }    
             
        while(LPC_GPIO->W0[13] == 0);            //while clock not high
        while(LPC_GPIO->W0[13] != 0);            //while clock not low
        
    }
    
  /* Step 9: Send parity bit */ 
    if(par == 0)                              //parity bit
     {
        LPC_IOCON->TMS_PIO0_12 = 0x00000081;            
        LPC_GPIO->DIR[0] |= 0x00001000; 
     }   
     else
     {
        LPC_IOCON->TMS_PIO0_12 = 0x00000091;    
        LPC_GPIO->DIR[0] &= 0xFFFFEFFF; 
     }   
    while(LPC_GPIO->W0[13] == 0);            //while clock not high
    while(LPC_GPIO->W0[13] != 0);            //while clock not low
    

  /* Step 10: release data line */      
    LPC_IOCON->TMS_PIO0_12 = 0x00000091;    /* make them input & pull high */         //release data line
    LPC_GPIO->DIR[0] &= 0xFFFFEFFF; 


/* Step 10: Wait for the device to bring Data low & clcok low */        
    while(LPC_GPIO->W0[12] != 0);            //while data not low
    while(LPC_GPIO->W0[13] != 0);            //while clock not low
    
 /* Step 11: Wait for the device to release Data and Clock */   
    while( ( LPC_GPIO->W0[13] == 0  ) || ( LPC_GPIO->W0[12] == 0  ) );  
    
    
    return 0;
}


void wait(uint8_t temp)
{
    LPC_SYSCON->SYSAHBCLKCTRL  |= (1UL<<9);   /* enable clock */                 
    LPC_CT32B0->TCR = 0x02;            /* reset timer */                        
    LPC_CT32B0->PR  = 0x00;             /* prescaler value */                    
    LPC_CT32B0->MR0 =  temp * 72;       
    LPC_CT32B0->IR =  0x003f;            /* disable interrupt initially */       
    LPC_CT32B0->MCR = 0x04;          /* TC will be zero on match & give int */ 
    LPC_CT32B0->TCR = 0x01;  
    while(LPC_CT32B0->TCR & 0x01);    

}
0 Kudos
0 Replies