⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 93lc46b.c

📁 430和93C46的接口程序
💻 C
字号:
/************************************************************ 
 * Processer : Microchip PIC12C508        *
 * Compiler : Hi-TECH PICC 8.00 PL2       *
 * Writer : Jason Kuo           *
 * Description : It can read/write 93LC46 (64 x 16-bit organization) *
 *************************************************************/ 


/*      OSCCAL bits     */

#include"msp430x14x.h"

    #define CS_1   P1OUT |=  BIT4              
                              //SDA = 1
    #define CS_0   P1OUT &=~ BIT4              
                              //SDA = 0
    #define CLK_1   P1OUT |=  BIT5              
                              //SDA = 1
    #define CLK_0   P1OUT &=~ BIT5              
                              //SDA = 0
    #define DO_1   P1OUT |=  BIT6              
                              //SCL = 1
    #define DO_0   P1OUT &=~ BIT6  
    #define DI_1   P2OUT |=  BIT6              
                              //SCL = 1
    #define DI_0   P2OUT &=~ BIT6            
                              //SCL = 0
    #define DIR_IN  P2DIR &=~ BIT6  //SDA_1      
                              //I/O口为输入
    #define DIR_OUT P2DIR |=  BIT6             
                              //I/0口为输出
    #define DI  (P2IN&BIT6)>>6        
                         //Read SDA                                                                                   

void Delay(unsigned int counter);
void Pulse(void);
void StartBit(void);
void EWEN(void);
void EWDS(void);
extern void Write93LC46(unsigned char Offset_Addr, unsigned int tx_data);  
extern unsigned int Read93LC46(unsigned char Offset_Addr);
void Init93lc46b(void);
/*---------------------------------------------------- 
 Function : Delay           
 Input : unsigned int (counter)        
 Output : None           
 Description : Delay routine        
 if counter=1  delay 35us , if counter=10 delay 134us, 
 if counter=100 delay 1.12ms,
 These delay is base on internal 4MHz      
------------------------------------------------------*/      
void Delay(unsigned int counter)
{
   while(counter>0) counter--;  
}                                                                              
                                          
void Pulse(void)
{
    CLK_1;
    Delay(35);
    CLK_0;
}     
void StartBit(void)
{
    CS_1;
    DO_1;
    Pulse();
}     
void EWEN(void)
{
    unsigned char i,temp;

    StartBit();                  /* 1 */

    temp = 0x80;                        /* 0011xxxx ,(opcode:00, Address:11xxxx) */
    for(i=0; i<8; i++) 
    {
        if(0x30 & temp)
            DO_1;
        else
            DO_0;
        Pulse();
        Delay(26);
        temp >>= 1;
     }
    CS_0;
    Delay(200);
}    
void EWDS(void)
{
    unsigned char i;

    StartBit();                  /* 1 */

    DO_0;                       /* 0000xxxx, (opcode:00, Address:00xxxx) */
    for(i=0; i<8; i++)
      {  Pulse();
         Delay(26);
         }

    CS_0;
    Delay(200);
}

/*---------------------------------------------------- 
 Function : Write93LC46           
 Input : unsigned char Offset Address, unsigned int tx_data        
 Output : None           
 Description :  Write 16bits data in to 93LC46 Offset Address
 ------------------------------------------------------*/   
void twi_w_rom(unsigned char Offset_Addr, unsigned int tx_data)
{
    unsigned char Addr, i;
    unsigned int temp;
    EWEN();
    StartBit();                  /* 1 */
    Offset_Addr=Offset_Addr&0x3F; /* 6bits address */
    Addr = Offset_Addr + 0x40;          /* 01AAAAAA ,(opcode:01, address:AAAAAA) */
    temp = 0x80;
    for(i=0; i<8; i++) 
    {
        if(Addr & temp)
            DO_1;
        else
            DO_0;
        Pulse();
        Delay(26);
        temp >>= 1;
    }
Delay(200);
    temp = 0x8000;                      /* DDDDDDDDDDDDDDDD(16bits data)*/
    for(i=0; i<16; i++) {
        if(tx_data & temp)
            DO_1;
        else
            DO_0;
        Pulse();
        Delay(26);
        temp >>= 1;
    }
    CS_0;
    EWDS();
    Delay(200);
}
/*---------------------------------------------------- 
 Function : Read93LC46           
 Input : unsigned char Offset Address
 Output : unsigned int           
 Description :  Read 16bits data from 93LC46 offset address
 ------------------------------------------------------*/      
unsigned int twi_r_rom(unsigned char Offset_Addr)
{
    unsigned char Addr, i, temp;
    unsigned int  rx_data;

    StartBit();                   /* 1 */
    Offset_Addr = Offset_Addr&0x3F; /* 6bits address */
    Addr = Offset_Addr + 0x80;           /* 10AAAAAA ,(opcode:10, address:AAAAAA) */
    temp = 0x80;
    for(i=0; i<8; i++) {
        if(Addr & temp)
            DO_1;
        else
            DO_0;
        Pulse();
        Delay(26);
        temp >>= 1;
    }
    Delay(200);
    rx_data = 0x0000;                    /* DDDDDDDDDDDDDDDD(16bits data)*/
    DIR_IN;
    for(i=0; i<16; i++) 
    {Delay(26);
        Pulse();
        if(DI)
            rx_data |= 0x0001;
        if(i < 15)
            rx_data <<= 1;
    }
    CS_0;
    DIR_OUT;
    Delay(200);
    return(rx_data);
}
void Init93lc46b(void)
{   WDTCTL = WDTPW + WDTHOLD;// Stop WDT
   P1DIR |= 0xfe; // Setup P3 for SPI mode
   P2DIR |= 0x00;
    CS_0;
    Delay(15);
    CS_1;
    Delay(15);
    CLK_0;
    DO_0;    
}

/* Main routine */
void main(void)
{
 unsigned char addr;
 unsigned int rx_buf;
 
 Init93lc46b();
 /* Read a word then +1 and write back to 93LC46 */
 while(1)
 {
 //for (addr = 0; addr < 10; addr++)
 //{
 for(addr = 0; addr < 64; addr++)
 { rx_buf = Read93LC46(0x80);
 // rx_buf = rx_buf+1;
 //Write93LC46(0x80, 0x5055);
  //Write93LC46(0x80+addr, 0x55);
 }
}
}

⌨️ 快捷键说明

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