93C46 Read and Write 51 Programs

Publisher:BlissfulBlissLatest update time:2016-10-30 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
// 93C46 read and write program, 8-bit data format

#include

sbit CS_93C46=P2^7;
sbit SK_93C46=P2^6;
sbit DI_93C46=P2^5;
sbit DO_93C46=P2^4;

unsigned char RD_93C46_byte(unsigned char addr); // read 1 byte data from the specified address inside 93c46
void WR_93C46_byte(unsigned char addr,unsigned char dat); // write 1 byte data to the specified address inside 93c46
void EWEN_93C46(void); // erase and write enabled
void EWDS_93C46(void); // erase and write disabled
void ERASE_93C46(unsigned char addr); // erase data from the specified address
void delaynms(unsigned char k); // ms delay
void leddisplay(); // digital tube display
unsigned char code tab[]={0xb7,0x12,0x67,0x76,0xd2,0xf4,0xf5,0x16,0xf7,0xf6,0xd7,0xf1,0xa5,0x73,0xe5,0xc5,0,0xff};
                          //Common cathode code 0-F, all off, all on
unsigned char ledxs[8]={16,16,16,16,16,0,0,0}; //Digital tube display buffer

void main()
{
  unsigned char temp;
  delaynms(200);
  leddisplay();
  delaynms(200);delaynms(200);delaynms(200);

  WR_93C46_byte(0x01,123);
  temp=RD_93C46_byte(0x01);
  ledxs[7]=temp%10;
  ledxs[6]=(temp/10)%10;
  ledxs[5]=temp/100;
  leddisplay();
  while(1);

}
//--------------------------------------------------------------------
// Read 1 byte of data at the specified address inside 93c46
//--------------------------------------------------------------------
unsigned char RD_93C46_byte(unsigned char addr)
{
  unsigned char dat=0,i;
  SK_93C46=0;
  CS_93C46=0;
  CS_93C46=1;

  DI_93C46=1;SK_93C46=1;SK_93C46=0; 
  DI_93C46=1;SK_93C46=1;SK_93C46=0; 
  DI_93C46=0;SK_93C46=1;SK_93C46=0; // Read data instruction: 110

  for(i=0;i<7;i++) // write 7-bit address
  {
    addr<<=1;
    if((addr&0x80)==0x80)
      DI_93C46=1;
    else
      DI_93C46=0;
    SK_93C46=1;
    SK_93C46=0;
  }
  
  DO_93C46=1; // DO=1, prepare for reading

  for(i=0;i<8;i++) // read 8-bit data
  {
    dat<<=1;
    SK_93C46=1;
    if(DO_93C46) dat+=1;
    SK_93C46=0;
  }
  CS_93C46=0;
  return(dat);
}
//---------------------------------------------------------------------------------
// Write 1 byte of data to the specified address inside 93c46
//---------------------------------------------------------------------------------
void WR_93C46_byte(unsigned char addr,unsigned char dat)
{
  unsigned char i;
  EWEN_93C46(); // erase and write allowed

  CS_93C46=0;
  SK_93C46=0;
  CS_93C46=1;

  DI_93C46=1;SK_93C46=1;SK_93C46=0; 
  DI_93C46=0;SK_93C46=1;SK_93C46=0; 
  DI_93C46=1;SK_93C46=1;SK_93C46=0; // Write data instruction: 101

  for(i=0;i<7;i++)   // 写7位地址
  {
    addr<<=1;
    if((addr&0x80)==0x80)
      DI_93C46=1;
    else
      DI_93C46=0;
    SK_93C46=1;
    SK_93C46=0;
  }

  for(i=0;i<8;i++) // write 8-bit data
  {
    if((dat&0x80)==0x80)
      DI_93C46=1;
    else
      DI_93C46=0;
    SK_93C46=1;
    SK_93C46=0;
    dat<<=1;
  }
  CS_93C46=0;
  DO_93C46=1;
  CS_93C46=1;
  while(DO_93C46==0); // detect busy/idle
  SK_93C46=0;
  CS_93C46=0;
  EWDS_93C46(); // erase and write prohibited

}
//----------------------------------------------------------------------------
// Erase and write are allowed, instruction format: 100 Address: 11xxxxx (7 bits)
//----------------------------------------------------------------------------
void EWEN_93C46(void)   
{
  unsigned char i,addr;
  CS_93C46=0;
  SK_93C46=0;
  CS_93C46=1;
  
  DI_93C46=1;SK_93C46=1;SK_93C46=0;
  DI_93C46=0;SK_93C46=1;SK_93C46=0;
  DI_93C46=0;SK_93C46=1;SK_93C46=0; // 100
  
  addr=0x7f; // 01111111B
  for(i=0;i<7;i++) // write 7-bit address 11xxxxx
  {
    addr<<=1;
    if((addr&0x80)==0x80)
      DI_93C46=1;
    else
      DI_93C46=0;
    SK_93C46=1;
    SK_93C46=0;
  }
  CS_93C46=0;
}
//-----------------------------------------------------------------------------
// Erase and write prohibited, instruction format: 100 Address: 00xxxxx (7 bits)
//-----------------------------------------------------------------------------
void EWDS_93C46(void)   
{
  unsigned char i,addr;
  CS_93C46=0;
  SK_93C46=0;
  CS_93C46=1;
  
  DI_93C46=1;SK_93C46=1;SK_93C46=0; 
  DI_93C46=0;SK_93C46=1;SK_93C46=0; 
  DI_93C46=0;SK_93C46=1;SK_93C46=0; // 100
  
  addr=0x00; // 00000000B
  for(i=0;i<7;i++) // write 7-bit address 00xxxxx
  {
    addr<<=1;
    if((addr&0x80)==0x80)
      DI_93C46=1;
    else
      DI_93C46=0;
    SK_93C46=1;
    SK_93C46=0;
  }
  CS_93C46=0;
}

//------------------------------------

//Serial digital tube display

//------------------------------------

void leddisplay()    
{
  unsigned char i;
  for(i=0;i<8;i++)
  {
    SBUF=tab[ledxs[i]];
    while(TI==0);
    TI=0;
  }
}

//---------------------------------------------------------------

//Delay n ms

//---------------------------------------------------------------
void delaynms(unsigned char k)  
{
  while(k)
  {
  int i;
  i=110;
  while(i--);
  k=k-1;
  }
}

Reference address:93C46 Read and Write 51 Programs

Previous article:X9313 51 programs
Next article:51 programs of TLC5615

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号