蓝牙BLE基础:CC2541通信系列之模拟IIC协议
来源:互联网 发布:淘宝关闭无法激活 编辑:程序博客网 时间:2024/06/08 01:25
cc2541模拟IIC:
//P0_2 --- clk
//P0_3 --- data
#define SDA_IN P0DIR &= ~BV(3); //SDA INPUT
#define SDA_OUT P0DIR |= BV(3); //SDA OUTPUT
#define SCL_IN P0DIR &= ~BV(2); //SCL INPUT
#define SCL_OUT P0DIR |= BV(2); //SCL OUTPUT
#define ActiveTime() (Delay5us())
#define SDA P0_3
#define SCL P0_2
#define SUCCESS 0
#define ERR -1
void iic_config_pin()
{
P0SEL &= ~BV(2); //gpio P0_2
P0SEL &= ~BV(3); //gpio P0_3
SDA_IN;
SCL_IN;
}
void Delay5us(void)
{
unsigned short i;
for(i=0;i<1;i++)
asm("NOP");
}
void Delay1us(void)
{
unsigned short i;
for(i=0;i<0;i++)
asm("NOP");
}
void Start(void)
{
SDA_OUT; //开路输出
SCL_OUT;
SDA=1;
Delay1us();
SCL=1;
ActiveTime(); //delya about 5us
SDA=0;
ActiveTime();
SCL=0;
ActiveTime();
}
void RepeatStart() //without stop signal before
{
SDA_OUT; //开路输出
SCL_OUT;
SDA=1;
Delay1us();
SCL=1;
ActiveTime(); //delya about 5us
SDA=0;
ActiveTime();
SCL=0;
ActiveTime();
}
void Stop(void) //SCL高状态的时候,是有效的控制状态,然后通过SDA的翻转来表示启动和停止
{
SDA_OUT; //开路输出
SCL_OUT;
SDA=0;
Delay1us();
SCL=1;
ActiveTime();
SDA=1;
ActiveTime();
SCL=0;
ActiveTime();
SDA_IN;
SCL_IN;
}
unsigned char ChkACK(void)
{
SDA_IN;
SCL_OUT;
ActiveTime();
SCL=1;
ActiveTime();
if(!SDA)
{
SCL=0;
ActiveTime();
return 0;
}
else
{
SCL=0;
ActiveTime();
return 1;
}
}
void SendAck(void)
{
SDA_OUT; //开路输出
SCL_OUT;
SDA=0;
Delay1us();
SCL=1;
ActiveTime();
SCL=0;
ActiveTime();
}
void SendNoAck(void)
{
SDA_OUT; //开路输出
SCL_OUT;
SCL=0;
ActiveTime();
SDA=1;
Delay1us();
SCL=1;
ActiveTime();
}
void Send8bit(unsigned char AData) //发送八位字节,先发送高位,在传输低位
{
unsigned char i=8;
SDA_OUT; //开路输出
SCL_OUT;
for(i=8; i>0;i--)
{
if(AData&0x80)
SDA=1;
else
SDA=0;
Delay1us();
SCL=1;
ActiveTime();
SCL=0;
AData<<=1;
ActiveTime();
}
}
unsigned char Read8bit(unsigned char ack) //ack=1表示需要传输停止位,为0则表示不需要传输停止位
{
unsigned char temp=0;
unsigned char i;
unsigned char label;
label=ack;
SDA_IN;
SCL_OUT;
Delay1us();
for(i=8;i>0;i--)
{
ActiveTime(); //保险起见,回头可以删掉
SCL=1;
ActiveTime();
if(SDA)
temp+=0x01;
else
temp&=0xfe;
SCL=0;
ActiveTime();
if(i>1)
temp<<=1;
}
if(label)
{
SendAck();
}
else
{
SendNoAck();
} //改成输出模式
return temp;
}
unsigned char RegWrite(unsigned char address, unsigned char reg,unsigned char val)
{
Start(); // Send Start
Send8bit(address); // Send IIC "Write" Address
if(ChkACK())
return ERR;
Send8bit(reg); // Send Register
if(ChkACK())
return ERR;
Send8bit(val); // Send Value
if(ChkACK())
return ERR;
Stop(); // Send Stop
return SUCCESS;
}
unsigned char RegWriteN(unsigned char address, unsigned char reg1,unsigned char N,unsigned char *array)
{
Start();
Send8bit(address);
if(ChkACK())
return ERR;
Send8bit(reg1);
if(ChkACK())
return ERR;
while (N>0)
{
Send8bit(*array);
array++;
N--;
if(ChkACK())
return ERR;
}
Stop();
return SUCCESS;
}
unsigned char RegRead(unsigned char address, unsigned char reg)
{
unsigned char b;
Start();
Send8bit(address);
if(ChkACK())
return ERR;
Send8bit(reg);
if(ChkACK())
return ERR;
RepeatStart(); //读取数据必须采用这种方式
Send8bit(address+1); //重新发送设备地址位,并且该读写属性为读取
if(ChkACK())
return ERR;
b=Read8bit(0);
Stop();
return b;
}
unsigned char RegReadN(unsigned char address, unsigned char reg1,unsigned char N,unsigned char *array)
{
unsigned char b;
Start();
Send8bit(address);
if(ChkACK())
return ERR;
Send8bit(reg1);
if(ChkACK())
return ERR;
RepeatStart();
Send8bit(address+1);
if(ChkACK())
return ERR;
while (N>1)
{
b =Read8bit(1); //每一次读取都发送了确认位
*array = b;
array++;
N--;
}
b =Read8bit(0); //最后一个接收的字节数据,不需要确认位(ACK)
*array = b;
Stop();
return SUCCESS;
}
//P0_2 --- clk
//P0_3 --- data
#define SDA_IN P0DIR &= ~BV(3); //SDA INPUT
#define SDA_OUT P0DIR |= BV(3); //SDA OUTPUT
#define SCL_IN P0DIR &= ~BV(2); //SCL INPUT
#define SCL_OUT P0DIR |= BV(2); //SCL OUTPUT
#define ActiveTime() (Delay5us())
#define SDA P0_3
#define SCL P0_2
#define SUCCESS 0
#define ERR -1
void iic_config_pin()
{
P0SEL &= ~BV(2); //gpio P0_2
P0SEL &= ~BV(3); //gpio P0_3
SDA_IN;
SCL_IN;
}
void Delay5us(void)
{
unsigned short i;
for(i=0;i<1;i++)
asm("NOP");
}
void Delay1us(void)
{
unsigned short i;
for(i=0;i<0;i++)
asm("NOP");
}
void Start(void)
{
SDA_OUT; //开路输出
SCL_OUT;
SDA=1;
Delay1us();
SCL=1;
ActiveTime(); //delya about 5us
SDA=0;
ActiveTime();
SCL=0;
ActiveTime();
}
void RepeatStart() //without stop signal before
{
SDA_OUT; //开路输出
SCL_OUT;
SDA=1;
Delay1us();
SCL=1;
ActiveTime(); //delya about 5us
SDA=0;
ActiveTime();
SCL=0;
ActiveTime();
}
void Stop(void) //SCL高状态的时候,是有效的控制状态,然后通过SDA的翻转来表示启动和停止
{
SDA_OUT; //开路输出
SCL_OUT;
SDA=0;
Delay1us();
SCL=1;
ActiveTime();
SDA=1;
ActiveTime();
SCL=0;
ActiveTime();
SDA_IN;
SCL_IN;
}
unsigned char ChkACK(void)
{
SDA_IN;
SCL_OUT;
ActiveTime();
SCL=1;
ActiveTime();
if(!SDA)
{
SCL=0;
ActiveTime();
return 0;
}
else
{
SCL=0;
ActiveTime();
return 1;
}
}
void SendAck(void)
{
SDA_OUT; //开路输出
SCL_OUT;
SDA=0;
Delay1us();
SCL=1;
ActiveTime();
SCL=0;
ActiveTime();
}
void SendNoAck(void)
{
SDA_OUT; //开路输出
SCL_OUT;
SCL=0;
ActiveTime();
SDA=1;
Delay1us();
SCL=1;
ActiveTime();
}
void Send8bit(unsigned char AData) //发送八位字节,先发送高位,在传输低位
{
unsigned char i=8;
SDA_OUT; //开路输出
SCL_OUT;
for(i=8; i>0;i--)
{
if(AData&0x80)
SDA=1;
else
SDA=0;
Delay1us();
SCL=1;
ActiveTime();
SCL=0;
AData<<=1;
ActiveTime();
}
}
unsigned char Read8bit(unsigned char ack) //ack=1表示需要传输停止位,为0则表示不需要传输停止位
{
unsigned char temp=0;
unsigned char i;
unsigned char label;
label=ack;
SDA_IN;
SCL_OUT;
Delay1us();
for(i=8;i>0;i--)
{
ActiveTime(); //保险起见,回头可以删掉
SCL=1;
ActiveTime();
if(SDA)
temp+=0x01;
else
temp&=0xfe;
SCL=0;
ActiveTime();
if(i>1)
temp<<=1;
}
if(label)
{
SendAck();
}
else
{
SendNoAck();
} //改成输出模式
return temp;
}
unsigned char RegWrite(unsigned char address, unsigned char reg,unsigned char val)
{
Start(); // Send Start
Send8bit(address); // Send IIC "Write" Address
if(ChkACK())
return ERR;
Send8bit(reg); // Send Register
if(ChkACK())
return ERR;
Send8bit(val); // Send Value
if(ChkACK())
return ERR;
Stop(); // Send Stop
return SUCCESS;
}
unsigned char RegWriteN(unsigned char address, unsigned char reg1,unsigned char N,unsigned char *array)
{
Start();
Send8bit(address);
if(ChkACK())
return ERR;
Send8bit(reg1);
if(ChkACK())
return ERR;
while (N>0)
{
Send8bit(*array);
array++;
N--;
if(ChkACK())
return ERR;
}
Stop();
return SUCCESS;
}
unsigned char RegRead(unsigned char address, unsigned char reg)
{
unsigned char b;
Start();
Send8bit(address);
if(ChkACK())
return ERR;
Send8bit(reg);
if(ChkACK())
return ERR;
RepeatStart(); //读取数据必须采用这种方式
Send8bit(address+1); //重新发送设备地址位,并且该读写属性为读取
if(ChkACK())
return ERR;
b=Read8bit(0);
Stop();
return b;
}
unsigned char RegReadN(unsigned char address, unsigned char reg1,unsigned char N,unsigned char *array)
{
unsigned char b;
Start();
Send8bit(address);
if(ChkACK())
return ERR;
Send8bit(reg1);
if(ChkACK())
return ERR;
RepeatStart();
Send8bit(address+1);
if(ChkACK())
return ERR;
while (N>1)
{
b =Read8bit(1); //每一次读取都发送了确认位
*array = b;
array++;
N--;
}
b =Read8bit(0); //最后一个接收的字节数据,不需要确认位(ACK)
*array = b;
Stop();
return SUCCESS;
}
阅读全文
0 0
- 蓝牙BLE基础:CC2541通信系列之模拟IIC协议
- 蓝牙BLE基础:CC2541通信系列之模拟SPI协议
- 蓝牙BLE基础:CC2541通信系列之模拟UART协议
- 蓝牙BLE基础:CC2541 BLE临界资源保护
- 【BLE】CC2541之硬件IIC(TMP102温度传感器)
- Android开发之蓝牙(二)——基于BLE协议蓝牙模块通信
- 【BLE】CC2541之notify
- 【BLE】CC2541之按键
- 【BLE】CC2541之RSSI
- 【BLE】CC2541之OLED
- 【BLE】CC2541之indicate
- 【BLE】CC2541之timer3
- 【BLE】CC2541之看门狗
- 【BLE】CC2541之SNV
- 【BLE】CC2541之OAD
- 【BLE】CC2541之PWM
- 【BLE】CC2541之OAD
- 【BLE】CC2541之BTool
- Python中的join()函数的用法
- 3个著名加密算法(MD5、RSA、DES)的解析
- anaconda3 安装python包
- 设计模式--缺省适配器模式
- 51nod 1279 扔盘子(单调栈)
- 蓝牙BLE基础:CC2541通信系列之模拟IIC协议
- seq2seq 的 keras 实现
- seq2seq 入门
- 关于VS动态库和静态库编译问题
- 什么是 Dropout
- 《软件工程(C编码实践篇)》学习心得
- 用 LSTM 做时间序列预测的一个小例子
- 蓝牙BLE基础:CC2541通信系列之模拟SPI协议
- Mybatis操作数据库的方式