|
給大家分享一個(gè)IIC_EEPROM_24LC64存儲(chǔ)芯片與TMS320F2812 dsp讀寫操作的源碼
dsp源程序如下:
- /*********************************************************************
- ** 實(shí)驗(yàn)?zāi)康?學(xué)習(xí)IIC接口的EEPROM數(shù)據(jù)讀寫 **
- ** 實(shí)驗(yàn)說(shuō)明:EEPROM采用24LC64芯片,串行數(shù)據(jù)線和串行時(shí)鐘線分別由DSP的 **
- ** PA13和PA15控制 **
- ** 實(shí)驗(yàn)結(jié)果:按照我們下面所設(shè)置的斷點(diǎn)運(yùn)行可讀出我們寫入EEPROM里的值 ** **
- **********************************************************************/
- #include "DSP28_Device.h"
- #include "DSP28_Globalprototypes.h"
- #define SDA1 GpioDataRegs.GPADAT.bit.GPIOA13 = 1////串行數(shù)據(jù)輸出高
- #define SDA0 GpioDataRegs.GPADAT.bit.GPIOA13 = 0////串行數(shù)據(jù)輸出低
- #define SCL1 GpioDataRegs.GPADAT.bit.GPIOA15 = 1////串行時(shí)鐘輸出高
- #define SCL0 GpioDataRegs.GPADAT.bit.GPIOA15 = 0////串行時(shí)鐘輸出低
-
- #define SDAIN EALLOW;GpioMuxRegs.GPADIR.bit.GPIOA13 = 0; EDIS
- #define SDAOUT EALLOW;GpioMuxRegs.GPADIR.bit.GPIOA13 = 1;EDIS
- #define SDADATA GpioDataRegs.GPADAT.bit.GPIOA13
- //24C64 =8K Bytes =13bit Add
- //頁(yè)編程平均400us寫一字節(jié),隨機(jī)讀平均1.8ms讀一字節(jié),連續(xù)讀平均600us一字節(jié)
- #define uchar unsigned char /*宏定義*/
- #define uint unsigned int
- void delay(unsigned int t);
- unsigned char ByteRead(unsigned int add,unsigned char *temp);
- unsigned char ByteWrite(unsigned int add,unsigned char dat);
- void main(void)
- {
- unsigned char t,s,a;
- /*初始化系統(tǒng)*/
- InitSysCtrl();
- /*關(guān)中斷*/
- DINT;
- IER = 0x0000;
- IFR = 0x0000;
- /*初始化PIE*/
- InitPieCtrl();
- /*初始化PIE中斷矢量表*/
- InitPieVectTable();
-
-
- InitGpio();
- SDA1;
- SCL1;
- t=0;
- s=ByteWrite(0,0x12);
- delay(10000);
- s=ByteRead(0,&t);
- a=0;//設(shè)斷點(diǎn),看變量t的值
- s=ByteWrite(1,0x34);
- delay(10000);
- s=ByteRead(1,&t);
- a=1; //設(shè)斷點(diǎn)
- s=ByteWrite(2,0x56);
- delay(10000);
- s=ByteRead(2,&t);
- a=2; //設(shè)斷點(diǎn)
- s=ByteWrite(3,0x78);
- delay(10000);
- s=ByteRead(3,&t);
- a=3; //設(shè)斷點(diǎn)
- for(;;);
- }
- void delay(unsigned int t)
- {
- while(t>0) t--;
-
- }
- //總線無(wú)效期間二線全為主高,總線有效期間時(shí)鐘線為低,一個(gè)高低跳變?yōu)橐粋€(gè)時(shí)鐘
- //周期,時(shí)鐘線為低時(shí)才能改變數(shù)據(jù)線狀態(tài),若時(shí)鐘線為高時(shí)改變數(shù)據(jù)線狀態(tài),則只
- //能是開始或結(jié)束總線操作
- unsigned char ack; /*應(yīng)答標(biāo)志位*/
-
- /*******************************************************************
- 起動(dòng)總線函數(shù)
- 時(shí)鐘線在高電平時(shí),數(shù)據(jù)線從高向低的跳變開始總線,最終二線全變低
- 函數(shù)原型: void Start_I2c();
- 功能: 啟動(dòng)I2C總線,即發(fā)送I2C起始條件.
- ********************************************************************/
- void Start_I2c()
- {
- SDA1; /*先跳高,為稍后的從高向低跳作準(zhǔn)備*/
- delay(60);
- SCL1;
- delay(500); /*起始條件建立時(shí)間大于4.7us,延時(shí)*/
- SDA0; /*發(fā)送起始信號(hào)*/
- delay(60); /* 起始條件鎖定時(shí)間大于4μs*/
- SCL0; /*鉗住I2C總線,準(zhǔn)備發(fā)送或接收數(shù)據(jù) */
- delay(60);
- }
- /*******************************************************************
- 結(jié)束總線函數(shù)
- 時(shí)鐘線在低電平時(shí),數(shù)據(jù)線從低向高的跳變結(jié)束總線,最終二線都變高
- 函數(shù)原型: void Stop_I2c();
- 功能: 結(jié)束I2C總線,即發(fā)送I2C結(jié)束條件.
- ********************************************************************/
- void Stop_I2c()
- {
- SDA0; /*先跳低,為稍后的從低向高跳變作準(zhǔn)備*/
- delay(60);
- SCL1; /*發(fā)送結(jié)束條件的時(shí)鐘信號(hào),結(jié)束條件建立時(shí)間大于4μs*/
- delay(60);
- SDA1; /*發(fā)送I2C總線結(jié)束信號(hào)*/
- delay(60);
- }
- /*******************************************************************
- 字節(jié)數(shù)據(jù)發(fā)送函數(shù)
- 函數(shù)原型: void SendByte(uchar c);
- 功能: 將數(shù)據(jù)c發(fā)送出去,可以是地址,也可以是數(shù)據(jù),發(fā)完后等待應(yīng)答,并對(duì)
- 此狀態(tài)位進(jìn)行操作.(不應(yīng)答或非應(yīng)答都使ack=0)
- 發(fā)送數(shù)據(jù)正常,ack=1; ack=0表示被控器無(wú)應(yīng)答或損壞。
- ********************************************************************/
- unsigned char SendByte(uchar c)
- {
- uchar BitCnt,t[8];
- for(BitCnt=0;BitCnt<8;BitCnt++) /*要傳送的數(shù)據(jù)長(zhǎng)度為8位*/
- {
- if((c<<BitCnt)&0x80)
- t[BitCnt]=1; /*判斷發(fā)送位*/
- else
- t[BitCnt]=0;
- }
-
- SCL0;
- delay(4);
- for(BitCnt=0;BitCnt<8;BitCnt++) /*要傳送的數(shù)據(jù)長(zhǎng)度為8位*/
- { SCL0;
- if(t[BitCnt])SDA1; /*判斷發(fā)送位*/
- else SDA0;
- delay(60);
- SCL1; /*置時(shí)鐘線為高,通知被控器開始接收數(shù)據(jù)位*/
- delay(60); /*保證時(shí)鐘高電平周期大于4μs*/
-
- // if((BitCnt==7) && t[BitCnt])
- // SDAIN; //設(shè)為輸入
- }
-
- SCL0;
- SDAIN; //設(shè)為輸入
- delay(60);
- SCL1;
- if(SDADATA)ack=1; //沒有收到應(yīng)答信號(hào)
- else ack=0; //收到應(yīng)答信號(hào)
- delay(60);
- SCL0;
- delay(60);
- SDAOUT; //重新設(shè)為輸出
-
-
- return ack;
- }
- /*******************************************************************
- 字節(jié)數(shù)據(jù)接收函數(shù)
- 函數(shù)原型: uchar RcvByte();
- 功能: 用來(lái)接收從器件傳來(lái)的數(shù)據(jù),并判斷總線錯(cuò)誤(不發(fā)應(yīng)答信號(hào)),
- 發(fā)完后請(qǐng)用應(yīng)答函數(shù)應(yīng)答從機(jī)。
- ********************************************************************/
- uchar RcvByte()
- {
- uchar retc;
- uchar BitCnt;
-
- retc=0;
- SCL0; //先將時(shí)鐘線拉低
- SDAIN; /*置數(shù)據(jù)線為輸入方式*/
- for(BitCnt=0;BitCnt<8;BitCnt++)
- {
- SCL0; /*置時(shí)鐘線為低,準(zhǔn)備接收數(shù)據(jù)位*/
- delay(60); /*時(shí)鐘低電平周期大于4.7μs*/
- SCL1; /*置時(shí)鐘線為高使數(shù)據(jù)線上數(shù)據(jù)有效*/
- retc=retc<<1;
- if(SDADATA)
- retc=retc+1; /*讀數(shù)據(jù)位,接收的數(shù)據(jù)位放入retc中 */
- delay(60);
- }
- SCL0; //從器件數(shù)據(jù)輸了階段在這一位后沒有響應(yīng),數(shù)據(jù)線補(bǔ)外部上拉電阻臨時(shí)拉為高
-
- delay(60);
- SDAOUT;
- return(retc);
-
- }
- /*******************************************************************
- 接口函數(shù)
- *******************************************************************/
- unsigned char ByteRead(unsigned int add,unsigned char *temp)
- {
- unsigned char rt;
- rt=0;
- Start_I2c(); /*啟動(dòng)總線*/
- if(SendByte(0xa0)) /*發(fā)送寫命令*/
- rt=1;
- if(SendByte(add>>8)) //發(fā)送地址
- rt= 2;
- if(SendByte(add)) //發(fā)送地址
- rt= 3;
- Start_I2c(); /*啟動(dòng)總線*/
- if(SendByte(0xa1)) //發(fā)送讀命令
- rt= 4;
-
- *temp=RcvByte();
-
- Stop_I2c(); /*結(jié)束總線*/
-
- return rt;
- }
- unsigned char ByteWrite(unsigned int add,unsigned char dat)
- {
- unsigned char rt;
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
IIC_EEPROM_24LC64芯片與TMS320F2812完成讀寫操作.rar
(250.44 KB, 下載次數(shù): 63)
2018-7-12 12:08 上傳
點(diǎn)擊文件名下載附件
|
|