#include<reg52.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;x<110;x++)
for(y=z;y>0;y--);
}
void prepare() //tlc5615的初始化
{
cs=1;
din=1;
clk=0;
cs=0; //cs的上升沿和下降沿必须在clk为低时进?
}
void Squtranslator() //方波函数
{
uchar j;
uchar dat1=0x7f;
while(flag)
{
do{
prepare();
dat=dat1;
for(j=0;j<12;j++)
{
din=(bit)(dat>>7); //将数据的最高位赋给din
clk=1;
dat=dat<<1; //一位位的传输
clk=0;
}
cs=1; //cs的上升沿和下降沿必须在clk为低时进行
delay(200); //使高低电平持续一段时间
if(dat1==0)
dat1=0x7f; //完成了0和0x7f之间的替换
else
dat1=0;
}while(flag);
}
}
void Tratranslator() //锯齿波的发生函数
{
uchar j;
uchar dat1=0x7f;
while(flag)
{
do{
prepare();
dat=dat1;
for(j=0;j<12;j++)
{
din=(bit)(dat>>7); //将数据的最高位赋给din
clk=1;
dat=dat<<1; //一位位的传输
clk=0;
}
cs=1; //cs的上升沿和下降沿必须在clk为低时进行
delay(2); //稍加延时
dat1--;
}while(flag && dat1); //一旦有终止信号就可以停止
do{
prepare();
dat=dat1;
for(j=0;j<12;j++)
{
din=(bit)(dat>>7); //将数据的最高位赋给din
clk=1;
dat=dat<<1; //一位位的传输
clk=0;
}
cs=1; //cs的上升沿和下降沿必须在clk为低时进行
delay(2); //稍加延时
dat1++;
}while(flag && (!(dat1==0x7f)));
}
}
void Sintranslator(uchar wave[],uchar num )//正弦波的转换函数
{
uchar i,j;
uchar dat1;
do{
for(i=0;i<num;i++)
{
prepare();
dat1=wave[i]; //打开片选 开始工作
for(j=0;j<12;j++)
{
din=(bit)(dat1>>7); //将数据的最高位赋给din
clk=1;
dat1=dat1<<1; //一位位的传输
clk=0;
if(flag==0)break;
}
cs=1; //cs的上升沿和下降沿必须在clk为低时进行
delay(1); //稍加延时
if(flag==0)break;
}
}while(flag); //等待控制键的暂停
}
void keyscan() //切换功能按键返回键值函数
{
uchar i;
for(i=0;i<4;i++)
{
if(key1==0)
{
delay(10);
if(key1==0)
{
keydat++;
do{}while(!key1); //松手检测
if(keydat==4)keydat=1;//加满回零处理
}
}
}
}
void keycountrl() //切断输出控制函数
{
if(key2==0)
{
delay(10);
if(key2==0)
{
flag=0;
do{}while(!key2); //松手检测
}
}
}
void main ()
{
uchar temp;
TMOD=0x01; //确定定时器的工作方式
TH0=(65536-50000)/256; //给定时器0赋予初值
TL0=(65536-50000)%256;
EA=1; //开总中断
ET0=1; //开启定时器0中断
TR0=1;
while(1)
{
do{
switch(keydat)
{
case 1: flag=1;do{Sintranslator(tosin,141);}while(flag);break;
case 2: flag=1;do{Tratranslator();}while(flag);break;
case 3: flag=1;do{Squtranslator();}while(flag);break;
default:break;
}
}while(flag);
temp=keydat; //装载键值
while(keydat==temp); //在这里等待键值的改变
}
}
void Time0() interrupt 1
{
TH0=(65536-50000)/256; //定时器0用来扫描按键不断地扫描d
TL0=(65536-50000)%256;
num++;
if(num==4)
{
keyscan();
keycountrl();
num=0;
}
}
|