我正在调AT24C64的读写程序,使用的单片机是STC12C5A60S2,程序的操作是:通过串口2输入控制指令,当输入3时,向AT24C64的前16个地址写入16个字符‘a’;当输入1时,从AT24C64的前十个地址中读出10个字符a,程序的代码如下:
主文件代码:
#include "STC12C5A60S2.h"
#include "Serial.h"
void main()
{
uchar read_high=0x00;//读地址
uchar read_low=0x00;
uchar write_high=0x00; //写地址
uchar write_low=0x00;
Uart_Two_Init(); //串口1和串口2初始化
Uart_One_Init();
while(1)
{
if(s2_flag==1)
{
s2_flag=0; //将标志位置0,防止if(s2_flag==1)内的程序体循环执行
switch(s2_com)
{
case '1'://当输入命令1时,从EEPOM中读出前10个字节
read_low=0x00;
for(;read_low<0x0a;read_low++)
{
delayms(20);
rom_read=AT24C64_R(read_high,read_low);
Uart_Two_Send(rom_read);
}
Uart_Two_Send('
');
break;
case '2'://当输入命令2时,输出no data
UART_Two_Printf("NO DATA!!!!!!!!
");
break;
case '3':
write_low=0x00;
for(;write_low<0x10;write_low++)
{
AT24C64_W('a',write_high,write_low);
delayms(20);
}
}
}
}
AT24C64.H文件的代码:
#define uchar unsigned char
#define uint unsigned int
sbit I2C_SDA=P2^0; //定义AT24C64的数据口
sbit I2C_SCK=P2^1; //定义AT24C64的时钟口
void Delay_10_uS(void)
{
char i=10;
while(i--);
}
void Delay_N_mS( uint n_milisecond) /* n mS delay */
{
uchar i;
while(n_milisecond--)
{
i=37;
while(i--);
}
}
bit I2C_Start(void) //启动IIC总线
{
Delay_10_uS();
I2C_SDA =1;
Delay_10_uS();
I2C_SCK =1;
Delay_10_uS();
if ( I2C_SDA == 0)
return 0;
if ( I2C_SCK == 0)
return 0;
I2C_SDA = 0;
Delay_10_uS();
I2C_SCK = 0;
Delay_10_uS();
return 1;
}
void I2C_Stop(void) //关闭IIC总线
{
Delay_10_uS();
I2C_SDA = 0;
Delay_10_uS();
I2C_SCK = 1;
Delay_10_uS();
I2C_SDA = 1;
Delay_10_uS();
}
void I2C_Ack(void)
{
Delay_10_uS();
I2C_SDA=0;
Delay_10_uS();
I2C_SCK=1;
Delay_10_uS();
I2C_SCK=0;
Delay_10_uS();
}
void I2C_Nack(void)
{
Delay_10_uS();
I2C_SDA=1;
Delay_10_uS();
I2C_SCK=1;
Delay_10_uS();
I2C_SCK=0;
Delay_10_uS();
}
bit I2C_Send_Byte( uchar d)
{
uchar i = 8;
bit bit_ack;
while( i-- )
{
Delay_10_uS();
if ( d &0x80 )
I2C_SDA =1;
else
I2C_SDA =0;
Delay_10_uS();
I2C_SCK = 1;
Delay_10_uS();
I2C_SCK = 0;
d = d << 1;
}
Delay_10_uS();
I2C_SDA = 1;
Delay_10_uS();
I2C_SCK = 1;
Delay_10_uS();
bit_ack = I2C_SDA;
I2C_SCK =0;
Delay_10_uS();
return bit_ack;
}
uchar I2C_Receive_Byte(void)
{
uchar i = 8, d;
Delay_10_uS();
I2C_SDA = 1;
while ( i--)
{
d = d << 1;
Delay_10_uS();
I2C_SCK =1;
if ( I2C_SDA )
d++;
Delay_10_uS();
I2C_SCK =0;
}
return d;
}
void AT24C64_W(uchar write_in,uchar address_high,uchar address_low)
{
I2C_Start();/*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/
/* 24C16? USE */
I2C_Send_Byte( 0xa0 );
I2C_Send_Byte( address_high);
I2C_Send_Byte( address_low );
I2C_Send_Byte( write_in );
I2C_Stop();
Delay_N_mS(10);/* waiting for write cycle to be completed */
}
uchar AT24C64_R(uchar address_high,uchar address_low)
{
uchar rom_read; //AT24C64的数据读缓冲
I2C_Start();/*I2C_Send_Byte( 0xa0 + AT24C64_address / 256 *2 );*/
/* 24C16 USE */
I2C_Send_Byte( 0xa0 );
I2C_Send_Byte( address_high );
I2C_Send_Byte( address_low );
I2C_Start();
/*I2C_Send_Byte( 0xa1 + AT24C64_address /256 *2 );*/
I2C_Send_Byte( 0xa1 );
rom_read = I2C_Receive_Byte();
I2C_Nack();
I2C_Stop();
return rom_read;
}
实验结果:
程序没有输出十个字符a的ascii码,而是输出了夹杂有F8,FF的一串信息,请高手指出错误
此帖出自
小平头技术问答
一周热门 更多>