找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

mouse.h頭文件下載( 51單片機(jī)ps2 鼠標(biāo) 驅(qū)動)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:71259 發(fā)表于 2014-12-29 01:02 | 只看該作者 回帖獎勵 |正序瀏覽 |閱讀模式
本帖最后由 daming 于 2014-12-29 01:05 編輯

在代碼的底部 點擊全部復(fù)制,保存為mouse.h即可頭文件下載到電腦
  1. /******************************************************
  2.      演示鼠標(biāo)引腳定義
  3. CLK : 白色
  4. SDA  : 綠色
  5. GND:   橙色
  6. VCC:   藍(lán)色
  7. *******************************************************/
  8. #include"delay52.h"
  9. #ifndef MOUSE_H
  10. #define MOUSE_H

  11. sbit mouse_SDA=P3^4;//數(shù)據(jù)線P3_5
  12. sbit mouse_CLK=P3^3;//時鐘線P3_3


  13.                            
  14. unsigned char bdata mouse_byte; //接收字節(jié)
  15. sbit mouse_byte_bit0=mouse_byte^0;//mouse_byte第0位
  16. sbit mouse_byte_bit1=mouse_byte^1;//mouse_byte第1位
  17. sbit mouse_byte_bit2=mouse_byte^2;//mouse_byte第2位
  18. sbit mouse_byte_bit3=mouse_byte^3;//mouse_byte第3位
  19. sbit mouse_byte_bit4=mouse_byte^4;//mouse_byte第4位
  20. sbit mouse_byte_bit5=mouse_byte^5;//mouse_byte第5位
  21. sbit mouse_byte_bit6=mouse_byte^6;//mouse_byte第6位
  22. sbit mouse_byte_bit7=mouse_byte^7;//mouse_byte第7位

  23. unsigned char bdata mouse_fuction;//功能信息字節(jié)
  24.                      
  25. unsigned char mouse_buffer[11];//接收位數(shù)據(jù)緩沖區(qū)
  26. unsigned char mouse_buffer_bit=0;//mouse_buffer[mouse_buffer_bit]
  27. unsigned char mouse_data[3];//接收鼠標(biāo)數(shù)據(jù)緩沖區(qū),分別存放:功能信息字節(jié),x位移量,y位移量
  28. unsigned char mouse_data_bit=0;//mouse_data[mouse_data_bit]

  29. unsigned int move_x=10000;//存放橫坐標(biāo)
  30. unsigned int move_y=10000;//存放縱坐標(biāo)

  31. void Init_mouse(void)
  32. {
  33.      TCON=0x00;
  34.      EA=1;
  35.      EX1=1;//允許外部中斷1
  36.      ET0=0x01;//允許全局中斷,允許定時器/計數(shù)器0溢出中斷
  37.      PX1=1;//設(shè)置中斷優(yōu)先級
  38. }


  39. /***********************************************************************
  40.   發(fā)送數(shù)據(jù)
  41. ************************************************************************/
  42. void mouse_send_data(unsigned char dat)
  43. {
  44. unsigned char i;
  45. EX1=0;                              /*關(guān)閉外部中斷1*/
  46. ACC=dat;                            /*將要發(fā)送的數(shù)據(jù)放入A寄存器*/
  47. mouse_CLK=0;                       /*拉低時鐘線*/
  48. delay10us(200);                      /*延時100us以上*/
  49. mouse_SDA=0;                        /*拉低數(shù)據(jù)線*/
  50. delay10us(40);
  51. mouse_CLK=1;                       /*釋放時鐘線*/
  52. for(i=0;i<=7;i++)             /*低位在前,一次發(fā)送8個數(shù)據(jù)位*/
  53. {
  54.   while(mouse_CLK==1);           /*等待設(shè)備拉低時鐘線*/
  55.   mouse_SDA=(dat>>i)&0x01;      /*發(fā)送數(shù)據(jù)位*/
  56.   while(mouse_CLK==0);           /*等待設(shè)備釋放時鐘線*/
  57. }
  58. while(mouse_CLK==1);
  59. mouse_SDA=~P;                       /*發(fā)送校驗位,奇校驗*/
  60. while(mouse_CLK==0);
  61. while(mouse_CLK==1);
  62. mouse_SDA=1;                        /*發(fā)送停止位*/
  63. while(mouse_CLK==0);        
  64. while(mouse_CLK==1);    /*應(yīng)答位*/
  65. while(mouse_CLK==0);
  66. EX1=1;                              /*打開外部中斷1*/
  67. }

  68. /*********************************************
  69.     奇校檢
  70. **********************************************/
  71. unsigned char Checkout(void)   
  72. {
  73. ACC=mouse_byte;
  74. if(~P==mouse_buffer[9])
  75.   return 1;
  76. else
  77.   return 0;
  78. }

  79. /*********************************************************
  80.      數(shù)據(jù)分析及處理
  81. **********************************************************/
  82. void data_analyse(void)
  83. {
  84. //將收到的11位信號中截取8位數(shù)據(jù)放進(jìn)mouse_byte
  85. mouse_byte_bit0=mouse_buffer[1];
  86. mouse_byte_bit1=mouse_buffer[2];
  87. mouse_byte_bit2=mouse_buffer[3];
  88. mouse_byte_bit3=mouse_buffer[4];
  89. mouse_byte_bit4=mouse_buffer[5];
  90. mouse_byte_bit5=mouse_buffer[6];
  91. mouse_byte_bit6=mouse_buffer[7];
  92. mouse_byte_bit7=mouse_buffer[8];
  93. if(Checkout())//如果校驗正確
  94. {
  95.   if(mouse_data_bit<3)        
  96.    mouse_data[mouse_data_bit++]=mouse_byte;
  97.   if(mouse_data_bit==3)
  98.   {
  99.    mouse_data_bit=0;
  100.    if(mouse_data[0]&0x10)//如果"X sign bit"為1,表示鼠標(biāo)向左移
  101.    {
  102.     move_x-=(256-mouse_data[1]);//x坐標(biāo)減
  103.    }
  104.    else
  105.    {
  106.     move_x+=mouse_data[1];//x坐標(biāo)加
  107.    }
  108.    if(mouse_data[0]&0x20)
  109.    {
  110.     move_y-=(256-mouse_data[2]);//y坐標(biāo)減
  111.    }
  112.    else
  113.    {
  114.     move_y+=mouse_data[2];//y坐標(biāo)加
  115.    }
  116.   }
  117. }
  118. }

  119. /**************************************************
  120.        外部中斷1
  121. ***************************************************/
  122. void ReceiveData(void)  interrupt 2
  123. {
  124. if(mouse_buffer_bit<=10)
  125. {                                 
  126.   while(mouse_CLK==0);//等待設(shè)備拉高時鐘線
  127.   mouse_buffer[mouse_buffer_bit++]=mouse_SDA;//接收數(shù)據(jù)
  128. }
  129. if(mouse_buffer_bit==10)
  130. {
  131.   data_analyse();//數(shù)據(jù)分析及處理
  132.   mouse_buffer_bit=0;
  133. }           
  134. }

  135. #endif
復(fù)制代碼


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

相關(guān)帖子

回復(fù)

使用道具 舉報

沙發(fā)
ID:690360 發(fā)表于 2020-8-13 14:34 | 只看該作者
delay52.h呢?
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

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