#Include <18f4550.h>
//#use delay(xtal=20Mhz)
//#fuses NOWDT,HS
#fuses HS,MCLR,NOWDT,NOPROTECT,NOLVP,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=2000000)
#use i2c(Master,sda=PIN_B0,scl=PIN_B1)
#define LCD_ENABLE_PIN PIN_C2
#define LCD_RS_PIN PIN_C1
#define LCD_RW_PIN PIN_C0
#define DATA_PORT PORTD
#include <lcd.c>
#define gyro_ADD_R 0xD3
#define gyro_ADD_W 0xD2
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
int16 x=0;
int8 xM=0,xL=0;
void writeRegister(int8 reg,int8 value)
{
i2c_start();
i2c_write(gyro_ADD_W);
i2c_write(reg);
i2c_write(value);
i2c_stop();
}
void init_gyro()
{
writeRegister(CTRL_REG1,0b00001111);
writeRegister(CTRL_REG2,0b00001000);
writeRegister(CTRL_REG3,0b00001000);
writeRegister(CTRL_REG4,0b10110000); //10110000
writeRegister(CTRL_REG5,0b00000000);
}
void main()
{
lcd_init();
lcd_gotoxy(1,1);
printf(lcd_putc," L3GD20"); // l3g4200d
delay_ms(1000);
init_gyro();
while(true)
{
i2c_start();
i2c_write(0XD2);
i2c_write(0x28);
i2c_start();
i2c_write(0XD3);
xL=i2c_read();
xM=i2c_read(0);
x=make16(xM,xL);
i2c_stop();
printf(lcd_putc"\f");
lcd_gotoxy(1,2);
printf(lcd_putc,"x=%Ld",x);
delay_ms(50);
}
}