Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "nrf24le1.h"
00015 #include "lib_adns7530.h"
00016 #include "hal_spi.h"
00017 #include "nrfr_mouse.h"
00018
00019 void uSecDelay(uint8_t len)
00020 {
00021 len += (len>>1);
00022 while(len--);
00023 }
00024
00025 void frameDelay()
00026 {
00027 uint8_t frameDel = 200;
00028 while(frameDel--)uSecDelay(98);
00029 }
00030 void hal_adns7530_enable(void)
00031 {
00032 SPI_CS = 0;
00033 uSecDelay(15);
00034 }
00035
00036 void hal_adns7530_disable(void)
00037 {
00038 uSecDelay(10);
00039 SPI_CS = 1;
00040 }
00041
00042 uint8_t hal_adns7530_readRegister(uint8_t regAddress)
00043 {
00044 uint8_t returnVal;
00045 hal_adns7530_enable();
00046 hal_spi_master_read_write(regAddress);
00047 uSecDelay(10);
00048 returnVal = hal_spi_master_read_write(0);
00049 hal_adns7530_disable();
00050 return (returnVal);
00051 }
00052
00053 void hal_adns7530_writeRegister(uint8_t regAddress, uint8_t innData)
00054 {
00055 hal_adns7530_enable();
00056 hal_spi_master_read_write(ADNS7530_WRITE | regAddress);
00057 uSecDelay(10);
00058 hal_spi_master_read_write(innData);
00059 hal_adns7530_disable();
00060 }
00061
00062 void hal_adns7530_init()
00063 {
00064
00065 hal_adns7530_writeRegister(ADNS7530_POWERUPRESET_ADDR, ADNS7530_POWERUPRESET_INIT);
00066 frameDelay();
00067 hal_adns7530_writeRegister(ADNS7530_OBSERVATION_ADDR, 0x00);
00068 frameDelay();
00069 hal_adns7530_readRegister(ADNS7530_OBSERVATION_ADDR);
00070
00071 frameDelay();
00072
00073 hal_adns7530_readRegister(ADNS7530_MOTION_ADDR);
00074 hal_adns7530_readRegister(ADNS7530_DELTAX_L_ADDR);
00075 hal_adns7530_readRegister(ADNS7530_DELTAY_L_ADDR);
00076 hal_adns7530_readRegister(ADNS7530_DELTAXY_H_ADDR);
00077
00078 hal_adns7530_writeRegister(0x3c,0x27);
00079 hal_adns7530_writeRegister(0x22,0x0a);
00080 hal_adns7530_writeRegister(0x21,0x01);
00081 hal_adns7530_writeRegister(0x3c,0x32);
00082 hal_adns7530_writeRegister(0x23,0x20) ;
00083 hal_adns7530_writeRegister(0x3c,0x05);
00084
00085 frameDelay();
00086
00087 hal_adns7530_writeRegister(ADNS7530_LSRPWRCFG0_ADDR, 0x7f);
00088 hal_adns7530_writeRegister(ADNS7530_LSRPWRCFG1_ADDR, 0x80);
00089 hal_adns7530_writeRegister(ADNS7530_LASERCTRL0_ADDR, 0xc0);
00090 hal_adns7530_writeRegister(ADNS7530_LASERCTRL1_ADDR, 0x00);
00091
00092 hal_adns7530_writeRegister(ADNS7530_CONFIG2_BITS_ADDR, ADNS7530_RES800CPI | ADNS7530_8MS_RUN_RATE);
00093 }
00094
00095 void hal_adns7530_mod_cpi()
00096 {
00097 static xdata uint8_t cpi = ADNS7530_RES800CPI;
00098
00099 cpi += 0x20;
00100 if(cpi > 0x60)
00101 {
00102 cpi = 0;
00103 }
00104 hal_adns7530_writeRegister(ADNS7530_CONFIG2_BITS_ADDR, cpi | ADNS7530_8MS_RUN_RATE);
00105 }
00106
00107 bool hal_adns7530_readMotionBurst(int16_t *deltaX, int16_t *deltaY) {
00108 uint8_t motion;
00109 int16_t deltaX_l, deltaY_l, deltaX_h, deltaY_h, deltaXY_h;
00110
00111 hal_adns7530_enable();
00112 hal_spi_master_read_write(ADNS7530_MOTIONBURST_ADDR);
00113 uSecDelay(10);
00114
00115 motion = hal_spi_master_read_write(0);
00116 deltaX_l = hal_spi_master_read_write(0);
00117 deltaY_l = hal_spi_master_read_write(0);
00118 deltaXY_h = hal_spi_master_read_write(0);
00119
00120 deltaX_h = ((int16_t)deltaXY_h << 4) & 0xF00;
00121 if(deltaX_h & 0x800)
00122 {
00123 deltaX_h |= 0xf000;
00124 }
00125
00126 deltaY_h = ((int16_t)deltaXY_h << 8) & 0xF00;
00127 if(deltaY_h & 0x800)
00128 {
00129 deltaY_h |= 0xf000;
00130 }
00131
00132 *deltaX = deltaX_h | deltaX_l;
00133 *deltaY = deltaY_h | deltaY_l;
00134
00135
00136 hal_adns7530_disable();
00137
00138
00139 if(motion & 0x80)
00140 {
00141 return true;
00142 }
00143 else
00144 {
00145 return false;
00146 }
00147 }
00148