新手求助,如果把这个51单片机的程序和仿真更改成MSP430的程序和仿真?

2019-07-15 15:44发布

新手求助,毕业设计马上要交,老师要求吧AT89C51改成msp430
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
8条回答
端木以鑫
1楼-- · 2019-07-15 21:31
#include <AT89X52.h>
#define uint unsigned int
#define uchar unsigned char           //宏定义
#define SET  P3_1                            //定义调整键
#define DEC  P3_2                            //定义减少键
#define ADD  P3_3                            //定义增加键
#define BEEP P3_6                            //定义蜂鸣器
#define LED_H P1_6                                //定义灯光报警
#define LED_L P1_7                                //定义灯光报警
#define DQ   P3_7                             //定义DS18B20总线I/O       
bit shanshuo_st;                            //闪烁间隔标志
bit beep_st;                                     //蜂鸣器间隔标志
bit H_ZF=0,L_ZF=0;
signed char shangxian=35;                  //上限报警温度
signed char xiaxian=15;                   //下限报警温度
bit fuhao=0;
uint wendu=0;
uchar x=0;
uchar qian=0,bai=0,shi=0,ge=0;
sbit DIAN = P0^5;                        //小数点
uchar set_st=0;                             //状态标志

//uchar code  LEDData[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xff};
uchar code  LEDData[]={0x5F,0x44,0x9D,0xDd,0xC6,0xD3,0xDB,0x47,0xDF,0xD7,0xCF,0xDA,0x9B,0xDC,0x9B,0x8B};

//============================================================================================
//====================================DS18B20=================================================
//============================================================================================
/*****延时子程序*****/
void Delay_DS18B20(int num)
{
  while(num--) ;
}
/*****初始化DS18B20*****/
void Init_DS18B20(void)
{
  unsigned char x=0;
  DQ = 0;         //DQ复位
  Delay_DS18B20(8);    //稍做延时
  DQ = 1;         //单片机将DQ拉低
  Delay_DS18B20(80);   //精确延时,大于480us
  DQ = 0;         //拉高总线
  Delay_DS18B20(34);
}
/*****读一个字节*****/
unsigned char ReadOneChar(void)
{
  unsigned char i=0;
  unsigned char dat = 0;
  for (i=8;i>0;i--)
  {
    DQ = 0;     // 给脉冲信号
    dat>>=1;
    DQ = 1;     // 给脉冲信号
    if(DQ)
    dat|=0x80;
    Delay_DS18B20(4);
  }
  return(dat);
}
/*****写一个字节*****/
void WriteOneChar(unsigned char dat)
{
  unsigned char i=0;
  for (i=8; i>0; i--)
  {
    DQ = 1;
    DQ = dat&&0x01;
    Delay_DS18B20(5);
    DQ = 0;
    dat>>=1;
  }
}
/*****读取温度*****/
unsigned int ReadTemperature(void)
{
  unsigned char a=0;
  unsigned char b=0;
  unsigned int t=0;
  float tt=0;
  Init_DS18B20();
  WriteOneChar(0xCC);  //跳过读序号列号的操作
  WriteOneChar(0x47);  //启动温度转换
  Init_DS18B20();
  WriteOneChar(0xCC);  //跳过读序号列号的操作
  WriteOneChar(0xBF);  //读取温度寄存器
  a=ReadOneChar();     //读低8位
  b=ReadOneChar();    //读高8位
  t=b;
  t<<=8;
  t=t|a;
  if(t&&0xf800)
  {
        t=~t+1;
        fuhao=1;
  }
  else
  fuhao=0;
  tt=t*0.00625;
  t= tt*10+0.5;     //放大10倍输出并四舍五入
  return(t);
}
//=====================================================================================
//=====================================================================================
//=====================================================================================


/*****延时子程序*****/
void Delay(uint num)
{
while( --num );
}
/*****初始化定时器0*****/
void InitTimer(void)
{
        TMOD=0x10;
        TL0=0x3c;
        TH0=0xb0;     //50ms(晶振12M)
        EA=1;      //全局中断开关
        TR0=1;
        ET0=1;      //开启定时器0
        IT0=1;        
        IT1=1;
}

/*****显示开机初始化等待画面*****/
void Disp_init(void)   
{
        P0 = 0x80;      //显示----
        P2 = 0xBF;
        Delay(200);
        P2 = 0xEF;
        Delay(200);   
        P2 = 0xFB;
        Delay(200);
        P2 = 0xFE;
        Delay(200);
        P2 = 0xFF;         //关闭显示
}
/*****显示温度子程序*****/
void Disp_Temperature(void)     //显示温度
{
        qian=wendu/1000;
        bai=wendu%1000/100;
        shi=wendu%1000/100/10;
        ge=wendu%1000%100/10;
        P0 = LEDData[ge];      //
        P2 = 0xBF;
        Delay(200);
        P2=0xff;

        P0=LEDData[shi];    //显示个位
        P2 = 0xEF;
        DIAN = 0;         //显示小数点
        Delay(200);
        P2=0xff;
       
        if((qian+bai)!=0)
        P0 =LEDData[bai];    //显示bai位
        else
        P0=0xff;
        P2 = 0xFB;
        Delay(200);
        P2=0xff;

        if(qian!=0&&fuhao==0)
        P0 =LEDData[qian];    //显示qian位
        else if(qian==0&&fuhao==0)
        P0=0xff;
        else if(fuhao==1)
        P0=0x80;
        P2 = 0xFE;
        Delay(200);
        P2 = 0xff;         //关闭显示
}
/*****显示报警温度子程序*****/
void Disp_alarm(uchar baojing)
{
        P0 =LEDData[baojing%10];      //
        P2 = 0xBF;
        Delay(100);
        P2=0xff;

        P0 =LEDData[baojing%100/10]; //显示十位
        P2 = 0xEF;
        Delay(100);
        P2=0xff;

        if(set_st==1&&H_ZF==0&&baojing/100!=0)
        P0 =LEDData[baojing/100]; //显示百位
        else if(set_st==1&&H_ZF==0&&baojing/100==0)
        P0=0xff;
        else if(set_st==1&&H_ZF==1)
        P0=0x80;
        else if(set_st==2&&L_ZF==0&&baojing/100!=0)
        P0 =LEDData[baojing/100]; //显示百位
        else if(set_st==2&&L_ZF==0&&baojing/100==0)
        P0=0xff;
        else if(set_st==2&&L_ZF==1)
        P0=0x80;
        P2 = 0xFB;
        Delay(100);
        P2=0xff;

        if(set_st==1)P0 =~0xCE;
        else if(set_st==2)P0 =0x1A; //上限H、下限L标示
        P2 = 0xFE;
        Delay(100);
        P2 = 0xff;         //关闭显示
}
/*****报警子程序*****/
void Alarm()
{
        if(x>=10){beep_st=~beep_st;x=0;}
        if((fuhao==0&&H_ZF==0&&wendu>shangxian)||(fuhao==1&&H_ZF==1&&wendu<shangxian)||(fuhao==0&&H_ZF==1))
        {
                if(beep_st==1)
                {
                        LED_H=0;
                        BEEP=0;
                }
                else
                {
                        LED_H=1;
                        BEEP=1;
                }
        }
        else if((fuhao==0&&L_ZF==0&&wendu<xiaxian)||(fuhao==1&&L_ZF==1&&wendu>xiaxian)||(fuhao==1&&L_ZF==0))
        {
                if(beep_st==1)
                {
                        LED_L=0;
                        BEEP=0;
                }
                else
                {
                        LED_L=1;
                        BEEP=1;
                }
        }
        else
        {
                LED_H=1;
                LED_L=1;
                BEEP=1;       
        }
}
/*****主函数*****/
void main(void)
{
        uint z;
//        uchar s;
        InitTimer();    //初始化定时器
        wendu=ReadTemperature()-5;                          //获取温度值并减去DS18B20的温漂误差
        for(z=0;z<300;z++)
        {
                Disp_init();        
        }
        while(1)
        {
                if(SET==0)
                {
                        BEEP=1;
                        Delay(200);
                        BEEP=0;
                        do{}while(SET==0);
                        LED_H=1;
                        LED_L=1;
                        BEEP=1;
                        set_st++;x=0;shanshuo_st=1;
                        if(set_st>2)set_st=0;
                }
                if(set_st==0)
                {
                        EX0=0;    //关闭外部中断0
                        EX1=0;    //关闭外部中断1
                        wendu=ReadTemperature();                          //获取温度值并减去DS18B20的温漂误差
//                        for(s=0;s<10;s++)
                        Disp_Temperature();
                        Alarm();   //报警检测
                }
                else if(set_st==1)
                {
                        BEEP=1;    //关闭蜂鸣器
                        EX0=1;    //开启外部中断0
                        EX1=1;    //开启外部中断1
                        if(x>=10){shanshuo_st=~shanshuo_st;x=0;}
                        if(shanshuo_st) {Disp_alarm(shangxian);}
                }
                else if(set_st==2)
                {
                        BEEP=1;    //关闭蜂鸣器
                        EX0=1;    //开启外部中断0
                        EX1=1;    //开启外部中断1
                        if(x>=10){shanshuo_st=~shanshuo_st;x=0;}
                        if(shanshuo_st) {Disp_alarm(xiaxian);}
                }
        }
}

/*****定时器0中断服务程序*****/
void timer0(void) interrupt 1
{
TL0=0x3c;
TH0=0xb0;
x++;
}
/*****外部中断0服务程序*****/
void int0(void) interrupt 0
{
        EX0=0;      //关外部中断0
        if(DEC==0&&set_st==1)
        {
                BEEP=1;
                Delay(2000);
                BEEP=0;
                do
                {
                        Disp_alarm(shangxian);
                }
                while(DEC==0);
                if(H_ZF==0&&L_ZF==0)
                {
                        shangxian--;
                        if(shangxian<=xiaxian+1)
                                shangxian=xiaxian+1;
                }
                else if(H_ZF==0&&L_ZF==1)
                {
                        shangxian--;
                        if(shangxian<0&&xiaxian==1)
                        {
                                shangxian=0;
                                H_ZF=0;
                        }
                        else if(shangxian<0&&xiaxian>1)
                        {
                                shangxian=1;
                                H_ZF=1;
                        }
                }
                else if(H_ZF==1)
                {
                        shangxian++;
                        if(shangxian>=xiaxian-1)
                        shangxian=xiaxian-1;
                }
        }
        else if(DEC==0&&set_st==2)
        {
                BEEP=0;
                Delay(2000);
                BEEP=1;
                do
                {
                        Disp_alarm(xiaxian);
                }
                while(DEC==0);
                if(L_ZF==0)
                {
                        xiaxian--;
                        if(xiaxian<0)
                        {
                                xiaxian=1;
                                L_ZF=1;
                        }
                }
                else if(L_ZF==1)
                {
                        xiaxian++;
                        if(xiaxian>=55)
                        {
                                xiaxian=55;
                        }
                }
        }
}
/*****外部中断1服务程序*****/
void int1(void) interrupt 2
{
        EX1=0;      //关外部中断1
        if(ADD==0&&set_st==1)
        {
                BEEP=0;
                Delay(2000);
                BEEP=1;
                do
                {
                        Disp_alarm(shangxian);
                }
                while(ADD==0);
                if(H_ZF==0)
                {
                        shangxian++;
                        if(shangxian>=125)shangxian=125;
                }
                else if(H_ZF==1)
                {
                        shangxian--;
                        if(shangxian<=0)
                        {
                                shangxian=0;
                                H_ZF=0;
                        }
                }
        }                         
        else if(ADD==0&&set_st==2)
        {
                BEEP=0;
                Delay(2000);
                BEEP=1;
                do
                {
                        Disp_alarm(xiaxian);
                }
                while(ADD==0);
                if(L_ZF==0)
                {
                        xiaxian++;
                        if(xiaxian>=shangxian-1)
                                xiaxian=shangxian-1;       
                }
                else if(H_ZF==0&&L_ZF==1)
                {
                        xiaxian--;
                        if(xiaxian<=0)
                        {
                                if(shangxian==0)
                                {
                                        xiaxian=1;
                                        L_ZF=1;
                                }
                                else
                                {
                                        xiaxian=0;
                                        L_ZF=0;
                                }
                        }
                }
                else if(H_ZF==1&&L_ZF==1)
                {
                        xiaxian--;
                        if(xiaxian<=shangxian+1)
                                xiaxian=shangxian+1;
                }
        }  
}
端木以鑫
2楼-- · 2019-07-16 00:31
仿真如图
{PNF5RLV@W$0O(X0YYU0)0B.png
端木以鑫
3楼-- · 2019-07-16 05:21
#include <AT89X52.h>
#define uint unsigned int
#define uchar unsigned char           //宏定义
#define SET  P3_1                            //定义调整键
#define DEC  P3_2                            //定义减少键
#define ADD  P3_3                            //定义增加键
#define BEEP P3_6                            //定义蜂鸣器
#define LED_H P1_6                                //定义灯光报警
#define LED_L P1_7                                //定义灯光报警
#define DQ   P3_7                             //定义DS18B20总线I/O       
bit shanshuo_st;                            //闪烁间隔标志
bit beep_st;                                     //蜂鸣器间隔标志
bit H_ZF=0,L_ZF=0;
signed char shangxian=35;                  //上限报警温度
signed char xiaxian=15;                   //下限报警温度
bit fuhao=0;
uint wendu=0;
uchar x=0;
uchar qian=0,bai=0,shi=0,ge=0;
sbit DIAN = P0^5;                        //小数点
uchar set_st=0;                             //状态标志

//uchar code  LEDData[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xff};
uchar code  LEDData[]={0x5F,0x44,0x9D,0xDd,0xC6,0xD3,0xDB,0x47,0xDF,0xD7,0xCF,0xDA,0x9B,0xDC,0x9B,0x8B};

//============================================================================================
//====================================DS18B20=================================================
//============================================================================================
/*****延时子程序*****/
void Delay_DS18B20(int num)
{
  while(num--) ;
}
/*****初始化DS18B20*****/
void Init_DS18B20(void)
{
  unsigned char x=0;
  DQ = 0;         //DQ复位
  Delay_DS18B20(8);    //稍做延时
  DQ = 1;         //单片机将DQ拉低
  Delay_DS18B20(80);   //精确延时,大于480us
  DQ = 0;         //拉高总线
  Delay_DS18B20(34);
}
/*****读一个字节*****/
unsigned char ReadOneChar(void)
{
  unsigned char i=0;
  unsigned char dat = 0;
  for (i=8;i>0;i--)
  {
    DQ = 0;     // 给脉冲信号
    dat>>=1;
    DQ = 1;     // 给脉冲信号
    if(DQ)
    dat|=0x80;
    Delay_DS18B20(4);
  }
  return(dat);
}
/*****写一个字节*****/
void WriteOneChar(unsigned char dat)
{
  unsigned char i=0;
  for (i=8; i>0; i--)
  {
    DQ = 1;
    DQ = dat&&0x01;
    Delay_DS18B20(5);
    DQ = 0;
    dat>>=1;
  }
}
/*****读取温度*****/
unsigned int ReadTemperature(void)
{
  unsigned char a=0;
  unsigned char b=0;
  unsigned int t=0;
  float tt=0;
  Init_DS18B20();
  WriteOneChar(0xCC);  //跳过读序号列号的操作
  WriteOneChar(0x47);  //启动温度转换
  Init_DS18B20();
  WriteOneChar(0xCC);  //跳过读序号列号的操作
  WriteOneChar(0xBF);  //读取温度寄存器
  a=ReadOneChar();     //读低8位
  b=ReadOneChar();    //读高8位
  t=b;
  t<<=8;
  t=t|a;
  if(t&&0xf800)
  {
        t=~t+1;
        fuhao=1;
  }
  else
  fuhao=0;
  tt=t*0.00625;
  t= tt*10+0.5;     //放大10倍输出并四舍五入
  return(t);
}
//=====================================================================================
//=====================================================================================
//=====================================================================================


/*****延时子程序*****/
void Delay(uint num)
{
while( --num );
}
/*****初始化定时器0*****/
void InitTimer(void)
{
        TMOD=0x10;
        TL0=0x3c;
        TH0=0xb0;     //50ms(晶振12M)
        EA=1;      //全局中断开关
        TR0=1;
        ET0=1;      //开启定时器0
        IT0=1;        
        IT1=1;
}

/*****显示开机初始化等待画面*****/
void Disp_init(void)   
{
        P0 = 0x80;      //显示----
        P2 = 0xBF;
        Delay(200);
        P2 = 0xEF;
        Delay(200);   
        P2 = 0xFB;
        Delay(200);
        P2 = 0xFE;
        Delay(200);
        P2 = 0xFF;         //关闭显示
}
/*****显示温度子程序*****/
void Disp_Temperature(void)     //显示温度
{
        qian=wendu/1000;
        bai=wendu%1000/100;
        shi=wendu%1000/100/10;
        ge=wendu%1000%100/10;
        P0 = LEDData[ge];      //
        P2 = 0xBF;
        Delay(200);
        P2=0xff;

        P0=LEDData[shi];    //显示个位
        P2 = 0xEF;
        DIAN = 0;         //显示小数点
        Delay(200);
        P2=0xff;
       
        if((qian+bai)!=0)
        P0 =LEDData[bai];    //显示bai位
        else
        P0=0xff;
        P2 = 0xFB;
        Delay(200);
        P2=0xff;

        if(qian!=0&&fuhao==0)
        P0 =LEDData[qian];    //显示qian位
        else if(qian==0&&fuhao==0)
        P0=0xff;
        else if(fuhao==1)
        P0=0x80;
        P2 = 0xFE;
        Delay(200);
        P2 = 0xff;         //关闭显示
}
/*****显示报警温度子程序*****/
void Disp_alarm(uchar baojing)
{
        P0 =LEDData[baojing%10];      //
        P2 = 0xBF;
        Delay(100);
        P2=0xff;

        P0 =LEDData[baojing%100/10]; //显示十位
        P2 = 0xEF;
        Delay(100);
        P2=0xff;

        if(set_st==1&&H_ZF==0&&baojing/100!=0)
        P0 =LEDData[baojing/100]; //显示百位
        else if(set_st==1&&H_ZF==0&&baojing/100==0)
        P0=0xff;
        else if(set_st==1&&H_ZF==1)
        P0=0x80;
        else if(set_st==2&&L_ZF==0&&baojing/100!=0)
        P0 =LEDData[baojing/100]; //显示百位
        else if(set_st==2&&L_ZF==0&&baojing/100==0)
        P0=0xff;
        else if(set_st==2&&L_ZF==1)
        P0=0x80;
        P2 = 0xFB;
        Delay(100);
        P2=0xff;

        if(set_st==1)P0 =~0xCE;
        else if(set_st==2)P0 =0x1A; //上限H、下限L标示
        P2 = 0xFE;
        Delay(100);
        P2 = 0xff;         //关闭显示
}
/*****报警子程序*****/
void Alarm()
{
        if(x>=10){beep_st=~beep_st;x=0;}
        if((fuhao==0&&H_ZF==0&&wendu>shangxian)||(fuhao==1&&H_ZF==1&&wendu<shangxian)||(fuhao==0&&H_ZF==1))
        {
                if(beep_st==1)
                {
                        LED_H=0;
                        BEEP=0;
                }
                else
                {
                        LED_H=1;
                        BEEP=1;
                }
        }
        else if((fuhao==0&&L_ZF==0&&wendu<xiaxian)||(fuhao==1&&L_ZF==1&&wendu>xiaxian)||(fuhao==1&&L_ZF==0))
        {
                if(beep_st==1)
                {
                        LED_L=0;
                        BEEP=0;
                }
                else
                {
                        LED_L=1;
                        BEEP=1;
                }
        }
        else
        {
                LED_H=1;
                LED_L=1;
                BEEP=1;       
        }
}
/*****主函数*****/
void main(void)
{
        uint z;
//        uchar s;
        InitTimer();    //初始化定时器
        wendu=ReadTemperature()-5;                          //获取温度值并减去DS18B20的温漂误差
        for(z=0;z<300;z++)
        {
                Disp_init();        
        }
        while(1)
        {
                if(SET==0)
                {
                        BEEP=1;
                        Delay(200);
                        BEEP=0;
                        do{}while(SET==0);
                        LED_H=1;
                        LED_L=1;
                        BEEP=1;
                        set_st++;x=0;shanshuo_st=1;
                        if(set_st>2)set_st=0;
                }
                if(set_st==0)
                {
                        EX0=0;    //关闭外部中断0
                        EX1=0;    //关闭外部中断1
                        wendu=ReadTemperature();                          //获取温度值并减去DS18B20的温漂误差
//                        for(s=0;s<10;s++)
                        Disp_Temperature();
                        Alarm();   //报警检测
                }
                else if(set_st==1)
                {
                        BEEP=1;    //关闭蜂鸣器
                        EX0=1;    //开启外部中断0
                        EX1=1;    //开启外部中断1
                        if(x>=10){shanshuo_st=~shanshuo_st;x=0;}
                        if(shanshuo_st) {Disp_alarm(shangxian);}
                }
                else if(set_st==2)
                {
                        BEEP=1;    //关闭蜂鸣器
                        EX0=1;    //开启外部中断0
                        EX1=1;    //开启外部中断1
                        if(x>=10){shanshuo_st=~shanshuo_st;x=0;}
                        if(shanshuo_st) {Disp_alarm(xiaxian);}
                }
        }
}

/*****定时器0中断服务程序*****/
void timer0(void) interrupt 1
{
TL0=0x3c;
TH0=0xb0;
x++;
}
/*****外部中断0服务程序*****/
void int0(void) interrupt 0
{
        EX0=0;      //关外部中断0
        if(DEC==0&&set_st==1)
        {
                BEEP=1;
                Delay(2000);
                BEEP=0;
                do
                {
                        Disp_alarm(shangxian);
                }
                while(DEC==0);
                if(H_ZF==0&&L_ZF==0)
                {
                        shangxian--;
                        if(shangxian<=xiaxian+1)
                                shangxian=xiaxian+1;
                }
                else if(H_ZF==0&&L_ZF==1)
                {
                        shangxian--;
                        if(shangxian<0&&xiaxian==1)
                        {
                                shangxian=0;
                                H_ZF=0;
                        }
                        else if(shangxian<0&&xiaxian>1)
                        {
                                shangxian=1;
                                H_ZF=1;
                        }
                }
                else if(H_ZF==1)
                {
                        shangxian++;
                        if(shangxian>=xiaxian-1)
                        shangxian=xiaxian-1;
                }
        }
        else if(DEC==0&&set_st==2)
        {
                BEEP=0;
                Delay(2000);
                BEEP=1;
                do
                {
                        Disp_alarm(xiaxian);
                }
                while(DEC==0);
                if(L_ZF==0)
                {
                        xiaxian--;
                        if(xiaxian<0)
                        {
                                xiaxian=1;
                                L_ZF=1;
                        }
                }
                else if(L_ZF==1)
                {
                        xiaxian++;
                        if(xiaxian>=55)
                        {
                                xiaxian=55;
                        }
                }
        }
}
/*****外部中断1服务程序*****/
void int1(void) interrupt 2
{
        EX1=0;      //关外部中断1
        if(ADD==0&&set_st==1)
        {
                BEEP=0;
                Delay(2000);
                BEEP=1;
                do
                {
                        Disp_alarm(shangxian);
                }
                while(ADD==0);
                if(H_ZF==0)
                {
                        shangxian++;
                        if(shangxian>=125)shangxian=125;
                }
                else if(H_ZF==1)
                {
                        shangxian--;
                        if(shangxian<=0)
                        {
                                shangxian=0;
                                H_ZF=0;
                        }
                }
        }                         
        else if(ADD==0&&set_st==2)
        {
                BEEP=0;
                Delay(2000);
                BEEP=1;
                do
                {
                        Disp_alarm(xiaxian);
                }
                while(ADD==0);
                if(L_ZF==0)
                {
                        xiaxian++;
                        if(xiaxian>=shangxian-1)
                                xiaxian=shangxian-1;       
                }
                else if(H_ZF==0&&L_ZF==1)
                {
                        xiaxian--;
                        if(xiaxian<=0)
                        {
                                if(shangxian==0)
                                {
                                        xiaxian=1;
                                        L_ZF=1;
                                }
                                else
                                {
                                        xiaxian=0;
                                        L_ZF=0;
                                }
                        }
                }
                else if(H_ZF==1&&L_ZF==1)
                {
                        xiaxian--;
                        if(xiaxian<=shangxian+1)
                                xiaxian=shangxian+1;
                }
        }  
}
端木以鑫
4楼-- · 2019-07-16 08:03
 精彩回答 2  元偷偷看……
端木以鑫
5楼-- · 2019-07-16 09:50
#include <AT89X52.h>
#define uint unsigned int
#define uchar unsigned char           //宏定义
#define SET  P3_1                            //定义调整键
#define DEC  P3_2                            //定义减少键
#define ADD  P3_3                            //定义增加键
#define BEEP P3_6                            //定义蜂鸣器
#define LED_H P1_6                                //定义灯光报警
#define LED_L P1_7                                //定义灯光报警
#define DQ   P3_7                             //定义DS18B20总线I/O       
bit shanshuo_st;                            //闪烁间隔标志
bit beep_st;                                     //蜂鸣器间隔标志
bit H_ZF=0,L_ZF=0;
signed char shangxian=35;                  //上限报警温度
signed char xiaxian=15;                   //下限报警温度
bit fuhao=0;
uint wendu=0;
uchar x=0;
uchar qian=0,bai=0,shi=0,ge=0;
sbit DIAN = P0^5;                        //小数点
uchar set_st=0;                             //状态标志

//uchar code  LEDData[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xff};
uchar code  LEDData[]={0x5F,0x44,0x9D,0xDd,0xC6,0xD3,0xDB,0x47,0xDF,0xD7,0xCF,0xDA,0x9B,0xDC,0x9B,0x8B};

//============================================================================================
//====================================DS18B20=================================================
//============================================================================================
/*****延时子程序*****/
void Delay_DS18B20(int num)
{
  while(num--) ;
}
/*****初始化DS18B20*****/
void Init_DS18B20(void)
{
  unsigned char x=0;
  DQ = 0;         //DQ复位
  Delay_DS18B20(8);    //稍做延时
  DQ = 1;         //单片机将DQ拉低
  Delay_DS18B20(80);   //精确延时,大于480us
  DQ = 0;         //拉高总线
  Delay_DS18B20(34);
}
/*****读一个字节*****/
unsigned char ReadOneChar(void)
{
  unsigned char i=0;
  unsigned char dat = 0;
  for (i=8;i>0;i--)
  {
    DQ = 0;     // 给脉冲信号
    dat>>=1;
    DQ = 1;     // 给脉冲信号
    if(DQ)
    dat|=0x80;
    Delay_DS18B20(4);
  }
  return(dat);
}
/*****写一个字节*****/
void WriteOneChar(unsigned char dat)
{
  unsigned char i=0;
  for (i=8; i>0; i--)
  {
    DQ = 1;
    DQ = dat&&0x01;
    Delay_DS18B20(5);
    DQ = 0;
    dat>>=1;
  }
}
/*****读取温度*****/
unsigned int ReadTemperature(void)
{
  unsigned char a=0;
  unsigned char b=0;
  unsigned int t=0;
  float tt=0;
  Init_DS18B20();
  WriteOneChar(0xCC);  //跳过读序号列号的操作
  WriteOneChar(0x47);  //启动温度转换
  Init_DS18B20();
  WriteOneChar(0xCC);  //跳过读序号列号的操作
  WriteOneChar(0xBF);  //读取温度寄存器
  a=ReadOneChar();     //读低8位
  b=ReadOneChar();    //读高8位
  t=b;
  t<<=8;
  t=t|a;
  if(t&&0xf800)
  {
        t=~t+1;
        fuhao=1;
  }
  else
  fuhao=0;
  tt=t*0.00625;
  t= tt*10+0.5;     //放大10倍输出并四舍五入
  return(t);
}
//=====================================================================================
//=====================================================================================
//=====================================================================================


/*****延时子程序*****/
void Delay(uint num)
{
while( --num );
}
/*****初始化定时器0*****/
void InitTimer(void)
{
        TMOD=0x10;
        TL0=0x3c;
        TH0=0xb0;     //50ms(晶振12M)
        EA=1;      //全局中断开关
        TR0=1;
        ET0=1;      //开启定时器0
        IT0=1;        
        IT1=1;
}

/*****显示开机初始化等待画面*****/
void Disp_init(void)   
{
        P0 = 0x80;      //显示----
        P2 = 0xBF;
        Delay(200);
        P2 = 0xEF;
        Delay(200);   
        P2 = 0xFB;
        Delay(200);
        P2 = 0xFE;
        Delay(200);
        P2 = 0xFF;         //关闭显示
}
/*****显示温度子程序*****/
void Disp_Temperature(void)     //显示温度
{
        qian=wendu/1000;
        bai=wendu%1000/100;
        shi=wendu%1000/100/10;
        ge=wendu%1000%100/10;
        P0 = LEDData[ge];      //
        P2 = 0xBF;
        Delay(200);
        P2=0xff;

        P0=LEDData[shi];    //显示个位
        P2 = 0xEF;
        DIAN = 0;         //显示小数点
        Delay(200);
        P2=0xff;
       
        if((qian+bai)!=0)
        P0 =LEDData[bai];    //显示bai位
        else
        P0=0xff;
        P2 = 0xFB;
        Delay(200);
        P2=0xff;

        if(qian!=0&&fuhao==0)
        P0 =LEDData[qian];    //显示qian位
        else if(qian==0&&fuhao==0)
        P0=0xff;
        else if(fuhao==1)
        P0=0x80;
        P2 = 0xFE;
        Delay(200);
        P2 = 0xff;         //关闭显示
}
/*****显示报警温度子程序*****/
void Disp_alarm(uchar baojing)
{
        P0 =LEDData[baojing%10];      //
        P2 = 0xBF;
        Delay(100);
        P2=0xff;

        P0 =LEDData[baojing%100/10]; //显示十位
        P2 = 0xEF;
        Delay(100);
        P2=0xff;

        if(set_st==1&&H_ZF==0&&baojing/100!=0)
        P0 =LEDData[baojing/100]; //显示百位
        else if(set_st==1&&H_ZF==0&&baojing/100==0)
        P0=0xff;
        else if(set_st==1&&H_ZF==1)
        P0=0x80;
        else if(set_st==2&&L_ZF==0&&baojing/100!=0)
        P0 =LEDData[baojing/100]; //显示百位
        else if(set_st==2&&L_ZF==0&&baojing/100==0)
        P0=0xff;
        else if(set_st==2&&L_ZF==1)
        P0=0x80;
        P2 = 0xFB;
        Delay(100);
        P2=0xff;

        if(set_st==1)P0 =~0xCE;
        else if(set_st==2)P0 =0x1A; //上限H、下限L标示
        P2 = 0xFE;
        Delay(100);
        P2 = 0xff;         //关闭显示
}
/*****报警子程序*****/
void Alarm()
{
        if(x>=10){beep_st=~beep_st;x=0;}
        if((fuhao==0&&H_ZF==0&&wendu>shangxian)||(fuhao==1&&H_ZF==1&&wendu<shangxian)||(fuhao==0&&H_ZF==1))
        {
                if(beep_st==1)
                {
                        LED_H=0;
                        BEEP=0;
                }
                else
                {
                        LED_H=1;
                        BEEP=1;
                }
        }
        else if((fuhao==0&&L_ZF==0&&wendu<xiaxian)||(fuhao==1&&L_ZF==1&&wendu>xiaxian)||(fuhao==1&&L_ZF==0))
        {
                if(beep_st==1)
                {
                        LED_L=0;
                        BEEP=0;
                }
                else
                {
                        LED_L=1;
                        BEEP=1;
                }
        }
        else
        {
                LED_H=1;
                LED_L=1;
                BEEP=1;       
        }
}
/*****主函数*****/
void main(void)
{
        uint z;
//        uchar s;
        InitTimer();    //初始化定时器
        wendu=ReadTemperature()-5;                          //获取温度值并减去DS18B20的温漂误差
        for(z=0;z<300;z++)
        {
                Disp_init();        
        }
        while(1)
        {
                if(SET==0)
                {
                        BEEP=1;
                        Delay(200);
                        BEEP=0;
                        do{}while(SET==0);
                        LED_H=1;
                        LED_L=1;
                        BEEP=1;
                        set_st++;x=0;shanshuo_st=1;
                        if(set_st>2)set_st=0;
                }
                if(set_st==0)
                {
                        EX0=0;    //关闭外部中断0
                        EX1=0;    //关闭外部中断1
                        wendu=ReadTemperature();                          //获取温度值并减去DS18B20的温漂误差
//                        for(s=0;s<10;s++)
                        Disp_Temperature();
                        Alarm();   //报警检测
                }
                else if(set_st==1)
                {
                        BEEP=1;    //关闭蜂鸣器
                        EX0=1;    //开启外部中断0
                        EX1=1;    //开启外部中断1
                        if(x>=10){shanshuo_st=~shanshuo_st;x=0;}
                        if(shanshuo_st) {Disp_alarm(shangxian);}
                }
                else if(set_st==2)
                {
                        BEEP=1;    //关闭蜂鸣器
                        EX0=1;    //开启外部中断0
                        EX1=1;    //开启外部中断1
                        if(x>=10){shanshuo_st=~shanshuo_st;x=0;}
                        if(shanshuo_st) {Disp_alarm(xiaxian);}
                }
        }
}

/*****定时器0中断服务程序*****/
void timer0(void) interrupt 1
{
TL0=0x3c;
TH0=0xb0;
x++;
}
/*****外部中断0服务程序*****/
void int0(void) interrupt 0
{
        EX0=0;      //关外部中断0
        if(DEC==0&&set_st==1)
        {
                BEEP=1;
                Delay(2000);
                BEEP=0;
                do
                {
                        Disp_alarm(shangxian);
                }
                while(DEC==0);
                if(H_ZF==0&&L_ZF==0)
                {
                        shangxian--;
                        if(shangxian<=xiaxian+1)
                                shangxian=xiaxian+1;
                }
                else if(H_ZF==0&&L_ZF==1)
                {
                        shangxian--;
                        if(shangxian<0&&xiaxian==1)
                        {
                                shangxian=0;
                                H_ZF=0;
                        }
                        else if(shangxian<0&&xiaxian>1)
                        {
                                shangxian=1;
                                H_ZF=1;
                        }
                }
                else if(H_ZF==1)
                {
                        shangxian++;
                        if(shangxian>=xiaxian-1)
                        shangxian=xiaxian-1;
                }
        }
        else if(DEC==0&&set_st==2)
        {
                BEEP=0;
                Delay(2000);
                BEEP=1;
                do
                {
                        Disp_alarm(xiaxian);
                }
                while(DEC==0);
                if(L_ZF==0)
                {
                        xiaxian--;
                        if(xiaxian<0)
                        {
                                xiaxian=1;
                                L_ZF=1;
                        }
                }
                else if(L_ZF==1)
                {
                        xiaxian++;
                        if(xiaxian>=55)
                        {
                                xiaxian=55;
                        }
                }
        }
}
/*****外部中断1服务程序*****/
void int1(void) interrupt 2
{
        EX1=0;      //关外部中断1
        if(ADD==0&&set_st==1)
        {
                BEEP=0;
                Delay(2000);
                BEEP=1;
                do
                {
                        Disp_alarm(shangxian);
                }
                while(ADD==0);
                if(H_ZF==0)
                {
                        shangxian++;
                        if(shangxian>=125)shangxian=125;
                }
                else if(H_ZF==1)
                {
                        shangxian--;
                        if(shangxian<=0)
                        {
                                shangxian=0;
                                H_ZF=0;
                        }
                }
        }                         
        else if(ADD==0&&set_st==2)
        {
                BEEP=0;
                Delay(2000);
                BEEP=1;
                do
                {
                        Disp_alarm(xiaxian);
                }
                while(ADD==0);
                if(L_ZF==0)
                {
                        xiaxian++;
                        if(xiaxian>=shangxian-1)
                                xiaxian=shangxian-1;       
                }
                else if(H_ZF==0&&L_ZF==1)
                {
                        xiaxian--;
                        if(xiaxian<=0)
                        {
                                if(shangxian==0)
                                {
                                        xiaxian=1;
                                        L_ZF=1;
                                }
                                else
                                {
                                        xiaxian=0;
                                        L_ZF=0;
                                }
                        }
                }
                else if(H_ZF==1&&L_ZF==1)
                {
                        xiaxian--;
                        if(xiaxian<=shangxian+1)
                                xiaxian=shangxian+1;
                }
        }  
}
端木以鑫
6楼-- · 2019-07-16 13:22
#include <AT89X52.h>
#define uint unsigned int
#define uchar unsigned char           //宏定义
#define SET  P3_1                            //定义调整键
#define DEC  P3_2                            //定义减少键
#define ADD  P3_3                            //定义增加键
#define BEEP P3_6                            //定义蜂鸣器
#define LED_H P1_6                                //定义灯光报警
#define LED_L P1_7                                //定义灯光报警
#define DQ   P3_7                             //定义DS18B20总线I/O       
bit shanshuo_st;                            //闪烁间隔标志
bit beep_st;                                     //蜂鸣器间隔标志
bit H_ZF=0,L_ZF=0;
signed char shangxian=35;                  //上限报警温度
signed char xiaxian=15;                   //下限报警温度
bit fuhao=0;
uint wendu=0;
uchar x=0;
uchar qian=0,bai=0,shi=0,ge=0;
sbit DIAN = P0^5;                        //小数点
uchar set_st=0;                             //状态标志

//uchar code  LEDData[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xff};
uchar code  LEDData[]={0x5F,0x44,0x9D,0xDd,0xC6,0xD3,0xDB,0x47,0xDF,0xD7,0xCF,0xDA,0x9B,0xDC,0x9B,0x8B};

//============================================================================================
//====================================DS18B20=================================================
//============================================================================================
/*****延时子程序*****/
void Delay_DS18B20(int num)
{
  while(num--) ;
}
/*****初始化DS18B20*****/
void Init_DS18B20(void)
{
  unsigned char x=0;
  DQ = 0;         //DQ复位
  Delay_DS18B20(8);    //稍做延时
  DQ = 1;         //单片机将DQ拉低
  Delay_DS18B20(80);   //精确延时,大于480us
  DQ = 0;         //拉高总线
  Delay_DS18B20(34);
}
/*****读一个字节*****/
unsigned char ReadOneChar(void)
{
  unsigned char i=0;
  unsigned char dat = 0;
  for (i=8;i>0;i--)
  {
    DQ = 0;     // 给脉冲信号
    dat>>=1;
    DQ = 1;     // 给脉冲信号
    if(DQ)
    dat|=0x80;
    Delay_DS18B20(4);
  }
  return(dat);
}
/*****写一个字节*****/
void WriteOneChar(unsigned char dat)
{
  unsigned char i=0;
  for (i=8; i>0; i--)
  {
    DQ = 1;
    DQ = dat&&0x01;
    Delay_DS18B20(5);
    DQ = 0;
    dat>>=1;
  }
}
/*****读取温度*****/
unsigned int ReadTemperature(void)
{
  unsigned char a=0;
  unsigned char b=0;
  unsigned int t=0;
  float tt=0;
  Init_DS18B20();
  WriteOneChar(0xCC);  //跳过读序号列号的操作
  WriteOneChar(0x47);  //启动温度转换
  Init_DS18B20();
  WriteOneChar(0xCC);  //跳过读序号列号的操作
  WriteOneChar(0xBF);  //读取温度寄存器
  a=ReadOneChar();     //读低8位
  b=ReadOneChar();    //读高8位
  t=b;
  t<<=8;
  t=t|a;
  if(t&&0xf800)
  {
        t=~t+1;
        fuhao=1;
  }
  else
  fuhao=0;
  tt=t*0.00625;
  t= tt*10+0.5;     //放大10倍输出并四舍五入
  return(t);
}
//=====================================================================================
//=====================================================================================
//=====================================================================================


/*****延时子程序*****/
void Delay(uint num)
{
while( --num );
}
/*****初始化定时器0*****/
void InitTimer(void)
{
        TMOD=0x10;
        TL0=0x3c;
        TH0=0xb0;     //50ms(晶振12M)
        EA=1;      //全局中断开关
        TR0=1;
        ET0=1;      //开启定时器0
        IT0=1;        
        IT1=1;
}

/*****显示开机初始化等待画面*****/
void Disp_init(void)   
{
        P0 = 0x80;      //显示----
        P2 = 0xBF;
        Delay(200);
        P2 = 0xEF;
        Delay(200);   
        P2 = 0xFB;
        Delay(200);
        P2 = 0xFE;
        Delay(200);
        P2 = 0xFF;         //关闭显示
}
/*****显示温度子程序*****/
void Disp_Temperature(void)     //显示温度
{
        qian=wendu/1000;
        bai=wendu%1000/100;
        shi=wendu%1000/100/10;
        ge=wendu%1000%100/10;
        P0 = LEDData[ge];      //
        P2 = 0xBF;
        Delay(200);
        P2=0xff;

        P0=LEDData[shi];    //显示个位
        P2 = 0xEF;
        DIAN = 0;         //显示小数点
        Delay(200);
        P2=0xff;
       
        if((qian+bai)!=0)
        P0 =LEDData[bai];    //显示bai位
        else
        P0=0xff;
        P2 = 0xFB;
        Delay(200);
        P2=0xff;

        if(qian!=0&&fuhao==0)
        P0 =LEDData[qian];    //显示qian位
        else if(qian==0&&fuhao==0)
        P0=0xff;
        else if(fuhao==1)
        P0=0x80;
        P2 = 0xFE;
        Delay(200);
        P2 = 0xff;         //关闭显示
}
/*****显示报警温度子程序*****/
void Disp_alarm(uchar baojing)
{
        P0 =LEDData[baojing%10];      //
        P2 = 0xBF;
        Delay(100);
        P2=0xff;

        P0 =LEDData[baojing%100/10]; //显示十位
        P2 = 0xEF;
        Delay(100);
        P2=0xff;

        if(set_st==1&&H_ZF==0&&baojing/100!=0)
        P0 =LEDData[baojing/100]; //显示百位
        else if(set_st==1&&H_ZF==0&&baojing/100==0)
        P0=0xff;
        else if(set_st==1&&H_ZF==1)
        P0=0x80;
        else if(set_st==2&&L_ZF==0&&baojing/100!=0)
        P0 =LEDData[baojing/100]; //显示百位
        else if(set_st==2&&L_ZF==0&&baojing/100==0)
        P0=0xff;
        else if(set_st==2&&L_ZF==1)
        P0=0x80;
        P2 = 0xFB;
        Delay(100);
        P2=0xff;

        if(set_st==1)P0 =~0xCE;
        else if(set_st==2)P0 =0x1A; //上限H、下限L标示
        P2 = 0xFE;
        Delay(100);
        P2 = 0xff;         //关闭显示
}

一周热门 更多>