⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2cled.c

📁 dspic6012A读写24LC02B的例程(用c语言编写)
💻 C
字号:
/************************************************
** 功能描述:I2C应用,24lc02B读写程序**
**该c程序已经调通2008、9、7日晚
************************************************/
#include "p30f6012A.h"
int failmemory[40];
unsigned int WriteTable[8]={0x11,0x23,0x22,0x13,0x34,0x56,0x77,0x89};
unsigned int ReadTable[8];
unsigned int ControlByteW=0xa0; //控制字:写准备(器件地址000)
unsigned int ControlByteR=0xa1; //控制字:读准备(器件地址000)
unsigned int AddressH=0x00;  //数据地址高7位
;unsigned int AddressL=0x00;  //数据地址低8位
unsigned char table[10]={0xc0,0xf9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned long int i = 0;
unsigned long int j=0;

void initial()
{
	INTCON1 = 0x0000;	
    INTCON2 = 0x0000;		//关闭所有中断
    I2CCONbits.I2CEN = 1; 	//启动I2C模块
    I2CBRG = 0x013;         //I2C波特率
   
  
	TRISD=0xff00;
	LATD-0xFFFF;
}   

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<8;i++)
	{
		I2CTRN = WriteTable[i];
        while(I2CSTATbits.TRSTAT==1){}   //等待发送完成
    }
    I2CCONbits.PEN = 1;          //发送停止位
    while(I2CCONbits.PEN==1){}   //等待停止操作完成    
}

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<8;i++)
	{ 
        I2CCONbits.RCEN = 1;         //接收数据使能   
        while(I2CSTATbits.RBF==0){}
        ReadTable[i] = I2CRCV;
		LATD=I2CRCV;
		for ( j=0;j<6500;j++){}     //页写操作后的等待
		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()
{

    	initial();					//初始化
    	pagewrite();
    	for (i=0;i<650000;i++){}     //页写操作后的等待
    	pageread(); 
		asm("nop");
		while(1){};
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -