新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > PIC16F72-AT24C64讀寫

        PIC16F72-AT24C64讀寫

        作者: 時間:2016-11-13 來源:網絡 收藏
        AT24C64和AT24C02程序上的主要是在讀寫時序上有些差別,具體看數據手冊。這里列舉一個字節寫的時序差別。

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

        /*************PIC16F72單片機程序******************************/
        /*********************************************************************/
        /*****File Function : PIC 讀寫AT24C64 *****/
        /*****Program Author :ClimberWin *****/
        /*****MCU : PIC16F72 外部晶振6MHZ *****/
        /*****Compile Date : 2011/04/15 *****/
        /*****Edition Info : V1.0 *****/
        /*************************************************************/
        #include
        #include"WIN24C64.H"
        __CONFIG(11111110111010);//bit13-bit7=1;bit6 欠壓使能(1 enable);bit5=1;bit4 代碼保護(0保護);
        //bit3 上電延時(0 enable);bit2 看門狗(1 enable);bit1-bit0 時鐘選擇 (11 RC 10 HS 01 XT OO LP)

        #define uchar unsigned char
        #define uint unsigned int

        void Init(void); //初始化子程序
        void delayms(unsigned int count);

        #define KEY RB0//外部中斷


        #define LED16 RC0
        #define LED15 RC1
        #define LED14 RC2
        #define LED13 RC3
        #define LED12 RC4
        #define LED11 RC5
        #define LED10 RC6
        #define LED9 RC7


        /*********************************************/
        void delayms(unsigned int count)
        {
        uint i,j;
        for(i=0;ifor(j=0;j<120;j++);
        }
        /*********************************************/
        void Init(void)
        {

        PORTB = 0B00000000;
        PORTC = 0B00000000;

        TRISB = 0B00000001;//設置RB0為輸入,作為按鍵口
        TRISC = 0B00000000;//設置RC輸出

        RBPU=0;//PORTB上拉使能

        }


        ////////////主程序/////////////////////////
        void main (void)
        {
        uint win;
        uchar i;

        Init();//初始化程序
        delayms(100);
        PORTC=0XFF;
        PORTB=0XFF;
        PORTA=0XFF;


        WIN24C64_write(0x0000,0x55);//存數據到EEPROM
        delayms(20);
        WIN24C64_write(0x0001,0xaa);//存數據到EEPROM
        delayms(20);

        while(1)
        {
        delayms(1000); //延時
        PORTC=~WIN24C64_read(0x0000);//讀取24C64地址00H中的數據
        delayms(1000); //延時
        PORTC=~WIN24C64_read(0x0001);//讀取24C64地址00H中的數據
        }

        }

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        WIN24C64.H 頭文件

        /**********************中文版本*******************************/
        /*****功能描述 : PIC單片機 24C64 讀寫程序*****/
        /*****作 者 : ClimberWin *****/
        /*****編寫日期 : 2011年4月15日*****/
        /*****版本信息 : V1.0 *****/
        /*****修改日期 : *****/
        /*******************************************************/
        #ifndef __WIN24C64_H__
        #define __WIN24C64_H__

        #include

        #define uchar unsigned char
        #define uint unsigned int


        #define SCL RB2 //SCL 引腳定義
        #define SDA RB1 //SDA 引腳定義

        uchar WIN24C64_read(uint address); //從24c02的地址address中讀取一個字節數據

        void WIN24C64_write(uint address,uchar info); //向24c02的address地址中寫入一字節數據info

        void WIN24C64_init(); //24C64初始化子程序

        void delay1(uchar x);

        void start();

        void stop();

        void writex(uchar j);

        uchar readx();

        void clock();

        void delay1(uchar x)
        {
        uint i;
        for(i=0;i}


        void WIN24C64_init()
        {
        TRISB = 0B00000001;//設置RB1 SDA為輸出
        SCL=1;
        asm("NOP");
        SDA=1;
        asm("NOP");
        }

        void start()
        {
        TRISB = 0B00000001;//設置RB1 SDA為輸出
        SDA=1;
        asm("NOP");
        SCL=1;
        asm("NOP");
        SDA=0;
        asm("NOP");
        SCL=0;
        asm("NOP");
        }

        void stop()

        {
        TRISB = 0B00000001;//設置RB1 SDA為輸出
        SDA=0;
        asm("NOP");
        SCL=1;
        asm("NOP");
        SDA=1;
        asm("NOP");
        }

        void writex(uchar j)

        {
        uchar i,temp;
        TRISB = 0B00000001;//設置RB1 SDA為輸出
        temp=j;

        for (i=0;i<8;i++)
        {

        SCL=0;
        asm("NOP");

        if (temp & 0x80) //讀取
        {
        SDA=1;
        }
        else
        {
        SDA=0;
        }
        temp=temp<<1;
        //SDA=CY;


        asm("NOP");
        SCL=1;
        asm("NOP");
        }

        SCL=0;
        asm("NOP");
        SDA=1;
        asm("NOP");

        }

        uchar readx()
        {
        uchar i,j,k=0;
        TRISB = 0B00000001;//設置RB1 SDA為輸出

        SCL=0;
        asm("NOP");

        SDA=1;

        TRISB = 0B00000011;//設置RB1 SDA為輸入
        for (i=0;i<8;i++)
        {
        asm("NOP");

        SCL=1;
        asm("NOP");

        if (SDA==1) j=1;
        else j=0;
        k=(k<<1)|j;
        SCL=0;

        }
        TRISB = 0B00000001;//設置RB1 SDA為輸出

        asm("NOP");
        return(k);

        }

        void clock()

        {
        uchar i=0;
        TRISB = 0B00000011;//設置RB1 SDA為輸入
        SCL=1;

        asm("NOP");
        while ((SDA==1)&&(i<255))i++;
        SCL=0;
        asm("NOP");
        TRISB = 0B00000001;//設置RB1 SDA為輸出

        }

        uchar WIN24C64_read(uint address)

        {
        uchar i;
        start();
        writex(0xa0);
        clock();
        writex(address/256);
        clock();
        writex(address%256);
        clock();
        start();
        writex(0xa1);
        clock();
        i=readx();
        stop();
        delay1(10);
        return(i);

        }

        void WIN24C64_write(uint address,uchar info)

        {

        start();
        writex(0xa0);
        clock();
        writex(address/256);
        clock();
        delay1(10);
        writex(address%256);
        clock();
        writex(info);
        clock();
        stop();
        delay1(200);

        }

        #endif



        關鍵詞: PIC16F72AT24C64讀

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 上林县| 荆州市| 屏边| 祥云县| 西贡区| 常宁市| 平乡县| 横峰县| 汨罗市| 文山县| 精河县| 新河县| 乐平市| 武宁县| 三明市| 定远县| 沽源县| 南投县| 东乡族自治县| 临泉县| 庐江县| 定州市| 桐梓县| 石城县| 板桥市| 通山县| 海林市| 洛隆县| 丰城市| 万山特区| 辰溪县| 耿马| 临安市| 筠连县| 墨江| 沽源县| 绥滨县| 桐柏县| 湘阴县| 龙陵县| 威信县|