nrf51822 --TWI(硬件IIC)

1.目的

   学习nrf51822硬件IIC通信

2.分析

    IIC是常用的通信接口,现在用IIC和MC3413加速度传感器通信来读取加速度值。TWI(Two Wire Interface)

3.平台:

协议栈版本:SDK10.0.0

编译软件:keil 5.14

硬件平台:nrf51822最小系统

例子:SDK 10.0.0\examples\ble_peripheral\ble_app_uart\pca10028\s110\arm4

4.步骤

  1.打开\SDK10.0\components\drivers_nrf\twi_master\deprecated目录

  nrf51822 --TWI(硬件IIC)_第1张图片

把twi_hw_master.c文件加入到工程里面。并设置路径

nrf51822 --TWI(硬件IIC)_第2张图片

在twi _hw_master.c  里面添加函数如下:

ii2c_device_wirte_byte(uint8_t  address, uint8_t data) : 写某个地址的数据,address为寄存器地址,data是写数据 
bool i2c_device_wirte_data(uint8_t  address, uint8_t *data,uint8_t lenght) :往某地地址写数据

uint8_t i2c_device_read_byte(uint8_t address, uint8_t *p_read_byte,uint8_t length);在某个地址开始处读数据。

/*******************************************************************************
 * ???? :   i2c_device_wirte_byte                                                                 
 * ??     :   wang                                                     
 *                                                                               
 * ?   ?  :  void                                                                   
 * ?   ?  :  void
 * ?   ?  :  void                                                            
 * ???? : 20160317                                                                    
 *******************************************************************************/
bool i2c_device_wirte_byte(uint8_t  address, uint8_t data){
     uint8_t   write_byte[2];
		 write_byte[0] = address;
		 write_byte[1] = data;
		 return  twi_master_transfer(i2c_device_address,write_byte,sizeof(write_byte),TWI_ISSUE_STOP);
}
/*******************************************************************************
 * ???? :   i2c_device_wirte_data                                                                 
 * ??     :   wang                                                     
 *                                                                               
 * ?   ?  :  void                                                                   
 * ?   ?  :  void
 * ?   ?  :  void                                                            
 * ???? : 20160317                                                                    
 *******************************************************************************/

bool i2c_device_wirte_data(uint8_t  address, uint8_t *data,uint8_t lenght)
{
		 return  twi_master_transfer(i2c_device_address,data,lenght,TWI_ISSUE_STOP);
}
/*******************************************************************************
 * ???? :   i2c_device_read_byte                                                                 
 * ??     :   wang                                                     
 *                                                                               
 * ?   ?  :  void                                                                   
 * ?   ?  :  void
 * ?   ?  :  void                                                            
 * ???? : 20160317                                                                    
 *******************************************************************************/
uint8_t i2c_device_read_byte(uint8_t address, uint8_t *p_read_byte,uint8_t length)
{	    bool i2c_trans_state  = false ;
	    i2c_trans_state = twi_master_transfer(i2c_device_address,&address,1,TWI_ISSUE_STOP);
	    while(i2c_trans_state == false);
     	i2c_trans_state =twi_master_transfer(i2c_device_address | TWI_READ_BIT,p_read_byte,length,TWI_ISSUE_STOP);
			while(i2c_trans_state == false); 

		  return  i2c_trans_state  ;
}


2.在主函数里面添加对应的头文件


    

3.在#include "twi_master_config.h"中,设置SCL, SDA的管脚,中断脚,以及传感器地址。

/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is property of Nordic Semiconductor ASA.
 * Terms and conditions of usage are described in detail in NORDIC
 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
 *
 * Licensees are granted free, non-transferable use of the information. NO
 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
 * the file.
 *
 */

#ifndef TWI_MASTER_CONFIG
#define TWI_MASTER_CONFIG

#define TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER (8)
#define TWI_MASTER_CONFIG_DATA_PIN_NUMBER  (9)

#define GSENSOR_INT_PIN_NUMBER             (10)



#define I2C_ADR(address)                   (unsigned char)(address << 1)

#define GSENSOR_I2C_DEVICE_ADDRESS         I2C_ADR(0x4c)


#endif


3.在主函数中

nrf51822 --TWI(硬件IIC)_第3张图片


仿真测试结果如下:读出器件的ID为0x1B

  nrf51822 --TWI(硬件IIC)_第4张图片

查看手册,画线的地方。*表示可以是任何值,可见0x1B是正确的、

 nrf51822 --TWI(硬件IIC)_第5张图片


表示通信成功。。。。。


附件:MC3413手册  http://share.weiyun.com/aec97649305e4e70dc10df94fe02ff49

你可能感兴趣的:(nordic,nrf51822,蓝牙4.0)