找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 815|回復: 0
打印 上一主題 下一主題
收起左側(cè)

PMS154C 24C02 DOME

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:1135245 發(fā)表于 2024-11-3 13:52 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  1. //********************************************************************************//
  2. //                功能名稱:        24C02模塊(EEPROM)
  3. //                功能編號:        180201
  4. //                適應芯片:        適用于所有芯片
  5. //--------------------------------------------------------------------------------//
  6. //程序說明:
  7. //        封裝引腳圖如下
  8. //           ┌─┐
  9. //   A0┤  ├VCC
  10. //         A1┤  ├WP
  11. //         A2┤  ├SCL
  12. //        GND┤  ├SDA
  13. //           └─┘
  14. //        引腳A0~A2地址輸入引腳,案例全接GND
  15. //        WP引腳寫保護,接GND進行正常讀寫,接VCC只讀模式,案例接GND
  16. //        案例為寫入一個地址0數(shù)據(jù)49,然后再讀出該地址的數(shù)據(jù)
  17. //        地址和數(shù)據(jù)可以隨意改動,數(shù)據(jù)的范圍為0~255,
  18. //        超過的范圍請參考連寫或連讀
  19. //注意事項:
  20. //        1.案例為PMS154C,其他芯片原理都相同
  21. //        2.連讀或連寫請參考對應的程序
  22. //********************************************************************************//
  23. #include        "extern.h"

  24. #define _5us    5*2
  25. bit     SCL     :   PA.3;//用于產(chǎn)生器件所有數(shù)據(jù)發(fā)送或接收的時鐘
  26. bit     SDA     :   PA.4;//用于器件所有數(shù)據(jù)的發(fā)送或接收

  27. byte        Address;
  28. byte        data;
  29. byte        Read_Data;
  30. byte        Write_Data;

  31. void    init(void)//初始化函數(shù)
  32. {
  33.     SCL = 1;
  34.     .delay _5us;
  35.     SDA = 1;//控制信號拉高
  36.     .delay _5us;
  37. }

  38. void    start(void)//啟動信號子函數(shù)//向ROM發(fā)送一個開始的信號
  39. {
  40.     SDA = 1;
  41.     .delay _5us;
  42.     SCL = 1;
  43.     .delay _5us;
  44.     SDA = 0;
  45.     .delay _5us;
  46. }

  47. void    stop(void)//停止信號子函數(shù)//向ROM發(fā)送一個停止的信號
  48. {
  49.     SDA = 0;
  50.     .delay _5us;
  51.     SCl = 1;
  52.     .delay _5us;
  53.     SDA = 1;
  54.     .delay _5us;
  55. }

  56. void    respons(void)//應答信號子函數(shù)//接收一信號后應答
  57. {
  58.     $ SDA in,pull;
  59.     .delay 100;
  60.     SCL = 1;
  61.     .delay _5us;
  62.     while(SDA==1)
  63.         nop;
  64.     SCL = 0;
  65.     .delay _5us;
  66.     $ SDA out,high;
  67.     .delay 100;
  68. }

  69. void    write_byte(void)//寫入一個字節(jié)數(shù)據(jù)//向E2PROM寫數(shù)據(jù)
  70. {
  71.     byte temp = 0,i = 8;
  72.     while(i--)
  73.     {
  74.         SCL = 0;//數(shù)據(jù)可以改變
  75.         .delay _5us;
  76.         temp = data & 0x80;//保留最高位
  77.         if(temp)//寫一位
  78.             SDA = 1;
  79.         else
  80.             SDA = 0;
  81.         .delay _5us;
  82.         data <<= 1;//要存的數(shù)據(jù)左移一位
  83.         SCL = 1;//數(shù)據(jù)穩(wěn)定不能寫了
  84.         .delay _5us;
  85.     }
  86.     SCL = 0;
  87.     .delay _5us;
  88.     SDA = 1;//穩(wěn)定數(shù)據(jù)
  89.     .delay _5us;
  90. }

  91. void    read_byte(void)//讀回一個字節(jié)
  92. {
  93.     byte  i = 8;
  94.     SCL = 0;//數(shù)據(jù)改變
  95.     .delay _5us;
  96.     SDA = 1;//可讀信號
  97.     .delay _5us;
  98.     $ SDA in,pull;
  99.     .delay 100;
  100.     Read_Data = 0;
  101.     while(i--)
  102.     {
  103.         Read_Data = Read_Data << 1;//寫入時從高位寫,讀出時也一樣
  104.         SCL = 1;//數(shù)據(jù)穩(wěn)定,可從SDA讀出一位
  105.         .delay _5us;
  106.         if(SDA)
  107.             Read_Data = Read_Data | 0x01;
  108.         else
  109.             Read_Data = Read_Data & 0xfe;
  110.         SCL = 0;//數(shù)據(jù)改變準備下一位
  111.         .delay _5us;
  112.     }
  113.     $ SDA out,high;
  114.     .delay 100;
  115. }

  116. void    write_add(void)//片內(nèi)隨意地址寫任意數(shù)//根據(jù)起始地址、數(shù)據(jù)個數(shù)及數(shù)據(jù)寫入E2PROM中
  117. {                       //Write_Data   address變量
  118.     start();//起始

  119.     data = 0xa0;//E2PROM地址(ATC02為0xa0)//器件地址+寫控制位
  120.     write_byte();
  121.     respons();//應答

  122.     data = address;//寫入數(shù)據(jù)要存放的地址
  123.     write_byte();
  124.     respons();//應答

  125.     data = Write_Data;//寫入數(shù)據(jù)
  126.     write_byte();
  127.     respons();//應答

  128.     stop();//終止
  129. }

  130. void    read_add(void)//片內(nèi)隨意地址讀數(shù)據(jù)//根據(jù)起始地址和讀出個數(shù),從E2PROM中讀出一串數(shù)據(jù)
  131. {                   //address變量
  132.     start();//起始

  133.     data = 0xa0;//器件地址+寫控制位
  134.     write_byte();
  135.     respons();//應答

  136.     data = address;//數(shù)據(jù)地址
  137.     write_byte();
  138.     respons();//應答

  139.     start();//起始

  140.     data = 0xa1;//器件地址+讀控制位
  141.     write_byte();
  142.     respons();

  143.         read_byte();
  144.         .delay 1000;

  145.         stop();//停止
  146. }

  147. void        FPPA0 (void)
  148. {
  149.         .ADJUST_IC        SYSCLK=IHRC/8, IHRC=16MHz, VDD=5V;

  150.     $ SDA out,high;
  151.     $ SCL out,high;

  152.         Address = 0;
  153.     init();
  154.         .delay 100;
  155.         while (1)
  156.         {
  157.                 Address = 0;//地址
  158.                 Write_Data = 49;//數(shù)據(jù)
  159.                 write_add();//寫入一個數(shù)據(jù)
  160.                 .delay 5000*2;//延時一下
  161.                 Address = 0;
  162.                 read_add();//讀出寫入的數(shù)據(jù)
  163.                 nop;
  164.         }
  165. }

  166. void        Interrupt (void)
  167. {
  168.         pushaf;

  169.         if (Intrq.T16)
  170.         {        //        T16 Trig
  171.                 //        User can add code
  172.                 Intrq.T16        =        0;
  173.                 //...
  174.         }

  175.         popaf;
  176. }

復制代碼

原理圖: 無
仿真: 無
代碼: PMS154C 24C02.zip (11.35 KB, 下載次數(shù): 1)

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表