PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Các ngôn ngữ lập trình khác (CCS C, HT PIC,...)

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 17-09-2013, 11:10 AM   #1
levui2009
Nhập môn đệ tử
 
Tham gia ngày: Oct 2009
Bài gửi: 9
:
Cảm biến gia tốc góc MPU6050_Pic16F887

Giao tiếp MPU0650 với Pic16F887 dùng Hitech - C. Không biết lỗi chỗ nào chương trình chưa chạy. Mong các bạn giúp. Cám ơn.

#include <htc.h>
#include <stdio.h>
#include "uart.h"
#include "i2c.h"
//#include "lcd.h"

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);

//baudrate 19200

//+++++++++++++++++++++++++DEFINE MPU6050+++++++++++++++++++++++++++
#define SMPLRT_DIV 0x19 //0x07(125Hz)
#define CONFIG 0x1A //0x06(5Hz)
#define GYRO_CONFIG 0x1B //0x18(2000deg/s)
#define ACCEL_CONFIG 0x1C //0x01(2G£¬5Hz)
#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48
#define PWR_MGMT_1 0x6B //
#define WHO_AM_I 0x75 //
#define MPU6050_ADD 0x68
#define MPU6050_SLAVE_WRT 0xD0 //dia chi cua cam bien mpu6050
#define MPU6050_SLAVE_RD 0xD1


int Mpu6050_Read(unsigned char address){
char lsb, msb;

i2c_start();
i2c_write(MPU6050_SLAVE_WRT);
i2c_write(address);
i2c_start();
i2c_write(MPU6050_SLAVE_RD);
msb = i2c_read(1);
lsb = i2c_read(0);
i2c_stop();
return ((msb<<8) | lsb);
}
void Mpu6050_Write(unsigned char address,unsigned char Data) {
i2c_start();
i2c_write(MPU6050_SLAVE_WRT);
i2c_write(address);
i2c_write(Data);
i2c_stop();
}
int GetData(unsigned char address){
char H,L;
H=Mpu6050_Read(address);
L=Mpu6050_Read(address+1);
return (H<<8)+L;
}

void Mpu6050_init(){
Mpu6050_Write(PWR_MGMT_1, 0x00); // internal 8MHz, disabled SLEEP mode, disable CYCLE mode
Mpu6050_Write(SMPLRT_DIV, 0x07); //sample rate: 8khz
Mpu6050_Write(CONFIG, 0x06); //DLPF disable
Mpu6050_Write(GYRO_CONFIG, 0x18); //full scale range mode 3 +-2000do/s
Mpu6050_Write(ACCEL_CONFIG, 0x01); //full scale range mode 1 +-4g

// Acc_MPU6050_init
//0x00: +-2g 16384 LSB/g
//0x08: +-4g 8192 LSB/g
//0x10: +-8g 4096 LSB/g
//0x18: +-16g 2048 LSB/g

}

//===================================
void main(){
int x_accel,y_accel,z_accel,x_gyro,y_gyro,z_gyro;

//lcd_init();
uart_init();
i2c_init();
Mpu6050_init();

while(1){
x_accel = GetData(ACCEL_XOUT_H);
//// y_accel = GetData(ACCEL_YOUT_H);
//// z_accel = GetData(ACCEL_ZOUT_H);
// x_gyro = GetData(GYRO_XOUT_H);
//// y_gyro = GetData(GYRO_YOUT_H);
z_gyro = GetData(GYRO_ZOUT_H);
//

printf("Ax= %u\r\n",x_accel);
__delay_ms(100);
}

}

void putch(unsigned char byte){
// output one byte
while(!TXIF) // set when register is empty
continue;
TXREG = byte;
}
levui2009 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 05:44 PM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam