|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
void SPI1_init(void)
{
/* Configure SPI1 related multi-function pins */
DrvGPIO_InitFunction(E_FUNC_SPI1);
/* Configure SPI1 as a master, 8-bit transaction */
DrvSPI_Open(eDRVSPI_PORT1, eDRVSPI_MASTER, eDRVSPI_TYPE5, 16);
/* Enable the automatic slave select function of SPI0 and select the slave select pin. */
DrvSPI_EnableAutoSS(eDRVSPI_PORT1);
/* Set SPI endian MSB FIRST */
DrvSPI_SetEndian(eDRVSPI_PORT1,eDRVSPI_MSB_FIRST);
/* Set the active level of slave select. */
DrvSPI_SetSlaveSelectActiveLevel(eDRVSPI_PORT1, eDRVSPI_ACTIVE_LOW_FALLING);
/* SPI clock rate 2MHz */
DrvSPI_SetClockFreq(eDRVSPI_PORT1, 5000000, 0);
printf("SPI1 initialize complete!\n");
}
void DrvBMA180_write(uint8_t address, uint8_t data)
{
//write any data byte to any single address
//adds a 0 to the MSB of the address byte (WRITE mode)
uint32_t u32Tmp;
address &= 0x7F;
u32Tmp = (address<<8)|data;
DrvSPI_SingleWrite(eDRVSPI_PORT1, &u32Tmp);
}
uint8_t DrvBMA180_read(uint8_t address)
{
//returns the contents of any 1 byte register from any address
//sets the MSB for every address byte (READ mode)
uint32_t u32Tmp;
address |= 0x80;
u32Tmp = address << 8;
DrvSPI_SingleWrite(eDRVSPI_PORT1, &u32Tmp);
DrvSPI_SingleRead(eDRVSPI_PORT1, &u32Tmp);
return (uint8_t)u32Tmp;
}
// init_BMA180
// Input: range is a 3-bit value between 0x00 and 0x06 will set the range as described in the BMA180 datasheet (pg. 27)
// bw is a 4-bit value between 0x00 and 0x09. Again described on pg. 27
// Output: -1 on error, 0 on success
uint8_t BMA180_init(void)
{
uint32_t temp, temp1;
uint8_t u8tmp;
// if connected correctly, ID register should be 3
temp = DrvBMA180_read(ID);
printf("ID = %x\n",temp);
if (temp != 0x03)
return -1;
temp = DrvBMA180_read(Version);
printf("Version = %x\n",temp);
// Set ee_w bit
temp = DrvBMA180_read(CTRLREG0);
printf("CTRL_RGE0 = %x\n",temp);
if ((temp&0x10)!=0x10)
{
temp |= 0x10;
DrvBMA180_write(CTRLREG0, temp);
// Have to set ee_w to write any other registers
temp = DrvBMA180_read(CTRLREG0);
printf("CTRL_RGE0 = %x\n",temp);
}
// Set new data int bit
temp = DrvBMA180_read(CTRLREG3);
printf("CTRL_RGE3 = %x\n",temp);
if (temp!=0x02)
{
temp = 0x02;
DrvBMA180_write(CTRLREG3, temp);
// Have to set ee_w to write any other registers
temp = DrvBMA180_read(CTRLREG3);
printf("CTRL_RGE3 = %x\n",temp);
}
// Set BW = 1200HZ 10HZ
temp = DrvBMA180_read(BWTCS);
printf("BWTCS = %x\n",temp);
if ((temp&0xf0)!=0)
{
temp &= 0x0f;
DrvBMA180_write(BWTCS, temp);
// Keep tcs<3:0> in BWTCS, but write new BW
temp = DrvBMA180_read(BWTCS);
printf("BWTCS = %x\n",temp);
}
// Set Range = 2g
temp = DrvBMA180_read(OLSB1);
printf("OFFSET_LSB1 = %x\n",temp);
if ((temp&RANGEMASK) != RANGE_020G)
{
temp &= (~RANGEMASK);
temp |= RANGE_020G;
DrvBMA180_write(OLSB1, temp); //Write new range data, keep other bits the same
temp = DrvBMA180_read(OLSB1);
printf("OFFSET_LSB1 = %x\n",temp);
}
// Set shadow_dis = 1
temp = DrvBMA180_read(GAIN_Y);
printf("GAIN_Y = %x\n",temp);
if ((temp&0x01) == 0)
{
temp |= SHADOW_DIS;
DrvBMA180_write(GAIN_Y, temp); //Write new range data, keep other bits the same
temp = DrvBMA180_read(GAIN_Y);
printf("GAIN_Y = %x\n",temp);
}
printf("BMA180 initialize complete!\n");
return 0;
}
// EINT0 中断响应程序 用于检测BMA180新数据
void EINT0Callback(void)
{
/* Toggle LED */
int_flag = 1;
}
void EINT0_init(void)
{
/* Configure P25 for EINT0 TEST */
DrvGPIO_InitFunction(E_FUNC_EXTINT0);
DrvGPIO_EnableEINT(E_EINT0_PIN, E_IO_RISING, E_MODE_EDGE, EINT0Callback);
printf("EINT0 initialize complet!\n");
} |
|