STM32软件IIC速度
如题,我把IIC里面的延时函数屏蔽为空函数
void SYSTICK_DelayUs(unsigned int Del_1ms)
{
// unsigned char j;
// while(Del_1ms–)
// {
// for(j=0;j<255;j++);
// }
}
1uS都不延时,居然跑得飞起,闭环速度提升了二三十倍;
然后用示波器测SCL波形看速度,如下图
如图2所示,算出来的速度好像是900多kHz,(两脉冲间隔大概1.08us)
哇!!!这么爽的吗!!!
(以下代码为沁和智能提供的AS5600驱动代码)
```c
#include "./AS5600IIC/AS5600.h"
void SYSTICK_DelayUs(unsigned int Del_1ms)
{
// unsigned char j;
// while(Del_1ms--)
// {
// for(j=0;j<255;j++);
// }
}
void IIC_Init_AS5600(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_11); //PB10,PB11 Êä³ö¸ß
}
void IIC_Start_AS5600(void)
{
SDA_OUT(); //sdaÏßÊä³ö
IIC_SDA=1;
IIC_SCL=1;
SYSTICK_DelayUs(4);
IIC_SDA=0; //START:when CLK is high,DATA change form high to low
SYSTICK_DelayUs(4);
IIC_SCL=0; //ǯסI2C×ÜÏߣ¬×¼±¸·¢ËÍ»ò½ÓÊÕÊý¾Ý
}
void IIC_Stop_AS5600(void)
{
SDA_OUT(); //sdaÏßÊä³ö
IIC_SCL=0;
IIC_SDA=0; //STOP:when CLK is high DATA change form low to high
SYSTICK_DelayUs(4);
IIC_SCL=1;
IIC_SDA=1; //·¢ËÍI2C×ÜÏß½áÊøÐźÅ
SYSTICK_DelayUs(4);
}
u8 IIC_Wait_Ack_AS5600(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDAÉèÖÃΪÊäÈë
IIC_SDA=1;
SYSTICK_DelayUs(1);
IIC_SCL=1;
SYSTICK_DelayUs(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop_AS5600();
return 1;
}
}
IIC_SCL=0; //ʱÖÓÊä³ö0
return 0;
}
void IIC_Ack(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=0;
SYSTICK_DelayUs(2);
IIC_SCL=1;
SYSTICK_DelayUs(2);
IIC_SCL=0;
}
void IIC_NAck(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=1;
SYSTICK_DelayUs(2);
IIC_SCL=1;
SYSTICK_DelayUs(2);
IIC_SCL=0;
}
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL=0; //ÀµÍʱÖÓ¿ªÊ¼Êý¾Ý´«Êä
for(t=0;t<8;t++)
{
//IIC_SDA=(txd&0x80)>>7;
if((txd&0x80)>>7)
IIC_SDA=1;
else
IIC_SDA=0;
txd<<=1;
SYSTICK_DelayUs(2);
IIC_SCL=1;
SYSTICK_DelayUs(2);
IIC_SCL=0;
SYSTICK_DelayUs(2);
}
}
u8 IIC_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
SDA_IN(); //SDAÉèÖÃΪÊäÈë
for(i=0;i<8;i++ )
{
IIC_SCL=0;
SYSTICK_DelayUs(2);
IIC_SCL=1;
receive<<=1;
if(READ_SDA)receive++;
SYSTICK_DelayUs(1);
}
if (!ack)
IIC_NAck(); //·¢ËÍnACK
else
IIC_Ack(); //·¢ËÍACK
return receive;
}
u8 AS5600_IIC_Read_OneByte(u8 deviceaddr,u8 readaddr)
{
u8 temp;
IIC_Start_AS5600();
IIC_Send_Byte(deviceaddr&0xfe);
IIC_Wait_Ack_AS5600();
IIC_Send_Byte(readaddr);
IIC_Wait_Ack_AS5600();
IIC_Start_AS5600();
IIC_Send_Byte(deviceaddr|0x01);
IIC_Wait_Ack_AS5600();
temp=IIC_Read_Byte(0);
IIC_Stop_AS5600();
return temp;
}
int get_angle_SRC(void)
{
int angle_SRC_inter;
angle_SRC_inter = AS5600_IIC_Read_OneByte((0x36<<1),0x0e);
angle_SRC_inter <<= 8;
angle_SRC_inter |= AS5600_IIC_Read_OneByte((0x36<<1),0x0f);
return angle_SRC_inter;
}