
高级技术员
 
- 积分
- 595
- 在线时间
- 239 小时
- 精华
- 0
- 注册时间
- 2016-5-30
- 最后登录
- 2021-3-23
|
#include <absacc.h>
#include <reg52.h>
#include<stdio.h>
#include <stdlib.h>
#include<math.h>
#define DA0832 XBYTE[0xfffe]
#define uchar unsigned char
#define uint unsigned int
sbit S1 = P1^0;
sbit S2 = P1^1;
sbit S3 = P1^2;
sbit S4 = P1^3;
//00锯齿波;01方波;10三角波;11正弦波
void Delay_MS(uint);// delay
void stair(uchar AMP);//锯齿波
void square(uchar AMP, uchar THL,uchar TLL);//方波
void trian(uchar AMP);//三角波
//void sin1();//正弦波
void getSinTab(uchar point,uint maxnum);
void scan(uint AMP);//扫描函数
void Uart1_Init();
static uint Times=0;
static uint amp=200;
void main()
{
Uart1_Init();
while(1)
{
if(0==S3)
{
Delay_MS(100);
while(1==S3);
amp-=50;
if(amp==0)amp=200;
}
if(0==S4)
{
Delay_MS(100);
while(1==S4);
Times+=50;
if(Times==200)Times=0;
}
scan(amp);
}
}
void scan(uint AMP)
{
if((0 == S1) &&(0 == S2))
stair(AMP);
else if((1 == S1) && (0 ==S2))
square(AMP,10,10);
else if((0 == S1) && (1 == S2))
trian(AMP);
else
getSinTab(300,AMP);
}
void Delay_MS(uint n)
{
uint k;
for(n; n >0 ;n--)
for(k = 10; k > 0 ;k--);
}
void stair(uchar AMP)
{
uchar i;
for(i = 0 ;i < AMP; i++)
{
DA0832 = i;
Delay_MS(Times);
}
}
void square(uchar AMP, uchar THL, uchar TLL)
{
DA0832 = 255 - AMP;
Delay_MS(THL);
DA0832 = 255;
Delay_MS(TLL);
Delay_MS(Times);
}
void trian(uchar AMP)
{
uchar i;
for(i = 255 - AMP ;i < 255; i++)
{
DA0832 = i;
Delay_MS(Times/2);
}
for(i-1 ;i > 255 - AMP; i--)
{
DA0832 = i;
Delay_MS(Times/2);
}
}
void getSinTab(uchar point,uint maxnum)
{
uchar i=0;
float x; //弧度
float jiao;//角度 分度角
jiao=360.000/point;
for(i=0;i<point;i++)
{
x=jiao*i; //得到角度值
x=x*0.01744; //角度转弧度 ?弧度=角度*(π/180)
DA0832=(maxnum/2)*sin(x)+(maxnum/2);
Delay_MS(Times);
}
}
void Uart1_Init()
{
SCON=0X50; //0101 000??????????sm0,sm1?01,??????1
TMOD=0X20; //?????????2
TH1 = 0xfd; //?????
TL1 = 0xfd;
EA=1; //?????
ES=1; //???????????????
TR1 = 1; //?????
}
void Usart() interrupt 4
{
uchar rec;
SBUF=55; // 发送数据55
while(!TI);
TI=0;
if(RI)
{
rec=SBUF;
RI=0;
}
//?????????
}
|
-
|