标题:
求解决PIC18F4520为什么不能读出MPU6050陀螺仪的数据
[打印本页]
作者:
武起风云
时间:
2017-9-11 15:14
标题:
求解决PIC18F4520为什么不能读出MPU6050陀螺仪的数据
#include <p18cxxx.h> /*18F系列单片机头文件*/
#include "k18.h" /*开发板头文件*/
#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); /*中断服务函数声明*/
void PIC18F_Low_isr(void);
#pragma code high_vector_section=0x8
void high_vector (void)
{
_asm goto PIC18F_High_isr _endasm /*通过一条跳转指令(汇编指令),跳转到中断服务函数(中断服务程序)处*/
}
#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;
}
/**************************实现函数********************************************
*函数原型: void IIC_Start(void)
*功 能: 产生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总线,准备发送或接收数据
}
/**************************实现函数********************************************
*函数原型: void IIC_Stop(void)
*功 能: //产生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;//发送I2C总线结束信号
Delay_us(5);
}
/**************************实现函数********************************************
*函数原型: u8 IIC_Wait_Ack(void)
*功 能: 等待应答信号到来
//返回值:1,接收应答失败
// 0,接收应答成功
*******************************************************************************/
uchar IIC_Wait_Ack(void)
{
uchar ucErrTime=0;
SDA_IN(); //SDA设置为输入
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;
}
/**************************实现函数********************************************
*函数原型: void IIC_Ack(void)
*功 能: 产生ACK应答
*******************************************************************************/
void IIC_Ack(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=0;
Delay_us(5);
IIC_SCL=1;
Delay_us(5);
IIC_SCL=0;
}
/**************************实现函数********************************************
*函数原型: void IIC_NAck(void)
*功 能: 产生NACK应答
*******************************************************************************/
void IIC_NAck(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=1;
Delay_us(5);
IIC_SCL=1;
Delay_us(5);
IIC_SCL=0;
}
/**************************实现函数********************************************
*函数原型: void IIC_Send_Byte(u8 txd)
*功 能: IIC发送一个字节
*******************************************************************************/
void IIC_Send_Byte(uint txd)
{
uint t;
SDA_OUT();
IIC_SCL=0;//拉低时钟开始数据传输
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);
}
}
/**************************实现函数********************************************
*函数原型: u8 IIC_Read_Byte(unsigned char ack)
*功 能: //读1个字节,ack=1时,发送ACK,ack=0,发送nACK
*******************************************************************************/
uchar IIC_Read_Byte(uchar ack)
{
uchar i,receive=0;
SDA_IN();//SDA设置为输入
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(); //发送ACK
else
IIC_NAck();//发送nACK
return receive;
}
/**************************实现函数********************************************
*函数原型: u8 IICreadBytes(u8 dev, u8 reg, u8 length, u8 *data)
*功 能: 读取指定设备 指定寄存器的 length个值
输入 dev 目标设备地址
reg 寄存器地址
length 要读的字节数
*data 读出的数据将要存放的指针
返回 读出来的字节数量
*******************************************************************************/
ulong IICreadBytes(uchar dev, uchar reg, ulong length, ulong *data)
{
uint count = 0;
IIC_Start();
IIC_Send_Byte(dev<<1); //发送写命令
IIC_Wait_Ack();
IIC_Send_Byte(reg); //发送地址
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的读数据
else data[count]=IIC_Read_Byte(0); //最后一个字节NACK
}
IIC_Stop();//产生一个停止条件
return count;
}
/**************************实现函数********************************************
*函数原型: u8 IICwriteBytes(u8 dev, u8 reg, u8 length, u8 *data)
*功 能: 将多个字节写入指定设备 指定寄存器
输入 dev 目标设备地址
reg 寄存器地址
length 要写的字节数
*data 将要写的数据的首地址
返回 返回是否成功
*******************************************************************************/
ulong IICwriteBytes(uchar dev, uchar reg, ulong length, ulong *data)
{
uint count = 0;
IIC_Start();
IIC_Send_Byte(dev<<1); //发送写命令
IIC_Wait_Ack();
IIC_Send_Byte(reg); //发送地址
IIC_Wait_Ack();
for(count=0;count<length;count++)
{
IIC_Send_Byte(data[count]);
IIC_Wait_Ack();
}
IIC_Stop();//产生一个停止条件
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开发板初始化*/
IIC_Init();
RCSTAbits.SPEN = 1; /*使能串口(将RX和TX引脚配置为串口引脚)*/
TXSTAbits.SYNC = 0; /*异步模式*/
SPBRG = 0x47; /*波特率寄存器置值,设置波特率38400*/
TXSTAbits.BRGH=1; /*速度模式:高速*/
RCSTAbits.CREN=1; /*接收使能*/
TXSTAbits.TXEN=1; /*发送使能*/
IPR1bits.RCIP=1; /*设定串口接收中断为高优先级,本句也可以省略,复位后默认为高优先级*/
PIE1bits.RCIE=1; /*串口接收中断允许*/
INTCONbits.PEIE=1; /*外设中断允许*/
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;*/
}
}
复制代码
作者:
yzwzfyz
时间:
2017-9-11 18:48
一定是你自己不会写程序
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1