📄 ee.c
字号:
#include <pic.h>
#include "ee.h"
#define SDA GP1
#define SCL GP2
#define TRUE 1
#define FALSE 0
#define WRITE_CONTROL_BYTE 0B10010000
#define READ_CONTROL_BYTE 0B10010001
#define ACCESS_CONFIG 0xAC
#define START_TEMP_CONVERT 0x51
#define READ_TEMPERATURE 0xAA
extern byte ConfigError, ReadError;
/******************************************************************************/
/*** 24C01A routines ***/
/******************************************************************************/
/******************************************************************************/
/*Name : ee_delay */
/*Description : Dummy procedure that delays for eerom */
/* */
/*Author : Cameron Pearce */
/*Date : 24/08/98 */
/*Version : 1.0 */
/* */
/*Inputs : none */
/*Outputs : none */
/*Destroys : nothing */
/*Globals : none */
/* */
/*Comments : */
/*Uses : a byte */
/******************************************************************************/
void ee_delay(void)
{byte delay;
for(delay=0;delay<10;delay++);
}
/******************************************************************************/
/*Name : ee_byte_to_ee */
/*Description : Places the dat byte at address in the EEROM */
/* */
/*Author : Cameron Pearce */
/*Date : 24/08/98 */
/*Version : 1.0 */
/* */
/*Inputs : dat, the byte written */
/* address, the byte address of the data */
/*Outputs : Returns True if write was successful */
/*Destroys : nothing */
/*Globals : none */
/* */
/*Comments : */
/*Uses : nothing */
/******************************************************************************/
bit ee_byte_to_ee(byte address, byte dat)
{
/* Returns True if write was successfully acknowledged */
ee_start(); /* Send start sequence */
ee_byte(0xa0); /* Control byte (WRITE) */
if (ack()) return(FALSE); /* Test Acknowledge */
ee_byte(address); /* Address Byte */
if (ack()) return(FALSE);
ee_byte(dat); /* Data byte */
if (ack()) return(FALSE);
SCL=0;
ee_delay();
SDA=0; /* Stop condition */
TRIS1=0; /* SDA OUTPUT */
ee_delay();
SCL = 1;
ee_delay();
SDA = 1;
ee_delay();
return(TRUE);
}
/******************************************************************************/
/*Name : ee_byte */
/*Description : transmits the x byte to the eerom */
/* */
/*Author : Cameron Pearce */
/*Date : 25/08/98 */
/*Version : 1.0 */
/* */
/*Inputs : x, the byte sent to the EEROM */
/*Outputs : none */
/*Destroys : nothing */
/*Globals : none */
/* */
/*Comments : */
/*Uses : 1 int */
/******************************************************************************/
void ee_byte(byte x)
{int i;
i=0x80;
do
{
SCL=0;
TRIS1 =0; /* SDA OUTPUT */
ee_delay();
if (x&i) SDA=1;
else SDA=0;
ee_delay();
SCL=1;
ee_delay();
i>>=1;
}while(i!=0);
}
/******************************************************************************/
bit ack(void)
{
SCL=0; /* Acknowledge */
ee_delay();
TRIS1=1; /* SDA Input */
ee_delay();
SCL=1;
ee_delay(); /* Test Acknowledge */
return(SDA);
}
/******************************************************************************/
/*Name : ee_byte_from_ee */
/*Description : Gets a byte at address from the EEROM */
/* */
/*Author : Cameron Pearce */
/*Date : 25/08/98 */
/*Version : 1.0 */
/* */
/*Inputs : dat, a pointer to the byte read */
/* address, the byte address of the data */
/*Outputs : Returns True if read was successful */
/*Destroys : nothing */
/*Globals : none */
/* */
/*Comments : */
/*Uses : nothing */
/******************************************************************************/
bit ee_byte_from_ee(byte address,byte *dat)
{
/* Returns True if read was successfully acknowledged */
ee_start(); /* Send start sequence */
ee_byte(0xa0); /* Write Control byte */
if (ack()) return(FALSE);
ee_byte(address); /* Address Byte */
if (ack()) return(FALSE);
SCL=0;
ee_delay();
SCL=1;
ee_delay();
SDA=0; /* Start Condition */
TRIS1=0; /* SDA OUTPUT */
ee_delay();
ee_byte(0xa1); /* Read Control byte */
if (ack()) return(FALSE);
ee_data(dat); /* Get the data byte */
ack();
SCL=0; /* Stop condition */
ee_delay();
SDA=0;
TRIS1=0; /* SDA OUTPUT */
ee_delay();
SCL=1;
ee_delay();
SDA=1;
return(TRUE);
}
/******************************************************************************/
/*Name : ee_data */
/*Description : receives a byte from the eerom */
/* */
/*Author : Cameron Pearce */
/*Date : 27/08/98 */
/*Version : 1.0 */
/* */
/*Inputs : x, a pointer to the byte received */
/*Outputs : none */
/*Destroys : nothing */
/*Globals : none */
/* */
/*Comments : */
/*Uses : 1 int */
/******************************************************************************/
void ee_data(byte *x)
{int i;
TRIS1=1; /* SDA INPUT */
*x=0;
i=0x80;
do
{
SCL=0;
ee_delay();
SCL=1;
ee_delay();
if (SDA) *x=(*x)|i;
i>>=1;
}while(i!=0);
}
/******************************************************************************/
void ee_start(void)
{
TRIS &= 0b11111001; /* make SCL and SDA outputs */
SDA = 0;
ee_delay();
SCL = 1;
ee_delay();
SDA = 1;
ee_delay(); /* Stop condition */
SDA=0;
ee_delay(); /* Start Condition */
}
/******************************************************************************/
/******************** I2C comms to the DS1775 *******************************/
bit ConfigByteDS1775(byte NewConfig)
{
/* Returns True if write was successfully acknowledged */
ee_start(); /* Send start sequence */
ee_byte(WRITE_CONTROL_BYTE); /* Control byte (WRITE) */
if (ack())
{
ConfigError=1;
return(FALSE); /* Test Acknowledge */
}
ee_byte(1); /* Set Data Pointer to Config register */
if (ack())
{
ConfigError=2;
return(FALSE); /* Test Acknowledge */
}
ee_byte(NewConfig); /* Transmit new Configuration */
if (ack())
{
ConfigError=3;
return(FALSE); /* Test Acknowledge */
}
SCL=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -