找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

帖子
查看: 7622|回復(fù): 1
收起左側(cè)

IIC_EEPROM_24LC64芯片與TMS320F2812完成讀寫操作

[復(fù)制鏈接]
ID:162777 發(fā)表于 2018-7-12 12:09 | 顯示全部樓層 |閱讀模式
給大家分享一個(gè)IIC_EEPROM_24LC64存儲(chǔ)芯片與TMS320F2812 dsp讀寫操作的源碼
0.png

dsp源程序如下:
  1. /*********************************************************************
  2. **        實(shí)驗(yàn)?zāi)康?學(xué)習(xí)IIC接口的EEPROM數(shù)據(jù)讀寫                            **
  3. **        實(shí)驗(yàn)說(shuō)明:EEPROM采用24LC64芯片,串行數(shù)據(jù)線和串行時(shí)鐘線分別由DSP的 **
  4. **           PA13和PA15控制                                         **
  5. **        實(shí)驗(yàn)結(jié)果:按照我們下面所設(shè)置的斷點(diǎn)運(yùn)行可讀出我們寫入EEPROM里的值 **                                   **
  6. **********************************************************************/


  7. #include "DSP28_Device.h"
  8. #include "DSP28_Globalprototypes.h"


  9. #define          SDA1            GpioDataRegs.GPADAT.bit.GPIOA13 = 1////串行數(shù)據(jù)輸出高
  10. #define          SDA0      GpioDataRegs.GPADAT.bit.GPIOA13 = 0////串行數(shù)據(jù)輸出低
  11. #define          SCL1            GpioDataRegs.GPADAT.bit.GPIOA15 = 1////串行時(shí)鐘輸出高
  12. #define          SCL0      GpioDataRegs.GPADAT.bit.GPIOA15 = 0////串行時(shí)鐘輸出低

  13.      
  14. #define          SDAIN          EALLOW;GpioMuxRegs.GPADIR.bit.GPIOA13  = 0; EDIS
  15. #define          SDAOUT    EALLOW;GpioMuxRegs.GPADIR.bit.GPIOA13  = 1;EDIS

  16. #define          SDADATA          GpioDataRegs.GPADAT.bit.GPIOA13

  17. //24C64  =8K Bytes =13bit Add
  18. //頁(yè)編程平均400us寫一字節(jié),隨機(jī)讀平均1.8ms讀一字節(jié),連續(xù)讀平均600us一字節(jié)
  19. #define  uchar unsigned char         /*宏定義*/
  20. #define  uint  unsigned int


  21. void delay(unsigned int t);
  22. unsigned char ByteRead(unsigned int add,unsigned char *temp);
  23. unsigned char ByteWrite(unsigned int add,unsigned char dat);

  24. void main(void)
  25. {   
  26.     unsigned char t,s,a;

  27.         /*初始化系統(tǒng)*/
  28.         InitSysCtrl();

  29.         /*關(guān)中斷*/
  30.         DINT;
  31.         IER = 0x0000;
  32.         IFR = 0x0000;
  33.         /*初始化PIE*/
  34.         InitPieCtrl();

  35.         /*初始化PIE中斷矢量表*/
  36.         InitPieVectTable();        
  37.         

  38.    
  39.         InitGpio();
  40.     SDA1;
  41.         SCL1;

  42.     t=0;

  43.     s=ByteWrite(0,0x12);
  44.     delay(10000);
  45.     s=ByteRead(0,&t);
  46.     a=0;//設(shè)斷點(diǎn),看變量t的值

  47.     s=ByteWrite(1,0x34);
  48.     delay(10000);
  49.     s=ByteRead(1,&t);
  50.         a=1; //設(shè)斷點(diǎn)

  51.         s=ByteWrite(2,0x56);
  52.         delay(10000);
  53.     s=ByteRead(2,&t);
  54.         a=2; //設(shè)斷點(diǎn)

  55.         s=ByteWrite(3,0x78);
  56.         delay(10000);
  57.     s=ByteRead(3,&t);
  58.         a=3; //設(shè)斷點(diǎn)


  59.         for(;;);

  60. }         

  61. void delay(unsigned int t)
  62. {
  63.   while(t>0) t--;
  64.   
  65. }



  66. //總線無(wú)效期間二線全為主高,總線有效期間時(shí)鐘線為低,一個(gè)高低跳變?yōu)橐粋€(gè)時(shí)鐘
  67. //周期,時(shí)鐘線為低時(shí)才能改變數(shù)據(jù)線狀態(tài),若時(shí)鐘線為高時(shí)改變數(shù)據(jù)線狀態(tài),則只
  68. //能是開始或結(jié)束總線操作

  69. unsigned char ack;                         /*應(yīng)答標(biāo)志位*/
  70.    

  71. /*******************************************************************
  72.                      起動(dòng)總線函數(shù)   
  73. 時(shí)鐘線在高電平時(shí),數(shù)據(jù)線從高向低的跳變開始總線,最終二線全變低            
  74. 函數(shù)原型: void  Start_I2c();  
  75. 功能:     啟動(dòng)I2C總線,即發(fā)送I2C起始條件.  
  76. ********************************************************************/
  77. void Start_I2c()
  78. {
  79.   SDA1;                   /*先跳高,為稍后的從高向低跳作準(zhǔn)備*/
  80.   delay(60);
  81.   SCL1;
  82.   delay(500);   /*起始條件建立時(shí)間大于4.7us,延時(shí)*/
  83.   SDA0;                   /*發(fā)送起始信號(hào)*/
  84.   delay(60);    /* 起始條件鎖定時(shí)間大于4μs*/
  85.   SCL0;                   /*鉗住I2C總線,準(zhǔn)備發(fā)送或接收數(shù)據(jù) */
  86.   delay(60);
  87. }


  88. /*******************************************************************
  89.                       結(jié)束總線函數(shù)   
  90. 時(shí)鐘線在低電平時(shí),數(shù)據(jù)線從低向高的跳變結(jié)束總線,最終二線都變高            
  91. 函數(shù)原型: void  Stop_I2c();  
  92. 功能:     結(jié)束I2C總線,即發(fā)送I2C結(jié)束條件.  
  93. ********************************************************************/
  94. void Stop_I2c()
  95. {
  96.   SDA0;                  /*先跳低,為稍后的從低向高跳變作準(zhǔn)備*/
  97.   delay(60);
  98.   SCL1;                  /*發(fā)送結(jié)束條件的時(shí)鐘信號(hào),結(jié)束條件建立時(shí)間大于4μs*/
  99.   delay(60);
  100.   SDA1;                  /*發(fā)送I2C總線結(jié)束信號(hào)*/
  101.   delay(60);
  102. }



  103. /*******************************************************************
  104.                  字節(jié)數(shù)據(jù)發(fā)送函數(shù)               
  105. 函數(shù)原型: void  SendByte(uchar c);
  106. 功能:     將數(shù)據(jù)c發(fā)送出去,可以是地址,也可以是數(shù)據(jù),發(fā)完后等待應(yīng)答,并對(duì)
  107.           此狀態(tài)位進(jìn)行操作.(不應(yīng)答或非應(yīng)答都使ack=0)     
  108.                發(fā)送數(shù)據(jù)正常,ack=1; ack=0表示被控器無(wú)應(yīng)答或損壞。
  109. ********************************************************************/
  110. unsigned char   SendByte(uchar c)
  111. {
  112. uchar BitCnt,t[8];
  113. for(BitCnt=0;BitCnt<8;BitCnt++)          /*要傳送的數(shù)據(jù)長(zhǎng)度為8位*/
  114.     {
  115.      if((c<<BitCnt)&0x80)
  116.         t[BitCnt]=1;                                   /*判斷發(fā)送位*/
  117.      else  
  118.        t[BitCnt]=0;  
  119.         }
  120.          
  121.          SCL0;
  122.          delay(4);

  123. for(BitCnt=0;BitCnt<8;BitCnt++)          /*要傳送的數(shù)據(jù)長(zhǎng)度為8位*/
  124.     { SCL0;
  125.      if(t[BitCnt])SDA1;                           /*判斷發(fā)送位*/
  126.        else  SDA0;               
  127.      delay(60);
  128.      SCL1;                               /*置時(shí)鐘線為高,通知被控器開始接收數(shù)據(jù)位*/
  129.      delay(60);                     /*保證時(shí)鐘高電平周期大于4μs*/
  130.       
  131.      // if((BitCnt==7) && t[BitCnt])
  132.         //        SDAIN;  //設(shè)為輸入                       
  133.     }         
  134.          
  135.     SCL0;
  136.         SDAIN;  //設(shè)為輸入
  137.     delay(60);           
  138.      SCL1;

  139.     if(SDADATA)ack=1;                    //沒有收到應(yīng)答信號(hào)
  140.        else ack=0;                        //收到應(yīng)答信號(hào)


  141.         delay(60);
  142.     SCL0;
  143.     delay(60);         
  144.         SDAOUT;                                         //重新設(shè)為輸出
  145.          
  146.         
  147.         return ack;
  148. }




  149. /*******************************************************************
  150.                  字節(jié)數(shù)據(jù)接收函數(shù)               
  151. 函數(shù)原型: uchar  RcvByte();
  152. 功能:            用來(lái)接收從器件傳來(lái)的數(shù)據(jù),并判斷總線錯(cuò)誤(不發(fā)應(yīng)答信號(hào)),
  153.           發(fā)完后請(qǐng)用應(yīng)答函數(shù)應(yīng)答從機(jī)。  
  154. ********************************************************************/        
  155. uchar  RcvByte()
  156. {
  157.   uchar retc;
  158.   uchar BitCnt;
  159.   
  160.   retc=0;
  161.   SCL0;                               //先將時(shí)鐘線拉低

  162.   SDAIN;                                     /*置數(shù)據(jù)線為輸入方式*/
  163.   for(BitCnt=0;BitCnt<8;BitCnt++)
  164.       {      
  165.         SCL0;                          /*置時(shí)鐘線為低,準(zhǔn)備接收數(shù)據(jù)位*/
  166.         delay(60);                     /*時(shí)鐘低電平周期大于4.7μs*/      
  167.         SCL1;                          /*置時(shí)鐘線為高使數(shù)據(jù)線上數(shù)據(jù)有效*/
  168.         retc=retc<<1;
  169.         if(SDADATA)
  170.                    retc=retc+1;                  /*讀數(shù)據(jù)位,接收的數(shù)據(jù)位放入retc中 */
  171.         delay(60);
  172.       }
  173.   SCL0; //從器件數(shù)據(jù)輸了階段在這一位后沒有響應(yīng),數(shù)據(jù)線補(bǔ)外部上拉電阻臨時(shí)拉為高
  174.   
  175.   delay(60);
  176.   SDAOUT;
  177.   return(retc);
  178.   
  179. }



  180. /*******************************************************************
  181.                  接口函數(shù)                                   
  182. *******************************************************************/


  183. unsigned char ByteRead(unsigned int add,unsigned char *temp)
  184. {
  185.         unsigned char rt;
  186.           rt=0;
  187.           Start_I2c();                       /*啟動(dòng)總線*/
  188.           if(SendByte(0xa0))                 /*發(fā)送寫命令*/
  189.             rt=1;
  190.           if(SendByte(add>>8))              //發(fā)送地址
  191.             rt= 2;
  192.           if(SendByte(add))                     //發(fā)送地址
  193.             rt= 3;
  194.           Start_I2c();                       /*啟動(dòng)總線*/
  195.           if(SendByte(0xa1))                   //發(fā)送讀命令
  196.                    rt= 4;
  197.    
  198.           *temp=RcvByte();
  199.         
  200.           Stop_I2c();                        /*結(jié)束總線*/
  201.   
  202.           return rt;
  203. }

  204. unsigned char ByteWrite(unsigned int add,unsigned char dat)
  205. {

  206.         unsigned char rt;
  207. ……………………

  208. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:

IIC_EEPROM_24LC64芯片與TMS320F2812完成讀寫操作.rar (250.44 KB, 下載次數(shù): 63)


回復(fù)

使用道具 舉報(bào)

ID:480811 發(fā)表于 2021-4-21 23:19 | 顯示全部樓層
正好有個(gè)項(xiàng)目要用這塊,謝謝了
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表