原子大神求救啊。我在EEPROM中地址为0的地方写进数字16,可是读出来却是255.这说明EEPROM在传数据的时候根本没有拉低电平,这是怎么回事啊?下面附有我的程序#include "IIC.h"//*******************************IIC总线*********************************//
#include "systick.h"
void IIC_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = IIC_SCL|IIC_SDA;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
IIC_SCL_H;
IIC_SDA_H;
}
void IIC_SDA_OUT()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = IIC_SDA;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void IIC_SDA_IN()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = IIC_SDA;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void IIC_Start()
{
IIC_SDA_OUT();
IIC_SDA_H;
IIC_SCL_H;
delay_us(5);
IIC_SDA_L;
delay_us(6);
IIC_SCL_L;
}
void IIC_Stop()
{
IIC_SDA_OUT();
IIC_SCL_L;
IIC_SDA_L;
IIC_SCL_H;
delay_us(6);
IIC_SDA_H;
delay_us(6);
}
void IIC_Ack() //主机应答
{
IIC_SCL_L;
IIC_SDA_OUT();
IIC_SDA_L;
delay_us(2);
IIC_SCL_H;
delay_us(5);
IIC_SCL_L;
}
void IIC_NAck() //主机非应答
{
IIC_SCL_L;
IIC_SDA_OUT();
IIC_SDA_H;
delay_us(2);
IIC_SCL_H;
delay_us(5);
IIC_SCL_L;
}
u8 IIC_Wait_Ack() //主机等待从机应答 失败:1 成功:0
{
u8 temptime=0;
IIC_SDA_IN();
IIC_SDA_H;
delay_us(1);
IIC_SCL_H;
delay_us(1);
while(GPIO_ReadInputDataBit(GPIO_IIC, IIC_SDA))
{
temptime++;
if(temptime>250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL_L;
return 0;
}
void IIC_Send_Byte(u8 txd)
{
u8 i=0;
IIC_SDA_OUT();
IIC_SCL_L;
for(i=0;i<8;i++)
{
if(txd&0X80>0)
IIC_SDA_H;
else
IIC_SDA_L;
txd<<=1;
IIC_SCL_H;
delay_us(2);
IIC_SCL_L;
delay_us(2);
}
}
u8 IIC_Read_Byte(u8 Ack)
{
u8 i=0;
u8 receive=0;
IIC_SDA_IN();
for(i=0;i<8;i++)
{
IIC_SCL_L;
delay_us(2);
IIC_SCL_H;
receive<<=1;
if(GPIO_ReadInputDataBit(GPIO_IIC, IIC_SDA))
receive++;
delay_us(1);
}
if(Ack==0)
IIC_NAck();
else
IIC_Ack();
return receive;
}
//******************************AT24C02写入读出**********************//
#include "AT24Cxx.h"
u8 AT24Cxx_ReadOneByte(u16 addr)
{
u8 temp=0;
IIC_Start();
if(EE_TYPE>AT24C16)
{
IIC_Send_Byte(0xA0);
IIC_Wait_Ack();
IIC_Send_Byte(addr>>8);
}
else
{
IIC_Send_Byte(0xA0+((addr/256)<<1));
}
IIC_Wait_Ack();
IIC_Send_Byte(addr%256);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0xA1);
IIC_Wait_Ack();
temp=IIC_Read_Byte(0);
IIC_NAck();
IIC_Stop();
return temp;
}
u8 AT24Cxx_ReadTwoByte(u16 addr)
{
u16 temp=0;
IIC_Start();
if(EE_TYPE>AT24C16)
{
IIC_Send_Byte(0xA0);
IIC_Wait_Ack();
IIC_Send_Byte(addr>>8);
}
else
{
IIC_Send_Byte(0xA0+((addr/256)<<1));
}
IIC_Wait_Ack();
IIC_Send_Byte(addr%256);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0xA1);
IIC_Wait_Ack();
temp=IIC_Read_Byte(1);
temp<<=8;
temp|=IIC_Read_Byte(0);
IIC_Stop();
return temp;
}
void AT24Cxx_WriteOneByte(u16 addr,u8 dt)
{
IIC_Start();
if(EE_TYPE>AT24C16)
{
IIC_Send_Byte(0xA0);
IIC_Wait_Ack();
IIC_Send_Byte(addr>>8);
}
else
{
IIC_Send_Byte(0xA0+((addr/256)<<1));
}
IIC_Wait_Ack();
IIC_Send_Byte(addr%256);
IIC_Wait_Ack();
IIC_Send_Byte(dt);
IIC_Wait_Ack();
IIC_Stop();
delay_ms(10);
}
void AT24Cxx_WriteTwoByte(u16 addr,u16 dt)
{
IIC_Start();
if(EE_TYPE>AT24C16)
{
IIC_Send_Byte(0xA0);
IIC_Wait_Ack();
IIC_Send_Byte(addr>>8);
}
else
{
IIC_Send_Byte(0xA0+((addr/256)<<1));
}
IIC_Wait_Ack();
IIC_Send_Byte(addr%256);
IIC_Wait_Ack();
IIC_Send_Byte(dt>>8);
IIC_Wait_Ack();
IIC_Send_Byte(dt&0x0ff);
IIC_Wait_Ack();
IIC_Stop();
delay_ms(10);
}
//****************************printf****************************//
#include "printf.h"
int fputc(int ch,FILE *p)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
return ch;
}
void printf_init()
{ GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //tx
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP ;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //rx
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING ;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None ;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure) ;
}
//******************************main**************************//
#include "systick.h"
#include "public.h"
#include "printf.h"
#include "AT24Cxx.h"
int main()
{
u8 wdate=16;
u8 value=0;
printf_init();
IIC_init();
delay_ms(1);
AT24Cxx_WriteOneByte(0,wdate);
printf("写入的是:%d
",wdate);
value=AT24Cxx_ReadOneByte(0);
printf("读出的是:%d
",value);
while(1);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
别忘记在SDA,SCL总线上,连接上拉电阻(4.7K, 10K都可以)。
一周热门 更多>