在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
芯片精品文章合集(500篇!)    创芯人才网--重磅上线啦!
查看: 4449|回复: 1

[资料] 基于 NUVOTON M051的BMA180 SPI驱动

[复制链接]
发表于 2011-5-27 09:46:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

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");
}
发表于 2014-1-7 16:31:16 | 显示全部楼层
你好,有详细的驱动文件吗?发我一份,1126949777@qq.com , thanks.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐 上一条 /2 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-5-1 05:11 , Processed in 0.020964 second(s), 10 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表