Team pumpkin pie. Tokai Univ., MicroMouse

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gp2y0e.cpp Source File

gp2y0e.cpp

00001 //2016/09/17
00002 
00003 #include "gp2y0e.hpp"
00004 
00005 #define GP2Y_WRITE_ADDR (0x80)
00006 
00007 void GP2Y_I2C::i2c_write(int addr,char regist,char data)
00008 {
00009     cmd[0]=regist;
00010     cmd[1]=data;
00011     _i2c.write(addr,cmd,2);
00012 }
00013 
00014 char GP2Y_I2C::i2c_read(int addr,char regist)
00015 {
00016     cmd[0]=regist;
00017     _i2c.write(addr,cmd,1);
00018     _i2c.read(addr,cmd,1);
00019     return cmd[0];
00020 }
00021 
00022 /* Distance */
00023 int GP2Y_I2C::Distance(int addr){
00024   CatchShiftBit = i2c_read(addr,ShiftBit_addr);
00025   return (int((i2c_read(addr,Distance_addr[0]))*16+(i2c_read(addr,Distance_addr[1]))/16/(CatchShiftBit*CatchShiftBit)));
00026 }
00027 
00028 /* ShiftBit Check */
00029 int GP2Y_I2C::ShiftBit_Check(int addr)
00030 {
00031   CatchShiftBit = i2c_read(addr,ShiftBit_addr);
00032   return (int(CatchShiftBit));
00033 }
00034 
00035 /**
00036  * @fn GP2Y Address Write Sequence
00037  * !!Warning!! "SelectAd" ... "0x0?". ex) Sensor Address 0x80 -> 0xA0. SelectAd=0x0A !Warning!
00038  **/
00039 #ifdef GP2Y0E_I2C_ADDR_WRITE
00040 void GP2Y_I2C::GP2Y_ADDR_Write_Sequence(char SelectAd){
00041   //wait 10 second
00042   for(int a=10 ; a>=0 ;a--){
00043     wait(1);
00044   }
00045   //Start!
00046 
00047   //Stage1
00048   i2c_write(GP2Y_WRITE_ADDR,0xEC,0xFF);
00049   wait_ms(1);
00050 
00051   //Writing Start!!
00052   _vpp = 1;
00053   wait(2); //wait...over 1 second
00054 
00055   //Stage2
00056   i2c_write(GP2Y_WRITE_ADDR,0xC8,0x00);
00057   wait_ms(1);
00058 
00059   //Stage3
00060   i2c_write(GP2Y_WRITE_ADDR,0xC9,0x45);
00061   wait_ms(1);
00062 
00063   //Stage4
00064   i2c_write(GP2Y_WRITE_ADDR,0xCD,SelectAd);
00065   wait_ms(1);
00066 
00067   //Stage5
00068   i2c_write(GP2Y_WRITE_ADDR,0xCA,0x01);
00069   wait_ms(1);
00070   wait(1);
00071 
00072   _vpp = 0;
00073 
00074   //Writing Done.
00075 
00076   wait(1);
00077 }
00078 #endif