標(biāo)題: DS18B20溫度測量顯示 [打印本頁]

作者: hgn1022    時間: 2017-2-27 10:24
標(biāo)題: DS18B20溫度測量顯示

/*
版本IAR3.4.2
LCD12864
開機顯示畫面: 動態(tài)顯示時間
按下K1鍵,顯示兩路溫度值,并顯示熱量
再按K1鍵,返回開機畫面
K2鍵為加一鍵
K3鍵為移位鍵
K4鍵畫面靜止
當(dāng)需要修改數(shù)據(jù)時,按下K4鍵,再按K3鍵移位,K2鍵循環(huán)加(0~9)
*/
#include<msp430x14x.h>
#define uchar unsigned char
//************************************************************
//18B20
#define DS18B20_DIR  P1DIR
#define DS18B20_IN  P1IN
#define DS18B20_OUT  P1OUT
#define NOP_1uS  _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP();
#define DS18B20_DQ  BIT5  //定義DS18B20的接口     P6.0
uchar keybuf;
signed char shift;
signed char up;
signed char down;
void Init_Port(void);
//定義LCD
#define CLR_RS P4OUT&=~BIT0
#define SET_RS P4OUT|=BIT0
#define CLR_RW P4OUT&=~BIT1
#define SET_RW P4OUT|=BIT1
#define CLR_EN P4OUT&=~BIT2
#define SET_EN P4OUT|=BIT2
#define CLR_CS1 P5OUT&=~BIT3
#define SET_CS1 P5OUT|=BIT3
#define CLR_CS2 P5OUT&=~BIT4
#define SET_CS2 P5OUT|=BIT4
#define BACK_ON  P6OUT|=BIT0
#define BACK_OFF P6OUT&=~BIT0
uchar state;
//LCD
void wcode(uchar code,uchar cs1,uchar cs2);
void delay(void);
void wdata(uchar data,uchar cs1,uchar cs2);
void set_startline(uchar i);
void set_adr(uchar x,uchar y);
void dison_off(uchar o);
void clr(void);
void full(void);
void han(uchar const *p,uchar lline,uchar x);          //lline ;行  x;列
void shu(uchar const *p,uchar lline,uchar x);
void busy(void);

// DS18B20 ROM命令宏定義
#define ReadRom   0x33
#define MatchRom  0x55
#define SearchRom  0xf0
#define AlertSearch  0xec
#define CopyScratchpad  0x48
#define SkipRom   0xcc
#define ReadPower  0xb4

// DS18B20 功能命令宏定義
#define ConvertTemperature 0x44
#define ReadScratchpad  0xbe
#define WriteScratchpad  0x4e
#define RecallE   0xb8

//定義DS1302的控制字節(jié)  最高有效位必須是1  最低位是 0:表示要進行寫操作 1:進行讀操作
#define DS1302_SEC_Reg 0x80        
#define DS1302_MIN_Reg 0x82
#define DS1302_HR_Reg 0x84
#define DS1302_DATE_Reg 0x86
#define DS1302_MONTH_Reg 0x88
#define DS1302_DAY_Reg 0x8a
#define DS1302_YEAR_Reg 0x8c
#define DS1302_CONTROL_Reg 0x8e
#define DS1302_CHARGER_Reg 0x90
#define DS1302_CLKBURST_Reg 0xbe

#define CLR_1302SCK P3DIR|=BIT0 ; P3OUT&=~BIT0
#define SET_1302SCK P3DIR|=BIT0 ; P3OUT|=BIT0
#define CLR_1302IO  P3DIR|=BIT1 ; P3OUT&=~BIT1
#define SET_1302IO  P3DIR|=BIT1 ; P3OUT|=BIT1
#define CLR_1302RST P3DIR|=BIT2 ; P3OUT&=~BIT2
#define SET_1302RST P3DIR|=BIT2 ; P3OUT|=BIT2
#define Read_1302IO P3DIR&=~BIT1

#define SMCLK   4000  //(KHz)用于系統(tǒng)延時

uchar sampleflag;
void Tmp_Measure();
unsigned char DS18B20_ReadByte(void);
unsigned char DS18B20_ReadBit(void);
void DS18B20_WriteByte(unsigned char oww_dat);
void DS18B20_WriteBit(unsigned char oww_dat);
unsigned char DS18B20_Init(void);
unsigned char DS18B20_ReadRom(void);
void convert(unsigned int temperature);
void Display(void);
void DelayX10us(unsigned int x10us);
unsigned  int i,j;
unsigned char rom[8][8]={{0x28,0x33,0x28,0x34,0x01,0x00,0x00,0x81},
                         {0x28,0x5d,0xf5,0x33,0x01,0x00,0x00,0x65},
                         {0x28,0x14,0xda,0x15,0x01,0x00,0x00,0x68},
                         };        //只有定義為全局變量才可以讀出正常數(shù)據(jù)
unsigned int a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7;
unsigned int temperature1,temperature2,t1,t2;
unsigned int tmp[8];
float wendu;
unsigned char GetScratchpad[9];
uchar keycode();
//1302
void WhiteOneChar(uchar Date);
uchar ReadOneChar(void);
void DS1302_Init(void);
void W_1302(uchar Addr, uchar Date);
uchar R_1302(uchar Addr);
void Set_1302(uchar *SetTime);
void Get_1302(uchar CurTime[]);
uchar Time[7]={0x00,0x00,0x08,0x00,0x00,0x00,0x00};  //設(shè)置時間初值s,min,h,r,y,n
uchar CurTime[7];
uchar con_flag=0;
unsigned int t1,t2,t3,t4,t5,t6,s,m,h;
void Dis_Time(void);

void shan(void);
uchar flag=0;
uchar const ha[224]=
{
/*--  文字:    --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0xFC,0x04,0x04,0xFC,0x20,0x20,0x50,0x48,0x44,0x43,0x44,0x48,0x58,0x30,0x10,0x00,
0x07,0x01,0x01,0x03,0x00,0x00,0x7F,0x21,0x21,0x21,0x21,0x21,0x7F,0x00,0x00,0x00,

/*--  文字:    --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x00,0x40,0x20,0x10,0x0C,0xCB,0x8A,0x18,0xE8,0x08,0x88,0x08,0x28,0x18,0x08,0x00,
0x00,0x10,0x08,0x04,0x02,0x01,0x40,0x80,0x7F,0x00,0x00,0x01,0x0E,0x04,0x00,0x00,

/*--  文字:    --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x08,0x11,0x06,0xC0,0x00,0x0C,0x04,0xE4,0xA4,0xA5,0x96,0x94,0x94,0x8C,0x04,0x00,
0x04,0xFC,0x03,0x00,0x80,0x84,0x44,0x27,0x04,0x04,0x04,0x17,0x24,0xC4,0x04,0x00,

/*--  文字:   --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x44,0x44,0xFC,0x44,0x44,0x00,0xFE,0x92,0x92,0xFE,0x92,0x92,0x92,0xFE,0x00,0x00,
0x10,0x10,0x0F,0x08,0x48,0x40,0x45,0x44,0x44,0x7F,0x44,0x44,0x44,0x45,0x40,0x00,

/*--  文字:    --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x00,0x00,0x02,0x02,0x02,0x02,0x02,0xFE,0x02,0x02,0x02,0x02,0x02,0x02,0x00,0x00,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00,

/*--  文字:  大  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x20,0x20,0x20,0x20,0x20,0x20,0xA0,0x7F,0xA0,0x20,0x20,0x20,0x20,0x20,0x20,0x00,
0x00,0x80,0x40,0x20,0x10,0x0C,0x03,0x00,0x01,0x06,0x08,0x30,0x60,0xC0,0x40,0x00,

/*--  文字:  學(xué)  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x40,0x30,0x10,0x12,0x5C,0x54,0x50,0x51,0x5E,0xD4,0x50,0x18,0x57,0x32,0x10,0x00,
0x00,0x02,0x02,0x02,0x02,0x02,0x42,0x82,0x7F,0x02,0x02,0x02,0x02,0x02,0x02,0x00,
};
uchar const hz[384]=
{
/*--  文字:  室  --*/
0x00,0x10,0x2C,0x24,0xA4,0x64,0x25,0x26,0x24,0x24,0xA4,0x24,0x34,0x2C,0x04,0x00,
0x40,0x40,0x48,0x49,0x49,0x49,0x49,0x7F,0x49,0x49,0x49,0x4B,0x48,0x40,0x40,0x00,
/*--  文字:  內(nèi)  --*/
0x00,0x00,0xF8,0x08,0x08,0x08,0x88,0x7F,0x48,0x88,0x08,0x08,0x08,0xF8,0x00,0x00,
0x00,0x00,0xFF,0x08,0x04,0x02,0x01,0x00,0x00,0x00,0x01,0x46,0x80,0x7F,0x00,0x00,
/*--  文字:  外  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x80,0x40,0x20,0x58,0x87,0x04,0xC4,0x3C,0x00,0x00,0xFF,0x20,0x40,0x80,0x00,0x00,
0x40,0x20,0x10,0x08,0x05,0x03,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x03,0x00,
/*--  文字:  溫  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x10,0x21,0x86,0x70,0x00,0x7E,0x4A,0x4A,0x4A,0x4A,0x4A,0x7E,0x00,0x00,0x00,0x00,
0x02,0xFE,0x01,0x40,0x7F,0x41,0x41,0x7F,0x41,0x41,0x7F,0x41,0x41,0x7F,0x40,0x00,
/*--  文字:  度  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x00,0x00,0xFC,0x04,0x24,0x24,0xFC,0xA5,0xA6,0xA4,0xFC,0x24,0x24,0x24,0x04,0x00,
0x80,0x60,0x1F,0x80,0x80,0x42,0x46,0x2A,0x12,0x12,0x2A,0x26,0x42,0xC0,0x40,0x00,
/*--  文字:  ℃ --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x00,0x02,0x05,0xE2,0x18,0x04,0x02,0x02,0x02,0x02,0x02,0x02,0x04,0x1E,0x00,0x00,
0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x40,0x40,0x40,0x40,0x40,0x20,0x18,0x00,0x00,
/*--  文字:  熱  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x44,0xC4,0x44,0xFF,0x24,0x04,0x14,0xA4,0x7F,0xC4,0x84,0x7C,0x84,0x00,0x00,0x00,
0xC0,0x62,0x34,0x03,0x14,0x62,0x01,0x08,0x30,0x60,0x01,0x08,0x33,0x64,0x07,0x00,
/*--  文字:  量  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x40,0x40,0x40,0xDF,0x55,0x55,0x55,0xD5,0x55,0x55,0x55,0xDF,0x40,0x40,0x40,0x00,
0x40,0x40,0x40,0x57,0x55,0x55,0x55,0x7F,0x55,0x55,0x55,0x57,0x50,0x40,0x40,0x00,
/*--  文字:  當(dāng)  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x00,0x00,0x40,0x42,0x5C,0x48,0x40,0x40,0x7F,0x40,0x50,0x4E,0x44,0xC0,0x00,0x00,
0x00,0x00,0x20,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x7F,0x00,0x00,

/*--  文字:  前  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x08,0x08,0xE8,0xA8,0xA9,0xAE,0xEA,0x08,0x08,0xC8,0x0C,0x0B,0xEA,0x08,0x08,0x00,
0x00,0x00,0x7F,0x04,0x24,0x44,0x3F,0x00,0x00,0x1F,0x40,0x80,0x7F,0x00,0x00,0x00,

/*--  文字:  時  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x00,0xFC,0x44,0x44,0x44,0xFC,0x10,0x90,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x00,
0x00,0x07,0x04,0x04,0x04,0x07,0x00,0x00,0x03,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,

/*--  文字:  間  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=16x16   --*/
0x00,0xF8,0x01,0x06,0x00,0xF0,0x92,0x92,0x92,0x92,0xF2,0x02,0x02,0xFE,0x00,0x00,
0x00,0xFF,0x00,0x00,0x00,0x07,0x04,0x04,0x04,0x04,0x07,0x40,0x80,0x7F,0x00,0x00,
};
const unsigned char sz[160]=
{/*--  文字:  0  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=8x16   --*/
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,

/*--  文字:  1  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=8x16   --*/
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,

/*--  文字:  2  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=8x16   --*/
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,

/*--  文字:  3  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=8x16   --*/
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,

/*--  文字:  4  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=8x16   --*/
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,

/*--  文字:  5  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=8x16   --*/
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,

/*--  文字:  6  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=8x16   --*/
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,

/*--  文字:  7  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=8x16   --*/
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,

/*--  文字:  8  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=8x16   --*/
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,

/*--  文字:  9  --*/
/*--  宋體12;  此字體下對應(yīng)的點陣為:寬x高=8x16   --*/
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,
};

uchar const kong[16]=
{
/*--  調(diào)入了一幅圖像:這是您新建的圖像  --*/
/*--  寬度x高度=8x16  --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
const unsigned char Symbols[32]=
{
/*--  符號:  ':'和'-' --*/
/*--  對應(yīng)的點陣為:寬x高=5x8   --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00
};
void Timer1_init(void);
uchar n=1;
uchar f=0;
int main( void )
{
  int i;
  
  WDTCTL=WDTPW+WDTHOLD;
  BCSCTL1&=~XT2OFF;        
  do{
   IFG1&=~OFIFG;
   for(i=0xff;i>0;i--);
  }while((IFG1&OFIFG));
  BCSCTL2|=(SELM1+SELS);
  P4DIR=0xff;                              //控制液晶引腳設(shè)成輸出狀態(tài)
  P5DIR=0xff;
  Init_Port();
  Timer1_init();
  _EINT();
  dison_off(0);
  busy();
  dison_off(1);
  full();
  busy();
  clr();
  Set_1302(Time);
while(1)
{
   if(con_flag==1)
     {  
      Tmp_Measure();
      n=0;
      }  
    else if(con_flag==0)
    {
      han(ha,1,16);
      han(ha+32,1,32);
      han(ha+64,1,48);
      han(ha+96,1,64);
      han(ha+128,1,80);
      han(ha+160,1,96);
      han(ha+192,1,112);
      Dis_Time();
      n=1;
      }
    if(keybuf==1)
   {
       keybuf=0;
       clr();
      if(con_flag==1)
        con_flag=0;
        else con_flag=1;
      }
   if(keybuf==2)  //加1鍵
   {
      keybuf=0;
           up++;
          if(up>9)
           {up=0;}
          if(f==1)
            shu(sz+16*up,3,64);
            h=up*10;
          if(f==2)
            shu(sz+16*up,3,72);
            Time[2]=h+up-6;
          if(f==3)
            shu(sz+16*up,3,88);
            m=up*10;
          if(f==4)
            shu(sz+16*up,3,96);
            Time[1]=m+up-6;
          if(f==5)
            shu(sz+16*up,3,112);
            s=up*10;
          if(f==5)
            shu(sz+16*up,3,120);
            Time[0]=s+up-6;
       //shu(sz+16*up,3,64+8*f);  //64,72列顯示時,80列為冒號,88,96列為分,104列為冒號
       Set_1302(Time);  //將修改值賦予計時器初值
    }
   if(keybuf==3)  //移位鍵
   {
       keybuf=0;
       f++;
       if (f==9)
       f=1;
     }
    if(keybuf==4&&n==1)
       {  
         keybuf=0;
         con_flag=2; //既不測溫也不顯示 處于靜止界面
        // shan();
       }
}
//_BIS_SR(LPM0_bits+GIE);
}
void shan(void)
{
  if(flag==1)
  {
  flag=0;
  switch (f)
  {
  case 1:
  shu(kong,3,112);
  shu(kong,3,120);
  //DelayX10us(50000);
  //Dis_Time();
//  DelayX10us(50000);
  break;
case 2:
  shu(kong,3,88);
  shu(kong,3,96);
  //DelayX10us(50000);
// Dis_Time();
  break;
case 3:
  shu(kong,3,64);
  shu(kong,3,72);
// DelayX10us(50000);
// Dis_Time();
  break;  
  }
  }
  }
void Init_Port(void)
{
WDTCTL=WDTPW+WDTHOLD;
P1DIR=0xf0;            //將P1口初始化為輸入方式
P1SEL=0;            //將P1初始化為一般I/O口  
P1IE=0;             //將中斷寄存器清零
P1IES=0;
P1IFG=0;   
P1IE|=BIT0;         //打開管腳的中斷功能
P1IES|=BIT0;        //下降沿使對應(yīng)標(biāo)志置位
P1IE|=BIT1;
P1IES|=BIT1;
P1IE|=BIT2;
P1IES|=BIT2;
P1IE|=BIT3;
P1IES|=BIT3;
_EINT();            //打開中斷
return;
}
void Timer1_init(void)
{
  CCTL0 = CCIE;                             // CCR0 interrupt enabled
  CCR0 =10000;                              //0.5s
  TACTL = ID_3+TASSEL_1 + MC_1;                  // 1/8分頻,ACLK, upmode
}


//*******************************************************
/*LCD顯示---------------------------------------------------*/
//初始化LCD
//**********************************************************************
void busy(void)
{
       P5DIR=0x00;
       P4DIR=0X00;
       SET_CS1;
       //SET_CS2;
       CLR_RS;
       SET_RW;
       CLR_EN;
       delay();
       SET_EN;
       //while((DataIn & 0x80)==0);
        if(state&0X01)
      P4OUT|=BIT3;
else
P4OUT&=~BIT3;
         if(state&0X02)
      P4OUT|=BIT4;
else
P4OUT&=~BIT4;
         if(state&0X40)
      P4OUT|=BIT5;
else
P4OUT&=~BIT5;
      if(state&0X80)
      P4OUT|=BIT6;
else
P4OUT&=~BIT6;

       if(state&0X10)
      P4OUT|=BIT7;
else
P4OUT&=~BIT7;
      
        if(state&0X20)
     P5OUT|=BIT0;
else
P5OUT&=~BIT0;
        if(state&0X40)
      P5OUT|=BIT1;
else
P5OUT&=~BIT1;
        if(state&0X80)
  P5OUT|=BIT2;
else
P5OUT&=~BIT2;   
       delay();
       CLR_EN;
       delay();
       P5DIR=0xff;
       P4DIR=0xff;
}

void wcode(uchar code,uchar cs1,uchar cs2)
    {
       //先片選:CS1,CS2
       //D/I=0
       //R-W=0
       //數(shù)據(jù)
       //E:0-1-0

       if(cs1==1)
       SET_CS1;
       else
       CLR_CS1;
       if(cs2==1)
       SET_CS2;
       else
       CLR_CS2;
       CLR_RS;
       CLR_RW;
  if(code&0X01)
P4OUT|=BIT3;
else
P4OUT&=~BIT3;

  if(code&0x02)
  P4OUT|=BIT4;
else
P4OUT&=~BIT4;
   if(code&0X04)
  P4OUT|=BIT5;
else
P4OUT&=~BIT5;
  if(code&0x08)
  P4OUT|=BIT6;
else
P4OUT&=~BIT6;
   if(code&0X10)
  P4OUT|=BIT7;
else
P4OUT&=~BIT7;
  if(code&0x20)
  P5OUT|=BIT0;
else
P5OUT&=~BIT0;
   if(code&0X40)
P5OUT|=BIT1;
else
P5OUT&=~BIT1;
  if(code&0x80)
P5OUT|=BIT2;
else
P5OUT&=~BIT2;
       CLR_EN;
       delay();
       SET_EN;
       delay();
       CLR_EN;
       }
void wdata(uchar data,uchar cs1,uchar cs2)
     {
       //先片選:CS1,CS2
       //D/I=1
       //R-W=0
       //數(shù)據(jù)
       //E:0-1-0
       if(cs1==1)
       SET_CS1;
       else
       CLR_CS1;
       if(cs2==1)
       SET_CS2;
       else
       CLR_CS2;
       SET_RS;
       CLR_RW;

  if(data&0X01)
P4OUT|=BIT3;
else
P4OUT&=~BIT3;
  if(data&0x02)
  P4OUT|=BIT4;
else
P4OUT&=~BIT4;
   if(data&0X04)
  P4OUT|=BIT5;
else
P4OUT&=~BIT5;
  if(data&0x08)
  P4OUT|=BIT6;
else
P4OUT&=~BIT6;
   if(data&0X10)
P4OUT|=BIT7;
else
P4OUT&=~BIT7;
  if(data&0x20)
P5OUT|=BIT0;
else
P5OUT&=~BIT0;
   if(data&0X40)
P5OUT|=BIT1;
else
P5OUT&=~BIT1;
  if(data&0x80)
P5OUT|=BIT2;
else
P5OUT&=~BIT2;
       CLR_EN;
       delay();
       SET_EN;
       delay();
       CLR_EN;
   }

void set_startline(uchar i)

{
       i=0xc0+i;
       wcode(i,1,1);
}


void set_adr(uchar x,uchar y)
{
       x=x+0xb8;y=y+0x40;
       wcode(x,1,1);
       wcode(y,1,1);
}


void dison_off(uchar o)
{
       o=o+0x3e;
       wcode(o,1,1);
}



void clr(void)
{ uchar loop,n;
      for(n=0;n<8;n++)
        {
           set_adr(n,0);
           set_startline(0);
           loop=0;
           for(loop=0;loop<64;loop++)
               {
                wdata(0,1,0);
                delay();
               }

           set_adr(n,0);
           set_startline(0);
           loop=0;
           for(loop=0;loop<64;loop++)
              {
                 wdata(0,0,1);
                 delay();
               }
          }

}


void full(void)
{ uchar loop,i;
      for(i=0;i<8;i++)
           {
           set_adr(i,0);
           set_startline(0);
           for(loop=0;loop<64;loop++)
               { wdata(0xff,1,0);
               }

           set_adr(i,0);
           set_startline(0);
           for(loop=0;loop<64;loop++)
               { wdata(0xff,0,1);
               }
           }

}

void han(uchar  const *p,uchar lline,uchar x)
          {
          uchar i,y,cs1;
          uchar cs2;
              y=x;
              if(x>63)
               {
                y=x-64;
                cs1=0;
                cs2=1;
               }
               else
                {
                 cs1=1;
                 cs2=0;
                }
                set_adr(2*lline,y);
                set_startline(0);
                for(i=0;i<16;i++)
                {
                 if(y>63)
                  {y=y-64;
                   set_adr(2*lline,y);
                   cs1=0;
                   cs2=1;
                    }
                  wdata(*(p+i),cs1,cs2);
                  y++;
                }
             y=x;
             if(x>63)
               {y=x-64;
                cs1=0;
                cs2=1;
                }
              else
                {
                 cs1=1;
                 cs2=0;
                }
             set_adr(2*lline+1,y);
             set_startline(0);
              for(i=16;i<32;i++)
                {
                 if(y>63)
                 {
                   y=y-64;
                   set_adr(2*lline+1,y);
                   cs1=0;
                   cs2=1;
                  }
                  wdata(*(p+i),cs1,cs2);
                  y++;
                }
}
void shu(uchar  const *p,uchar lline,uchar x)
          {
          uchar i,y,cs1;
          uchar cs2;
              y=x;
              if(x>63)
               {
                y=x-64;
                cs1=0;
                cs2=1;
               }
               else
                {
                 cs1=1;
                 cs2=0;
                }
                set_adr(2*lline,y);
                set_startline(0);
                for(i=0;i<8;i++)
                {
                 if(y>63)
                  {y=y-64;
                   set_adr(2*lline,y);
                   cs1=0;
                   cs2=1;
                    }
                  wdata(*(p+i),cs1,cs2);
                  y++;
                }
             y=x;
             if(x>63)
               {y=x-64;
                cs1=0;
                cs2=1;
                }
              else
                {
                 cs1=1;
                 cs2=0;
                }
             set_adr(2*lline+1,y);
             set_startline(0);
              for(i=8;i<16;i++)
                {
                 if(y>63)
                 {
                   y=y-64;
                   set_adr(2*lline+1,y);
                   cs1=0;
                   cs2=1;
                  }
                  wdata(*(p+i),cs1,cs2);
                  y++;
                }
}
void Tmp_Measure(void)
{
if(sampleflag==1)
{
  sampleflag=0;
  for(i=0;i<3;i++)                        
  {
   DS18B20_Init();                         //復(fù)位
   DS18B20_WriteByte(MatchRom);            //發(fā)匹配ROM命令
   for(j=0;j<8;j++)                        //讀64位ROM碼
    {
     DS18B20_WriteByte(rom[i][j]);         //i:第幾個DS18B20; j:第i個DS18B20的序列號
    }
   DS18B20_WriteByte(ConvertTemperature);  //對一個DS18B20進行溫度轉(zhuǎn)換
   DelayX10us(60000);
   DelayX10us(14000);
  }    //全部轉(zhuǎn)換完畢
for(i=0;i<3;i++)
  {
  DS18B20_Init();               //復(fù)位
  DS18B20_WriteByte(MatchRom); //匹配ROM
   for(j=0;j<8;j++)
   {
  DS18B20_WriteByte(rom[i][j]);
   }
   
  DS18B20_WriteByte(ReadScratchpad);
  GetScratchpad[0]=DS18B20_ReadByte();       //讀溫度到內(nèi)存
  GetScratchpad[1]=DS18B20_ReadByte();
  DS18B20_Init();
  tmp[i]=( (GetScratchpad[1]*256)+GetScratchpad[0]);
  
}
      temperature1=tmp[0];
      temperature2=tmp[2];
      convert(temperature1);            //此處設(shè)置的是對第二個序列號對應(yīng)的DS18B20溫度進行轉(zhuǎn)換顯示
    han(hz,0,0);       ////室內(nèi)溫度
    han(hz+32,0,16);
    han(hz+96,0,32);
    han(hz+128,0,48);
    shu(Symbols,0,64);//:
    shu(Symbols+16,0,88); //.
    han(hz+160,0,112);  //攝氏度
    shu(sz+a2*16,0,72); //溫度
    shu(sz+a3*16,0,80);
    shu(sz+a4*16,0,96);
    shu(sz+a5*16,0,104);
  convert(temperature2);
    han(hz,1,0);        //室外溫度
    han(hz+64,1,16);
    han(hz+96,1,32);
    han(hz+128,1,48);
    shu(Symbols,1,64);//:
    shu(Symbols+16,1,88); //.
    han(hz+160,1,112);  //攝氏度
    shu(sz+a2*16,1,72);  //溫度
    shu(sz+a3*16,1,80);
    shu(sz+a4*16,1,96);
    shu(sz+a5*16,1,104);
    han(hz+192,2,32);   //熱量
    han(hz+224,2,48);
    shu(Symbols,2,64);//:
  }
}
void convert(unsigned int temperature)
{
  float f1,f2,f3;
  int volt1,volt2;
  //volt=(float)(t1);         
  wendu=(temperature*0.0625);         //實際溫度值,例如23.5625攝氏度
  f3=wendu;                           //f3=23.5625
  volt1=(int)(wendu);                 //volt1=23
  f2=(float)(volt1);                  //f2=23.0
  f1=(f3-f2);                         //f1=0.5625
  a1=volt1/100;                       //a1=0
  b1=volt1%100;                       //b1=23
  a2=b1/10;                           //a2=2
  a3=b1%10;                           //a3=3
  volt2=(int)(f1*10000);              //volt2=5625
  a4=volt2/1000;                      //a4=5
  b4=volt2%1000;                      //b4=625
  a5=b4/100;                          //a5=6
  b5=b4%100;                          //b5=25
  a6=b5/10;                           //a6=2
  a7=b5%10;                           //a7=5
}


//ReadRom
/*unsigned char DS18B20_ReadRom(void)
{
  int i,j;
  DS18B20_WriteByte(ReadRom);
  
  for(i=0;i<8;i++)
  {
   for(j=0;j<8;j++)
   {
   rom[i][j]=DS18B20_ReadByte();
   }
  }
}*/
//*******************************************************  
//18B20
unsigned char DS18B20_Init(void)
  {
  unsigned char result;
  DS18B20_DIR |= DS18B20_DQ;       // ow output
  DS18B20_OUT &= ~DS18B20_DQ;      // DS18B20_DQ=0;
  DelayX10us(48);       // Bus master pulling low 480us minimum;
  DS18B20_OUT |= DS18B20_DQ;       // DS18B20_DQ=1;
  DelayX10us(5);        // Resister pull up 15-60us;
  DS18B20_DIR &= ~DS18B20_DQ;      // ow input
  result = DS18B20_IN & DS18B20_DQ;
  DS18B20_DIR |= DS18B20_DQ;       // ow output
  DelayX10us(48);       // End of timeslot total 480us;
  return(result);       // any 1 wire device ?result:=1 no devide; ?result:=0 have device;
}
//Intialization the 1-wire devices;

unsigned char DS18B20_ReadBit(void)
{
  unsigned char result;
  DS18B20_DIR |= DS18B20_DQ;       // ow output
  DS18B20_OUT &= ~DS18B20_DQ;      // DS18B20_DQ=0;
  _NOP();   _NOP();             // Start of timeslot;
  DS18B20_OUT |= DS18B20_DQ;       // DS18B20_DQ=1;
  _NOP();_NOP();_NOP();_NOP(); _NOP();_NOP();
          // Wait from the start;
  DS18B20_DIR &= ~DS18B20_DQ;      // ow input
  result = DS18B20_IN & DS18B20_DQ;
  DS18B20_DIR |= DS18B20_DQ;       // ow output
  return(result);       // return the result of the 1-wire devide;
}//Read a bit on the 1-wire bus;

void DS18B20_WriteBit(unsigned char oww_dat){
  DS18B20_DIR |= DS18B20_DQ;       // ow output
  DS18B20_OUT &= ~DS18B20_DQ;      // DS18B20_DQ=0;
  if (1 == oww_dat)
    DS18B20_OUT |= DS18B20_DQ;     // DS18B20_DQ=1;
  DelayX10us(10);       // Remain the state for 100us;
  DS18B20_OUT |= DS18B20_DQ;       // DS18B20_DQ=1;
}//Write a bit to the 1-wire bus;

unsigned char DS18B20_ReadByte(void)
{
  unsigned char i;
  unsigned  char result=0;
  for(i = 0; i < 8; i++){
    if(DS18B20_ReadBit())
      result |= 0x01 << i;
    DelayX10us(12);     // ??
  }
  return(result);       // return the result of the 1-wire device;
}//Read a byte from the 1-wire bus;

void DS18B20_WriteByte(unsigned char oww_dat){
  unsigned char i,temp;
  for(i = 0; i < 8; i++){
    temp = oww_dat >> i;
    temp &= 0x01;
    DS18B20_WriteBit(temp);
  }
  DelayX10us(7);        // ??
}//Write a byte to the 1-wire bus;

//DS1302
//設(shè)置時鐘顯示初始值
void Set_1302(uchar *SetTime)
{
   uchar i;
   uchar Addr=0x80;
   W_1302(0x8e,0x00);    //將控制寄存器值設(shè)為零,最高位WP=0允許寫
   for(i=0; i<7; i++)    //將7個時間初值寫入對應(yīng)寄存器
   {
   W_1302(Addr,*SetTime); //寫對應(yīng)時鐘寄存器的值
   SetTime++;
   Addr+=2;
   }
   W_1302(0x8e,0x80);   //寫保護,防止干擾影響時間值
   return;
}
//寫一個字節(jié)到1302
void WhiteOneChar(uchar Date)
{  uchar IO;
   uchar i;
   SET_1302RST;        //啟動數(shù)據(jù)傳送
   for(i=0; i<8; i++)  //循環(huán)8次,寫入8位數(shù)據(jù),從低位到高位
   {
   IO=Date&0x01;        //將數(shù)據(jù)值賦給時鐘數(shù)據(jù)線
   if(IO==1) {SET_1302IO;}
   else      {CLR_1302IO;}
   SET_1302SCK;
    //CLR_1302SCK;
   _NOP();
   _NOP();
   CLR_1302SCK;
  // SET_1302SCK;         ?//在時鐘線的上升沿寫入1位數(shù)據(jù)
   Date>>=1;
   }
}
//讀出1302的數(shù)據(jù)
uchar ReadOneChar(void)
{
   uchar i;
   uchar Date=0;
   SET_1302RST;        //啟動數(shù)據(jù)傳送
   for(i=0; i<8; i++)  //讀出8位數(shù)據(jù),從低位到高位
   {
    Date>>=1;
    Read_1302IO;      //P3口輸入之前置1
    _NOP();
    _NOP();
   //if((P1IN&0x08)==0x08) Date|=0x80;
   if((P3IN&0x02)==0x02) Date|=0x80;
    _NOP();
    _NOP();
   SET_1302SCK;
   _NOP();
   _NOP();
   CLR_1302SCK;      //在時鐘的下降沿讀出DS1302的一位數(shù)據(jù)
   }
   return(Date);
}

void W_1302(uchar Addr, uchar Date)
{
   //CLR_1302RST;
   CLR_1302RST;
   CLR_1302SCK;
   SET_1302RST;
   WhiteOneChar(Addr);
   WhiteOneChar(Date);
   //SET_1302SCK;
   SET_1302SCK;
   CLR_1302RST;

}

uchar R_1302(uchar Addr)
{
   uchar Date;
   //CLR_1302RST;
   CLR_1302RST;
   CLR_1302SCK;
   _NOP();
   SET_1302RST;
   _NOP();
   _NOP();
   WhiteOneChar(Addr);
   Date=ReadOneChar();
   //SET_1302SCK;
   CLR_1302RST;
   return(Date);
}
void Get_1302(uchar CurTime[])
{
   uchar i,l,h;
   uchar Addr=0x81;
   for(i=0; i<7; i++)
   {
   CurTime[i]=R_1302(Addr);   //將時間數(shù)據(jù)(8421BCD碼)從1302對應(yīng)的寄存器中讀出
   l=CurTime[i]&0x0f;         //l存放time的低4位,即個位
   h=(CurTime[i]>>4)&0x0f;    //h存放time的高4位,即十位
   CurTime[i]=h*10+l;         //時間數(shù)據(jù)以十進制形式放進CurTime[i]中
   Addr+=2;                  //地址移動,確保地址為奇數(shù)
    }
}
//顯示1302計時時間 秒 分 時
void Dis_Time(void)
{
  uchar sec,min,hour;
  Get_1302(CurTime);
  sec=CurTime[0];
  t1=sec/10;
  t2=sec%10;
  shu(sz+16*t1,3,112);
  shu(sz+16*t2,3,120);
  min=CurTime[1];
  t3=min/10;
  t4=min%10;
  shu(sz+16*t3,3,88);
  shu(sz+16*t4,3,96);
  shu(Symbols,3,104);//:
  hour=CurTime[2];
  t5=hour/10;
  t6=hour%10;
  shu(sz+16*t5,3,64);
  shu(sz+16*t6,3,72);
  shu(Symbols,3,80);//:
}
//延時函數(shù)
void DelayX10us(unsigned int n)
{
while(n--)
{
  NOP_1uS;NOP_1uS;NOP_1uS;
  NOP_1uS;NOP_1uS;NOP_1uS;
  NOP_1uS;
}
}
void delay(void)
{
_NOP();
_NOP();
//_NOP();
}
void DELAY(unsigned long n)
     {
     unsigned long i;
           for(i=n;i>0;i--);
     }

#pragma vector=PORT1_VECTOR
__interrupt void port1 (void)
{

/* P1IFG=0x00;
        DELAY(2000);
        if((P1IN&0x0f)==0x0f)
         {return;}
       else
         {keybuf=keycode();}
         while((P1IN&0x0f)!=0x0f);*/
DELAY(2000);
int nP10,nP11,nP12,nP13;
nP10=P1IN&BIT0;
if (nP10==0) keybuf=1;
nP11=(P1IN&BIT1)>>1;
if (nP11==0) keybuf=2;
nP12=(P1IN&BIT2)>>2;
if (nP12==0) keybuf=3;
nP13=(P1IN&BIT3)>>3;
if (nP13==0) keybuf=4;
  if(P1IFG&BIT0)
  {
   P1IFG&=~(BIT0);    //清除中斷標(biāo)致位
  }
  if(P1IFG&BIT1)
  {
   P1IFG&=~(BIT1);   
  }
  if(P1IFG&BIT2)
  {
   P1IFG&=~(BIT2);
  }
   if(P1IFG&BIT3)
  {
   P1IFG&=~(BIT3);
  }
}
//Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A(void)
{
      sampleflag=1;
      flag=1;
}





歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1