DOC

#include

By Deborah Ward,2014-11-25 17:16
10 views 0
#include

#include <reg52.h>

    unsigned char code

    LED_show1[]={0xc0,0xcf,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//0~9

    unsigned char code

    LED_show2[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xff};//0.~9.

    unsigned char code bitcode[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //数码管位选代码

    unsigned char dispbuff[8]={0,0,0,10,10,10,10,10};

    unsigned char disp_count; unsigned char flag=1; unsigned char flag_count,check_count,alarm;

    unsigned long getdata; sbit ST=P3^0;

    sbit OE=P3^1;

    sbit EOC=P3^2;

    sbit SPK=P3^5;

    sbit LED_G=P3^6;

    sbit LED_R=P3^7;

    void delay(unsigned char a) {

     unsigned char i;

     while(--a!=0)

     {

     for(i=0;i<125;i++);

     }

    }

void main(void)

    {

     TMOD=0x01; //选择定时器T0?方式为1;16位定时器;

     TH0=(65536-3000)/256; //定时3ms初值

     TL0=(65536-3000)%256;

     TR0=1;

     ET0=1;

     EA=1;

     ST=0;

     OE=0;

     SPK=0;

     while(1)

     {

     if(flag==1) //每隔600ms启动一次AD采样

     {

     flag=0;

     ST=0; //驱动AD转换

     ST=1;

     ST=0; //保持低电平

     while(EOC==0); //等待AD转换结束

     TR0=0; //AD转换结束时先停止T0

     P1=0xff; //关闭数码管显示

     P0=0xff; // 清空三态输出锁存器上的数据

     OE=1; //转换结束?允许AD值输出

     getdata=P0;//读出AD

     OE=0; //禁止输出

     getdata=(getdata*850)/256;//转换成电压

     TR0=1; //重新启动T0

     }

     dispbuff[0]=getdata/100;

     dispbuff[1]=(getdata%100)/10;

     dispbuff[2]=getdata%10;

     if(getdata>300)

     {

     LED_G=1;

     LED_R=0;

     alarm=1;

     }

     else

     {

     LED_G=0;

     LED_R=1;

     }

     }

    }

    void t0(void) interrupt 1 {

     TH0=(65536-3000)/256;

     TL0=(65536-3000)%256;

     if(disp_count==1)

     P1=LED_show2[dispbuff[disp_count]];

     else

     P1=LED_show1[dispbuff[disp_count]];

     P2=bitcode[disp_count];

     disp_count++;

     check_count++;

     flag_count++;

     if(disp_count==8)

     {

     disp_count=0;

     }

     if(check_count==200)

     {

     flag=1;

     check_count=0;

     }

     if(flag_count==300)

     {

     if(alarm==1)

     {

     alarm=0;

     flag_count=0;

     SPK=~SPK;

     }

     }

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com