您当前的位置:五五电子网电子知识单片机-工控设备源码-程序基于HI-TECH C的PIC12C508读写93LC4源程序 正文
基于HI-TECH C的PIC12C508读写93LC4源程序

基于HI-TECH C的PIC12C508读写93LC4源程序

点击数:7910 次   录入时间:03-04 11:58:51   整理:http://www.55dianzi.com   源码-程序
 Description :  ERASE/WRITE DISAble
 ------------------------------------------------------*/     
void EWDS(void)
{
    unsigned char i;

        StartBit();                  /* 1 */

        DI = 0;                      /* 0000xxxx, (opcode:00, Address:00xxxx) */
    for(i=0; i<8; i++)
        Pulse();

        CS = 0;
}

    /*----------------------------------------------------
 Function : Write93LC46         
 Input : unsigned char Offset Address, unsigned int tx_data       
 Output : None         
 Description :  Write 16bits data in to 93LC46 Offset Address
 ------------------------------------------------------*/     
void Write93LC46(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 = 0x0080;
    for(i=0; i<8; i++) {
        if(Addr & temp)
            DI = 1;
        else
            DI = 0;
        Pulse();
        temp >>= 1;
    }

        temp = 0x8000;                      /* DDDDDDDDDDDDDDDD(16bits data)*/
    for(i=0; i<16; i++) {
        if(tx_data & temp)
            DI = 1;
        else
            DI = 0;
        Pulse();
        temp >>= 1;
    }
    CS = 0;

        EWDS();
}

    /*----------------------------------------------------
 Function : Read93LC46         
 Input : unsigned char Offset Address
 Output : unsigned int         
 Description :  Read 16bits data from 93LC46 offset address
 ------------------------------------------------------*/     
unsigned int Read93LC46(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)
            DI = 1;
        else
            DI = 0;
        Pulse();
        temp >>= 1;
    }

        rx_data = 0x0000;                    /* DDDDDDDDDDDDDDDD(16bits data)*/
    for(i=0; i<16; i++) {
        Pulse();
        if(DO)
            rx_data |= 0x0001;
        if(i < 15)
            rx_data <<= 1;
    }
    CS = 0;

        return(rx_data);
}

    void InitPIC(void)
{
    OPTION = (GPWU | GPPU | PS2 | PS1 | PS0);
    TRIS = 0x10;   
    CS = 0;
    CLK = 0;
    DI = 0;   
}

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

    }



上一页  [1] [2] 


本文关键字:源程序  源码-程序单片机-工控设备 - 源码-程序