利用STM32的硬件平台实现的IIC模拟通信,这里实现了两路IIC接口,可以选择。
bsp_iic.c
-
#include "bsp_iic.h"
-
-
-
static void IIC_Init(E_I2cChn e_i2cChn)
-
{
-
uint16_t u16SDAPin, u16SCLPin;
-
GPIO_InitTypeDef GPIO_InitStructure;
-
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
-
-
if( e_i2cChn_RhTemp == e_i2cChn)
-
{
-
u16SDAPin = GPIO_Pin_7;
-
u16SCLPin = GPIO_Pin_6;
-
}
-
-
else
-
{
-
u16SDAPin = GPIO_Pin_11;
-
u16SCLPin = GPIO_Pin_10;
-
}
-
-
GPIO_InitStructure.GPIO_Pin = u16SDAPin | u16SCLPin;
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;
-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
GPIO_Init(GPIOB, &GPIO_InitStructure);
-
-
IIC_SCL(e_i2cChn, 1);
-
IIC_SDA(e_i2cChn, 1);
-
}
-
-
-
static void IIC_Start(E_I2cChn e_i2cChn)
-
{
-
SDA_OUT(e_i2cChn);
-
IIC_SDA(e_i2cChn, 1);
-
IIC_SCL(e_i2cChn, 1);
-
-
delay_us(4);
-
IIC_SDA(e_i2cChn, 0); //START:when CLK is high,DATA change form high to low
-
-
delay_us(4);
-
IIC_SCL(e_i2cChn, 0);
-
}
-
-
-
static void IIC_Stop(E_I2cChn e_i2cChn)
-
{
-
SDA_OUT(e_i2cChn);
-
IIC_SCL(e_i2cChn, 0);
-
IIC_SDA(e_i2cChn, 0);//STOP:when CLK is high DATA change form low to high
-
-
delay_us(4);
-
IIC_SCL(e_i2cChn, 1);
-
IIC_SDA(e_i2cChn, 1);
-
-
delay_us(4);
-
}
-
-
-
// 等待应答信号到来 等待应答信号到来
-
// 返回值: 0,接收应答失败
-
// 1,接受应答成功
-
static etError IIC_receivedAck(E_I2cChn e_i2cChn)
-
{
-
uint8_t ucErrTime = 0;
-
SDA_IN(e_i2cChn);
-
IIC_SDA(e_i2cChn, 1);
-
delay_us(1);
-
-
IIC_SCL(e_i2cChn, 1);
-
delay_us(1);
-
-
while(READ_SDA(e_i2cChn))
-
{
-
ucErrTime++;
-
if(ucErrTime > 250)
-
{
-
IIC_Stop(e_i2cChn);
-
return ACK_ERROR;
-
}
-
}
-
IIC_SCL(e_i2cChn, 0);
-
return NO_ERROR;
-
}
-
-
/* ACK: SDA low level */
-
static void IIC_sendAck(E_I2cChn e_i2cChn)
-
{
-
IIC_SCL(e_i2cChn, 0);
-
SDA_OUT(e_i2cChn);
-
-
IIC_SDA(e_i2cChn, 0);
-
delay_us(2);
-
-
IIC_SCL(e_i2cChn, 1);
-
delay_us(2);
-
-
IIC_SCL(e_i2cChn, 0);
-
}
-
-
/* no ack: SDA high level */
-
static void IIC_sendNAck(E_I2cChn e_i2cChn)
-
{
-
IIC_SCL(e_i2cChn, 0);
-
SDA_OUT(e_i2cChn);
-
-
IIC_SDA(e_i2cChn, 1);
-
delay_us(2);
-
-
IIC_SCL(e_i2cChn, 1);
-
delay_us(2);
-
-
IIC_SCL(e_i2cChn, 0);
-
}
-
-
-
static etError IIC_Send_Byte(E_I2cChn e_i2cChn, uint8_t u8SendByte)
-
{
-
etError error;
-
uint8_t i;
-
SDA_OUT(e_i2cChn);
-
IIC_SCL(e_i2cChn, 0); // scl = low level, to enable data transimit in IIC bus.
-
-
for(i = 0; i < 8; i ++)
-
{
-
/* send bits via IIC bus */
-
IIC_SDA(e_i2cChn, ((u8SendByte & 0x80) >> 7));
-
u8SendByte <<= 1;
-
delay_us(2);
-
-
IIC_SCL(e_i2cChn, 1);
-
delay_us(2);
-
-
IIC_SCL(e_i2cChn, 0);
-
delay_us(2);
-
}
-
-
-
/**
-
* check whether received ACK from slave device after sending command to slave device.
-
* if received ACK, indicate NO_ERROR; else indicate error.
-
**/
-
if(IIC_receivedAck(e_i2cChn) == NO_ERROR)
-
{
-
error = NO_ERROR;
-
}
-
else
-
error = ACK_ERROR;
-
-
return error;
-
}
-
-
-
-
/**
-
* etAck: ack signal.
-
*/
-
static uint8_t IIC_Read_Byte(E_I2cChn e_i2cChn, etI2cAck etAck)
-
{
-
uint8_t i = 0, receive = 0;
-
-
SDA_IN(e_i2cChn);
-
for(i = 0; i < 8; i++)
-
{
-
IIC_SCL(e_i2cChn, 0);
-
delay_us(2);
-
-
IIC_SCL(e_i2cChn, 1);
-
receive <<= 1;
-
-
if(READ_SDA(e_i2cChn))
-
receive++;
-
delay_us(2);
-
}
-
-
/**
-
* master read one byte from slave device, then send ACK/nACK signal to slave device
-
* to enable continously sending command to slave(sending ACK) or stop commuincation(sending nACK).
-
*/
-
if (etAck == ACK)
-
IIC_sendAck(e_i2cChn);
-
else
-
IIC_sendNAck(e_i2cChn);
-
return receive;
-
}
-
-
-
const T_IICOpera g_tIICOpera = {
-
-
IIC_Init,
-
-
IIC_Start,
-
IIC_Stop,
-
-
/* master send/read byte to/from slave */
-
IIC_Send_Byte,
-
IIC_Read_Byte,
-
};
bsp_iic.h
阅读(2699) | 评论(0) | 转发(0) |