蓝桥杯单片机设计与开发-底层驱动代码编写(iic,onewire,ds1302)

一.onewire.c及onewire.h的代码(输出整数温度)

使用:unsigned char temp;

           temp = temp_read();


#include <stc15f2k60s2.h>

sbit DQ = P1^4;  //单总线接口

//单总线延时函数
void Delay_OneWire(unsigned int t)  //STC89C52RC
{
	while(t--);
}

//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(50);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(50);
}

//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
			dat |= 0x80;
		}	    
		Delay_OneWire(50);
	}
	return dat;
}

//DS18B20设备初始化
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(120);
  	DQ = 0;
  	Delay_OneWire(800);
  	DQ = 1;
  	Delay_OneWire(100); 
    initflag = DQ;     
  	Delay_OneWire(50);
  
  	return initflag;
}

unsigned char temp_read()
{
	unsigned char H, L, temp;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	Delay_OneWire(100);
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	L = Read_DS18B20();
	H = Read_DS18B20();
	temp = L>>4;
	temp |= H<<4;
	return(temp);
}




#ifndef __ONEWIRE_H
#define __ONEWIRE_H

unsigned char rd_temperature(void); 

unsigned char temp_read();
#endif

二.onewire.c及onewire.h的代码(输出4位温度*10,即25.25摄氏度 表示为2525,记得得加小数点)

使用:unsigned char temp;

           temp = temp_read();

#include <stc15f2k60s2.h>

sbit DQ = P1^4;  //单总线接口

//单总线延时函数
void Delay_OneWire(unsigned int t)  //STC89C52RC
{
	while(t--);
}

//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(50);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(50);
}

//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
			dat |= 0x80;
		}	    
		Delay_OneWire(50);
	}
	return dat;
}

//DS18B20设备初始化
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(120);
  	DQ = 0;
  	Delay_OneWire(800);
  	DQ = 1;
  	Delay_OneWire(100); 
    initflag = DQ;     
  	Delay_OneWire(50);
  
  	return initflag;
}

unsigned int temp_read()
{
	unsigned char H, L;
	unsigned int temp;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	Delay_OneWire(100);
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	L = Read_DS18B20();
	H = Read_DS18B20();
	temp = L;
	temp |= H<<8;
	temp *= 6.25;
	return(temp);
}
#ifndef __ONEWIRE_H
#define __ONEWIRE_H

unsigned char rd_temperature(void);  

unsigned int temp_read();
#endif

三.iic.c及iic.h(使用iic的有,RB2输出电压读取,光敏电阻读取,EEPROM,模拟电压输出)

使用:unsigned char dat;

           EEPROM_write(0x11,12);   delay(3);   dat = EEPROM_read(0x11);

           //EEPROM写入和读取,连续使用中间必须加小延迟

          unsigned char light,  AD;

          light  = AD_read(0x01);   AD  = AD_read(0x03);   

          //AD_read()可用于光敏电阻读取,和RB2输出电压读取,区别只有读取地址不同

         AD_write(x*51);  //输出 xV 电压 

#include <stc15f2k60s2.h>
#include "intrins.h"

#define DELAY_TIME 20

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

//总线引脚定义
sbit SDA = P2^1;  /* 数据线 */
sbit SCL = P2^0;  /* 时钟线 */

void IIC_Delay(unsigned char i)
{
    do{_nop_();}
    while(i--);        
}
//总线启动条件
void IIC_Start(void)
{
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

//总线停止条件
void IIC_Stop(void)
{
    SDA = 0;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//发送应答
//void IIC_SendAck(bit ackbit)
//{
//    SCL = 0;
//    SDA = ackbit;  					// 0:应答,1:非应答
//    IIC_Delay(DELAY_TIME);
//    SCL = 1;
//    IIC_Delay(DELAY_TIME);
//    SCL = 0; 
//    SDA = 1;
//    IIC_Delay(DELAY_TIME);
//}

//等待应答
bit IIC_WaitAck(void)
{
    bit ackbit;
	
    SCL  = 1;
    IIC_Delay(DELAY_TIME);
    ackbit = SDA;
    SCL = 0;
    IIC_Delay(DELAY_TIME);
    return ackbit;
}

//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
    unsigned char i;

    for(i=0; i<8; i++)
    {
        SCL  = 0;
        IIC_Delay(DELAY_TIME);
        if(byt & 0x80) SDA  = 1;
        else SDA  = 0;
        IIC_Delay(DELAY_TIME);
        SCL = 1;
        byt <<= 1;
        IIC_Delay(DELAY_TIME);
    }
    SCL  = 0;  
}

//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
    unsigned char i, da;
    for(i=0; i<8; i++)
    {   
    	SCL = 1;
	IIC_Delay(DELAY_TIME);
	da <<= 1;
	if(SDA) da |= 1;
	SCL = 0;
	IIC_Delay(DELAY_TIME);
    }
    return da;    
}

void EEPROM_write(unsigned char add, unsigned char dat)
{
	EA = 0;
	IIC_Start();	
	IIC_SendByte(0XA0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
	EA = 1;
}

unsigned char EEPROM_read(unsigned char add)
{
	unsigned char dat;
	EA = 0;
	IIC_Start();	
	IIC_SendByte(0XA0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_Stop();

	IIC_Start();	
	IIC_SendByte(0XA1);
	IIC_WaitAck();
	dat = IIC_RecByte();
	IIC_WaitAck();
	IIC_Stop();
	EA = 1;
	return(dat);
}

void AD_write(unsigned char dat)
{
	EA = 0;
	IIC_Start();	
	IIC_SendByte(0X90);
	IIC_WaitAck();
	IIC_SendByte(0X40);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
	EA = 1;
}

unsigned char AD_read(unsigned char add)
{
	unsigned char dat;
	EA = 0;
	IIC_Start();	
	IIC_SendByte(0X90);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_Stop();

	IIC_Start();	
	IIC_SendByte(0X91);
	IIC_WaitAck();
	dat = IIC_RecByte();
	IIC_WaitAck();
	IIC_Stop();
	EA = 1;
	return(dat);
}
#ifndef _IIC_H
#define _IIC_H

void IIC_Start(void); 
void IIC_Stop(void);  
bit IIC_WaitAck(void);  
void IIC_SendAck(bit ackbit); 
void IIC_SendByte(unsigned char byt); 
unsigned char IIC_RecByte(void); 

void EEPROM_write(unsigned char add, unsigned char dat);
unsigned char EEPROM_read(unsigned char add);
void AD_write(unsigned char dat);
unsigned char AD_read(unsigned char add);

#endif

四.onewire.c及onewire.h(板子问题,使用的是厂家的代码)

#include <stc15f2k60s2.h>
sbit DQ = P1^4;
#define  uchar unsigned char
/*************ds18b20****************/
uchar temget();
uchar read();
void dsinit();
void write(uchar dat);
void Delay100us();
void Delay500us();


uchar read()
{
	uchar i;
	uchar dat;
	
	for(i=0;i<8;i++)
	{
		DQ=0;
		;
		dat>>=1;
		DQ=1;
		if(DQ==1)
		{
			dat |= 0x80;// dat=dat|0x80;   0000 0000 | 1000 0000 =1000 0000
		}
		Delay100us();
	}
	return dat;
}

void write(uchar dat)
{
	uchar i;
	for(i=0;i<8;i++)
	{
		DQ=0;
		DQ=dat&0x01;//0100 0100 & 0000 0001 // 0000 0001
		Delay100us();
		DQ=1;
		dat>>=1;//dat=dat>>1;//1001 0000//0010 0001
	}
}

void dsinit()
{
	DQ=0;
	Delay500us();
	DQ=1;
	Delay500us();
}



void Delay100us()		//@11.0592MHz
{
	unsigned char i, j;

	;
	;
	i = 2;
	j = 15;
	do
	{
		while (--j);
	} while (--i);
}

void Delay500us()		//@11.0592MHz
{
	unsigned char i, j;

	;
	;
	i = 6;
	j = 93;
	do
	{
		while (--j);
	} while (--i);
}

unsigned int temp_read()
{
	unsigned char H, L;
	unsigned int temp;
	dsinit();
	write(0xcc);
	write(0x44);
	Delay100us();

	dsinit();
	write(0xcc);
	write(0xbe);
	L = read();
	H = read();
	temp = H<<8;
	temp |= L;
	temp *= 0.625;
	return(temp);
}
#ifndef __DS18B20_H
#define __DS18B20_H
#include <intrins.h>
 
#define  uchar unsigned char 

uchar read();
void dsinit();
void write(uchar dat);
void Delay100us();
void Delay500us();

unsigned int temp_read();
#endif

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值