標(biāo)題:
求解決PIC18F4520為什么不能讀出MPU6050陀螺儀的數(shù)據(jù)
[打印本頁]
作者:
武起風(fēng)云
時間:
2017-9-11 15:14
標(biāo)題:
求解決PIC18F4520為什么不能讀出MPU6050陀螺儀的數(shù)據(jù)
#include <p18cxxx.h> /*18F系列單片機頭文件*/
#include "k18.h" /*開發(fā)板頭文件*/
#include "REG.h"
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define ushort unsigned short
#define IIC_SCL PORTCbits.RC5
#define IIC_SDA PORTCbits.RC4
#define READ_SDA PORTCbits.RC4 //輸入SDA
#define IIC_SDA_TRIS TRISCbits.TRISC3
#define SDA_IN() 1
#define SDA_OUT() 0
float Angle[3]; //陀螺儀角度
float a[3]; //陀螺儀加速度
float w[3]; //陀螺儀角速度
float h[3];
uchar chrTemp[22];
ushort data[3];
void PIC18F_High_isr(void); /*中斷服務(wù)函數(shù)聲明*/
void PIC18F_Low_isr(void);
#pragma code high_vector_section=0x8
void high_vector (void)
{
_asm goto PIC18F_High_isr _endasm /*通過一條跳轉(zhuǎn)指令(匯編指令),跳轉(zhuǎn)到中斷服務(wù)函數(shù)(中斷服務(wù)程序)處*/
}
#pragma code low_vector_section=0x18
void low_vector (void)
{
_asm goto PIC18F_Low_isr _endasm
}
#pragma code
#pragma interrupt PIC18F_High_isr
void PIC18F_High_isr (void)
{
PIR1bits.RCIF=0;
}
#pragma interruptlow PIC18F_Low_isr
void PIC18F_Low_isr (void)
{
}
void Delay_us(uchar n)
{
uint j,k;
for(j=0;j<n;j++)
for(k=1;k>0;k--);
}
void Delay_ms(uint n)
{
uint j,k;
for(j=0;j<n;j++)
for(k=730;k>0;k--);
}
void IIC_Init(void)
{
SDA_OUT(); //sda線輸出
IIC_SDA=1;
IIC_SCL=1;
}
/**************************實現(xiàn)函數(shù)********************************************
*函數(shù)原型: void IIC_Start(void)
*功 能: 產(chǎn)生IIC起始信號
*******************************************************************************/
void IIC_Start(void)
{
SDA_OUT(); //sda線輸出
IIC_SDA=1;
IIC_SCL=1;
Delay_us(5);
IIC_SDA=0;//START:when CLK is high,DATA change form high to low
Delay_us(5);
IIC_SCL=0;//鉗住I2C總線,準備發(fā)送或接收數(shù)據(jù)
}
/**************************實現(xiàn)函數(shù)********************************************
*函數(shù)原型: void IIC_Stop(void)
*功 能: //產(chǎn)生IIC停止信號
*******************************************************************************/
void IIC_Stop(void)
{
SDA_OUT();//sda線輸出
IIC_SCL=0;
IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
Delay_us(5);
IIC_SCL=1;
IIC_SDA=1;//發(fā)送I2C總線結(jié)束信號
Delay_us(5);
}
/**************************實現(xiàn)函數(shù)********************************************
*函數(shù)原型: u8 IIC_Wait_Ack(void)
*功 能: 等待應(yīng)答信號到來
//返回值:1,接收應(yīng)答失敗
// 0,接收應(yīng)答成功
*******************************************************************************/
uchar IIC_Wait_Ack(void)
{
uchar ucErrTime=0;
SDA_IN(); //SDA設(shè)置為輸入
IIC_SDA=1;
Delay_us(5);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>50)
{
IIC_Stop();
return 1;
}
Delay_us(5);
}
IIC_SCL=1;
Delay_us(5);
IIC_SCL=0;//時鐘輸出0
return 0;
}
/**************************實現(xiàn)函數(shù)********************************************
*函數(shù)原型: void IIC_Ack(void)
*功 能: 產(chǎn)生ACK應(yīng)答
*******************************************************************************/
void IIC_Ack(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=0;
Delay_us(5);
IIC_SCL=1;
Delay_us(5);
IIC_SCL=0;
}
/**************************實現(xiàn)函數(shù)********************************************
*函數(shù)原型: void IIC_NAck(void)
*功 能: 產(chǎn)生NACK應(yīng)答
*******************************************************************************/
void IIC_NAck(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=1;
Delay_us(5);
IIC_SCL=1;
Delay_us(5);
IIC_SCL=0;
}
/**************************實現(xiàn)函數(shù)********************************************
*函數(shù)原型: void IIC_Send_Byte(u8 txd)
*功 能: IIC發(fā)送一個字節(jié)
*******************************************************************************/
void IIC_Send_Byte(uint txd)
{
uint t;
SDA_OUT();
IIC_SCL=0;//拉低時鐘開始數(shù)據(jù)傳輸
for(t=0;t<8;t++)
{
IIC_SDA=(txd&0x80)>>7;
txd<<=1;
Delay_us(5);
IIC_SCL=1;
Delay_us(5);
IIC_SCL=0;
Delay_us(5);
}
}
/**************************實現(xiàn)函數(shù)********************************************
*函數(shù)原型: u8 IIC_Read_Byte(unsigned char ack)
*功 能: //讀1個字節(jié),ack=1時,發(fā)送ACK,ack=0,發(fā)送nACK
*******************************************************************************/
uchar IIC_Read_Byte(uchar ack)
{
uchar i,receive=0;
SDA_IN();//SDA設(shè)置為輸入
for(i=0;i<8;i++ )
{
IIC_SCL=0;
Delay_us(5);
IIC_SCL=1;
receive<<=1;
if(READ_SDA)receive++;
Delay_us(5);
}
if (ack)
IIC_Ack(); //發(fā)送ACK
else
IIC_NAck();//發(fā)送nACK
return receive;
}
/**************************實現(xiàn)函數(shù)********************************************
*函數(shù)原型: u8 IICreadBytes(u8 dev, u8 reg, u8 length, u8 *data)
*功 能: 讀取指定設(shè)備 指定寄存器的 length個值
輸入 dev 目標(biāo)設(shè)備地址
reg 寄存器地址
length 要讀的字節(jié)數(shù)
*data 讀出的數(shù)據(jù)將要存放的指針
返回 讀出來的字節(jié)數(shù)量
*******************************************************************************/
ulong IICreadBytes(uchar dev, uchar reg, ulong length, ulong *data)
{
uint count = 0;
IIC_Start();
IIC_Send_Byte(dev<<1); //發(fā)送寫命令
IIC_Wait_Ack();
IIC_Send_Byte(reg); //發(fā)送地址
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte((dev<<1)+1); //進入接收模式
IIC_Wait_Ack();
for(count=0;count<length;count++)
{
if(count!=length-1)data[count]=IIC_Read_Byte(1); //帶ACK的讀數(shù)據(jù)
else data[count]=IIC_Read_Byte(0); //最后一個字節(jié)NACK
}
IIC_Stop();//產(chǎn)生一個停止條件
return count;
}
/**************************實現(xiàn)函數(shù)********************************************
*函數(shù)原型: u8 IICwriteBytes(u8 dev, u8 reg, u8 length, u8 *data)
*功 能: 將多個字節(jié)寫入指定設(shè)備 指定寄存器
輸入 dev 目標(biāo)設(shè)備地址
reg 寄存器地址
length 要寫的字節(jié)數(shù)
*data 將要寫的數(shù)據(jù)的首地址
返回 返回是否成功
*******************************************************************************/
ulong IICwriteBytes(uchar dev, uchar reg, ulong length, ulong *data)
{
uint count = 0;
IIC_Start();
IIC_Send_Byte(dev<<1); //發(fā)送寫命令
IIC_Wait_Ack();
IIC_Send_Byte(reg); //發(fā)送地址
IIC_Wait_Ack();
for(count=0;count<length;count++)
{
IIC_Send_Byte(data[count]);
IIC_Wait_Ack();
}
IIC_Stop();//產(chǎn)生一個停止條件
return 1; //status == 0;
}
void ShortToChar(short sData,unsigned char cData[])
{
cData[0]=sData&0xff;
cData[1]=sData>>8;
}
short CharToShort(unsigned char cData[])
{
return ((short)cData[1]<<8)|cData[0];
}
void main(void)
{
k18_init(); /*K18開發(fā)板初始化*/
IIC_Init();
RCSTAbits.SPEN = 1; /*使能串口(將RX和TX引腳配置為串口引腳)*/
TXSTAbits.SYNC = 0; /*異步模式*/
SPBRG = 0x47; /*波特率寄存器置值,設(shè)置波特率38400*/
TXSTAbits.BRGH=1; /*速度模式:高速*/
RCSTAbits.CREN=1; /*接收使能*/
TXSTAbits.TXEN=1; /*發(fā)送使能*/
IPR1bits.RCIP=1; /*設(shè)定串口接收中斷為高優(yōu)先級,本句也可以省略,復(fù)位后默認為高優(yōu)先級*/
PIE1bits.RCIE=1; /*串口接收中斷允許*/
INTCONbits.PEIE=1; /*外設(shè)中斷允許*/
INTCONbits.GIE=1; /*開總中斷*/
while(1)
{
Delay_ms(100);
IICreadBytes(0x55, AX, 24,&chrTemp[0]);
//a[0] = (float)CharToShort(&chrTemp[0])/32768*16;
//a[1] = (float)CharToShort(&chrTemp[2])/32768*16;
// a[2] = (float)CharToShort(&chrTemp[4])/32768*16;
// w[0] = (float)CharToShort(&chrTemp[6])/32768*2000;
// w[1] = (float)CharToShort(&chrTemp[8])/32768*2000;
// w[2] = (float)CharToShort(&chrTemp[10])/32768*2000;
// h[0] = CharToShort(&chrTemp[12]);
// h[1] = CharToShort(&chrTemp[14]);
// h[2] = CharToShort(&chrTemp[16]);
Angle[0] = (float)CharToShort(&chrTemp[18])/32768*180;
Angle[1] = (float)CharToShort(&chrTemp[20])/32768*180;
Angle[2] = (float)CharToShort(&chrTemp[22])/32768*180;
/* IICreadBytes(0x51, AX, 24,&chrTemp[0]);
a[0] = (float)CharToShort(&chrTemp[0])/32768*16;
a[1] = (float)CharToShort(&chrTemp[2])/32768*16;
a[2] = (float)CharToShort(&chrTemp[4])/32768*16;
w[0] = (float)CharToShort(&chrTemp[6])/32768*2000;
w[1] = (float)CharToShort(&chrTemp[8])/32768*2000;
w[2] = (float)CharToShort(&chrTemp[10])/32768*2000;
h[0] = CharToShort(&chrTemp[12]);
h[1] = CharToShort(&chrTemp[14]);
h[2] = CharToShort(&chrTemp[16]);
Angle[0] = (float)CharToShort(&chrTemp[18])/32768*180;
Angle[1] = (float)CharToShort(&chrTemp[20])/32768*180;
Angle[2] = (float)CharToShort(&chrTemp[22])/32768*180;
*/
// uint i;
//data[0]=MPU6050_I2C_ReadByte(GXH);
// a[1]=data[1]/32768*16;
//a[2]=data[2]/32768*16;
//a[3]=data[3]/32768*16;
//if((data[0]=0x51)|(data[0]=0x53)|(data[0]=0x50)) //讀取MPU6050的地址
//{
// for(i=0;i<3;i++)
// data[1]=(GXH<<8|GXL)/32768*180;
// data[2]=(GYH<<8|GYL)/32768*180;
// data[3]=(GZH<<8|GZL)/32768*180;
// TXREG=data[0];
//}
/*a[0] = (float)CharToShort(&chrTemp[0])/32768*16;
a[1] = (float)CharToShort(&chrTemp[2])/32768*16;
a[2] = (float)CharToShort(&chrTemp[4])/32768*16;
w[0] = (float)CharToShort(&chrTemp[6])/32768*2000;
w[1] = (float)CharToShort(&chrTemp[8])/32768*2000;
w[2] = (float)CharToShort(&chrTemp[10])/32768*2000;
h[0] = CharToShort(&chrTemp[12]);
h[1] = CharToShort(&chrTemp[14]);
h[2] = CharToShort(&chrTemp[16]);
Angle[0] = (float)CharToShort(&chrTemp[18])/32768*180;
Angle[1] = (float)CharToShort(&chrTemp[20])/32768*180;
Angle[2] = (float)CharToShort(&chrTemp[22])/32768*180;*/
}
}
復(fù)制代碼
作者:
yzwzfyz
時間:
2017-9-11 18:48
一定是你自己不會寫程序
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1