#include<public.h>
#define dac0832 XBYTE[0x7fff]
sbit P3_0=P3^0;
sfr16 RCAP2 = 0xCA; //T2捕获寄存器
sfr T2MOD = 0xC9;
float freq_test=99;
uint freq_test1=50,Now=20000;
uint freq=50,time_0=0,freq_10,freq_50,freq_100;
char th0,tl0,vaule=0;
uchar sin_point=0,sin_count=0,flag=0;
uchar code sin[]={0x7F,0x8B,0x98,0xA4,0xB0,0xBB,0xC6,0xD0,0xD9,0xE2,0xE9,0xEF,0xF5,0xF9,0xFC,0xFE
,0xFE,0xFE,0xFC,0xF9,0xF5,0xEF,0xE9,0xE2,0xD9,0xD0,0xC6,0xBB,0xB0,0xA4,0x98,0x8B
,0x7F,0x73,0x66,0x5A,0x4E,0x43,0x38,0x2E,0x25,0x1C,0x15,0x0F,0x09,0x05,0x02,0x00
,0x00,0x00,0x02,0x05,0x09,0x0F,0x15,0x1C,0x25,0x2E,0x38,0x43,0x4E,0x5A,0x66,0x73
,0x7F};
uchar code sin_256[]={ 0x7F,0x82,0x85,0x88,0x8B,0x8F,0x92,0x95,0x98,0x9B,0x9E,0xA1,0xA4,0xA7,0xAA,0xAD
,0xB0,0xB3,0xB6,0xB8,0xBB,0xBE,0xC1,0xC3,0xC6,0xC8,0xCB,0xCD,0xD0,0xD2,0xD5,0xD7
,0xD9,0xDB,0xDD,0xE0,0xE2,0xE4,0xE5,0xE7,0xE9,0xEB,0xEC,0xEE,0xEF,0xF1,0xF2,0xF4
,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFB,0xFC,0xFD,0xFD,0xFE,0xFE,0xFE,0xFE,0xFE
,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFD,0xFD,0xFC,0xFB,0xFB,0xFA,0xF9,0xF8,0xF7,0xF6
,0xF5,0xF4,0xF2,0xF1,0xEF,0xEE,0xEC,0xEB,0xE9,0xE7,0xE5,0xE4,0xE2,0xE0,0xDD,0xDB
,0xD9,0xD7,0xD5,0xD2,0xD0,0xCD,0xCB,0xC8,0xC6,0xC3,0xC1,0xBE,0xBB,0xB8,0xB6,0xB3
,0xB0,0xAD,0xAA,0xA7,0xA4,0xA1,0x9E,0x9B,0x98,0x95,0x92,0x8F,0x8B,0x88,0x85,0x82
,0x7F,0x7C,0x79,0x76,0x73,0x6F,0x6C,0x69,0x66,0x63,0x60,0x5D,0x5A,0x57,0x54,0x51
,0x4E,0x4B,0x48,0x46,0x43,0x40,0x3D,0x3B,0x38,0x36,0x33,0x31,0x2E,0x2C,0x29,0x27
,0x25,0x23,0x21,0x1E,0x1C,0x1A,0x19,0x17,0x15,0x13,0x12,0x10,0x0F,0x0D,0x0C,0x0A
,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00
,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08
,0x09,0x0A,0x0C,0x0D,0x0F,0x10,0x12,0x13,0x15,0x17,0x19,0x1A,0x1C,0x1E,0x21,0x23
,0x25,0x27,0x29,0x2C,0x2E,0x31,0x33,0x36,0x38,0x3B,0x3D,0x40,0x43,0x46,0x48,0x4B
,0x4E,0x51,0x54,0x57,0x5A,0x5D,0x60,0x63,0x66,0x69,0x6C,0x6F,0x73,0x76,0x79,0x7C};
void time_2() interrupt 5
{
if(EXF2)
{
if(vaule==2)
{
EXF2=0;//外部标志位需要软件清零;
Now=RCAP2;
vaule=0;
}
else
{
EXF2=0;
vaule++;
TH2=0;
TL2=0;
}
}
else
{
TF2=0;
}
}
void time0() interrupt 1
{
TH0=th0;
TL0=tl0;
switch(flag)
{
case 0:if(sin_point>64)sin_point=0;dac0832=sin[sin_point++];break;
case 1:if(sin_point>255)sin_point=0;dac0832=sin_256[sin_point];sin_point+=freq_10;break;
case 2:if(sin_point>255)sin_point=0;dac0832=sin_256[sin_point];sin_point+=freq_50;break;
// case 3:if(sin_point>255)sin_point=0;dac0832=sin_256[sin_point];sin_point+=freq_100;break;
}
}
void main()
{
uint j=0;
system_init();
while(1)
{
if(freq<123)
flag=0;
else if(freq<227)
flag=1;
else
flag=2;
switch(flag)
{
case 0:
if(freq<53)
{
th0=(65536-time_0+10)/256;
tl0=(65536-time_0+10)%256;
}
else if(freq>=53&&freq<123)
{
th0=(65536-time_0+40)/256;
tl0=(65536-time_0+40)%256;
}
break;
case 1:
time_0=390; //10hz理论值390.6
th0=(65536-time_0+50)/256;
tl0=(65536-time_0+50)%256;
break;
case 2:
time_0=78;//50hz理论值78
th0=(65536-time_0+30)/256;
tl0=(65536-time_0+30)%256;
break;
}
freq_10=freq/10;
freq_50=freq/50-1;
freq_100=freq/100-1;
keycode_return();
}
}
void system_init()
{
//************显示初始化***********//
display_init();
//************设置频率输出**************//
//************定时器0初始化*************//
time_0=13604/freq; //理论值15624
th0=(65536-time_0+15)/256;
tl0=(65536-time_0+15)%256;
TH0=th0;
TL0=tl0;
ET0=1;
TMOD=0x01;
EA=1;
TR0=1;
//************频率测量T2初始化**********//
ET2=1;//开T2中断
T2MOD=0X00;
T2CON=0x0d;//16位捕获模式,定时,外部使能
TH2=0;
TL2=0;
}
- 1
- 2
前往页