又郁闷了很久,后来按datasheet和示波器现实的调了下,总算是能显示了,先贴代码,以后再说从中学到的,要注意的吧。

main.c

   1: //  [4/8/2011 uty]

   2: #include <reg51.h> 

   3: #include <intrins.h>

   4: #include "time.h"

   5: #include "18b20.h"

   6: //-----------------------------------------------------------------------------//

   7: //此表为 LED 的字模, 共阴数码管 0-9  - 

   8: unsigned char code   Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};  //段码控制

   9: //此表为8个数码管位选控制, 共阴数码管 1-8个  - 

  10: unsigned char code   dispbit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF,0x7F};    //位选控制   查表的方法控制

  11: //-----------------------------------------------------------------------------//

  12: void delay(unsigned int i)

  13: {

  14:     char j;

  15:     for(i; i > 0; i--)

  16:         for(j = 200; j > 0; j--);

  17: }

  18: //-----------------------------------------------------------------------------//

  19: void InterruptInit()

  20: {

  21:     TMOD|= 0x11;

  22:     TH1 = 0xD8;    //10

  23:     TL1 = 0xF0;    

  24:     IE = 0x8A;    

  25:     TR1  = 1;

  26: }

  27: //-----------------------------------------------------------------------------//

  28: void DisplayTemperature (unsigned int Temperature)

  29: {

  30:     unsigned char temp = 0;

  31:     unsigned int NegativeTemperature = 0;

  32:  

  33:     // 4位数最前面的0不显示,bFront控制是否是最前面的0

  34:     unsigned char bFront = 1;

  35:  

  36:     temp = Temperature % 10000 / 1000;

  37:  

  38:     if (0 != temp)

  39:     {

  40:         // negative number

  41:  

  42:         NegativeTemperature = 0x1000 - Temperature;

  43:  

  44:         temp = NegativeTemperature % 10000 / 1000;

  45:         if (0 != temp || !bFront)

  46:         {

  47:             bFront = 0;

  48:             P3 = ~dispbit[0];

  49:             P2 = ~Disp_Tab[temp];

  50:             STC89_12M_DelayMs(5);

  51:         }

  52:         else

  53:         {

  54:             // display '-'

  55:             if (0 != (NegativeTemperature % 1000 / 100))

  56:             {

  57:                 P3 = ~dispbit[0];

  58:                 P2 = ~Disp_Tab[10]; //'-'

  59:                 STC89_12M_DelayMs(5);

  60:             }

  61:         }

  62:  

  63:         temp = NegativeTemperature % 1000 / 100;

  64:         if (0 != temp || !bFront)

  65:         {

  66:             bFront = 0;

  67:             P3 = ~dispbit[1];

  68:             P2 = ~Disp_Tab[temp];

  69:             STC89_12M_DelayMs(5);

  70:         }

  71:         else

  72:         {

  73:             // display '-'

  74:             if (0 != (NegativeTemperature % 100 / 10))

  75:             {

  76:                 P3 = ~dispbit[1];

  77:                 P2 = ~Disp_Tab[10]; //'-'

  78:                 STC89_12M_DelayMs(5);

  79:             }

  80:         }

  81:  

  82:  

  83:         temp = NegativeTemperature % 100 / 10;

  84:         if (0 != temp || !bFront)

  85:         {

  86:             bFront = 0;

  87:             P3 = ~dispbit[2];

  88:             P2 = ~Disp_Tab[temp];

  89:             STC89_12M_DelayMs(5);

  90:         }

  91:         else

  92:         {

  93:             // display '-'

  94:             if (0 != (NegativeTemperature % 10))

  95:             {

  96:                 P3 = ~dispbit[2];

  97:                 P2 = ~Disp_Tab[10]; //'-'

  98:                 STC89_12M_DelayMs(5);

  99:             }

 100:         }

 101:  

 102:         temp = NegativeTemperature % 10;

 103:         P3 = ~dispbit[3];

 104:         P2 = ~Disp_Tab[temp];

 105:         STC89_12M_DelayMs(5);

 106:  

 107:     }

 108:     else

 109:     {

 110:         temp = Temperature % 10000 / 1000;

 111:         if (0 != temp || !bFront)

 112:         {

 113:             bFront = 0;

 114:             P3 = ~dispbit[0];

 115:             P2 = ~Disp_Tab[temp];

 116:             STC89_12M_DelayMs(5);

 117:         }

 118:  

 119:         temp = Temperature % 1000 / 100;

 120:         if (0 != temp || !bFront)

 121:         {

 122:             bFront = 0;

 123:             P3 = ~dispbit[1];

 124:             P2 = ~Disp_Tab[temp];

 125:             STC89_12M_DelayMs(5);

 126:         }

 127:  

 128:  

 129:         temp = Temperature % 100 / 10;

 130:         if (0 != temp || !bFront)

 131:         {

 132:             bFront = 0;

 133:             P3 = ~dispbit[2];

 134:             P2 = ~Disp_Tab[temp];

 135:             STC89_12M_DelayMs(5);

 136:         }

 137:  

 138:         // 如果温度是0  最后一位还是要显示

 139:         temp = Temperature % 10;

 140:         P3 = ~dispbit[3];

 141:         P2 = ~Disp_Tab[temp];

 142:         STC89_12M_DelayMs(5);

 143:     }    

 144: }

 145: //-----------------------------------------------------------------------------//

 146: void main (void)

 147: {

 148:     unsigned int idata nCounter = 0;

 149:     unsigned int idata nTemperature = 0;

 150:  

 151:     nCounter = 10000;

 152:  

 153:     while (1)

 154:     {      

 155:         if (nCounter >= 300)

 156:         {

 157:             nTemperature = GetTemperature ();

 158:             nCounter = 0;

 159:         }

 160:  

 161:         DisplayTemperature(nTemperature);

 162:  

 163:         nCounter++;                                                                                                                                   ;

 164:     }

 165: }

 166: //-----------------------------------------------------------------------------//

 

ds18b20.c

   1: //  [4/29/2011 uty]

   2: // 18b20 driver

   3: #include <reg52.H>

   4: #include <intrins.h>

   5: #include "time.h"

   6: #include "18b20.h"

   7: //-----------------------------------------------------------------------------//

   8: sbit    D18B20=P1^7;

   9: //-----------------------------------------------------------------------------//

  10: unsigned char code CrcTable [256]={

  11: 0,  94, 188,  226,  97,  63,  221,  131,  194,  156,  126,  32,  163,  253,  31,  65,

  12: 157,  195,  33,  127,  252,  162,  64,  30,  95,  1,  227,  189,  62,  96,  130,  220,

  13: 35,  125,  159,  193,  66,  28,  254,  160,  225,  191,  93,  3,  128,  222,  60,  98,

  14: 190,  224,  2,  92,  223,  129,  99,  61,  124,  34,  192,  158,  29,  67,  161,  255,

  15: 70,  24,  250,  164,  39,  121,  155,  197,  132,  218,  56,  102,  229,  187,  89,  7,

  16: 219,  133, 103,  57,  186,  228,  6,  88,  25,  71,  165,  251,  120,  38,  196,  154,

  17: 101,  59, 217,  135,  4,  90,  184,  230,  167,  249,  27,  69,  198,  152,  122,  36,

  18: 248,  166, 68,  26,  153,  199,  37,  123,  58,  100,  134,  216,  91,  5,  231,  185,

  19: 140,  210, 48,  110,  237,  179,  81,  15,  78,  16,  242,  172,  47,  113,  147,  205,

  20: 17,  79,  173,  243,  112,  46,  204,  146,  211,  141,  111,  49,  178,  236,  14,  80,

  21: 175,  241, 19,  77,  206,  144,  114,  44,  109,  51,  209,  143,  12,  82,  176,  238,

  22: 50,  108,  142,  208,  83,  13,  239,  177,  240,  174,  76,  18,  145,  207,  45,  115,

  23: 202,  148, 118,  40,  171,  245,  23,  73,  8,  86,  180,  234,  105,  55,  213, 139,

  24: 87,  9,  235,  181,  54,  104,  138,  212,  149,  203,  41,  119,  244,  170,  72,  22,

  25: 233,  183,  85,  11,  136,  214,  52,  106,  43,  117,  151,  201,  74,  20,  246,  168,

  26: 116,  42,  200,  150,  21,  75,  169,  247,  182,  232,  10,  84,  215,  137,  107,  53}; 

  27: //-----------------------------------------------------------------------------//

  28: #define Delay1us _nop_

  29: //-----------------------------------------------------------------------------//

  30: void Delay480us ()

  31: {

  32:     char i = 0;

  33:  

  34:     // 504 us

  35:     for (i = 0; i < 12; i++)

  36:     {

  37:         STC89_12M_Delay42us();

  38:     }    

  39: }

  40: //-----------------------------------------------------------------------------//

  41: void Delay240us ()

  42: {

  43:     char i = 0;

  44:  

  45:     // 462 us

  46:     for (i = 0; i < 5; i++)

  47:     {

  48:         STC89_12M_Delay42us();

  49:     }

  50:  

  51:     STC89_12M_Delay10us();

  52:     STC89_12M_Delay10us();

  53:     STC89_12M_Delay10us();

  54: }

  55: //-----------------------------------------------------------------------------//

  56: void Delay60us ()

  57: {

  58:     STC89_12M_Delay10us();

  59:     STC89_12M_Delay10us();

  60:     STC89_12M_Delay10us();

  61:     STC89_12M_Delay10us();

  62:     STC89_12M_Delay10us();

  63:     STC89_12M_Delay10us();

  64: }

  65: //-----------------------------------------------------------------------------//

  66: void Delay45us ()

  67: {

  68:     STC89_12M_Delay10us();

  69:     STC89_12M_Delay10us();

  70:     STC89_12M_Delay10us();

  71:     STC89_12M_Delay10us();

  72:  

  73:     Delay1us();

  74:     Delay1us();

  75:     Delay1us();

  76:     Delay1us();

  77:     Delay1us();

  78: }

  79: //-----------------------------------------------------------------------------//

  80: unsigned char reset(void)

  81: {

  82:     unsigned char ch = 0;

  83:  

  84:     D18B20 = 1;

  85:     Delay1us();

  86:     D18B20 = 0;  // 输入,外部上拉,等待18b20拉低

  87:     Delay480us();

  88:     D18B20 = 1;  // 输入,外部上拉,等待18b20拉低

  89:  

  90:     Delay60us();

  91:  

  92:     ch = D18B20;

  93:  

  94:     Delay480us();

  95:  

  96:     D18B20 = 1;

  97:  

  98:     if (1 == ch)

  99:     {

 100:         return 0;

 101:     }

 102:     else

 103:     {

 104:         return 1;

 105:     }

 106: }

 107: //-----------------------------------------------------------------------------//

 108: void write (unsigned char ch)

 109: {

 110:     unsigned char i = 0;

 111:  

 112:     for (i = 0;i < 8; i++)

 113:     {

 114:         D18B20 = 0;

 115:         Delay1us();

 116:         Delay1us();

 117:         Delay1us();

 118:         Delay1us();

 119:         Delay1us();

 120:         Delay1us();

 121:         Delay1us();

 122:         Delay1us();

 123:         Delay1us();

 124:         Delay1us();

 125:         D18B20 = (ch & 0x01);

 126:         Delay45us();

 127:         Delay45us();

 128:         D18B20 = 1;

 129:         ch >>= 1;

 130:     }

 131: }

 132: //-----------------------------------------------------------------------------//

 133: unsigned char read (void)

 134: {

 135:     unsigned char i = 0;

 136:     unsigned char u = 0;

 137:  

 138:     for (i = 0; i < 8; i++)

 139:     {

 140:         u >>= 1;

 141:  

 142:         D18B20 = 1;

 143:         Delay1us();

 144:         Delay1us();

 145:         Delay1us();

 146:         D18B20 = 0;

 147:         Delay1us();

 148:         Delay1us();

 149:         D18B20 = 1;

 150:         Delay1us();

 151:         Delay1us();

 152:         Delay1us();

 153:         Delay1us();

 154:         if (1 == D18B20)

 155:         {

 156:             u |= 0x80;

 157:         }

 158:  

 159:  

 160:         D18B20 = 1;

 161:         Delay60us();    

 162:     }

 163:  

 164:     return u;

 165: }

 166: //-----------------------------------------------------------------------------//

 167: void read_bytes (unsigned char* buffer, unsigned char len)

 168: {

 169:     unsigned char i = 0;

 170:  

 171:     for(i = 0; i < len; i++)

 172:     {

 173:         buffer[i] = read();

 174:     }

 175: }

 176: //-----------------------------------------------------------------------------//

 177: unsigned char CRC (unsigned char* buff, unsigned char j)

 178: {

 179:        unsigned char idata i,crc_data=0;

 180:       for(i=0;i<j;i++)  //查表校验

 181:         crc_data = CrcTable[crc_data^buff[i]];

 182:     return (crc_data);

 183: }

 184: //-----------------------------------------------------------------------------//

 185: unsigned int

 186: ReadTemperature (void)

 187: {

 188:     unsigned char ScratchpadBuffer[9] = {0};

 189:     unsigned int nTemperature = 0;

 190:  

 191:     read_bytes(ScratchpadBuffer, 9);

 192:     if (0 == CRC(ScratchpadBuffer, 9)) //校验正确

 193:     {

 194:         nTemperature = ScratchpadBuffer[1] * 0x100 + ScratchpadBuffer[0];

 195:         //nTemperature *= 0.625;

 196:         nTemperature /= 16;

 197:     }

 198:  

 199:     return nTemperature;

 200: }

 201: //-----------------------------------------------------------------------------//

 202: unsigned int

 203: GetTemperature (void)

 204: {

 205:     unsigned int nTemperature = 0;

 206:     // 先取温度,然后再去转换温度,新转换的数据下次再取

 207:     reset ();

 208:     write(0xcc);   //skip rom

 209:     write(0xbe);   //read Temperature

 210:     nTemperature = ReadTemperature();

 211:  

 212:     reset ();

 213:     write(0xcc);   //skip rom

 214:     write(0x44);   //Temperature convert

 215:  

 216:     return nTemperature;

 217: }

 218:  

 219: /*************************************

 220:  [ t1 (10ms)中断] 中断

 221: *************************************/

 222: // void T1zd(void) interrupt 3 

 223: // {

 224: //    TH1 = 0xD8;    //10

 225: //    TL1 = 0xF0;

 226: //    TIM++;

 227: // 

 228: // }

 229:  

 230: //-----------------------------------------------------------------------------//

 231:  

 232: /************************************************************

 233: *Function:内部配置

 234: *parameter:

 235: *Return:

 236: *Modify:

 237: *************************************************************/

 238: // void Config18b20 (void)  //重新配置报警限定值和分辨率

 239: // {

 240: //      reset();

 241: //      write(0xcc);  //skip rom

 242: //      write(0x4e);  //write scratchpad

 243: //      write(0x19);  //上限

 244: //      write(0x1a);  //下限

 245: //      write(0x7f);     //set 12 bit (0.125)

 246: // 

 247: //      reset();

 248: //      write(0xcc);  //skip rom

 249: //      write(0x48);  //保存设定值

 250: //      Init18b20();

 251: //      write(0xcc);  //skip rom

 252: //      write(0xb8);  //回调设定值

 253: // }

 254:  

 255: /************************************************************

 256: *Function:读18B20ID

 257: *parameter:

 258: *Return:

 259: *Modify:

 260: *************************************************************/

 261: // void ReadID (void)//读取器件 id

 262: // {

 263: //     reset();

 264: //     write(0x33);  //read rom

 265: //     read_bytes(8);

 266: // }

 

time.c

   1: //  [4/19/2011 uty]

   2: #include <intrins.h>

   3: //-----------------------------------------------------------------------------//

   4: void

   5: STC89_12M_Delay1ms (void)

   6: {

   7:     char i,j;

   8:     for(i = 110; i > 0; i--)

   9:         for(j = 200; j > 0; j--);

  10: }

  11: //-----------------------------------------------------------------------------//

  12: //

  13: // max 255 ms

  14: //

  15: void

  16: STC89_12M_DelayMs (

  17:     unsigned char i

  18:     )

  19: {

  20:     while (i--)

  21:     {

  22:         STC89_12M_Delay1ms();

  23:     }

  24: }

  25: //-----------------------------------------------------------------------------//

  26: void STC89_12M_Delay10us () 

  27: {

  28:     // LCALL指令(2 μs)

  29:  

  30:     _nop_( );

  31:     _nop_( );

  32:     _nop_( );

  33:     _nop_( );

  34:     _nop_( );

  35:     _nop_( );

  36:  

  37:     // RET指令(2 μs)

  38: }

  39: //-----------------------------------------------------------------------------//

  40: void STC89_12M_Delay42us ()

  41: {

  42:     STC89_12M_Delay10us();

  43:     STC89_12M_Delay10us();

  44:     STC89_12M_Delay10us();

  45:     STC89_12M_Delay10us();

  46: }

  47: //-----------------------------------------------------------------------------//

 

再上张图

照片-0027_1


评论

该日志第一篇评论

发表评论

评论也有版权!