main.c:
#include "stm32f4xx.h" // Device header
#include "serial.h"
#include "delay.h"
#include "OLED.h"
#include "eeprom.h"
uint8_t currentAddre=0;
uint8_t readData=0;
uint8_t testvalue=0xf0;
int main(void)
{
Serial_Init();
EEPROM_Init();
printf("123success\r\n");
printf("testvalue=%x\r\n",testvalue);
currentAddre=Read_CurrentAddre();
printf("currentAddre:%x\r\n",currentAddre);
Write_ByteData(0xff,0x31);
readData=Read_ByteData(0xff);
printf("readData=%x\r\n",readData);
while (1)
{
}
}
eeprom.c:
#include "stm32f4xx.h" // Device header
#include "i2c.h"
#include "delay.h"
#define DeviceAddress 0xA0
/*读取当前地址*/
uint8_t Read_CurrentAddre(){
uint8_t address=0xff;
i2c_start();
i2c_WriteData(DeviceAddress|0x01);
Ack_Receive();
address=i2c_ReadData();
Ack_Give(1);
i2c_Stop();
return address;
}
void Write_ByteData(uint8_t wordAddress,uint8_t Data){
i2c_start();
i2c_WriteData(DeviceAddress);//地址写0xA0
Ack_Receive();
i2c_WriteData(wordAddress);
Ack_Receive();
i2c_WriteData(Data);
Ack_Receive();
i2c_Stop();
Delay_ms(1000);//等待写入完成
}
/*任意地址读*/
uint8_t Read_ByteData(uint8_t wordAddress){
uint8_t readData=0;
i2c_start();
i2c_WriteData(DeviceAddress);//写地址
Ack_Receive();
i2c_WriteData((uint8_t)wordAddress);
Ack_Receive();
i2c_start();
i2c_WriteData(DeviceAddress|0x01);
Ack_Receive();
readData=Read_CurrentAddre();
Ack_Give(1);
i2c_Stop();
return readData;
}
void EEPROM_Init(){
/*SCL*/
SCL_GPIO_Init();
/*SDA*/
SDA_GPIO_Init();
/*A0,A1,A2*/
/*A0,A1,A2*/
//GPIO_ResetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_4);
/*VCC*/
//GPIO_SetBits(GPIOD,GPIO_Pin_3);
GPIO_SetBits(GPIOF,GPIO_Pin_0|GPIO_Pin_1);
}
i2c.c:
#include "stm32f4xx.h" // Device header
#include "delay.h"
void SDA(uint8_t BitValue){
GPIO_WriteBit(GPIOF,GPIO_Pin_0,(BitAction)BitValue);
Delay_us(10);
}
void SCL(uint8_t BitValue){
GPIO_WriteBit(GPIOF,GPIO_Pin_1,(BitAction)BitValue);
Delay_us(10);
}
void SCL_GPIO_Init(){
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
GPIO_InitTypeDef SCL_InitStructure;
SCL_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
SCL_InitStructure.GPIO_OType=GPIO_OType_OD;
SCL_InitStructure.GPIO_Pin=GPIO_Pin_1;
SCL_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
SCL_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOF,&SCL_InitStructure);
}
void SDA_GPIO_Init(){
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
GPIO_InitTypeDef SDA_InitStructure;
SDA_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
SDA_InitStructure.GPIO_OType=GPIO_OType_OD;
SDA_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
SDA_InitStructure.GPIO_Pin=GPIO_Pin_0;
SDA_InitStructure.GPIO_Speed=GPIO_Speed_50MHz ;
GPIO_Init(GPIOF,&SDA_InitStructure);
}
void i2c_start(){
SCL(1);
SDA(1);
Delay_us(10);
SDA(0);
Delay_us(10);
SCL(0);
Delay_us(10);
}
void i2c_Stop(){
SCL(0);
SDA(0);
Delay_us(10);
SCL(1);
Delay_us(10);
SDA(1);
Delay_us(10);
}
/*stm32接收Ack应答*/
void Ack_Receive(){
SCL(0);
Delay_us(10);
SDA(1);//SDA等待从机设置
Delay_us(10);
SCL(1);
Delay_us(10);
SCL(0);
Delay_us(10);
}
/*stm32给出Ack应答*/
void Ack_Give(uint8_t value){
SCL(0);
Delay_us(10);
if(value==0){
SDA(0);
Delay_us(10);
}
else{
SDA(1);
Delay_us(10);
}
SCL(1);
Delay_us(10);
SCL(0);
}
void i2c_WriteData(uint8_t Data){
uint8_t finger=0x80;
SCL(0);
Delay_us(10);
for(uint8_t k=0;k<8;k++){
if(Data&finger){
SDA(1);
Delay_us(10);
}
else{
SDA(0);
Delay_us(10);
}
SCL(1);//读走SDA电平
Delay_us(10);
SCL(0);
Delay_us(10);
finger=finger>>1;
}
}
uint8_t i2c_ReadData(){
SCL(0);
Delay_us(10);
SDA(1);
Delay_us(10);
uint8_t k=0;
uint8_t ReadData=0x00;
uint8_t finger=0x80;
for(k=0;k<8;k++){
SCL(1);
Delay_us(10);
if(GPIO_ReadInputDataBit(GPIOF,GPIO_Pin_0)==1){
ReadData=ReadData|(finger>>k);
}
else{
}
Delay_us(10);
SCL(0);
Delay_us(10);
//finger=finger>>1;
}
return ReadData;
}
请问为什么读取数据全是f,代码哪里有问题?