⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ps2_io.c

📁 这是nrf24lu1的无线鼠标源代码,应用平台是keil c
💻 C
字号:
/* Copyright (c) 2004 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is confidential property of 
 * Nordic Semiconductor. The use, copying, transfer or disclosure 
 * of such information is prohibited except by express written
 * agreement with Nordic Semiconductor.
 */ 

/** @file
 * Source code for PS2 interface
 * @author Runar Kjellhaug
 *
 */

#include "ps2_io.h"
#include "hal_nrf_hw.h"

uint16_t buf_of_count=0;
uint8_t ps2_max_count=0;

static uint8_t ps2_buf_tail;
static uint8_t ps2_buf_head;
static uint8_t ps2_data_cnt;
static bool ps2_data_rdy;
static int8_t ps2_buf[PS2_BUFSIZE]; 

static void Delay100us(uint8_t count);

uint8_t ps2_write(uint8_t byte, uint8_t resp_count)
{
uint8_t bit_ctr;
uint8_t parity_bit=0;
uint8_t respons;

  COUT_LOW();                         // Bring CLK low
  Delay100us(1);                      // delay 100祍
  DOUT_LOW();                         // bring Data line low
  Delay100us(1);
  COUT_HIGH();                        // and release COUT.
  
  while(!CIN);                        // wait for CLK  

  for(bit_ctr=0;bit_ctr<8;bit_ctr++)  // output byte...
  {
    while(CIN);                       // wait for CLK
    if(byte & 0x01)                   // current bit '1' ?
    {
      DOUT_HIGH();
      parity_bit++;
    }
    else
      DOUT_LOW();
    byte >>= 1;                       // rotate to next bit
    while(!CIN);                      // wait for next CLK
  }
  
  while(CIN);                         // wait for CLK
  if(parity_bit & 0x01)               // output parity_bit
    DOUT_LOW();
  
  while(!CIN);                        // wait for next CLK
  while(CIN);                         // wait for CLK
  
  DOUT_HIGH();                        // output STOPBIT

  while(!CIN);                        // wait for next CLK
  while(CIN);                         // wait for CLK

  while(!CIN);                        // finale clk

  while(resp_count--)
    respons = ps2_read();    
  
  return respons;
}

uint8_t ps2_read(void)
{
uint8_t bit_ctr;
uint8_t ps2_byte=0;

  while(CIN);                           // wait for STARTBIT
  while(!CIN);                          // wait for first bit in byte..

  for(bit_ctr=0;bit_ctr<8;bit_ctr++)
  {
    ps2_byte >>= 1;                     // rotate byte right
    while(CIN);                         // wait for CLK
    if(DIN)
    {
      ps2_byte |= 0x80;
    }
    while(!CIN);
  }

  while(CIN);                         // wait for parity bit
  while(!CIN);                        // wait for STOPBIT
  while(CIN);

  while(!CIN);                        // finale CLK bit

  return(ps2_byte);
}

void ps2_flush_fifo()
{
   ps2_buf_head=0;
   ps2_buf_tail=0;
   ps2_data_cnt=0;
   ps2_data_rdy=false;
}

void ps2_put_fifo()
{  
  if(ps2_data_cnt > ps2_max_count)
  {
    ps2_max_count = ps2_data_cnt;
  }  

  if(ps2_data_cnt<PS2_BUFSIZE)
  {
    
  }
  else
  {
    buf_of_count++;

    // Throw away oldest data
    ps2_data_cnt = ps2_data_cnt - PS2_PACKET_SIZE; 
    ps2_buf_tail = (ps2_buf_tail+PS2_PACKET_SIZE)%PS2_BUFSIZE;  
  }

  ps2_buf[ps2_buf_head]=ps2_read();         
 
  ps2_buf_head = (ps2_buf_head+1)%PS2_BUFSIZE;         
  ps2_data_cnt++;
  ps2_data_rdy=true;
}

bool ps2_fifo_get_status()
{
  return ps2_data_rdy;
}

uint8_t ps2_get_fifo()
{
  uint8_t ret=0;
  while(ps2_data_rdy==false);          // Wait for incomming data
  ret=ps2_buf[ps2_buf_tail];
  ps2_buf_tail = (ps2_buf_tail+1)%PS2_BUFSIZE;
  ps2_data_cnt--;
  if(ps2_data_cnt==0)
  {
    ps2_data_rdy=false;   
  }
  return ret;
}

void Delay100us(uint8_t cnt)
{
  uint8_t delay;
  while(cnt--)
  for(delay=0;delay<117;delay++);    // 100祍 for C8051F320-TB
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -