- #include <stdio.h>
- #include "NUC1xx.h"
- #include "DrvADC.h"
- #include "DrvGPIO.h"
- #include "DrvUART.h"
- #include "DrvSYS.h"
- #include "ds18b20.h"
- #include "hw_config.h"
- /*读取18B20温度值并根据温度来决定点亮哪些LED*/
- unsigned char DS18B20_ID[8]={0,0,0,0,0,0,0,0,};
- unsigned char NOW_TEMP[6]={0};
- void LEDonoff(uint32_t ledno)
- {
- DrvGPIO_SetBit(E_GPA,2);
- DrvGPIO_SetBit(E_GPA,3);
- DrvGPIO_SetBit(E_GPA,4);
- DrvGPIO_SetBit(E_GPA,5);
-
- if(ledno>33)
- {
- DrvGPIO_ClrBit(E_GPA,2);
- DrvGPIO_ClrBit(E_GPA,3);
- DrvGPIO_ClrBit(E_GPA,4);
- DrvGPIO_ClrBit(E_GPA,5);
- }
- else
- {
- switch(ledno)
- {
- case 20:
- DrvGPIO_ClrBit(E_GPA,2);
- break;
- case 21:
- DrvGPIO_ClrBit(E_GPA,3);
- break;
- case 22:
- DrvGPIO_ClrBit(E_GPA,4);
- break;
- case 23:
- DrvGPIO_ClrBit(E_GPA,5);
- break;
- case 24:
- DrvGPIO_ClrBit(E_GPA,2);
- DrvGPIO_ClrBit(E_GPA,3);
- break;
- case 25:
- DrvGPIO_ClrBit(E_GPA,2);
- DrvGPIO_ClrBit(E_GPA,4);
- break;
- case 26:
- DrvGPIO_ClrBit(E_GPA,2);
- DrvGPIO_ClrBit(E_GPA,5);
- break;
- case 27:
- DrvGPIO_ClrBit(E_GPA,3);
- DrvGPIO_ClrBit(E_GPA,4);
- break;
- case 28:
- DrvGPIO_ClrBit(E_GPA,3);
- DrvGPIO_ClrBit(E_GPA,5);
- break;
- case 29:
- DrvGPIO_ClrBit(E_GPA,4);
- DrvGPIO_ClrBit(E_GPA,5);
- break;
- case 30:
- DrvGPIO_ClrBit(E_GPA,2);
- DrvGPIO_ClrBit(E_GPA,3);
- DrvGPIO_ClrBit(E_GPA,4);
- break;
- case 31:
- DrvGPIO_ClrBit(E_GPA,2);
- DrvGPIO_ClrBit(E_GPA,3);
- DrvGPIO_ClrBit(E_GPA,5);
- break;
- case 32:
- DrvGPIO_ClrBit(E_GPA,3);
- DrvGPIO_ClrBit(E_GPA,4);
- DrvGPIO_ClrBit(E_GPA,5);
- break;
- case 33:
- DrvGPIO_ClrBit(E_GPA,2);
- DrvGPIO_ClrBit(E_GPA,3);
- DrvGPIO_ClrBit(E_GPA,4);
- DrvGPIO_ClrBit(E_GPA,5);
- break;
- default:
- break;
- }
- }
- }
- int main (void)
- {
- unsigned int temp=0;
- unsigned char temp_H=0;
- unsigned char temp_L=0;
- unsigned char i=0;
- Set_System();
- printf("\n================================================\n");
- printf("\n DS18B20 读取温度 LED显示 \n");
- get_rom(DS18B20_ID);
- printf("本机DS18B20 的64位ID ---> ");
- for(i=0;i<8;i++)
- printf(" 0x%x ",DS18B20_ID[i]);
- printf("\n================================================\n\n\n");
- DrvSYS_Delay(50000000);
- while(1)
- {
- printf(" 当前温度:");
- temp = readTempDS18B20();
- temp_H = temp/100;
- temp_L = temp%100;
- printf("%d",temp_H);
- printf(".%d 度\n",temp_L);
- LEDonoff(temp_H);
- if(20 <= temp_H <= 33)
- DrvSYS_Delay(50000000);
- }
- }
-
-
ds18b20.c
-
- /***************************************************/
- /* 本页程序来自 烈火狂龙 TEST_18B20 */
- /* 2011/11/19 */
- /*-------------------------------------------------*/
- #include "ds18b20.h"
- #include "hw_config.h"
- unsigned char DS18B20_sign_flag=0;
- /**********************************************************************
- 延时
- **********************************************************************/
- void delay_nus(unsigned int t)
- {
- unsigned int i=0;
- while(t--)
- {
- for(i=0;i<1;i++);
- }
- }
- /**********************************************************************
- functionName: unsigned char resetDS18B20(void)
- description :DS18B20初始化
- **********************************************************************/
- unsigned char resetDS18B20(void)
- {
- unsigned char errTime=0;
- HLD_DS18B20; //控制总线
- CLR_DS18B20; //强制拉低
- delay_nus(260); //以上延时大于480us
- SET_DS18B20;
- RLS_DS18B20; //释放总线,总线自动上拉 DDR
- delay_nus(7); //15~60us
- while (STU_DS18B20)
- {
- delay_nus(3); //5.15us
- errTime++;
- if (errTime>20)
- return(0x00); //如果等待大于约 5.15us*20就返回0x00,报告复位失败(实际上只要等待15-60us)
- }
- errTime=0;
- while (!(STU_DS18B20))
- {
- delay_nus(3); //5.15us
- errTime++;
- if (errTime>50)
- return(0x00); //如果等待大于约 5.15us*50就返回0x00,报告复位失败(实际上只要等待60-240us)
- }
- HLD_DS18B20; //控制总线
- SET_DS18B20; //强制拉高
- return(0xff);
- }
- /**********************************************************************
- functionName: unsigned char readByteDS18B20(void)
- description :读DS18B20一个字节
- **********************************************************************/
- unsigned char readByteDS18B20(void)
- {
- unsigned char i;
- unsigned char retVal=0;
- //RLS_DS18B20; //释放总线
- for (i=8;i>0;i--)
- {
- retVal>>=1;
- HLD_DS18B20; //控制总线
- CLR_DS18B20; //强制拉低
- delay_nus(1); //延时大于1us
- SET_DS18B20; //释放总线,DS18B20会将总线强制拉低
- RLS_DS18B20; //释放总线
- delay_nus(1);
- if (STU_DS18B20)
- retVal|=0x80;
- delay_nus(15); //31us
- }
- HLD_DS18B20; //控制总线
- SET_DS18B20; //强制拉高
- return(retVal);
- }
- /**********************************************************************
- functionName: unsigned char readByteDS18B20(void)
- description :写DS18B20一个字节
- **********************************************************************/
- void writeByteDS18B20(unsigned char wb)
- {
- unsigned char i;
- unsigned char temp;
- //RLS_DS18B20; //释放总线
- for (i=0;i<8;i++)
- {
- HLD_DS18B20; //控制总线
- CLR_DS18B20; //强制拉低
- delay_nus(1); //14.92us
- temp=wb>>i;
- if (temp&=0x01)
- SET_DS18B20; //释放总线
- else
- CLR_DS18B20; //强制拉低
- delay_nus(15); //30.38us
- SET_DS18B20; //释放总线
- delay_nus(1); //2.71us(大于1us就行了)
- }
- }
- /**********************************************************************
- functionName: get_rom(unsigned char* p)
- description :读ROM
- **********************************************************************/
- void get_rom(unsigned char* p)
- {
- unsigned char i;
- if (resetDS18B20()==0xff)
- {
- writeByteDS18B20(ds18b20_read_rom);
- for (i=0;i<8;i++)
- {
- *p++ = readByteDS18B20();
- }
- }
- }
- /**********************************************************************
- functionName: void set_ds18b20(char th,char tl,unsigned char config)
- description :设定DS18B20模式
- **********************************************************************/
- void set_ds18b20(char th,char tl,unsigned char config)
- {
- if (resetDS18B20()==0xff)
- {
- writeByteDS18B20(ds18b20_skip_rom);
- writeByteDS18B20(ds18b20_write_ram);
- writeByteDS18B20(th);
- writeByteDS18B20(tl);
- writeByteDS18B20(config);
- }
- }
- /**********************************************************************
- functionName: convert_ds18b20(void)
- description :写DS18B20一个字节
- **********************************************************************/
- void convert_ds18b20(void)
- {
- if (resetDS18B20()==0xff)
- {
- writeByteDS18B20(ds18b20_skip_rom); //跳过ROM
- writeByteDS18B20(ds18b20_convert_tem); //启动温度转换
- }
- }
- /**********************************************************************
- functionName: unsigned int readTempDS18B20(void)
- description :读DS18B20温度 有正负符号标志和两个小数点精度
- **********************************************************************/
- unsigned int readTempDS18B20(void)
- {
- unsigned char tempL,tempH,wm0,wm2;
- unsigned int x;
- if (resetDS18B20()==0)
- return 0;
- writeByteDS18B20(ds18b20_skip_rom); //跳过ROM
- writeByteDS18B20(ds18b20_read_ram); //读数据
- tempL=readByteDS18B20();
- tempH=readByteDS18B20();
- wm0=tempL;
- wm0=wm0>>4;
- tempH=tempH<<4;
- wm2=wm0+tempH; //温度的整数值
- if (wm2&0x80) //测试符号位
- {
- DS18B20_sign_flag=1;
- wm2=256-wm2;
- }
- else
- DS18B20_sign_flag=0;
- x = wm2*100; //字符型赋给整形,扩大100倍
- if (tempL&0x08) //提高测试精度
- {
- x=x+50;
- }
- if (tempL&0x04)
- {
- x=x+25;
- }
- if (tempL&0x02)
- {
- x=x+12;
- }
- if (tempL&0x01)
- {
- x=x+6;
- }
- convert_ds18b20();//启动温度转换
- return(x);
- }
-
工程文件