PIC Vietnam

Go Back   PIC Vietnam > Truyền thông > Giao tiếp USB, CAN, I2C, SPI, USART...

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

Giao tiếp USB, CAN, I2C, SPI, USART... Những giao tiếp được tích hợp trên PIC

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 22-06-2012, 06:32 PM   #1
mai9989
Nhập môn đệ tử
 
Tham gia ngày: Jul 2009
Bài gửi: 3
:
lcd hiện giá trị sensor gyroscope

Mình đang code con gyroscope Module 3-Axis L3G4200D giao tiếp i2c với pic 18f45k22 bằng MikroC, không hiểu hàm con có vấn đề gì mà gọi trong main thì không thể hiển thị giá trị lên lcd. Tình đã test hiển thì lcd bằng cách thay vào bằng 1 số cụ thể thì nếu hàm main không gọi hàm con --> lcd hiển thị được, có gọi hàm con thì dù có hiển thị số cụ thể cũng không được. Pro nào biết giải thích giúp mình với!

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23

// LCD module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

//I2C module connection
sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
//end I2C module connection

//unsigned short int Addr = 105; // I2C address of gyro
unsigned int x,y,oz;
char text1[15], text2[5], text3[5] ;

void setup () {
I2C1_Init(100000); //initialize I2C communication

// Turn on all axes, disable power down
I2C1_Start(); //issue I2C start signal
I2C1_Wr(0xD0); //slave address+W
I2C1_Wr(CTRL_REG1);
I2C1_Wr(0x1f);
I2C1_stop();
delay_100ms();

// Enable control ready signal
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(CTRL_REG3);
I2C1_Wr(0x08);
I2C1_stop();
delay_100ms();

// Set scale (500 deg/sec)
I2C1_Start(); //issue I2C start signal
I2C1_Wr(0xD0);
I2C1_Wr(CTRL_REG4);
I2C1_Wr(0x80);
I2C1_stop();
delay_100ms ();
}

void getGyroValue(unsigned int x,unsigned int y,unsigned int oz) {
char MSB, LSB;
I2C1_Init(100000);

I2C1_Start(); // issue I2C start signal
I2C1_Wr(0xD1); //slave address+R
MSB = I2C1_Rd(0x29);
LSB = I2C1_Rd(0x28);
I2C1_stop();
x = ((MSB << 8) | LSB);

I2C1_Start();
I2C1_Wr(0xD1);
MSB = I2C1_Rd(0x2B);
LSB = I2C1_Rd(0x2A);
I2C1_Stop();
y = ((MSB << 8) | LSB);

I2C1_Start();
I2C1_Wr(0xD1);
MSB = I2C1_Rd(0x2D);
LSB = I2C1_Rd(0x2C);
I2C1_stop();
oz = ((MSB << 8) | LSB);
return;
}

void main() {

ANSELB=0;
ANSELC=0;
LATC=0xff;
//TRISB=0;
TRISD=0;
LATD=0xff;
setup();
getGyroValue(x,y,oz);

delay_ms(100);
while(1) {
LCD_Init(); //goi LCD
Lcd_Cmd(_LCD_CLEAR); //xoa man hinh
Lcd_Cmd(_LCD_CURSOR_OFF); //cursor off

LCD_out(1,1,"truc Ox:");
Lcd_out(2,9,"rad/s" );
floatToStr(x/114,text1); //dua gia tri x ve dang text de hien lcd
Lcd_out(2,1,text1);
delay_ms(9000);
Lcd_Cmd(_LCD_CLEAR);


LCD_out(1,1,"truc Oy:");
Lcd_out(2,9,"rad/s" );
floatToStr(y/114,text2); //dua gia tri y ve dang text de hien lcd
Lcd_out(2,1,text2);
delay_ms(9000);
Lcd_Cmd(_LCD_CLEAR);

LCD_out(1,1,"truc Oz:");
Lcd_out(2,9,"rad/s" );
floatToStr(z/114,text1); //dua gia tri oz ve dang text de hien lcd
Lcd_out(2,1,text1);
delay_ms(9000);
Lcd_Cmd(_LCD_CLEAR); }

}
Kết nối: http://forums.parallax.com/attachmen...4&d=1332790730

thay đổi nội dung bởi: mai9989, 22-06-2012 lúc 06:37 PM.
mai9989 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:53 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