c语言锯齿波发生函数 锯齿波c语言程序( 七 )


EA=1;
while(1)
{
//DAout(0xff);//可输出TTL波形
//DAout(0x80);
//T_temp=32;
}
}
void timer0() interrupt 1
{
TH0=THtemp;
TL0=TLtemp;
if(waveform==0)sine_out();
else if(waveform==1) triangle_out();
else if(waveform==2) square_out();
else if(waveform==3) sawtooth_out();//tian jian
}
void key_int0() interrupt 0
{
uchar keytemp;
uint total_freq; //总频率
EA=0; TR0=0;//关总中断与定时器
delay(5);//延时够吗???
if(key==0)//确实有按键按下而引发中断
{
keytemp=P30xf0; //获取P3口高四位的值
switch(keytemp)
{
case 0xe0://选择波形
waveform++;
if(waveform3) waveform=0;//jiang 2 gai wei 3
break;
case 0xd0://频率按规定单位依次增加
wavefreq[waveform]++;
if(wavefreq[waveform]10) wavefreq[waveform]=1; // /*这边要用“10” , 因为它比“=11”可靠
break; //性更高,使加数有个上限,不会一直加下去*/
case 0xb0://频率按规定单位依次衰减
wavefreq[waveform]--;
if(wavefreq[waveform]1) wavefreq[waveform]=10; //这边要用“1” , 因为它比“=0”可靠性更高
break;
case 0x70://TTL输出
DA_S2=1; //使DAC寄存器关闭
break;
}
THtemp=waveTH[waveform*10+(wavefreq[waveform]-1)]; //方括号中选取第几个数后,并把该值赋给T_temp
TLtemp=waveTL[waveform*10+(wavefreq[waveform]-1)];
total_freq= wavefreq[waveform] * freq_unit[waveform]; //求输出频率(个数*单位)
lcd_hang2[5]=total_freq%10+0x30;//在液晶中显示个位,(0x30 在液晶显示中表示数字0)
total_freq/=10; lcd_hang2[4]=total_freq%10+0x30; //在液晶中显示时十位
total_freq/=10; lcd_hang2[3]=total_freq%10+0x30; //在液晶中显示时百位
total_freq/=10; lcd_hang2[2]=total_freq%10+0x30; //在液晶中显示时千位
disp_lcd(0x80,lcd_hang1[waveform*16]);//在第一行显示
disp_lcd(0xc0,lcd_hang2);//在第二行显示
}
wavecount=0; //'抽点'计数清零
while(!key);
EA=1; TR0=1;//开启总中断与定时器
产生三角波数据值的C程序,/**************************************/
/*信号发生器 (正弦波,方波,三角波)*/
/*************************************/
#includereg52.h
#include intrins.h
#define uchar unsigned char
#define uint unsigned int
sbit cs=P2^0;//tlc5615片选端口
sbit clk=P2^1;//tlc5615时钟线
sbit din=P2^2;//tlc5615传输端口
sbit key1=P1^0;
sbit key2=P1^1;//按键的单片机接口
uchar keydat;
uchar flag;//波形发生终止信号的标志位一旦被置零立马停止发信号
uchar flagsqu;//方波高低电平控制为(运用定时器1中断控制)
uchar m,num;
uchar dat=0xff;
uchar code tosin[141]={//正弦波的编码
0x00,0x01,0x02,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0a,0x0b,0x0d,0x0e,0x10,0x11,0x13,0x15,0x16,
0x18,0x1a,0x1c,0x1e,0x20,0x22,0x25,0x27,0x29,0x2b,
0x2e,0x30,0x33,0x35,0x38,0x3a,0x3d,0x40,0x43,0x45,
0x48,0x4c,0x4e,0x51,0x55,0x57,0x5a,0x5d,0x60,0x63,
0x66,0x69,0x6c,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,
0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7e,
0x7f,0x80,0x7f,0x7e,0x7e,0x7d,0x7c,0x7b,0x7a,0x79,
0x78,0x77,0x76,0x75,0x74,0x73,0x72,0x6f,0x6c,0x69,
0x66,0x63,0x60,0x5d,0x5a,0x57,0x55,0x51,0x4e,0x4c,
0x48,0x45,0x43,0x40,0x3d,0x3a,0x38,0x35,0x33,0x30,
0x2e,0x2b,0x29,0x27,0x25,0x22,0x20,0x1e,0x1c,0x1a,
0x18,0x16,0x15,0x13,0x11,0x10,0x0e,0x0d,0x0b,0x0a,
0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x02,0x01,
0x00};
void delay(uchar z)//延时函数
{
uchar x,y;
for(x=0;x110;x++)
for(y=z;y0;y--);
}

推荐阅读