新聞中心

        EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 使用PIC12C508讀寫93LC4的HI-TECH C代碼

        使用PIC12C508讀寫93LC4的HI-TECH C代碼

        作者: 時(shí)間:2012-07-18 來(lái)源:網(wǎng)絡(luò) 收藏

        本文引用地址:http://www.104case.com/article/171143.htm

        /*----------------------------------------------------
        Function : StartBit
        Input : None
        Output : None
        Description :
        1. Set Chip Select(CS) = 1 (high)
        2. Set a Start Bit(1) to Serial Data Input(DI)
        ------------------------------------------------------*/
        void StartBit(void)
        {
        CS = 1;
        DI = 1;
        Pulse();
        }

        /*----------------------------------------------------
        Function : EWEN
        Input : None
        Output : None
        Description : ERASE/WRITE Enable
        ------------------------------------------------------*/
        void EWEN(void)
        {
        unsigned char i,temp;

        StartBit(); /* 1 */

        temp = 0x80; /* 0011xxxx ,(opcode:00, Address:11xxxx) */
        for(i=0; i8; i++) {
        if(0x30 temp)
        DI = 1;
        else
        DI = 0;
        Pulse();
        temp >>= 1;
        }

        CS = 0;
        }

        /*----------------------------------------------------
        Function : EWDS
        Input : None
        Output : None
        Description : ERASE/WRITE Disable
        ------------------------------------------------------*/
        void EWDS(void)
        {
        unsigned char i;

        StartBit(); /* 1 */

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

        CS = 0;
        }

        /*----------------------------------------------------
        Function : Write6
        Input : unsigned char Offset Address, unsigned int tx_data
        Output : None
        Description : Write 16bits data in to 6 Offset Address
        ------------------------------------------------------*/
        void Write6(unsigned char Offset_Addr, unsigned int tx_data)
        {
        unsigned char Addr, i;
        unsigned int temp;

        EWEN();

        StartBit(); /* 1 */
        Offset_Addr=Offset_Addr0x3F; /* 6bits address */
        Addr = Offset_Addr + 0x40; /* 01AAAAAA ,(opcode:01, address:AAAAAA) */
        temp = 0x0080;
        for(i=0; i8; i++) {
        if(Addr temp)
        DI = 1;
        else
        DI = 0;
        Pulse();
        temp >>= 1;
        }

        temp = 0x8000; /* DDDDDDDDDDDDDDDD(16bits data)*/
        for(i=0; i16; 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_Addr0x3F; /* 6bits address */
        Addr = Offset_Addr + 0x80; /* 10AAAAAA ,(opcode:10, address:AAAAAA) */
        temp = 0x80;
        for(i=0; i8; i++) {
        if(Addr temp)
        DI = 1;
        else
        DI = 0;
        Pulse();
        temp >>= 1;
        }

        rx_data = 0x0000; /* DDDDDDDDDDDDDDDD(16bits data)*/
        for(i=0; i16; 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);
        }

        }

        51單片機(jī)相關(guān)文章:51單片機(jī)教程


        c語(yǔ)言相關(guān)文章:c語(yǔ)言教程



        上一頁(yè) 1 2 下一頁(yè)

        關(guān)鍵詞: HI-TECH 代碼 93LC4 讀寫 PIC12C508 使用

        評(píng)論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 广丰县| 邹城市| 商河县| 永安市| 门源| 临邑县| 若尔盖县| 沅陵县| 班戈县| 彭水| 乌兰察布市| 桐梓县| 平利县| 绥江县| 水城县| 海淀区| 静宁县| 双流县| 兴业县| 阳西县| 罗山县| 长岭县| 河津市| 卓尼县| 衡阳县| 霍林郭勒市| 阜康市| 济源市| 伊宁县| 吉安县| 裕民县| 石首市| 丹寨县| 镶黄旗| 延津县| 惠来县| 巧家县| 贡觉县| 广河县| 白银市| 海淀区|