整理的ADS1110和adt7410的STM32 读写程序,使用IIC硬件操作,没有使用DMA
uint8_t buf[13],Pbuf[5];
void Iic_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//PB6-I2C1_SCL PB7-I2C1_SDA PB10-I2C2_SCL PB11-I2C2_SDA
/* Configure IO connected to IIC*********************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0X010;
I2C_InitStructure.I2C_Ack = I2C_Ack_Disable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Cmd(I2C2, ENABLE);
I2C_Init(I2C2, &I2C_InitStructure);
/*允许1字节1应答模式*/
I2C_AcknowledgeConfig(I2C2, ENABLE);
}
/**********************************************************************/
/*IIC写一个字节 */
/* */
/**********************************************************************/
void I2C_WriteByte(unsigned char id,unsigned char write_address,unsigned char byte,bool UseAddress)
{
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C2,ENABLE);
//产生起始条件
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//等待ACK
I2C_Send7bitAddress(I2C2,id,I2C_Direction_Transmitter);
//向设备发送设备地址
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//等待ACK
if(FALSE != UseAddress){
/*********************************************************************************/
I2C_SendData(I2C2, write_address);
//寄存器地址
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/*********************************************************************************/
} //等待ACK
I2C_SendData(I2C2, byte);
//发送数据
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//发送完成
I2C_GenerateSTOP(I2C2, ENABLE);
//产生结束信号
if(TRUE == UseAddress){
do
{
/* Send START condition */
I2C_GenerateSTART(I2C2, ENABLE);
/* Read I2C1 SR1 register */
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C2, id, I2C_Direction_Transmitter);
}while(!(I2C_ReadRegister(I2C2, I2C_Register_SR1) & 0x0002));
/* Clear AF flag */
I2C_ClearFlag(I2C2, I2C_FLAG_AF);
/* STOP condition */
I2C_GenerateSTOP(I2C2, ENABLE);
}
}
/**********************************************************************/
/*IIC读数据 */
/* */
/**********************************************************************/
unsigned char I2C_ReadByte(unsigned char id,
unsigned char read_address,
unsigned char *pBuf,
bool UseAddress)
{
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));
//等待I2C
/**************************************************************************/
if(TRUE==UseAddress)
{
I2C_GenerateSTART(I2C2, ENABLE);
//产生起始信号
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//EV5
I2C_Send7bitAddress(I2C2, id, I2C_Direction_Transmitter);
//发送地址
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//EV6
I2C_Cmd(I2C1, ENABLE);
//重新设置可以清楚EV6
I2C_SendData(I2C2, read_address);
//发送读得地址
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
/**************************************************************************/
//EV8
I2C_GenerateSTART(I2C2, ENABLE);
//重新发送
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//EV5
I2C_Send7bitAddress(I2C2, id, I2C_Direction_Receiver);
//发送读地址
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
//EV6
if(FALSE==UseAddress)
{
//关闭应答和停止条件产生
while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)));
*pBuf++ = I2C_ReceiveData(I2C2);
//关闭应答和停止条件产生
while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)));
*pBuf++ = I2C_ReceiveData(I2C2);
}
I2C_AcknowledgeConfig(I2C2, DISABLE);
I2C_GenerateSTOP(I2C2, ENABLE);
while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)));
*pBuf = I2C_ReceiveData(I2C2);
I2C_AcknowledgeConfig(I2C2, ENABLE);
return 0;
}
void ADT7410_Read(uint8_t *pbuf,uint8_t Number,uint8_t StartAddress,bool UseAddress)
{
uint8_t num=Number;
uint8_t addr=StartAddress;
while(num--)
I2C_ReadByte(0x94,addr++,pbuf++,UseAddress);
}
void ADS1110_Read(uint8_t *pbuf,uint8_t Number,uint8_t StartAddress,bool UseAddress)
{
I2C_ReadByte(0x90,StartAddress,pbuf,UseAddress);
}
void ADT7410_Write(uint8_t StartAddress,uint8_t SetData,bool UseAddress)
{
I2C_WriteByte(0x94,StartAddress,SetData,UseAddress);
}
void ADS1110_Write(uint8_t StartAddress,uint8_t SetData,bool UseAddress)
{
I2C_WriteByte(0x90,StartAddress,SetData,UseAddress);
}
static void Delay( uint16_t nCount)
{
for(; nCount != 0; nCount--);
}
int main(void)
{
SystemInit();
Iic_Init();
ADT7410_Write(3,0X80,TRUE);
ADS1110_Write(0,0x8c,FALSE);
while (1){
ADT7410_Read(buf,12,0,TRUE);
Delay(5000);
ADS1110_Read (Pbuf,0,0,FALSE);
Delay(5000);
}
}转载
|