我贴个代码给你吧,用51的单片机来实现,也有详细链接自己可以看看
#include <reg52.h>
#include <intrins.h>
sbit OE=P3^0;
sbit EOC=P3^1;
sbit CLK=P3^4;
sbit ST=P3^2;
sbit K1=P1^4;
sbit K2=P1^5;
sbit K3=P1^6;
sbit K4=P1^7;
sbit LEDB=P1^0;
sbit LEDG=P1^1;
sbit LEDR=P1^2;
sbit LEDY=P1^3;
#define ADC P2
#define uchar unsigned char
#define uint unsigned int
uchar code sin[]={0,0,0,0,1,1,2,3,4,5,6,8,
9,11,13,15,17,19,22,24,
27,30,33,36,39,42,46,49,
53,56,60,64,68,72,76,80,
84,88,92,97,101,105,110,
114,119,123,128,132,136,
141,145,150,154,158,163,
167,171,175,179,183,187,
191,195,199,202,206,209,
213,216,219,222,225,228,
231,233,236,238,240,242,
244,246,247,249,250,251,
252,253,254,254,255,255,255};
void timer () interrupt 1 //用定时器来创造时钟函数
{
CLK=~CLK;
}
uchar adc0808()
{
uchar t;
ST=0;
ST=1; //形成下跳脉冲信号,开启ad
ST=0;
while(!EOC); //等待转换完成
OE=1; //允许输出
t=ADC; //获取输出
return (t);
}
/**********方波*********/
void square()
{
uchar a,b;
for(a=0;a<127;a++)
{
P0=0xff;
P2=0xff;
b=adc0808();
b=~b;
while(b--);
}
for(a=0;a<127;a++)
{
P0=0x00;
P2=0xff;
b=adc0808();
b=~b;
while(b--);
}
}
/********锯齿波*******/
void sawtooth()
{
uchar a,b;
for(a=0;a<255;a++)
{
P0=a;
P2=0xff;
b=adc0808();
b=~b;
while(b--);
}
}
/*******三角波*******/
void triang()
{
uchar a,b;
for(a=0;a<254;a=a+2)
{
P0=a;
P2=0xff;
b=adc0808();
b=~b;
while(b--);
}
for(a;a>1;a=a-2)
{
P0=a;
P2=0xff;
b=adc0808();
b=~b;
while(b--);
}
}
/*******正弦波******/
void sinwave()
{
uchar a,b;
for(a=0;a<92;a++)
{
P0=sin[a];
P2=0xff;
b=adc0808();
b=~b;
while(b--);
}
for(a=a-1;a>0;a--)
{
P0=sin[a];
P2=0xff;
b=adc0808();
b=~b;
while(b--);
}
}
main()
{
EA=1; //开定时
ET0=1;
TMOD=0X02;
TH0=0Xff;
TL0=0Xff;
TR0=1;
while(1)
{
P0=0;
if(K1==0)
{
LEDB=0;
square(); //方波,蓝灯
}
LEDB=1;
if(K2==0)
{
LEDG=0;
sawtooth(); //锯齿波,绿灯
}
LEDG=1;
if(K3==0)
{
LEDR=0;
triang(); //三角波, 红灯
}
LEDR=1;
if(K4==0)
{
LEDY=0;
sinwave(); //正弦波,黄灯
}
LEDY=1;
}
}
————————————————
版权声明:本文为CSDN博主「森屿~~」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_62281625/article/details/122104086 |