#Include <18f4550.h>
#fuses NOWDT,HS
//#fuses HS,MCLR,NOWDT,NOPROTECT,NOLVP,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=4000000)
#use i2c(Master,sda=PIN_B0,scl=PIN_B1)
#define LCD_ENABLE_PIN PIN_C2
#define LCD_RS_PIN PIN_C0
#define LCD_RW_PIN PIN_C1
#define DATA_PORT PORTD
#include<lcd.c>
#define gyro_ADD_R 0b11010011 //11010111
#define gyro_ADD_W 0b11010010 //11010110
// SA0 states
#define L3G_SA0_LOW 0
#define L3G_SA0_HIGH 1
#define L3G_SA0_AUTO 2
// register addresses
#define L3G_WHO_AM_I 0x0F
#define L3G_CTRL_REG1 0x20
#define L3G_CTRL_REG2 0x21
#define L3G_CTRL_REG3 0x22
#define L3G_CTRL_REG4 0x23
#define L3G_CTRL_REG5 0x24
#define L3G_REFERENCE 0x25
#define L3G_OUT_TEMP 0x26
#define L3G_STATUS_REG 0x27
#define L3G_OUT_X_L 0x28
#define L3G_OUT_X_H 0x29
#define L3G_OUT_Y_L 0x2A
#define L3G_OUT_Y_H 0x2B
#define L3G_OUT_Z_L 0x2C
#define L3G_OUT_Z_H 0x2D
#define L3G_FIFO_CTRL_REG 0x2E
#define L3G_FIFO_SRC_REG 0x2F
#define L3G_INT1_CFG 0x30
#define L3G_INT1_SRC 0x31
#define L3G_INT1_THS_XH 0x32
#define L3G_INT1_THS_XL 0x33
#define L3G_INT1_THS_YH 0x34
#define L3G_INT1_THS_YL 0x35
#define L3G_INT1_THS_ZH 0x36
#define L3G_INT1_THS_ZL 0x37
#define L3G_INT1_DURATION 0x38
int16 x=0;
int8 xM=0,xL=0;
int8 yM=0,yL=0;
int8 zM=0,zL=0;
void writeRegister(int8 reg,int8 value)
{
i2c_start();
i2c_write(gyro_ADD_W);
i2c_write(reg);
i2c_write(value);
i2c_stop();
//delay_ms(50);
}
void init_gyro()
{
writeRegister(L3G_CTRL_REG1,0b00001111);
writeRegister(L3G_CTRL_REG2,0b00001000);
writeRegister(L3G_CTRL_REG3,0b00001000);
writeRegister(L3G_CTRL_REG4,0b00110000); //10110000
writeRegister(L3G_CTRL_REG5,0b00000000);
}
void main()
{
set_tris_d(0x00);
set_tris_c(0x00); //FE
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_spi(SPI_SS_DISABLED);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
//lcd_init();
init_gyro();
lcd_init();
printf(lcd_putc, "\f");
lcd_gotoxy(1,1);
printf(lcd_putc," L3GD20"); // l3g4200d
delay_ms(500);
while(1)
{
i2c_start();
i2c_write(0b11010010 );
i2c_write(L3G_OUT_x_H);
i2c_start();
i2c_write(0b11010011 );
xL=i2c_read();
xM=i2c_read(0);
i2c_stop();
i2c_start();
i2c_write(0b11010010 );
i2c_write(L3G_OUT_y_H);
i2c_start();
i2c_write(0b11010011 );
yL=i2c_read();
yM=i2c_read(0);
i2c_stop();
i2c_start();
i2c_write(0b11010010 );
i2c_write(L3G_OUT_z_H);
i2c_start();
i2c_write(0b11010011);
zL=i2c_read();
zM=i2c_read(0);
i2c_stop();
x=x+1;
// if (x=1)
// {
x=make16(xM,xL);
printf(lcd_putc"\f");
//lcd_gotoxy(1,1);
//printf(lcd_putc,"\%Ld ",x); //x=%Ld
lcd_gotoxy(0, 2);
printf(lcd_putc,"\%d \%d \%d",xm,ym,zm);
lcd_gotoxy(5, 2);
printf(lcd_putc,"\%d \%d \%d",xl,yl,zl);
delay_ms(500);
x=0;
xM=0,xL=0;
yM=0,yL=0;
zM=0,zL=0;
// }
}
}