📄 ds18b20.c
字号:
/*********************************************************************************************
* File: Temperature_test.c
* Author: embest
* Desc: Temperature_test
* History:
*********************************************************************************************/
/*------------------------------------------------------------------------------------------*/
/* include files */
/*------------------------------------------------------------------------------------------*/
#include "2410lib.h"
#include "math.h"
/*------------------------------------------------------------------------------------------*/
/* constants define */
/*------------------------------------------------------------------------------------------*/
#define DQ GPIO11
#define CFG_IN 0x0
#define CFG_OUT 0x1
#define Warning 30
#define uchar unsigned char
#define rCPLDLEDADDR (*(volatile unsigned char*)0x21140000) // LED Address
extern void iic_write_8led(UINT32T unSlaveAddr,UINT32T unAddr,UINT8T ucData);
extern void iic_init_8led(void);
unsigned char f_szDigital[10] ={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6}; // 0 ~ 9
/*********************************************************************************************
* name: beep_on_off
* func: beep_on_off test
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void beep_on_off(int time)
{
int i;
rCPLDLEDADDR = 0x00;
for(i = 0; i < 10000/time; i++);
rCPLDLEDADDR = 0xFE;
for(i = 0; i < 10000/time; i++);
}
/*********************************************************************************************
* name: udelay
* func: delay (num*2+10) us
* para: num
* ret:
* modify:
* comment:
*********************************************************************************************/
void udelay(int num)
{
int i;
for(i=0; i < num;i++);
}
/*********************************************************************************************
* name: s3c2410_gpio_cfgpin
* func: set the pin for out or in
* para: state
* ret: none
* modify:
* comment:
*********************************************************************************************/
void s3c2410_gpio_cfgpin(int state)
{
if(state == CFG_IN)
rGPECON = (rGPECON & 0xff3fffff) | 0x00000000; //set GE11
else
rGPECON = (rGPECON & 0xff3fffff) | 0x00400000; //set GE11
}
/*********************************************************************************************
* name: s3c2410_gpio_pullup
* func: enable or disable the pull up function
* para: state
* ret: none
* modify:
* comment:
*********************************************************************************************/
void s3c2410_gpio_pullup(int state)
{
if(state == 0)
rGPEUP = rGPEUP & 0xf7ff;
else
rGPEUP = rGPEUP | 0x0800;
}
/*********************************************************************************************
* name: s3c2410_gpio_setpin
* func: set io pin
* para: the pin state
* ret: none
* modify:
* comment:
*********************************************************************************************/
void s3c2410_gpio_setpin(int bit)
{
if(bit == 0)
rGPEDAT = rGPEDAT & 0xf7ff;
else
rGPEDAT = rGPEDAT | 0x0800;
}
/*********************************************************************************************
* name: s3c2410_gpio_getpin
* func: get the state of io pin.
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
int s3c2410_gpio_getpin(int dq)
{
if((rGPEDAT & 0x0800) == 0x0)
return 0;
else
return 1;
}
/*********************************************************************************************
* name: ds18b20_reset
* func: ds18b20 test
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
int ds18b20_reset(void) //ds18b20复位,返回0成功,返回1失败
{
int ret;
s3c2410_gpio_cfgpin(CFG_OUT);
s3c2410_gpio_pullup(0);
s3c2410_gpio_setpin(0); //拉低数据线电平
udelay(300); //精确延时480us-960us
s3c2410_gpio_setpin(1); //拉高电平
udelay(25); //精确延时15us-60us
s3c2410_gpio_cfgpin(CFG_IN);
ret = s3c2410_gpio_getpin(DQ);
if(ret)
{
uart_printf(" Error\n");
return -1;
}
else
{
// uart_printf("OK!");
udelay(40);
}
return ret;
}
/*********************************************************************************************
* name: write_byte
* func: write a byte data to ds18b20
* para: data
* ret: none
* modify:
* comment:
*********************************************************************************************/
void write_byte(unsigned char data)//向18b20写一个字节
{
char i = 0;
s3c2410_gpio_cfgpin(CFG_OUT);
s3c2410_gpio_pullup(0);
for(i=0; i<8; i++){
s3c2410_gpio_setpin(1);
i = i;
s3c2410_gpio_setpin(0);
//udelay(2);
udelay(1);
s3c2410_gpio_setpin(data&0x01);
udelay(25);
data >>= 1;
}
s3c2410_gpio_setpin(1);
}
/*********************************************************************************************
* name: read_byte
* func: read a byte data from ds18b20
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
unsigned char read_byte(void)//从18b20读一个字节
{
unsigned char i;
unsigned char data=0;
s3c2410_gpio_cfgpin(CFG_OUT);
s3c2410_gpio_pullup(0);
for(i=0; i<8; i++)
{
data >>= 1;
s3c2410_gpio_cfgpin(CFG_OUT);
//s3c2410_gpio_setpin(1);
//udelay(6);
s3c2410_gpio_setpin(0);
i = i;
s3c2410_gpio_setpin(1);
i = i;
s3c2410_gpio_cfgpin(CFG_IN);
if(s3c2410_gpio_getpin(DQ))
data |= 0x80;
else
data &= 0x7f;
// udelay(2);
//s3c2410_gpio_cfgpin(CFG_OUT);
//s3c2410_gpio_pullup(0);
//s3c2410_gpio_setpin(1);
//
}
udelay(2);
// s3c2410_gpio_cfgpin(CFG_OUT);
// s3c2410_gpio_pullup(0);
// s3c2410_gpio_setpin(1);
return data;
}
/*********************************************************************************************
* name: temperature_test
* func: temperature test
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void temperature_test()
{
unsigned int a = 0, b = 0,t = 0;
float tt;
int temp = 0,ret,i;
uart_printf(" Temperature Test!\n");
s3c2410_gpio_cfgpin(CFG_OUT);
iic_init_8led();
do{
ret = ds18b20_reset();
if (ret == -1)
return ;
write_byte(0xcc);
write_byte(0x44);
udelay(1000*800);
ds18b20_reset();
write_byte(0xcc);
write_byte(0xbe);
a = read_byte();
b = read_byte();
t = b;
t <<= 8;
t = t | a;
tt = t * 0.0625;
tt = tt * 1 + 0.5; //放大10倍输出并四舍五入
uart_printf(" The current temperature is: %5.4fC\r",tt);
temp = (int)tt;
iic_write_8led(0x70, 0x10, f_szDigital[(temp%10)]); //0
iic_write_8led(0x70, 0x11, f_szDigital[temp/10]);
if (temp >= Warning)
{
for(i = 0; i < 10; i++ )
{
iic_write_8led(0x70, 0x12, f_szDigital[8]);
beep_on_off(temp);
udelay(1000);
iic_write_8led(0x70, 0x12, f_szDigital[0]);
beep_on_off(temp);
udelay(1000);
}
}
}while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -