立即注册 登录
返回首页

uid:255056的个人空间

日志

单片机关于函数信号发生器代码

已有 508 次阅读2017-11-29 10:44 | 函数信号发生器

#include<reg51.h>

unsigned char f_h;

unsigned int yichu_count;

bit count_over;

sbit s1=P0^5;

sbit s2=P0^6;

sbit rl=P2^0;

char zixing[10]={ 0x88,0xEB,0x4C,0x49,0x2B,0x19,0x18,0xCB,0x08,0x09};

unsigned long f;

void main(void)

{

     void Delay(char i);

    char shift_164(char a);

     void test_f(void);

     shift_164(0x88);

    Delay(200);

    shift_164(0xEB);

    Delay(200);

    shift_164(0x4C);

    Delay(200);

    shift_164(0x49);

    Delay(200);

    shift_164(0x2B);

    Delay(200);

    // while(1);

     if(P0^2==0)

          {  

            s1=0;

            s2=1;

         test_f();

         }

      if(P0^3==0)

          {  

            s1=0;

            s2=0;

         test_f();

         }

       if(P0^4==0)

          {   

            s1=1;

            s2=0;

         test_f();

         }

}

void test_f(void)

{

     unsigned long get_f();

    char x1,x2,x3,x4,x5,x6;

    void Delay(char i);

loop:   

     f=get_f();

    if(f==0)

    {

      rl=0;

       T0=1;

      f=get_f();

      }

    //           x6=f/100000;

    x5=(f-x6*100000)/10000;

    x4=(f-x6*100000-x5*10000)/1000;

    x3=(f-x6*100000-x5*10000-x4*1000)/100;

    x2=(f-x6*100000-x5*10000-x4*1000-x3*100)/10;

    x1=f-x6*100000-x5*10000-x4*1000-x3*100-x2*10;

 

    shift_164(zixing[x1]);

    Delay(200);

    shift_164(zixing[x2]);

    Delay(200);

    shift_164(zixing[x3]);

    Delay(200);

    shift_164(zixing[x4]);

    Delay(200);

    shift_164(zixing[x5]);

    Delay(200);

    shift_164(zixing[x6]);

    Delay(200);

    goto loop;

}

//******************************测频率子程序*******************

void intt0_f()interrupt 1//T0作为频率值最高八位,频率值共24

{

   f_h=f_h+1;

}

void intt1_time()interrupt 3//T1作为门控时间产生器200US中断一次

{

   if (yichu_count>=5000)

        {

         TR0=0;//频率停止计数

         TR1=0;//时间停止计数

            EA=0;//关中断

         count_over=1;

         yichu_count=0;

         return;

         }

   yichu_count=yichu_count+1;

}

 

unsigned long get_f()

{

   unsigned long f;

   count_over=0;

   TMOD=0X25;

   TH1=TL1=0X9C;//设置T1计数初值,可以100US中断一次

   f_h=TH0=TL0=0;//频率值清零

   count_over=0;//清零计数完成标志

   EA=1;//开所有中断

   ET0=1;

   ET1=1;

   TR0=1;//频率计数器开始计数

   TR1=1;//时间计数器开始计时

wait_count_over:

   if(count_over!=1)

   goto wait_count_over;

   f=(TL0+TH0*256+f_h*65536);

   return (f);

 }

void Delay(char i)

{   int j;

    for(;i>0;i--)

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

    {;}

}

char shift_164(char a)

{

    SCON=0x00;

    TI=0x00;

    SBUF=a;

     if(TI==0)

    {};

    return(a);

   

}


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

返回顶部