找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 4208|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

ps2 3D鼠標(biāo) 單片機(jī)讀取···

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:71259 發(fā)表于 2014-12-29 00:59 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
本帖最后由 daming 于 2014-12-29 01:03 編輯


/*此程序是原始版本,只能夠在單片機(jī)上顯示X軸數(shù)據(jù),因?yàn)榕嫉膯纹瑱C(jī)的液晶用做他途了*/
#include<reg52.h>
#include"mouse.h"  //
mouse.h頭文件下載地址:http://www.torrancerestoration.com/bbs/dpj-29724-1.html

void main()
{
Init_mouse();      //初始化素單片機(jī)設(shè)置      
mouse_send_data(0xff);    //復(fù)位鼠標(biāo)命令
delay(500);       //因?yàn)槭髽?biāo)會(huì)自己返回?cái)?shù)據(jù)給INT0,這兒我們忽略,理論上這個(gè)延時(shí)是多余的
mouse_send_data(0xf4);    //發(fā)送鼠標(biāo)工作命令,我們忽略所有從鼠標(biāo)返回的信息值,for 簡(jiǎn)便程序
while(1)       //大循環(huán)  在鼠標(biāo)不動(dòng)的時(shí)候顯示X軸上的數(shù)據(jù)
{
  init_smg();      //這個(gè)也是多余的,單純測(cè)試單片機(jī)是否工作
  P1=tab[move_x/10000];   //顯示,數(shù)碼管,沒(méi)什么多的,這都不會(huì),回家睡覺(jué)吧
  P0=4;
  delay(200);
  P1=tab[move_x/1000%10];
  P0=3;
  delay(200);
  P1=tab[move_x/100%10];
  P0=2;
  delay(200);
  P1=tab[move_x/10%10];
  P0=1;
  delay(200);  
  P1=tab[move_x%10];
  P0=0;
  delay(200);
}
}
/*over,謝謝網(wǎng)友提供的資料*/
/*by jack*/


下面是mouse.h的頭文件
/******************************************************
     演示鼠標(biāo)引腳定義
CLK : 白色
SDA  : 綠色
GND:   橙色
VCC:   藍(lán)色
*******************************************************/
#include<reg52.h>
#ifndef MOUSE_H     //避免重復(fù)定義
#define MOUSE_H
sbit mouse_SDA=P3^0;//數(shù)據(jù)線P3_5
sbit mouse_CLK=P3^2;//時(shí)鐘線P3_3

                           
unsigned char bdata mouse_byte; //接收字節(jié)
sbit mouse_byte_bit0=mouse_byte^0;//mouse_byte第0位
sbit mouse_byte_bit1=mouse_byte^1;//mouse_byte第1位
sbit mouse_byte_bit2=mouse_byte^2;//mouse_byte第2位
sbit mouse_byte_bit3=mouse_byte^3;//mouse_byte第3位
sbit mouse_byte_bit4=mouse_byte^4;//mouse_byte第4位
sbit mouse_byte_bit5=mouse_byte^5;//mouse_byte第5位
sbit mouse_byte_bit6=mouse_byte^6;//mouse_byte第6位
sbit mouse_byte_bit7=mouse_byte^7;//mouse_byte第7位
unsigned char bdata mouse_fuction;//功能信息字節(jié)
                     
unsigned char mouse_buffer[11];//接收位數(shù)據(jù)緩沖區(qū)
unsigned char mouse_buffer_bit=0;//mouse_buffer[mouse_buffer_bit]
unsigned char mouse_data[4];//接收鼠標(biāo)數(shù)據(jù)緩沖區(qū),分別存放:功能信息字節(jié),x位移量,y位移量
unsigned char mouse_data_bit=0;//mouse_data[mouse_data_bit]
unsigned int move_x=10000;//存放橫坐標(biāo)
unsigned int move_y=10000;//存放縱坐標(biāo)
code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(unsigned int cnt)   //延時(shí),us級(jí)別的
{
while(--cnt);
}
void delay10us(int i)     //延時(shí),貌似接近ms
{
int y;
while(i--)
{
  for(y=1;y<2;y++);
}
}
void  init_smg()     //單片機(jī)測(cè)試用滴
{
P1=tab[3];
P0=5;
}
void Init_mouse(void)
{
     TCON=0x00;
     EA=1;
     EX0=1;//允許外部中斷1
     ET0=0x01;//允許全局中斷,允許定時(shí)器/計(jì)數(shù)器0溢出中斷
     PX0=1;//設(shè)置中斷優(yōu)先級(jí)
}

/***********************************************************************
  發(fā)送數(shù)據(jù)
************************************************************************/
void mouse_send_data(unsigned char dat)
{
unsigned char i;
EX0=0;                              /*關(guān)閉外部中斷1*/
ACC=dat;                            /*將要發(fā)送的數(shù)據(jù)放入A寄存器*/
mouse_CLK=0;                       /*拉低時(shí)鐘線*/
delay10us(200);                      /*延時(shí)100us以上*/
mouse_SDA=0;                        /*拉低數(shù)據(jù)線*/
delay10us(40);
mouse_CLK=1;                       /*釋放時(shí)鐘線*/
for(i=0;i<=7;i++)             /*低位在前,一次發(fā)送8個(gè)數(shù)據(jù)位*/
{
  while(mouse_CLK==1);           /*等待設(shè)備拉低時(shí)鐘線*/
  mouse_SDA=(dat>>i)&0x01;      /*發(fā)送數(shù)據(jù)位*/
  while(mouse_CLK==0);           /*等待設(shè)備釋放時(shí)鐘線*/
}
while(mouse_CLK==1);
mouse_SDA=~P;                       /*發(fā)送校驗(yàn)位,奇校驗(yàn)*/
while(mouse_CLK==0);
while(mouse_CLK==1);
mouse_SDA=1;                        /*發(fā)送停止位*/
while(mouse_CLK==0);         
while(mouse_CLK==1);    /*應(yīng)答位*/
while(mouse_CLK==0);
EX0=1;                              /*打開(kāi)外部中斷1*/
}
/*********************************************
    奇校檢
**********************************************/
unsigned char Checkout(void)   
{
ACC=mouse_byte;
if(~P==mouse_buffer[9])
  return 1;
else
  return 0;
}
/*********************************************************
     數(shù)據(jù)分析及處理
**********************************************************/
void data_analyse(void)
{
//將收到的11位信號(hào)中截取8位數(shù)據(jù)放進(jìn)mouse_byte
mouse_byte_bit0=mouse_buffer[1];
mouse_byte_bit1=mouse_buffer[2];
mouse_byte_bit2=mouse_buffer[3];
mouse_byte_bit3=mouse_buffer[4];
mouse_byte_bit4=mouse_buffer[5];
mouse_byte_bit5=mouse_buffer[6];
mouse_byte_bit6=mouse_buffer[7];
mouse_byte_bit7=mouse_buffer[8];
if(Checkout())//如果校驗(yàn)正確
{
  if(mouse_data_bit<4)         
   mouse_data[mouse_data_bit++]=mouse_byte;
  if(mouse_data_bit==4)
  {
   mouse_data_bit=0;
   if(mouse_data[0]&0x10)//如果"X sign bit"為1,表示鼠標(biāo)向左移
   {
    move_x-=(256-mouse_data[1]);//x坐標(biāo)減
   }
   else
   {
    move_x+=mouse_data[1];//x坐標(biāo)加
   }
   if(mouse_data[0]&0x20)
   {
    move_y-=(256-mouse_data[2]);//y坐標(biāo)減
   }
   else
   {
    move_y+=mouse_data[2];//y坐標(biāo)加
   }
  }
  P1=tab[move_x/10000];    //避免執(zhí)行串口程序時(shí)單片機(jī)不顯示而設(shè)計(jì)
  P0=4;
  delay(200);
  P1=tab[move_x/1000%10];
  P0=3;
  delay(200);
  P1=tab[move_x/100%10];
  P0=2;
  delay(200);
  P1=tab[move_x/10%10];
  P0=1;
  delay(200);  
  P1=tab[move_x%10];
  P0=0;
  delay(200);
}
}
/**************************************************
       外部中斷1
***************************************************/
void ReceiveData(void)  interrupt 0
{
if(mouse_buffer_bit<=10)
{                                 
  while(mouse_CLK==0);//等待設(shè)備拉高時(shí)鐘線
  mouse_buffer[mouse_buffer_bit++]=mouse_SDA;//接收數(shù)據(jù)
}
if(mouse_buffer_bit==11)
{
  data_analyse();//數(shù)據(jù)分析及處理
  mouse_buffer_bit=0;
}           
}
#endif

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表