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 04-01-2012, 04:14 PM   #1
Hoanghovuong
Nhập môn đệ tử
 
Tham gia ngày: Nov 2011
Bài gửi: 1
:
Giao tiếp thông qua I2C

Chào mọi người, em đang có đề tài thực hiện đọc dữ liệu từ VĐK Pic 16F887 với gyroscope ITG 3200 thông qua giao tiếp I2C, em viết code test thử địa chỉ thì PIC nhận được 4 địa chỉ D2, D3, D4, D5. Khi thực hiện giao tiếp thì có vẻ như hàm đọc giá trị hoạt động, còn hàm ghi giá trị không hoạt động, vì tất cả các giá trị thanh ghi đọc về đều là 0xFF trong khi em đã cố gắng ghi các giá trị khởi động vào cá thanh ghi trên. Nhờ anh em xem giúp đoạn code chương trình giùm mình xem có sai sót chỗ nào mà mạch không chạy, hoặc code hàm đọc cũng bị sai rồi mà mình không biết (giá trị thanh ghi WHO_I_AM mặc định là 0x69 nhưng đọc về lại là 0xff)

Code:
#include <16F887.h> 
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT 
#use delay (clock=20000000) 
#use I2C(MASTER, sda=PIN_C4, scl=PIN_C3, FAST, FORCE_HW)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, BITS=8, PARITY=N, ERRORS) 
#include "LCD_CHANGED.c"
#include "ITG3200.h"

//int i2c_address = 0xD0; 
int i2c_write   = 0xD2;
int i2c_read    = 0xD3;


int Read_gyro(int address)
{
int data;
i2c_start();
i2c_write(i2c_write);
i2c_write(address);
i2c_write(i2c_read);
data = i2c_read();
i2c_stop();
return data;
}

/*void Write_gyro(int address,int data)
{
i2c_start();
i2c_write(i2c_write);
i2c_write(0xD4);
i2c_write(address);
i2c_write(data);
i2c_stop();  
} */

 void ITG3200_init ()
{
i2c_start();
i2c_write(i2c_write);
i2c_write(PWR_M);
i2c_write(0x00);
delay_us(5);
i2c_write(SMPL);
i2c_write(0x00);
delay_us(5);
i2c_write(DLPF);
i2c_write(0x19);
delay_us(5);
     /* Write_gyro(PWR_M, 0x00);           Reset
      Write_gyro(SMPL, 0x00);           Sapmle Rate Divider
      Write_gyro(DLPF, 0x19);           DLPF Full Scale
      Write_gyro(0x17, 0x00);       
      write (ITG, 0x3E, 0x40);            Power Management - Sleeping
      delay_us(10); */
}

void main() 
{ 
   int temp;
   int16 Gx, Gy, Gz;
   lcd_init();
   delay_ms(100); 
   //printf(lcd_putc,"\f %u ",i2c_command);
   ITG3200_init();
   delay_ms(100); 
   printf(lcd_putc," Doc Gyro  ");
   delay_ms(1000);
   printf("\nWHO_AM_I (0x00): 0x%x\n", Read_gyro(WHO));
   delay_ms(1000);
   printf("SMPLRT_DIV (0x15): 0x%x\n", Read_gyro(SMPL));
   printf("DLPF_FS (0x16): 0x%x\n", Read_gyro(DLPF));
   printf("INT_CFG (0x17): 0x%x\n", Read_gyro(INT_C));
   printf("INT_STATUS (0x1A): 0x%x\n", Read_gyro(INT_S));
   printf("TEMP_OUT_H (0x1B): 0x%x\n", Read_gyro(TMP_H));
   printf("TEMP_OUT_L (0x1C): 0x%x\n", Read_gyro(TMP_L));
   printf("GYRO_XOUT_H (0x1D): 0x%x\n", Read_gyro(GX_H));
   printf("GYRO_XOUT_L (0x1E): 0x%x\n", Read_gyro(GX_L));
   printf("GYRO_YOUT_H (0x1F): 0x%x\n", Read_gyro(GY_H));
   printf("GYRO_YOUT_L (0x20): 0x%x\n", Read_gyro(GY_L));
   printf("GYRO_ZOUT_H (0x21): 0x%x\n", Read_gyro(GZ_H));
   printf("GYRO_ZOUT_L (0x22): 0x%x\n", Read_gyro(GZ_L));
   printf("PWR_MGM (0x3E): 0x%x\n", Read_gyro(PWR_M));

   while (true) 
   { 
        temp = 0;
        temp = Read_gyro(GX_H);
        Gx   = temp << 8;
        Gx  |= Read_gyro(GX_L);
        
        temp = 0;
        temp = Read_gyro(GY_H);
        Gy   = temp << 8;
        Gy  |= Read_gyro(GY_L); 
        
        temp = 0;
        temp = Read_gyro(GZ_H);
        Gz   = temp << 8;
        Gz  |= Read_gyro(GZ_L);
           
   printf(lcd_putc,"\f Gx %lu ",Gx);
   delay_ms(500);
   printf(lcd_putc,"\f Gy %lu ",Gy);
   delay_ms(500);
   printf(lcd_putc,"\f Gz %lu ",Gz);
   delay_ms(500);
   } 
}
File Mplab project và datasheet của ITG 3200 đính kèm bên dưới
File Kèm Theo
File Type: rar test.rar (28.6 KB, 13 lần tải)
File Type: pdf PS-ITG-3200-00-01.4.pdf (1.13 MB, 37 lần tải)
Hoanghovuong 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à 11:28 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