FL2440溫度感應器實驗

來源:互聯網
上載者:User

#define GPGCON (*(volatile unsigned *)0x56000060)     //18b20寄存器設定
#define GPGDAT (*(volatile unsigned *)0x56000064)
#define GPGUP (*(volatile unsigned *)0x56000068)
 
#define UFCON0     (*(volatile unsigned *)0x50000008)   //串口寄存器設定
#define UMCON0     (*(volatile unsigned *)0x5000000c)
#define ULCON0     (*(volatile unsigned *)0x50000000) 
#define UCON0      (*(volatile unsigned *)0x50000004)
#define UBRDIV0    (*(volatile unsigned *)0x50000028)
#define UTRSTAT0   (*(volatile unsigned *)0x50000010)
#define UTXH0   (*(volatile unsigned*)0x50000020)
#define URXH0   (*(volatile unsigned*)0x50000024)

unsigned char wd[4]; 
unsigned int sdata;//測量到的溫度的整數部分
unsigned char xiaoshu1;//小數第一位
unsigned char xiaoshu2;//小數第二位
unsigned char xiaoshu;//兩位小數

void zh(void);
void Delay(unsigned int x) ;
void uart(void);
void DS18B20PRO(void);
void dmsec (unsigned int t);
void tmreset (void);
unsigned char tmrbit (void);
unsigned char tmrbyte (void) ;
void tmwbyte (unsigned char dat);
void tmstart (void) ;
void tmrtemp (void) ;

int Main(void)                       //不停迴圈檢測溫度值,並轉換為asc||碼,通過串口傳出來

 //  GPGUP&= 0xffffff1f;
    while(1)
     { 
          DS18B20PRO();     
        //  sdata=39.20;  
          zh();
          uart();
          Delay(30);                    
     }
  
  return(0);
}

void dmsec (unsigned int t)       //精確延時函數 
 {  
 unsigned int i;
    unsigned int j;
    j=1*t;
    for(i = 0; i < j; i++);
 }
        
void tmreset (void)       //18b20初始化
{  
 unsigned int i;                           
   
 GPGCON&=0xfffffffc;    //設定寄存器對18b20進行寫操作
 GPGCON|=0x01;
 
 GPGDAT|=0x01;
    dmsec(100);

 GPGDAT&=0xfffe;    
 dmsec(600);
 
        
 GPGDAT|=0x01;
 
 dmsec(100);
 GPGCON&=0xfffffffc;     //設定寄存器對18b20進行讀操作
 i=GPGDAT;
 
}            

 

unsigned char tmrbyte (void)        //讀一個位元組函數
{  
     unsigned int j;             
  unsigned char i,u=0;     
       
  for (i=1;i<=8;i++)     
  {
    GPGCON&=0xfffffffc;
   GPGCON|=0x01;            //GPG0設為輸出
         
    GPGDAT&=0xfffe; 
   // Delay(120);
   u >>= 1;
  // GPGDAT|=0x01;
  
    GPGCON&=0xfffffffc;     //設為輸入口
    j=GPGDAT;
    if(j&0x01)  u |= 0x80;
  
    dmsec(46);
    GPGDAT|=0x01; 
  }       
  return (u);      
 
}
        
void tmwbyte (unsigned char dat)     //寫一個位元組函數
{                      
  unsigned char j;     
 
  GPGCON&=0xfffffffc;
  GPGCON|=0x01;      
  for (j=1;j<=8;j++)     
  {
   GPGDAT&=0xfffe;
     dmsec(1);     
     GPGDAT|= (dat & 0x01); 
     dmsec(47);
     GPGDAT|=0x01;  
     dat = dat >> 1;     
 }       
}

 
void tmstart (void)         //發送ds1820 開始轉換
  {                         
  tmreset();             //複位   
  dmsec(120);            //延時   
  tmwbyte(0xcc);         //跳過序號命令  
  tmwbyte(0x44);         //發轉換命令 44H,
  }        

 

void tmrtemp (void)       //讀取溫度
{                         
  unsigned char a,b;
  tmreset ();      //複位    
  dmsec (2000);     //延時    
  tmwbyte (0xcc);  //跳過序號命令  
  tmwbyte (0xbe);  //發送讀取命令    
  a = tmrbyte ();  //讀取低位溫度   
  b = tmrbyte ();  //讀取高位溫度          
  
  sdata = a/16+b*16;      //整數部分
  
  xiaoshu1 = (a&0x0f)*10/16;    //小數第一位
  xiaoshu2 = (a&0x0f)*100/16%10;//小數第二位
  xiaoshu=xiaoshu1*10+xiaoshu2; //小數兩位
}

void zh()//將溫度值轉換為ascii值
{
 unsigned int wdata;
  wdata=sdata;
  wd[0]=wdata / 10+0x30;
//  if(wd[0]>0x33)
//  while(1);
  wd[1]=wdata % 10+0x30;
  wd[2]=xiaoshu / 10+0x30;
  wd[3]=xiaoshu % 10+0x30;
}

void DS18B20PRO(void)        
{   
  tmstart();  // uart(10);    
  dmsec(5);  //如果是不斷地讀取的話可以不延時 //
  tmrtemp();  //讀取溫度,執行完畢溫度將存於TMP中 //
}          

      
void uart()               //串口函數用於發送溫度值

 
    UFCON0 = 0x0;
    UMCON0 = 0x0;
    ULCON0 = 0x3; 
    UCON0  = 0x245; 
    UBRDIV0=( (int)(52000000/16/115200+0.5) -1 );   //115200
   
     while(!(UTRSTAT0 & 0x2));
  UTXH0='/n';
  while(!(UTRSTAT0 & 0x2));           
  UTXH0=wd[0];
  while(!(UTRSTAT0 & 0x2));
  UTXH0=wd[1];
  while(!(UTRSTAT0 & 0x2));
  UTXH0=46;
  while(!(UTRSTAT0 & 0x2));
  UTXH0=wd[2];
  while(!(UTRSTAT0 & 0x2));
  UTXH0=wd[3];
  while(!(UTRSTAT0 & 0x2));
  UTXH0=39;
  while(!(UTRSTAT0 & 0x2));
  UTXH0=67;
}

void Delay(unsigned int x)       
{
  unsigned int i,j,k;
  for(i=0;i<=x;i++)
    for(j=0;j<0xff;j++)
      for(k=0;k<0xff;k++);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.