⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 +

📁 基于msp430和1286的红外控制的万年历
💻
字号:
 //程序中精确演示会使用到定时器A
#include <msp430x14x.h>
#include "ds18b20.h"
typedef unsigned char uchar;
typedef unsigned int  uint;
 unsigned char temp_temperture[4];



void delayus(unsigned int time)
{
    while(time--);
       
}
 

/*******************************************
函数名称:DelayNus
功    能:实现N个微秒的延时
参    数:n--延时长度
返回值  :无
说明    :定时器A的计数时钟是1MHz,CPU主频8MHz
          所以通过定时器延时能够得到极为精确的
          us级延时
********************************************/
void DelayNus(uint n)
{
    CCR0 = n;
    TACTL |= MC_1;           //增计数到CCR0
    while(!(TACTL & BIT0));   //等待
    TACTL &= ~MC_1;          //停止计数
    TACTL &= ~BIT0;          //清除中断标志
}

/*******************************************
函数名称:Init_18B20
功    能:对DS18B20进行复位操作
参    数:无
返回值  :初始化状态标志:1--失败,0--成功
********************************************/
uchar Init_18B20(void)
{
    uchar Error;
   
     _DINT();
    DQ0;
    // DelayNus(500);
     delayus(64);//500us
    DQ1;
  // DelayNus(55);
      delayus(5);//57.5us
 DQ_IN;
 
    _NOP();
    if(DQ)      
    {
        Error = 1;          //初始化失败
      DQ_OUT;
 
    }
    else
    {
        Error = 0;          //初始化成功
       DQ_OUT;
 
        DQ1;
    }
    _EINT();
    
   // DelayNus(400);
      delayus(51);//400us
    return Error;
}
/*******************************************
函数名称:Write_18B20
功    能:向DS18B20写入一个字节的数据
参    数:wdata--写入的数据
返回值  :无
********************************************/
void Write_18B20(uchar wdata)
{
    uchar i;
    
    _DINT();
    for(i = 0; i < 8;i++)
    {
        DQ0;
        // DelayNus(6);            //延时6us
         _NOP();_NOP();_NOP();_NOP();//6US
        if(wdata & 0X01)    DQ1;
        else                DQ0;
        wdata >>= 1;
        // DelayNus(50);           //延时50us
         delayus(4);//50us
        DQ1;
        //DelayNus(10);           //延时10us
         _NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();
    }
    _EINT();
}
/*******************************************
函数名称:Read_18B20
功    能:从DS18B20读取一个字节的数据
参    数:无
返回值  :读出的一个字节数据
********************************************/
uchar Read_18B20(void)
{
    uchar i;
    uchar temp = 0;
    
    _DINT();
    for(i = 0;i < 8;i++)
    {
        temp >>= 1;
        DQ0;
       // DelayNus(6);            //延时6us
        _NOP();_NOP();_NOP();_NOP();_NOP();//6,25US
        DQ1;
       // DelayNus(8);            //延时9us
        _NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();//8.75US
        DQ_IN;
    
        _NOP();
        if(DQ)   temp |= 0x80;
      //  DelayNus(45);           //延时45us
          delayus(4);//50us
        DQ_OUT;
  
        DQ1;
       // DelayNus(10);           //延时10us
         _NOP();_NOP();_NOP();_NOP();
         _NOP();_NOP();_NOP();_NOP();//10US
    }
    _EINT();
    
    return  temp;
}

/*******************************************
函数名称:Skip
功    能:发送跳过读取产品ID号命令
参    数:无
返回值  :无
********************************************/
void Skip(void)
{
    Write_18B20(0xcc);
}
/*******************************************
函数名称:Convert_1
功    能:发送温度转换命令
参    数:无
返回值  :无
********************************************/
void Convert_1(void)
{
    Write_18B20(0x44);
}
/*******************************************
函数名称:Read_SP
功    能:发送读ScratchPad命令
参    数:无
返回值  :无
********************************************/
void Read_SP(void)
{
    Write_18B20(0xbe);
}
/*******************************************
函数名称:ReadTemp
功    能:从DS18B20的ScratchPad读取温度转换结果
参    数:无
返回值  :读取的温度数值
********************************************/
uint ReadTemp(void)
{
    uchar temp_low;
    uint  temp;
    
    temp_low = Read_18B20();      //读低位
    temp = Read_18B20();     //读高位
    temp = (temp<<8) | temp_low;
    
    return  temp;
}
/*******************************************
函数名称:ReadTemp
功    能:控制DS18B20完成一次温度转换
参    数:无
返回值  :测量的温度数值
********************************************/
uint Do1Convert(void)
{
    uchar i; 
    
    do
    {
        i = Init_18B20();
    }
    while(i);
    Skip();
    Convert_1();
    for(i = 20;i > 0;i--)  
       // DelayNus(60000); //延时800ms以上
     // delay6ms(120);//800ms
      delayus(65535);
    delayus(41129);
        do
    {
        i = Init_18B20();
    }
    while(i);
    Skip();
    Read_SP();
    return ReadTemp();
}

void get_ds18b20_temperture()
{
    static unsigned char i=0;   
     i++; 
     if(i==1)//每隔100ms毫秒执行一次
     {     
      unsigned char j ;
      do
      {
          j = Init_18B20();
      }
      while(j);//初始化
      
      
      Skip();
      Convert_1();
      
     }
     else if(i>8)//每隔800ms执行一次
     {
       i = 0;
       unsigned char jj;
            do
      {
            jj = Init_18B20();
      }
      while(jj);//初始化
      
      
      Skip();
      Read_SP();
       unsigned int temm= ReadTemp(); 
       temm=temm*0.0625;//这里只是用温度的整数位
       
       temp_temperture[0]=temm/10; 
       temp_temperture[1]=temm%10;
   
        }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -