[新手园地] 第四批笔记之18B20读温度并点亮相应LED

[复制链接]
2520|1
 楼主| hello某人 发表于 2011-11-20 11:14 | 显示全部楼层 |阅读模式
拿到助学板也有一段时间了,其实一直想学习,但水平太低,加上工作跟这个也没关系,学得也就慢了。这个周未闲着想做个温度计玩玩吧,LCD不知道怎么点亮,就用LED灯的显示情况来表示温度。根据现在的气温,用L1-L4 4个LED的点亮情况来表示20-32度的温度,不在这个范围就用全亮和全灭表示了。

main.c

  1. #include <stdio.h>
  2. #include "NUC1xx.h"
  3. #include "DrvADC.h"
  4. #include "DrvGPIO.h"
  5. #include "DrvUART.h"
  6. #include "DrvSYS.h"
  7. #include "ds18b20.h"
  8. #include "hw_config.h"

  9. /*读取18B20温度值并根据温度来决定点亮哪些LED*/
  10. unsigned char DS18B20_ID[8]={0,0,0,0,0,0,0,0,};
  11. unsigned char NOW_TEMP[6]={0};
  12. void LEDonoff(uint32_t ledno)
  13. {
  14.    DrvGPIO_SetBit(E_GPA,2);
  15.    DrvGPIO_SetBit(E_GPA,3);
  16.    DrvGPIO_SetBit(E_GPA,4);
  17.    DrvGPIO_SetBit(E_GPA,5);
  18.    
  19.    if(ledno>33)
  20.    {
  21.        DrvGPIO_ClrBit(E_GPA,2);
  22.        DrvGPIO_ClrBit(E_GPA,3);
  23.        DrvGPIO_ClrBit(E_GPA,4);
  24.        DrvGPIO_ClrBit(E_GPA,5);
  25.     }
  26. else
  27. {   
  28.         switch(ledno)
  29.         {
  30.             case 20:
  31.              DrvGPIO_ClrBit(E_GPA,2);
  32.              break;
  33.    case 21:
  34.        DrvGPIO_ClrBit(E_GPA,3);
  35.     break;
  36.    case 22:
  37.        DrvGPIO_ClrBit(E_GPA,4);
  38.     break;
  39.    case 23:
  40.        DrvGPIO_ClrBit(E_GPA,5);
  41.     break;
  42.    case 24:
  43.        DrvGPIO_ClrBit(E_GPA,2);
  44.     DrvGPIO_ClrBit(E_GPA,3);
  45.     break;
  46.    case 25:
  47.        DrvGPIO_ClrBit(E_GPA,2);
  48.     DrvGPIO_ClrBit(E_GPA,4);
  49.     break;
  50.    case 26:
  51.        DrvGPIO_ClrBit(E_GPA,2);
  52.     DrvGPIO_ClrBit(E_GPA,5);
  53.     break;
  54.    case 27:
  55.        DrvGPIO_ClrBit(E_GPA,3);
  56.     DrvGPIO_ClrBit(E_GPA,4);
  57.     break;
  58.    case 28:
  59.        DrvGPIO_ClrBit(E_GPA,3);
  60.     DrvGPIO_ClrBit(E_GPA,5);
  61.     break;
  62.    case 29:
  63.        DrvGPIO_ClrBit(E_GPA,4);
  64.     DrvGPIO_ClrBit(E_GPA,5);
  65.     break;
  66.    case 30:
  67.        DrvGPIO_ClrBit(E_GPA,2);
  68.     DrvGPIO_ClrBit(E_GPA,3);
  69.     DrvGPIO_ClrBit(E_GPA,4);
  70.     break;
  71.    case 31:
  72.        DrvGPIO_ClrBit(E_GPA,2);
  73.     DrvGPIO_ClrBit(E_GPA,3);
  74.     DrvGPIO_ClrBit(E_GPA,5);
  75.     break;
  76.    case 32:
  77.        DrvGPIO_ClrBit(E_GPA,3);
  78.     DrvGPIO_ClrBit(E_GPA,4);
  79.     DrvGPIO_ClrBit(E_GPA,5);
  80.     break;
  81.    case 33:
  82.        DrvGPIO_ClrBit(E_GPA,2);
  83.                 DrvGPIO_ClrBit(E_GPA,3);
  84.                 DrvGPIO_ClrBit(E_GPA,4);
  85.                 DrvGPIO_ClrBit(E_GPA,5);
  86.     break;
  87.          default:
  88.              break;
  89.         }
  90.    }
  91. }

  92. int main (void)
  93. {
  94. unsigned int temp=0;
  95. unsigned char temp_H=0;
  96. unsigned char temp_L=0;
  97. unsigned char i=0;
  98.     Set_System();
  99.     printf("\n================================================\n");
  100.     printf("\n       DS18B20 读取温度 LED显示                 \n");
  101.    get_rom(DS18B20_ID);
  102.    printf("本机DS18B20 的64位ID ---> ");
  103.    for(i=0;i<8;i++)
  104.       printf(" 0x%x ",DS18B20_ID[i]);
  105.     printf("\n================================================\n\n\n");
  106.     DrvSYS_Delay(50000000);
  107. while(1)
  108.   {
  109.    printf("      当前温度:");
  110.    temp  = readTempDS18B20();
  111.    temp_H  = temp/100;
  112.    temp_L  =   temp%100;
  113.             printf("%d",temp_H);
  114.    printf(".%d 度\n",temp_L);
  115.    LEDonoff(temp_H);
  116.    if(20 <= temp_H <= 33)
  117.    DrvSYS_Delay(50000000);
  118.   }
  119. }




ds18b20.c

  1. /***************************************************/
  2. /*     本页程序来自 烈火狂龙 TEST_18B20            */
  3. /*     2011/11/19           */
  4. /*-------------------------------------------------*/
  5. #include "ds18b20.h"
  6. #include "hw_config.h"
  7. unsigned char DS18B20_sign_flag=0;
  8. /**********************************************************************
  9. 延时
  10. **********************************************************************/
  11. void delay_nus(unsigned int t)
  12. {
  13.     unsigned int i=0;
  14.     while(t--)
  15.     {
  16.         for(i=0;i<1;i++);
  17.     }
  18. }
  19. /**********************************************************************
  20. functionName: unsigned char resetDS18B20(void)
  21. description :DS18B20初始化
  22. **********************************************************************/
  23. unsigned char resetDS18B20(void)
  24. {
  25.     unsigned char errTime=0;
  26.     HLD_DS18B20;     //控制总线
  27.     CLR_DS18B20;     //强制拉低      
  28.     delay_nus(260);        //以上延时大于480us
  29.     SET_DS18B20;
  30.     RLS_DS18B20;     //释放总线,总线自动上拉 DDR
  31.     delay_nus(7);          //15~60us
  32.     while (STU_DS18B20)
  33.     {
  34.         delay_nus(3);    //5.15us
  35.         errTime++;
  36.         if (errTime>20)
  37.         return(0x00);  //如果等待大于约 5.15us*20就返回0x00,报告复位失败(实际上只要等待15-60us)
  38.     }
  39.     errTime=0;
  40.     while (!(STU_DS18B20))
  41.     {
  42.         delay_nus(3);    //5.15us
  43.         errTime++;
  44.         if (errTime>50)
  45.             return(0x00);  //如果等待大于约 5.15us*50就返回0x00,报告复位失败(实际上只要等待60-240us)
  46.     }
  47.     HLD_DS18B20;     //控制总线
  48.     SET_DS18B20;     //强制拉高
  49.     return(0xff);
  50. }
  51. /**********************************************************************
  52. functionName: unsigned char readByteDS18B20(void)
  53. description :读DS18B20一个字节
  54. **********************************************************************/
  55. unsigned char readByteDS18B20(void)
  56. {
  57.     unsigned char i;
  58.     unsigned char retVal=0;
  59.     //RLS_DS18B20;     //释放总线
  60.     for (i=8;i>0;i--)
  61.     {
  62.         retVal>>=1;
  63.         HLD_DS18B20;    //控制总线
  64.         CLR_DS18B20;    //强制拉低
  65.         delay_nus(1);    //延时大于1us
  66.         SET_DS18B20;    //释放总线,DS18B20会将总线强制拉低
  67.         RLS_DS18B20;    //释放总线
  68.         delay_nus(1);
  69.         if (STU_DS18B20)
  70.             retVal|=0x80;
  71.         delay_nus(15);   //31us
  72.     }
  73.     HLD_DS18B20;     //控制总线
  74.     SET_DS18B20;     //强制拉高
  75.     return(retVal);
  76. }
  77. /**********************************************************************
  78. functionName: unsigned char readByteDS18B20(void)
  79. description :写DS18B20一个字节
  80. **********************************************************************/
  81. void writeByteDS18B20(unsigned char wb)
  82. {
  83.     unsigned char i;
  84.     unsigned char temp;
  85.     //RLS_DS18B20;         //释放总线
  86.     for (i=0;i<8;i++)
  87.     {
  88.         HLD_DS18B20;  //控制总线
  89.         CLR_DS18B20;  //强制拉低
  90.         delay_nus(1);   //14.92us
  91.         temp=wb>>i;
  92.         if (temp&=0x01)
  93.             SET_DS18B20;    //释放总线
  94.         else
  95.             CLR_DS18B20; //强制拉低
  96.         delay_nus(15);     //30.38us
  97.         SET_DS18B20;  //释放总线
  98.         delay_nus(1);   //2.71us(大于1us就行了)
  99.     }
  100. }
  101. /**********************************************************************
  102. functionName: get_rom(unsigned char* p)
  103. description :读ROM
  104. **********************************************************************/
  105. void get_rom(unsigned char* p)
  106. {
  107.     unsigned char i;
  108.     if (resetDS18B20()==0xff)
  109.     {
  110.         writeByteDS18B20(ds18b20_read_rom);
  111.         for (i=0;i<8;i++)
  112.         {
  113.             *p++ = readByteDS18B20();
  114.         }
  115.     }
  116. }
  117. /**********************************************************************
  118. functionName: void set_ds18b20(char th,char tl,unsigned char config)
  119. description :设定DS18B20模式
  120. **********************************************************************/
  121. void set_ds18b20(char th,char tl,unsigned char config)
  122. {
  123.     if (resetDS18B20()==0xff)
  124.     {
  125.         writeByteDS18B20(ds18b20_skip_rom);
  126.         writeByteDS18B20(ds18b20_write_ram);
  127.         writeByteDS18B20(th);
  128.         writeByteDS18B20(tl);
  129.         writeByteDS18B20(config);
  130.     }
  131. }
  132. /**********************************************************************
  133. functionName: convert_ds18b20(void)
  134. description :写DS18B20一个字节
  135. **********************************************************************/
  136. void convert_ds18b20(void)
  137. {
  138.     if (resetDS18B20()==0xff)
  139.     {
  140.         writeByteDS18B20(ds18b20_skip_rom); //跳过ROM
  141.         writeByteDS18B20(ds18b20_convert_tem); //启动温度转换
  142.     }
  143. }
  144. /**********************************************************************
  145. functionName: unsigned int readTempDS18B20(void)
  146. description :读DS18B20温度   有正负符号标志和两个小数点精度
  147. **********************************************************************/
  148. unsigned int readTempDS18B20(void)
  149. {
  150.     unsigned char tempL,tempH,wm0,wm2;
  151.     unsigned int x;
  152.     if (resetDS18B20()==0)
  153.         return 0;
  154.     writeByteDS18B20(ds18b20_skip_rom); //跳过ROM
  155.     writeByteDS18B20(ds18b20_read_ram); //读数据
  156.     tempL=readByteDS18B20();
  157.     tempH=readByteDS18B20();
  158.     wm0=tempL;
  159.     wm0=wm0>>4;
  160.     tempH=tempH<<4;
  161.     wm2=wm0+tempH; //温度的整数值
  162.     if (wm2&0x80)  //测试符号位
  163.     {
  164.         DS18B20_sign_flag=1;
  165.         wm2=256-wm2;
  166.     }
  167.     else
  168.         DS18B20_sign_flag=0;
  169.     x = wm2*100;   //字符型赋给整形,扩大100倍
  170.     if (tempL&0x08) //提高测试精度
  171.     {
  172.         x=x+50;
  173.     }
  174.     if (tempL&0x04)
  175.     {
  176.         x=x+25;
  177.     }
  178.     if (tempL&0x02)
  179.     {
  180.         x=x+12;
  181.     }
  182.     if (tempL&0x01)
  183.     {
  184.         x=x+6;
  185.     }
  186.     convert_ds18b20();//启动温度转换
  187.     return(x);
  188. }



工程文件

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
hotpower 发表于 2011-11-21 14:49 | 显示全部楼层
这个是烈火先做的,自己看明白就好。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

18

主题

298

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部