51单片机利用IIC总线对LM75A温度进行读取

#include 
#include "..\config\c8051f350.h"
#include "..\config\const.h"
#include "..\driver\system.h"
#include "..\driver\other.h"

#define IIC_WRITE 0
#define IIC_READ  1
#define VREF 24380

static unsigned long sysclk=24500000;

sbit SDA=P0^0;
sbit SCL=P0^1;
sbit led=P1^2; 

void Delay_us(unsigned int times){
	unsigned int i;
	for(i=0;i>7;
		  Delay_us(2);         
          SCL = 1;
		  Delay_us(3);
          dd <<= 1;
    } 
	SCL = 0;    
}					     //输出一个字节

bool iic_write_addr(unsigned char addr,unsigned char data_addr)
{
	  iic_start();
      out_byte(addr); 
      read_ack();   
      out_byte(data_addr);
      read_ack();     
      return TRUE;          
}                        //写入器件地址和所需读取寄存器的地址

void IICA_Read(unsigned char id, unsigned char addr, unsigned char *p, unsigned int len)
{                                                    
         int i;
         bit EA_SAVE = EA;

	     EA = 0;
     	 iic_write_addr(id|IIC_WRITE,addr);  
     	 iic_start();
     	 out_byte(id|IIC_READ);
     	 read_ack();     
     	 for (i=0;i>5;
	tempforh=temp_high*8+low;

	judge_posneg=(temp_high & 0x80)>>7;
	if(judge_posneg==0){
		temp_value=tempforh*0.125;
		return temp_value;
		}else {
			tempforh=(tempforh^0x7FF)+1;
			temp_value=tempforh*(-0.125);   
			return temp_value;
			}
}					    //读取并计算当前温度

float main(){
	float tempvalue=0.0;
	PCA0MD&=~0x40;    // 禁止看门狗定时器

	Sysclk_Init();    // 配置系统时钟
	SysPort_Init();   // 端口配置
	tempvalue=readTemperature();
	while(1){
	led=1;
	Delay_ms(1000);
	led=0;
	Delay_ms(1000);
	}
	SDA=1;
	return tempvalue;
	}

你可能感兴趣的:(单片机开发)