DS18B20+12864代码

DS18B20+12864代码

在家学习了DS18B20传感器的使用,花费了自己两天时间,终于把程序跑出来了(实属不易),以下内容,我会把所有文件都上传到文章中,希望对你有所帮助。
注意事项:
DS18B20对时序要求很严格,一定要按照时序写。
在每次读数据位的时候最后不需要释放总线(放了可能会出错)

1.DS18B20代码 分H文件和C文件
2.LCD12864代码 分H文件和C文件

#ifndef __DS12B20_H
#define __DS12B20_H

#include <STC12C5A60S2.H>
#include <intrins.h>
#include "LCD12864.h"


#ifndef __UDEFINE_
#define __UDEFINE_
#define uchar unsigned char
#define uint unsigned int
#endif /*__UDEFINE_*/




/*DS18B20*/
sbit DS_data = P1^1;


/*DS18B20*/
uchar DS_Init(void);
void DS_Write(unsigned char cmd);
unsigned char DS_Read();
unsigned int get_temp();
void Display(unsigned int Tem);

#endif /*__DS12B20_H*/
#include "DS18B20.h"


uchar a,b;
/*******************************************DS18B20*************************************************/
/**
  *输入:无
  *输出:0/1
  */
void Display(uint Tem)
{
	uchar count;
	LCD_Pos(2,3);
	if((Tem >> 12) & 0x0001)
	{
		LCD_WriteDat('-');
		Tem = ~Tem + 1;
	}
	else
	{
		LCD_WriteDat('+');
	}
	Tem = Tem * 0.625;
	
	count = Tem /1000;
	if(count != 0)
	{
		LCD_WriteDat(count+48);
	}
	count = Tem /100 %10;
	LCD_WriteDat(count+48);
	count = Tem / 10 % 10;
	LCD_WriteDat(count+48);
	LCD_WriteDat('.');
	count = Tem % 10;
	LCD_WriteDat(count+48);

	

}
uchar DS_Init()
{
	uchar flag = 0;
	DS_data = 0;
    /*500us*/
	for(b=251;b>0;b--)
        for(a=4;a>0;a--);
    _nop_();
	DS_data = 1; 		//释放总线
    /*15us*/
	for(b=1;b>0;b--)
        for(a=38;a>0;a--);
    _nop_();

	while(DS_data == 1);
	while(DS_data == 0)
	{
		flag = 1;	
	}
	DS_data = 1;
	return flag;			
}

void DS_Write(uchar cmd)
{
	uchar i;
	for(i = 0; i < 8; i++)
	{
		DS_data = 0;
	   	/*2us*/
		for(a=4;a>0;a--);
		DS_data = ((cmd >> i) & 0x01);
	    /*60us*/
		for(a=164;a>0;a--);
    	_nop_();
		DS_data = 1; 
	}
}

uchar DS_Read()
{
	uchar dat = 0x00;
	uchar i,num;
	for(i = 0; i < 8;i++)
	{
		DS_data = 0;
	   	/*2us*/
		for(a=4;a>0;a--);
		DS_data = 1;
		/*6us*/
		for(a=15;a>0;a--);
		num = DS_data;
		dat	= (dat | (num << i));
	    /*60us*/
		for(a=164;a>0;a--);
    	_nop_();
//		DS_data = 1;
	}
	return dat;
}

uint get_temp()
{
	uchar flag;
	uint temperature = 0x0000;
	uchar LSB,MSB;
	flag = DS_Init();
	delay1ms();
	if(flag)
	{
		DS_Write(0xcc);
		DS_Write(0x44);
		delay1s();
		flag = DS_Init();
		delay1ms();
		if(flag)
		{
			DS_Write(0xcc);
			DS_Write(0xBE);
			LSB = DS_Read();
			MSB = DS_Read();
			temperature = MSB;
			temperature <<= 8;
			temperature |= LSB;
			return temperature;
		}
		else
		{
			return 88;
		}
	}
	else
	{
	 	return 88;
	}		
}
/***************************************************************************************************/
#ifndef __LCD12864_H
#define __LCD12864_H

#include <STC12C5A60S2.H>
#include <intrins.h>


#ifndef __UDEFINE_
#define __UDEFINE_
#define uchar unsigned char
#define uint unsigned int
#endif /*__UDEFINE_*/




/*LCD12864*/
#define LCD_data P0
sbit EN = P3^4;
sbit RS = P3^5;
sbit RW = P3^6;
sbit PSB = P3^7;


/*LCD12864函数声明*/
void delay1ms(void);
void delay1s(void);
void LCD_Init(void);
void LCD_WriteCmd(uchar cmd);
void LCD_WriteDat(uchar dat);
void LCD_Pos(uchar x,uchar y);
void LCD_Test(void);


#endif /*__LCD12864_H*/
#include "LCD12864.h"





/*******************************************LCD12864************************************************/
void LCD_Test()
{
	uchar i,j;
	uchar TestStr[] = "                测试                ";
	LCD_Init();
	LCD_Pos(1,0);
	while(1)
	{
	LCD_WriteCmd(0x0c);
		for(i = 0;i < 20;i+=2)
		{
			LCD_Pos(1,0);
			for(j = i;j< 16+i;j++)
				LCD_WriteDat(TestStr[j]);
			delay1s();
		}
	}
}


void LCD_Pos(uchar x,uchar y)
{
	uchar pos;
	if(1 == x)
	{
		x = 0x80;
	}
	else if(2 == x)
	{
		x = 0x90;
	}
	else if(3 == x)
	{
		x = 0x88;
	}
	else if(4 == x)
	{
		x = 0x98;
	}
	pos = x+y;
	LCD_WriteCmd(pos);		
}


void LCD_Init()
{
	PSB = 1;
	/*清除显示*/  //将DDRAM填满"20H",并且设定DDRAM的地址计数器(AC)到"00H"
	LCD_WriteCmd(0x01);
	delay1ms();
	/*设置功能*/	// 4/8数据 AND 是否打开拓展功能
	LCD_WriteCmd(0x30);
	delay1ms();
	/*显示状态开关*/	//不知道干啥的
	LCD_WriteCmd(0x0C);
	delay1ms();	
}


void delay1s(void)   //误差 -0.000000000099us
{
    unsigned char a,b,c;
    for(c=217;c>0;c--)
        for(b=171;b>0;b--)
            for(a=73;a>0;a--);
}

void delay1ms(void)   //误差 -0.018084490741us
{
    unsigned char a,b;
    for(b=21;b>0;b--)
        for(a=130;a>0;a--);
    _nop_();  //if Keil,require use intrins.h
}

void LCD_WriteCmd(uchar cmd)
{
	RW = 0;
	RS = 0;
	EN = 0;
	delay1ms();
	LCD_data = cmd;
	delay1ms();
	EN = 1;
	delay1ms();
	EN = 0;
}


void LCD_WriteDat(uchar dat)
{
	RW = 0;
	RS = 1;
	EN = 0;
	delay1ms();
	LCD_data = dat;
	delay1ms();
	EN = 1;
	delay1ms();
	EN = 0;
}
/***************************************************************************************************/
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值