Share DSP281x read and write EEPROM 24C02 routines
[Copy link]
The microcontroller source program is as follows:
/**********************************************************************/
/*Copyright (C), 2008-2009, LiTian Tech.Co.Ltd. */
/* Module Name : GPIO */
/* File Name : main.c */
/* Author : Hou Changbo */
/* Version : 2.0 */
/* Function : Read and write EEPROM 24C02 */
/* Description : */
/**********************************************************************/
/*****************头文件********************/
#include "DSP281x_Device.h"
#include "System.h"
/**************** port definition****************/
#define SCL_OUT GpioDataRegs.GPDDAT.bit.GPIOD0
#define SCL_DIR GpioMuxRegs.GPDDIR.bit.GPIOD0
#define SDA_DAT GpioDataRegs.GPDDAT.bit.GPIOD1
#define SDA_DIR GpioMuxRegs.GPDDIR.bit.GPIOD1
/****************Constant macro definition*****************/
#define TRUE 1
#define FALSE 0
/***************Global variable definition****************/
unsigned char w_buffer[8]={23,18,18,20,32,25,28,29};
unsigned char r_buffer[8]={0};
/****************函数声明*******************/
void Init_24C02(void);
void Set_SDA_In(void);
void Set_SDA_Out(void);
void Start_IIC(void);
void Stop_IIC(void);
void Master_ACK(void);
void Master_NO_ACK(void);
void Write_Byte(unsigned char content);
unsigned char Read_Byte(void);
unsigned char EEPROM_Check(void);
void EEPROM_Write_Byte(unsigned char Adr,unsigned char Data);
unsigned char EEPROM_Read_Byte(unsigned char Adr);
void EEPROM_Write_Page(unsigned char Adr,unsigned char length,unsigned char *Data);
void EEPROM_Read_Page(unsigned char Adr,unsigned length,unsigned char *buffer);
unsigned char EEPROM_Current_Addr_Read(void);
void EEPROM_Sequential_Read(unsigned length,unsigned char *buffer);
/*------------------------------------------*/
/*Formal parameter: void */
/*Return value: void */ /
*Function description: main function */
/*------------------------------------------*/
void main(void)
{
unsigned char i,temp=0,w_data=17,err=0,r_data;
unsigned char * pointer;
InitSysCtrl(); // System initialization subroutine, in DSP28_sysctrl.c
Init_24C02();
for(i=0;i<8;i++)
r_buffer[i]=0;
for(i=0;i<5;i++)
{
EEPROM_Write_Byte(temp++,w_data++);
}
temp=0;
w_data=17;
for(i=0;i<5;i++)
{
r_data=EEPROM_Read_Byte(temp++);
if(r_data!=w_data)
err=err+1;
w_data++;
}
EEPROM_Write_Page(0x28,8,w_buffer);
EEPROM_Read_Page(0x28,8,r_buffer);
while(1);
}
/*------------------------------------------*/
/*Formal parameter: void */
/*Return value: void */
/*Function description: Initialize 24C02 */
/*------------------------------------------*/
void Init_24C02(void)
{
EALLOW;
SCL_DIR=1;
SDA_DIR=1;
EDIS;
}
/*------------------------------------------*/
/*Formal parameter: void */
/*Return value: void */ /
*Function description: Set SDA pin input */
/*------------------------------------------*/
void Set_SDA_In(void)
{
EALLOW;
SDA_DIR=0;
EDIS;
}
/*------------------------------------------*/
/*Formal parameter: void */
/*Return value: void */ /
*Function description: Set SDA pin output */
/*------------------------------------------*/
void Set_SDA_Out(void)
{
EALLOW;
SDA_DIR=1;
EDIS;
}
/*------------------------------------------*/
/*Formal parameter: void */
/*Return value: void */ /
*Function description: Complete the start condition operation of IIC */
/*------------------------------------------*/
void Start_IIC(void)
{
unsigned char i;
SCL_OUT=1;
SDA_DAT=1;
for(i=0;i<30;i++);
SDA_DAT=0;
for(i=0;i<30;i++);
SCL_OUT=0;
for(i=0;i<30;i++);
}
/*------------------------------------------*/
/*Formal parameter: void */
/*Return value: void */ /
*Function description: Complete the termination condition operation of IIC */
/*------------------------------------------*/
void Stop_IIC(void)
{
unsigned char i;
SDA_DAT=0;
for(i=0;i<30;i++);
SCL_OUT=1;
for(i=0;i<30;i++);
SDA_DAT=1;
for(i=0;i<30;i++);
}
/*------------------------------------------*/
/*Formal parameter: void */
/*Return value: void */ /
*Function description: Complete the host response operation of IIC */
/*------------------------------------------*/
void Master_ACK(void)
{
unsigned char i;
SDA_DAT=0;
for(i=0;i<30;i++);
SCL_OUT=1;
for(i=0;i<30;i++);
SCL_OUT=0;
for(i=0;i<10;i++);
SDA_DAT=1;
for(i=0;i<30;i++);
}
/*------------------------------------------*/
/*Formal parameter: void */
/*Return value: void */ /
*Function description: Complete the IIC host no response operation */
/*------------------------------------------*/
void Master_NO_ACK(void)
{
unsigned char i;
SDA_DAT=1;
for(i=0;i<30;i++);
SCL_OUT=1;
for(i=0;i<30;i++);
SCL_OUT=0;
for(i=0;i<30;i++);
SDA_DAT=0;
for(i=0;i<30;i++);
}
/*------------------------------------------*/
/*Formal parameter: void */
/*Return value: whether the slave has a response: 1--yes, 0--no */
/*Function description: Check the response operation of the slave */
/*------------------------------------------*/
unsigned char EEPROM_Check(void)
{
unsigned char i,Slave_ACK;
SDA_DAT=1;
for(i=0;i<30;i++);
SCL_OUT=1;
for(i=0;i<30;i++);
Set_SDA_In();
for(i=0;i<30;i++);
Slave_ACK = SDA_DAT; //Read SDA value
SCL_OUT=0;
for(i=0;i<30;i++);
Set_SDA_Out();
if(Slave_ACK==0)
return TRUE;
else
return FALSE;
}
/*------------------------------------------*/
/*Formal parameter: content--sent data */
/*Return value: void */
/*Function description: Send one byte of data to the IIC bus */
/*------------------------------------------*/
void Write_Byte(unsigned char content)
{
unsigned char i,j;
for(i = 8;i > 0;i--)
{
if((content&0x80)==0x80)
{
SDA_DAT=1;
for(j=0;j<30;j++);
SCL_OUT=1;
for(j=0;j<30;j++);
SCL_OUT=0;
for(j=0;j<30;j++);
}
else
{
SDA_DAT=0;
for(j=0;j<30;j++);
SCL_OUT=1;
for(j=0;j<30;j++);
SCL_OUT=0;
for(j=0;j<30;j++);
}
content <<= 1;
}
SDA_DAT=1;
for(i=0;i<30;i++);
}
/*------------------------------------------*/
/*Formal parameter: void */
/*Return value: read data */
/*Function description: read one byte of data from the IIC bus */
/*------------------------------------------*/
unsigned char Read_Byte(void)
{
unsigned char i,j,temp=0;
for(i = 0;i < 8;i++)
{
Set_SDA_Out();
SDA_DAT=1;
SCL_OUT=1;
for(j=0;j<30;j++);
Set_SDA_In();
if(SDA_DAT==1)
temp |= (0x01 << (7-i));
SCL_OUT=0;
for(j=0;j<30;j++);
}
Set_SDA_Out();
return(temp);
}
/*------------------------------------------*/
/*Formal parameters: Adr--address, Data--data */
/*Return value: void */
/*Function description: Write one byte of data to the specified address */
/*------------------------------------------*/
void EEPROM_Write_Byte(unsigned char Adr,unsigned char Data)
{
unsigned char temp=0;
unsigned int i;
Start_IIC();//Enable data bus
Write_Byte(0xA0);//Send write command
temp=EEPROM_Check();//Wait for ACK
if(temp==FALSE)
{
return;
}
Write_Byte(Adr);//Send pointer register
temp=EEPROM_Check();//Wait for ACK
if(temp==FALSE)
{
return;
}
Write_Byte(Data);//Send data
temp=EEPROM_Check();//Wait for ACK
if(temp==FALSE)
{
return;
}
Stop_IIC();
for(i=0;i<10000;i++);//Wait for writing to complete
}
/*------------------------------------------*/
/*Formal parameter: Adr--address */
/*Return value: data read from the specified address */
/*Function description: read one byte of data from the specified address */
/*------------------------------------------*/
unsigned char EEPROM_Read_Byte(unsigned char Adr)
{
unsigned char temp=0,r_data;
Start_IIC();//Enable data bus
Write_Byte(0xA0);//Send write command
temp=EEPROM_Check();//Wait for ACK
if(temp==FALSE)
{
return FALSE;
}
Write_Byte(Adr);//Send pointer register
temp=EEPROM_Check();//Wait for ACK
if(temp==FALSE)
{
return FALSE;
}
Start_IIC();//Enable data bus
Write_Byte(0xA1);//Send read command
temp=EEPROM_Check();//Wait for ACK
if(temp==FALSE)
{
return FALSE;
}
r_data=Read_Byte();
Master_NO_ACK();
Stop_IIC();
return r_data;
}
/*------------------------------------------*/
/*Formal parameters: Adr--address length--length *Data--pointer to data*/
/*Return value: void */
/*Function description: Write one byte of data to the specified address */
/*------------------------------------------*/
void EEPROM_Write_Page(unsigned char Adr,unsigned char length,unsigned char *Data)
{
unsigned char temp=0;
unsigned int i,j;
Start_IIC();//Enable data bus
Write_Byte(0xA0);//Send write command
temp=EEPROM_Check();//Wait for ACK
if(temp==FALSE)
{
return;
}
Write_Byte(Adr);//Send pointer register
temp=EEPROM_Check();//Wait for ACK
if(temp==FALSE)
{
return;
}
for(i=0;i<length;i++)
{
Write_Byte(*Data);//Send data
Data++;
temp=EEPROM_Check();//Wait for ACK
if(temp==FALSE)
{
return;
}
}
Stop_IIC();
for(j=0;j<10000;j++);//Wait for writing to complete
}
/*------------------------------------------*/
/*Formal parameters: Adr--address length--length buffer--data storage area*/
/*Return value: void */
/*Function description: Read length bytes of data from the specified address*/
/*------------------------------------------*/
void EEPROM_Read_Page(unsigned char Adr,unsigned length,unsigned char *buffer)
{
unsigned char temp=0,i;
Start_IIC();//Enable data bus
Write_Byte(0xA0);//Send write command
temp=EEPROM_Check();//Wait for ACK
if(temp==FALSE)
{
return ;
}
Write_Byte(Adr);//Send pointer register
temp=EEPROM_Check();//Wait for ACK
if(temp==FALSE)
{
return ;
}
Start_IIC();//Enable data bus
Write_Byte(0xA1);//Send read command
temp=EEPROM_Check();//Wait for ACK
if(temp==FALSE)
{
return ;
}
for(i=0;i<length-1;i++)
{
*buffer=Read_Byte();
buffer++;
Master_ACK();
}
*buffer=Read_Byte();
Master_NO_ACK();
Stop_IIC();
}
/*---------------------------------------------*/
|