新手上路
- 积分
- 40
- 金钱
- 40
- 注册时间
- 2014-7-28
- 在线时间
- 1 小时
|

楼主 |
发表于 2014-7-28 12:29:58
|
显示全部楼层
程序很简单,只是一般的I2C通信程序而已,不明白哪里出错了。。
void I2C_TC74_Init(void){
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//GPIOB periph clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE);
// open I2C1 clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //??I2C1??PB6,PB7
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; //?????????
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*
// init for led
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11
| GPIO_Pin_12 |GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
*/
I2C_DeInit(I2C1);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; //???I2C??,???SMBus??
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; //??I2C????Tlow / Thigh = 2
I2C_InitStructure.I2C_OwnAddress1 = 0x00; //??I2C??????,????STM32?I2C????,??0
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; //??ACK???
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; //??7???,??????
I2C_InitStructure.I2C_ClockSpeed = 200000; //??I2C????
I2C_Init(I2C1, &I2C_InitStructure);
I2C_ITConfig(I2C1, I2C_IT_EVT|I2C_IT_ERR, ENABLE);
I2C_Cmd(I2C1, ENABLE);
/*
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
*/
}
s8 Read_Temp(u8 TC74A5_Addr){
s8 result, result2;
I2C_AcknowledgeConfig(I2C1, ENABLE);
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)){ /* wait EV5 */
}
I2C_Send7bitAddress(I2C1, TC74A5_Addr, I2C_Direction_Transmitter);
//while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)){ /* EV6 */
//
//}
I2C_SendData(I2C1, 0x00); // indicate which register to read
// while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)){ /* EV8 */
//
//}
// recieve temp
I2C_GenerateSTOP(I2C1,ENABLE);
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)){ /* EV5 */
}
I2C_Send7bitAddress(I2C1, TC74A5_Addr, I2C_Direction_Receiver);
result = I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1,ENABLE);
return result;
}
|
|