24c256.c

来自「一个基于C51开发的计价秤」· C语言 代码 · 共 179 行

C
179
字号
//
//24C32---24C2566
//
#include <SST89x5xxRD2.h>
#include <intrins.h>
#include <stdio.h>
#define uchar unsigned char 
#define uint unsigned int
#define ulong unsigned long

uchar buf[50];
sbit SDA=P1^0;
sbit SCL=P1^1;

sbit a0=ACC^0;
sbit a1=ACC^1;
sbit a2=ACC^2;
sbit a3=ACC^3;
sbit a4=ACC^4;
sbit a5=ACC^5;
sbit a6=ACC^6;
sbit a7=ACC^7;

void delay(uchar n)
{
	uchar i;
	uint j;
	for(i=0;i<n;i++)
		for(j=0;j<1000;j++);	
}

void Wait(void)
     {
      _nop_();
      _nop_();
      _nop_();
      _nop_();
	  _nop_();
     }

void set_sda_hight(void)
     {
	  SDA=1;
      Wait();
	 }

void set_sda_low(void)
     {
	  SDA=0;
      Wait();
	 }

void set_scl_hight(void)
     {
	  SCL=1;
      Wait();
	 }

void set_scl_low(void)
     {
	  SCL=0;
      Wait();
	 }

void Start(void)
{                
  	Wait();
  	set_sda_hight();
  	set_scl_hight();
  	set_sda_low();
  	set_scl_low();
}

void Stop(void)
{ 
	Wait();
  	set_scl_low();
  	set_sda_low();
  	set_scl_hight();
  	set_sda_hight();
  	delay(10);
}

/*bit get_ack(void)
{
 	bit result;
 	set_sda_hight();
 	set_scl_hight();
 	result=SDA;
 	set_scl_low();
 	return(result);
}*/

/*void set_ack(void)
{
 	set_scl_low();
 	set_sda_low();
 	set_scl_hight();
 	set_scl_low();
}*/

uchar RB24(void) {
    ACC=0x00;
    set_sda_hight();
	set_scl_hight();a7=SDA;set_scl_low();
	set_scl_hight();a6=SDA;set_scl_low();
	set_scl_hight();a5=SDA;set_scl_low();
	set_scl_hight();a4=SDA;set_scl_low();
	set_scl_hight();a3=SDA;set_scl_low();
	set_scl_hight();a2=SDA;set_scl_low();
	set_scl_hight();a1=SDA;set_scl_low();
	set_scl_hight();a0=SDA;set_scl_low();
    set_sda_hight();set_scl_hight();set_scl_low();
	return(ACC);
}


void WB24(uchar dd) {
	ACC=dd;
	SDA=a7;set_scl_hight();set_scl_low();
	SDA=a6;set_scl_hight();set_scl_low();
	SDA=a5;set_scl_hight();set_scl_low();
	SDA=a4;set_scl_hight();set_scl_low();
	SDA=a3;set_scl_hight();set_scl_low();
	SDA=a2;set_scl_hight();set_scl_low();
	SDA=a1;set_scl_hight();set_scl_low();
	SDA=a0;set_scl_hight();set_scl_low();
    set_sda_hight();set_scl_hight();
    set_scl_low();
}

uchar read_byter(uchar address)
{
	uchar dd;
	Start();
	WB24(0xa0);
	WB24(address);
	Start();
	WB24(0xa1);
	dd=RB24();
	Stop();
	return(dd);
}

void write_byter(uchar address,uchar dd)
{    
	Start();
	WB24(0xa0);
	WB24(address);
	WB24(dd);
	Stop();
}

void write_eeprom()
{
	uchar i;
	for(i=0;i<50;i++)
  	{
   		write_byter(i,i);
  	}
}

void read_eeprom()
{
	uchar i;
	for(i=0;i<50;i++)
  	{
   		buf[i]=read_byter(i);
  	}
}
main()
{
	while(1)
	{
		write_eeprom();
		read_eeprom();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?