📄 temp_e2prom.c
字号:
#include "iom16.h"
#include "ioavr.h"
#include "inavr.h"
#include "avr_macros.h"
#include "userdef.h"
#include "userfun.h"
#include "userram.h"
#include "disdata.h"
//#include "usertype.h"
/******* 延时 *********/
void I2cWait(void)
{
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
}
/************************* I2C通信启动 ***************************/
void I2cStart(void)
{
SDA_HIGH; //数据线输出高电平
SCL_HIGH; //时钟线输出高电平
I2cWait(); //延时
SDA_LOW; //数据线拉低
I2cWait(); //延时
SCL_LOW; //时钟线拉低
}
/******* I2C通信停止 *********/
void I2cStop(void) //I2c停止信号
{
SDA_LOW; //数据线拉低
I2cWait(); //延时
I2cWait();
SCL_HIGH; //时钟线拉高
I2cWait(); //延时
SDA_HIGH; //数据线变高
}
/****** 发送确认信号 ********/
void SendAck (unsigned char ack)
{
if ( ack==0 ) { SDA_LOW; } //发送 0
else { SDA_HIGH; } //发送 1
SCL_HIGH; //时钟信号变高
I2cWait(); //延时
SCL_LOW; //时钟信号变低
I2cWait();
SDA_HIGH; //数据线变高,为下次传送作准备
}
/******* 发送一个字节数据 *********/
void I2cSentByte(unsigned char bytedata)
{
unsigned char i,ack;
for(i=0;i<8;i++) //循环 8 次
{
if(bytedata & 0x80)
{
SDA_HIGH;
}
else
{
SDA_LOW;
}
bytedata <<= 1; //数据左移
I2cWait(); //延时
SCL_HIGH; //时钟信号变高,模拟正脉冲
I2cWait(); //延时
SCL_LOW; //时钟信号变低,为下一次输入做准备
I2cWait(); //延时
}
SDA_HIGH; //数据线变高,为接受响应信号作准备
I2cWait(); //延时
SCL_HIGH; //时钟信号变高
I2cWait(); //延时
ack= !(PINB BIT3) ? 0:1 ; //取得确认信号
SCL_LOW; //时钟信号变低
I2cWait(); //延时
write_error=ack;
}
/******* 接收一个字节数据 *********/
unsigned char I2cReceiveByte(void)
{
unsigned char i,bytedata=0;
SDA_HIGH;
for(i=0;i<8;i++) //循环 8 次
{
SCL_HIGH; //时钟信号变高
I2cWait(); //延时
bytedata <<= 1; //数据左移
if( PINB BIT3 ) bytedata|=0x01; //取得输入数据
SCL_LOW; //时钟信号变低
I2cWait(); //延时
}
return bytedata; //返回接收数据
}
/******* 从LM77中读出温度数据 *********/
unsigned int ReadLM77(void)
{
unsigned int temp1;
unsigned int i;
I2cStart();
I2cSentByte(LM77Address);
I2cSentByte(0x00); //SELECT TEMPERATURE REGISTER
I2cStart();
I2cSentByte(LM77Address+1);
i=I2cReceiveByte();
SendAck(0);
temp1=I2cReceiveByte();
SendAck(1);
I2cStop();
i=i<<8;
i=i+temp1;
i=i>>3;
for(temp1=0;temp1<16000;temp1++);
return i;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -