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

📄 end.c

📁 正弦波发生电路如果其他会员下载一次您上载的源码
💻 C
字号:
#include<reg52.h>
#include"I2C.h"
#include"ZLG7290.h"

#define uchar unsigned char
#define uint unsigned int

#define N 50

sbit wr=P2^5;
sbit chab=P2^7;						//接口定义
sbit cs=P2^6;

uint Ph=0;
uchar Amp=10;
uchar chPA=2;                                           //相幅转换;

uchar code Sin[N]={64,72,80,88,95,102,108,114,118,122,125,127,127,127,126,124,120,116,111,105,98,91,84,76,68,59,51,43,36,29,22,16,11,7,3,1,0,0,0,2,5,9,13,19,25,32,39,47,55,63};
uchar SinB[N]={0};

/*延时0.01s*/
void Delay(unsigned int t)
{
	do
	{
		TH0 = 0xDC;
		TL0 = 0x00;
		TR0 = 1;
		while ( !TF0 );
		TF0 = 0;
		TR0 = 0;
	} while (--t);
}

/*全部清显示*/
void ClearAll()
{
  uchar i;
  for (i=0;i<8;i++)
   ZLG7290_Download(i,0,0,31);
}

/*系统初始化*/
void SystemInit()
{
	I2C_Init();
	TMOD = 0x01;
	Delay(30);
}

/*显示相位*/
void DisplayPhase()
    { if(Ph==0)ZLG7290_Download(7,0,0,0);
           else if(Ph<100){ZLG7290_Download(7,0,0,Ph/10);ZLG7290_Download(6,0,0,0);}
	      else {ZLG7290_Download(7,0,0,Ph/100);ZLG7290_Download(6,0,0,Ph/10%10);ZLG7290_Download(5,0,0,0);}
     }

/*显示振幅*/
void DisplayAmp()
     {
      ZLG7290_Download(2,1,0,Amp/10);
      ZLG7290_Download(1,0,0,Amp%10);
      ZLG7290_Download(0,0,0,0);
    }

/*移相*/
void PhaseUp()
{uint i,j;uchar temp;
 if(Ph!=330)
  {Ph=Ph+30;
    for(j=0;j<4;j++)
       {temp=SinB[N-1];
          for(i=N-1;i>0;i--)
           {SinB[i]=SinB[i-1];
            }
         SinB[0]=temp;
        }
   }
 else{Ph=0;for(i=0;i<N;i++)SinB[i]=(int)((float)Sin[i]*(float)(Amp)/10.0); }
}
void PhaseDn()
{uint i,j;uchar temp;
if(Ph>=30)
   {Ph=Ph-30;
    for(j=0;j<4;j++)
    {  temp=SinB[0];
       for(i=0;i<N;i++)
       {SinB[i]=SinB[i+1];
       }
       SinB[N-1]=temp;
    }
   }
}

/*变幅*/
void AmpUp()
{
 uint i;
 if(Amp<20)
   {
    Amp=Amp+2;
    for(i=0;i<N;i++)
      {
       SinB[i]=(int)(SinB[i]*(float)(Amp)/(float)(Amp-2));
      }
   }

}
void AmpDn()
{
uint i;
 if(Amp>2)
   {
    Amp=Amp-2;
    for(i=0;i<N;i++)
      {
       SinB[i]=(int)(SinB[i]*(float)(Amp)/(float)(Amp+2));
      }
   }

}

/*重置*/
void reset()
{ uint i;
 Ph=0;Amp=10;
 for(i=0;i<N;i++)
 SinB[i]=Sin[i]; 
 //
}



/*测试按键功能*/
void KeyT()interrupt 0
{
	unsigned char KeyValue;
	ClearAll();		
	ZLG7290_ReadReg(ZLG7290_Key,&KeyValue);
	                                    
switch(KeyValue)
{case 1:chPA=(chPA+1)%3;break; 
 case 2:switch(chPA)
         {case 2:break;
          case 0:PhaseUp();break;
          case 1:AmpUp();break;
         }break;
 case 3:switch(chPA)
         {case 2:break;
          case 0:PhaseDn();break;
          case 1:AmpDn();break;
         }break;
 case 4:reset();break;
}
 DisplayPhase();DisplayAmp();
 ZLG7290_WriteReg(ZLG7290_FlashOnOff,0x11);   
 if(chPA==0)ZLG7290_Flash(0xf0);
    else if(chPA==1)ZLG7290_Flash(0x0f);
           else ZLG7290_Flash(0x00);	   
		  
}

void main()
{  uchar i;
	EA = 0;
	IT0 = 1;	//负边沿触发中断
	EX0 = 1;	//允许外部中断
	EA = 1;
SystemInit();
ClearAll();
DisplayPhase();DisplayAmp();
ZLG7290_Flash(0x00);

for(i=0;i<N;i++)SinB[i]=Sin[i];

 cs=0;wr=1;chab=1;i=0;

 while(1)
 {
  wr=1;
  chab=!chab;
  if(chab==0)P0=Sin[i];
  else P0=SinB[i];
  wr=0 ;
  if(chab){i++; if(i>=N)i=0; }
 }
}

⌨️ 快捷键说明

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