高中生
最后登录1970-1-1
在线时间 小时
注册时间2015-3-31
|
以下是小弟自己写了一段I2C的程序 程序大概就是往E2PROM的一个地址里写一个数据在往一个地址里读一个数据出来
可是写子函数没问题,到了读子函数就死在里面了,也不知道自己的程序到底可不可行求大神指导!!!急急急!!!
初始化程序全是按照火哥视频来的 可是程序到读子函数的
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
printf(" \r\n ÒѶÁ2 \r\n ");
就死了串口输不错字符了;
#include "EEPROM.H"
#include "chuan.h"
void GPIO_INIT_EEPROM_CONFING()
{
GPIO_InitTypeDef GPIO_INIT;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
GPIO_INIT.GPIO_Mode=GPIO_Mode_AF_OD;
GPIO_INIT.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
GPIO_INIT.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_INIT);
}
void EEPROM_INIT_CONFING()
{
I2C_InitTypeDef I2C_INIT;
I2C_INIT.I2C_Ack=I2C_Ack_Enable;
I2C_INIT.I2C_AcknowledgedAddress=I2C_AcknowledgedAddress_7bit;
I2C_INIT.I2C_ClockSpeed=400000;
I2C_INIT.I2C_DutyCycle=I2C_DutyCycle_2;
I2C_INIT.I2C_Mode=I2C_Mode_I2C;
I2C_INIT.I2C_OwnAddress1=0X0A;
I2C_Init(I2C1,&I2C_INIT);
I2C_Cmd(I2C1,ENABLE);
}
void I2C_INIT1()
{
GPIO_INIT_EEPROM_CONFING();
EEPROM_INIT_CONFING();
}
void write_i2c(uint8_t addr,uint8_t data)
{
//·¢ËÍÆðʼÐźÅ
I2C_GenerateSTART(I2C1,ENABLE);
//µÈ´ýÓ¦´ð
while(!(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)));
//·¢ËÍдÃüÁî
I2C_Send7bitAddress(I2C1,0XA0,I2C_Direction_Transmitter);
//µÈ´ýÓ¦´ð
while(!(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)));
//·¢Ë͵ØÖ·
I2C_SendData(I2C1,addr);
//µÈ´ýÓ¦´ð
while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)));
//·¢ËÍÊý¾Ý
I2C_SendData(I2C1,data);
//µÈ´ýÓ¦´ð
while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)));
//·¢ËͽáÊøÖ¸Áî
I2C_GenerateSTOP(I2C1,ENABLE);
//Êä³öÒÑ·¢ËÍ×Ö·û
printf(" \r\n ÒÑ·¢ËÍ \r\n ");
}
int read_i2c(uint8_t addr)
{
uint16_t temp;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); // Added by Najoua 27/08/2008
I2C_AcknowledgeConfig(I2C1, DISABLE);
/* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE);
//*((u8 *)0x4001080c) &=~0x80;
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
printf(" \r\n ÒѶÁ1 \r\n ");
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, 0XA0, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
printf(" \r\n ÒѶÁ2 \r\n ");
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2C1, ENABLE);
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2C1, addr);
/* Test on EARM and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C1, 0XA0, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
temp=I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1,ENABLE);
printf(" \r\n ÒѶÁÍê \r\n ");
return temp;
}
|
|