?? i2ceeprom.c
字號:
/********************************************************
** 文件名 :I2CEEPROM.c **
** 功能描述: I2C應用,24LC256頁讀寫程序,頁長度為16字節**
********************************************************/
#include "p30f6014.h"
int failmemory[40];
unsigned int WriteTable[16]={0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,
0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
unsigned int ReadTable[16];
unsigned int ControlByteW=0xa0; //控制字:寫準備(器件地址000)
unsigned int ControlByteR=0xa1; //控制字:讀準備(器件地址000)
unsigned int AddressH=0x00; //數據地址高7位
unsigned int AddressL=0x00; //數據地址低8位
unsigned long int i = 0;
//I2CEEPROM配置子程序
void ConfigI2CEEPROM()
{
SRbits.IPL = 7; //關閉所有中斷
TRISG = TRISG&0xfeff; //RG8引腳為輸出
I2CCONbits.I2CEN = 1; //啟動I2C模塊
I2CBRG = 0x064; //I2C波特率
}
//向24LC256中寫入16個字節的數據
void PageWrite()
{
I2CCONbits.SEN = 1; //發送啟動位
while(I2CCONbits.SEN == 1); //等待啟動操作完成
I2CTRN = ControlByteW; //發送控制字:寫準備
while(I2CSTATbits.TRSTAT == 1); //等待發送完成
I2CTRN = AddressH;
while(I2CSTATbits.TRSTAT == 1); //等待發送完成
I2CTRN = AddressL;
while(I2CSTATbits.TRSTAT == 1); //等待發送完成
for(i=0; i<16; i++)
{
I2CTRN = WriteTable[i];
while(I2CSTATbits.TRSTAT == 1); //等待發送完成
}
I2CCONbits.PEN = 1; //發送停止位
while(I2CCONbits.PEN == 1); //等待停止操作完成
}
//從24LC256中讀出16個字節的數據
void PageRead()
{
I2CCONbits.SEN = 1; //發送啟動位
while(I2CCONbits.SEN == 1); //等待啟動操作完成
I2CTRN = ControlByteW; //發送控制字:寫準備
while(I2CSTATbits.TRSTAT == 1); //等待發送完成
I2CTRN = AddressH;
while(I2CSTATbits.TRSTAT == 1); //等待發送完成
I2CTRN = AddressL;
while(I2CSTATbits.TRSTAT == 1); //等待發送完成
I2CCONbits.RSEN = 1; //發送重啟動位
while(I2CCONbits.RSEN == 1); //等待重啟動操作完成
I2CTRN = ControlByteR; //發送控制字:讀準備
while(I2CSTATbits.TRSTAT == 1); //等待發送完成
for(i=0; i<16; i++)
{
I2CCONbits.RCEN = 1; //接收數據使能
while(I2CSTATbits.RBF == 0);
ReadTable[i] = I2CRCV;
I2CCONbits.ACKDT = 0;
if (i==15)
I2CCONbits.ACKDT = 1; //最后一字節則不發ACK
I2CCONbits.ACKEN = 1;
while(I2CCONbits.ACKEN == 1);
}
I2CCONbits.PEN = 1; //發送停止位
while(I2CCONbits.PEN == 1); //等待停止操作完成
}
int main()
{
ConfigI2CEEPROM();
PORTGbits.RG8 = 0;
PageWrite();
for(i=0; i<650000; i++); //頁寫操作后的等待
PageRead();
while(1);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -