久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

求解決PIC18F4520為什么不能讀出MPU6050陀螺儀的數(shù)據(jù)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:169653 發(fā)表于 2017-9-11 15:14 | 只看該作者 回帖獎勵 |倒序?yàn)g覽 |閱讀模式
100黑幣
  1. #include <p18cxxx.h>    /*18F系列單片機(jī)頭文件*/
  2. #include "k18.h"        /*開發(fā)板頭文件*/
  3. #include "REG.h"
  4. #define uchar    unsigned char
  5. #define uint     unsigned int
  6. #define ulong    unsigned long
  7. #define ushort   unsigned short  
  8. #define IIC_SCL  PORTCbits.RC5
  9. #define IIC_SDA  PORTCbits.RC4
  10. #define READ_SDA PORTCbits.RC4  //輸入SDA
  11. #define IIC_SDA_TRIS TRISCbits.TRISC3
  12. #define SDA_IN()   1
  13. #define SDA_OUT()  0
  14. float   Angle[3];        //陀螺儀角度        
  15. float   a[3];            //陀螺儀加速度
  16. float   w[3];            //陀螺儀角速度
  17. float   h[3];                           
  18. uchar   chrTemp[22];
  19. ushort  data[3];
  20. void PIC18F_High_isr(void);  /*中斷服務(wù)函數(shù)聲明*/
  21. void PIC18F_Low_isr(void);

  22. #pragma code high_vector_section=0x8

  23. void high_vector (void)
  24. {
  25.         _asm goto PIC18F_High_isr _endasm   /*通過一條跳轉(zhuǎn)指令(匯編指令),跳轉(zhuǎn)到中斷服務(wù)函數(shù)(中斷服務(wù)程序)處*/
  26. }
  27. #pragma code low_vector_section=0x18
  28. void low_vector (void)
  29. {
  30.         _asm goto PIC18F_Low_isr _endasm
  31. }
  32. #pragma code

  33. #pragma interrupt PIC18F_High_isr
  34. void PIC18F_High_isr (void)
  35. {   
  36.         PIR1bits.RCIF=0;

  37. }
  38. #pragma interruptlow PIC18F_Low_isr

  39. void PIC18F_Low_isr (void)
  40. {

  41. }
  42. void Delay_us(uchar n)
  43. {
  44.     uint j,k;
  45.     for(j=0;j<n;j++)         
  46.       for(k=1;k>0;k--);
  47. }
  48. void Delay_ms(uint n)
  49. {
  50.     uint j,k;
  51.     for(j=0;j<n;j++)
  52.       for(k=730;k>0;k--);
  53. }
  54. void IIC_Init(void)
  55. {                        
  56.         
  57.         SDA_OUT();     //sda線輸出
  58.         IIC_SDA=1;                    
  59.         IIC_SCL=1;
  60. }
  61. /**************************實(shí)現(xiàn)函數(shù)********************************************
  62. *函數(shù)原型:                void IIC_Start(void)
  63. *功  能:                產(chǎn)生IIC起始信號
  64. *******************************************************************************/
  65. void IIC_Start(void)
  66. {
  67.         SDA_OUT();     //sda線輸出
  68.         IIC_SDA=1;                    
  69.         IIC_SCL=1;        
  70.         Delay_us(5);
  71.          IIC_SDA=0;//START:when CLK is high,DATA change form high to low         
  72.         Delay_us(5);
  73.         IIC_SCL=0;//鉗住I2C總線,準(zhǔn)備發(fā)送或接收數(shù)據(jù)
  74. }

  75. /**************************實(shí)現(xiàn)函數(shù)********************************************
  76. *函數(shù)原型:                void IIC_Stop(void)
  77. *功  能:            //產(chǎn)生IIC停止信號
  78. *******************************************************************************/         
  79. void IIC_Stop(void)
  80. {
  81.         SDA_OUT();//sda線輸出
  82.         IIC_SCL=0;
  83.         IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
  84.          
  85.         Delay_us(5);
  86.         IIC_SCL=1;
  87.         IIC_SDA=1;//發(fā)送I2C總線結(jié)束信號
  88.         
  89.         Delay_us(5);                                                                  
  90. }

  91. /**************************實(shí)現(xiàn)函數(shù)********************************************
  92. *函數(shù)原型:                u8 IIC_Wait_Ack(void)
  93. *功  能:            等待應(yīng)答信號到來
  94. //返回值:1,接收應(yīng)答失敗
  95. //        0,接收應(yīng)答成功
  96. *******************************************************************************/
  97. uchar IIC_Wait_Ack(void)
  98. {
  99.         uchar  ucErrTime=0;
  100.         SDA_IN();      //SDA設(shè)置為輸入  
  101.         IIC_SDA=1;
  102.         Delay_us(5);         
  103.         while(READ_SDA)
  104.         {
  105.                 ucErrTime++;
  106.                 if(ucErrTime>50)
  107.                 {
  108.                         IIC_Stop();
  109.                         return 1;
  110.                 }
  111.                 Delay_us(5);
  112.         }  
  113.             IIC_SCL=1;
  114.             Delay_us(5);
  115.             IIC_SCL=0;//時鐘輸出0  
  116.             return 0;  
  117. }

  118. /**************************實(shí)現(xiàn)函數(shù)********************************************
  119. *函數(shù)原型:                void IIC_Ack(void)
  120. *功  能:            產(chǎn)生ACK應(yīng)答
  121. *******************************************************************************/
  122. void IIC_Ack(void)
  123. {
  124.         IIC_SCL=0;
  125.         SDA_OUT();
  126.         IIC_SDA=0;
  127.         Delay_us(5);
  128.         IIC_SCL=1;
  129.         Delay_us(5);
  130.         IIC_SCL=0;
  131. }
  132.         
  133. /**************************實(shí)現(xiàn)函數(shù)********************************************
  134. *函數(shù)原型:                void IIC_NAck(void)
  135. *功  能:            產(chǎn)生NACK應(yīng)答
  136. *******************************************************************************/            
  137. void IIC_NAck(void)
  138. {
  139.         IIC_SCL=0;
  140.         SDA_OUT();
  141.         IIC_SDA=1;
  142.         
  143.         Delay_us(5);
  144.         IIC_SCL=1;
  145.         Delay_us(5);
  146.         IIC_SCL=0;
  147. }                                                                              

  148. /**************************實(shí)現(xiàn)函數(shù)********************************************
  149. *函數(shù)原型:                void IIC_Send_Byte(u8 txd)
  150. *功  能:            IIC發(fā)送一個字節(jié)
  151. *******************************************************************************/                  
  152. void IIC_Send_Byte(uint txd)
  153. {                        
  154.     uint t;
  155.         SDA_OUT();            
  156.     IIC_SCL=0;//拉低時鐘開始數(shù)據(jù)傳輸
  157.     for(t=0;t<8;t++)
  158.     {              
  159.         IIC_SDA=(txd&0x80)>>7;
  160.         txd<<=1;           
  161.                         
  162.                 Delay_us(5);   
  163.                 IIC_SCL=1;
  164.                 Delay_us(5);
  165.                 IIC_SCL=0;        
  166.                 Delay_us(5);
  167.     }         
  168. }         

  169. /**************************實(shí)現(xiàn)函數(shù)********************************************
  170. *函數(shù)原型:                u8 IIC_Read_Byte(unsigned char ack)
  171. *功  能:            //讀1個字節(jié),ack=1時,發(fā)送ACK,ack=0,發(fā)送nACK
  172. *******************************************************************************/  
  173. uchar  IIC_Read_Byte(uchar ack)
  174. {
  175.         uchar i,receive=0;
  176.         SDA_IN();//SDA設(shè)置為輸入
  177.     for(i=0;i<8;i++ )
  178.         {
  179.         IIC_SCL=0;

  180.                 Delay_us(5);
  181.                 IIC_SCL=1;
  182.         receive<<=1;
  183.         if(READ_SDA)receive++;   
  184.                
  185.                 Delay_us(5);
  186.     }                                         
  187.     if (ack)
  188.         IIC_Ack(); //發(fā)送ACK
  189.     else
  190.         IIC_NAck();//發(fā)送nACK  
  191.     return receive;
  192. }

  193. /**************************實(shí)現(xiàn)函數(shù)********************************************
  194. *函數(shù)原型:                u8 IICreadBytes(u8 dev, u8 reg, u8 length, u8 *data)
  195. *功  能:            讀取指定設(shè)備 指定寄存器的 length個值
  196. 輸入        dev  目標(biāo)設(shè)備地址
  197.                 reg          寄存器地址
  198.                 length 要讀的字節(jié)數(shù)
  199.                 *data  讀出的數(shù)據(jù)將要存放的指針
  200. 返回   讀出來的字節(jié)數(shù)量
  201. *******************************************************************************/
  202. ulong IICreadBytes(uchar dev, uchar reg, ulong length, ulong  *data)
  203. {
  204.     uint count = 0;
  205.         
  206.         IIC_Start();
  207.         IIC_Send_Byte(dev<<1);           //發(fā)送寫命令
  208.         IIC_Wait_Ack();
  209.         IIC_Send_Byte(reg);   //發(fā)送地址
  210.     IIC_Wait_Ack();         
  211.         IIC_Start();
  212.         IIC_Send_Byte((dev<<1)+1);  //進(jìn)入接收模式        
  213.         IIC_Wait_Ack();        
  214.     for(count=0;count<length;count++)
  215.             {                 
  216.                  if(count!=length-1)data[count]=IIC_Read_Byte(1);  //帶ACK的讀數(shù)據(jù)
  217.                  else  data[count]=IIC_Read_Byte(0);         //最后一個字節(jié)NACK
  218.             }
  219.     IIC_Stop();//產(chǎn)生一個停止條件
  220.     return count;
  221. }

  222. /**************************實(shí)現(xiàn)函數(shù)********************************************
  223. *函數(shù)原型:                u8 IICwriteBytes(u8 dev, u8 reg, u8 length, u8 *data)
  224. *功  能:            將多個字節(jié)寫入指定設(shè)備 指定寄存器
  225. 輸入        dev     目標(biāo)設(shè)備地址
  226.                 reg            寄存器地址
  227.                 length  要寫的字節(jié)數(shù)
  228.                 *data   將要寫的數(shù)據(jù)的首地址
  229. 返回   返回是否成功
  230. *******************************************************************************/
  231. ulong IICwriteBytes(uchar dev, uchar reg, ulong length, ulong  *data)
  232. {

  233.          uint count = 0;
  234.         IIC_Start();
  235.         IIC_Send_Byte(dev<<1);           //發(fā)送寫命令
  236.         IIC_Wait_Ack();
  237.         IIC_Send_Byte(reg);   //發(fā)送地址
  238.         IIC_Wait_Ack();         
  239.         for(count=0;count<length;count++)
  240.       {
  241.                 IIC_Send_Byte(data[count]);
  242.                 IIC_Wait_Ack();
  243.       }
  244.         IIC_Stop();//產(chǎn)生一個停止條件

  245.     return 1; //status == 0;
  246.         
  247. }
  248. void ShortToChar(short sData,unsigned char cData[])
  249. {
  250.         cData[0]=sData&0xff;
  251.         cData[1]=sData>>8;
  252. }
  253. short CharToShort(unsigned char cData[])
  254. {
  255.         return ((short)cData[1]<<8)|cData[0];
  256. }
  257. void main(void)
  258. {        
  259.         k18_init();           /*K18開發(fā)板初始化*/
  260.         IIC_Init();        
  261.         RCSTAbits.SPEN = 1;   /*使能串口(將RX和TX引腳配置為串口引腳)*/
  262.         TXSTAbits.SYNC = 0;   /*異步模式*/
  263.         SPBRG = 0x47;         /*波特率寄存器置值,設(shè)置波特率38400*/
  264.         TXSTAbits.BRGH=1;  /*速度模式:高速*/
  265.         RCSTAbits.CREN=1;  /*接收使能*/
  266.         TXSTAbits.TXEN=1;  /*發(fā)送使能*/               
  267.         IPR1bits.RCIP=1;      /*設(shè)定串口接收中斷為高優(yōu)先級,本句也可以省略,復(fù)位后默認(rèn)為高優(yōu)先級*/
  268.         PIE1bits.RCIE=1;      /*串口接收中斷允許*/
  269.         INTCONbits.PEIE=1;    /*外設(shè)中斷允許*/
  270.         INTCONbits.GIE=1;     /*開總中斷*/        
  271.         while(1)
  272.         {   
  273.                 Delay_ms(100);
  274.                 IICreadBytes(0x55, AX, 24,&chrTemp[0]);
  275.                
  276.                 //a[0] = (float)CharToShort(&chrTemp[0])/32768*16;
  277.                 //a[1] = (float)CharToShort(&chrTemp[2])/32768*16;
  278.         //        a[2] = (float)CharToShort(&chrTemp[4])/32768*16;
  279.         //        w[0] = (float)CharToShort(&chrTemp[6])/32768*2000;
  280.         //        w[1] = (float)CharToShort(&chrTemp[8])/32768*2000;
  281.         //        w[2] = (float)CharToShort(&chrTemp[10])/32768*2000;
  282.         //        h[0] = CharToShort(&chrTemp[12]);
  283.         //        h[1] = CharToShort(&chrTemp[14]);
  284.         //        h[2] = CharToShort(&chrTemp[16]);
  285.                 Angle[0] = (float)CharToShort(&chrTemp[18])/32768*180;
  286.             Angle[1] = (float)CharToShort(&chrTemp[20])/32768*180;
  287.                 Angle[2] = (float)CharToShort(&chrTemp[22])/32768*180;
  288.                

  289.         /*        IICreadBytes(0x51, AX, 24,&chrTemp[0]);
  290.                 a[0] = (float)CharToShort(&chrTemp[0])/32768*16;
  291.                 a[1] = (float)CharToShort(&chrTemp[2])/32768*16;
  292.                 a[2] = (float)CharToShort(&chrTemp[4])/32768*16;
  293.                 w[0] = (float)CharToShort(&chrTemp[6])/32768*2000;
  294.                 w[1] = (float)CharToShort(&chrTemp[8])/32768*2000;
  295.                 w[2] = (float)CharToShort(&chrTemp[10])/32768*2000;
  296.                 h[0] = CharToShort(&chrTemp[12]);
  297.                 h[1] = CharToShort(&chrTemp[14]);
  298.                 h[2] = CharToShort(&chrTemp[16]);
  299.                 Angle[0] = (float)CharToShort(&chrTemp[18])/32768*180;
  300.                 Angle[1] = (float)CharToShort(&chrTemp[20])/32768*180;
  301.                 Angle[2] = (float)CharToShort(&chrTemp[22])/32768*180;
  302.         */
  303.                  // uint i;
  304.                   //data[0]=MPU6050_I2C_ReadByte(GXH);
  305.                  // a[1]=data[1]/32768*16;
  306.                   //a[2]=data[2]/32768*16;
  307.                   //a[3]=data[3]/32768*16;
  308.               //if((data[0]=0x51)|(data[0]=0x53)|(data[0]=0x50))  //讀取MPU6050的地址
  309.          //{
  310.                    //  for(i=0;i<3;i++)
  311.                    //  data[1]=(GXH<<8|GXL)/32768*180;
  312.                    //  data[2]=(GYH<<8|GYL)/32768*180;
  313.                     // data[3]=(GZH<<8|GZL)/32768*180;
  314.                    //  TXREG=data[0];
  315.                   
  316.                
  317.                //}
  318.                 /*a[0] = (float)CharToShort(&chrTemp[0])/32768*16;
  319.                 a[1] = (float)CharToShort(&chrTemp[2])/32768*16;
  320.                        a[2] = (float)CharToShort(&chrTemp[4])/32768*16;
  321.                     w[0] = (float)CharToShort(&chrTemp[6])/32768*2000;
  322.                      w[1] = (float)CharToShort(&chrTemp[8])/32768*2000;
  323.                     w[2] = (float)CharToShort(&chrTemp[10])/32768*2000;
  324.                     h[0] = CharToShort(&chrTemp[12]);
  325.                     h[1] = CharToShort(&chrTemp[14]);
  326.                     h[2] = CharToShort(&chrTemp[16]);               
  327.                     Angle[0] = (float)CharToShort(&chrTemp[18])/32768*180;
  328.                     Angle[1] = (float)CharToShort(&chrTemp[20])/32768*180;
  329.                     Angle[2] = (float)CharToShort(&chrTemp[22])/32768*180;*/              
  330.         }
  331. }


復(fù)制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:123289 發(fā)表于 2017-9-11 18:48 | 只看該作者
一定是你自己不會寫程序
回復(fù)

使用道具 舉報(bào)

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

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 亚洲一区二区精品视频在线观看 | 日韩精品一区二区三区在线观看 | 日韩视频在线播放 | 青青草免费在线视频 | 日韩二| 亚洲国产激情 | 国产成人综合网 | 国产乱码久久久 | 欧美特级黄色 | 一区二区三区国产好 | 亚洲一区二区三区观看 | 国产一级久久久久 | 国产伦精品一区二区三区在线 | 日韩在线免费 | 日本激情视频在线播放 | 欧美精品第一页 | 日韩中文字幕一区 | 精品久久久久久国产 | 日韩久久久久久 | 99精品在线观看 | 91精品观看 | 国产一级电影在线 | 国产精品久久久久久久7777 | 91色在线 | 日韩专区中文字幕 | 一区二区日韩精品 | 久久在线 | 国产视频二区在线观看 | 91国内精品| 久久国产激情视频 | 天天操天天舔 | 久久久久久久国产精品 | 久久久夜夜夜 | 久久久噜噜噜www成人网 | 成人在线中文字幕 | 国产精品久久国产精品 | 日韩和的一区二在线 | 黄色一级大片在线免费看产 | 欧美日韩一区在线播放 | www精品美女久久久tv | 亚洲中午字幕 |