51单片机按键控制的波形发生器,用TLC芯片请给出代码
时间: 2024-03-08 20:46:37 浏览: 52
基于51单片机的波形发生器
5星 · 资源好评率100%
好的,以下是基于51单片机按键控制TLC芯片的波形发生器代码:
```c
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit SCK=P3^5;
sbit SDI=P3^6;
sbit LE=P3^7;
sbit KEY1=P1^0;
sbit KEY2=P1^1;
sbit KEY3=P1^2;
uchar code SIN[128]={128,131,134,137,140,143,146,149,152,156,159,162,165,167,170,173,176,179,182,185,187,190,193,196,198,201,203,206,208,211,213,215,218,220,222,224,226,228,230,232,233,235,237,238,240,241,243,244,245,247,248,249,250,251,252,253,254,254,255,255,255,255,255,255,255,255,255,254,254,253,252,251,250,249,248,247,245,244,243,241,240,238,237,235,233,232,230,228,226,224,222,220,218,215,213,211,208,206,203,201,198,196,193,190,187,185,182,179,176,173,170,167,165,162,159,156,152,149,146,143,140,137,134,131,128,124,121,118,115,112,109,106,102,99,96,93,90,88,85,82,79,76,73,70,68,65,62,59,57,54,52,49,47,44,42,40,37,35,33,30,28,26,24,22,20,18,16,15,13,11,10,8,7,5,4,3,2,1,1,0,0,0,0,0,0,0,0,0,1,1,2,3,4,5,6,7,8,10,11,12,14,15,17,18,20,22,23,25,27,29,31,33,35,38,40,42,44,47,49,52,55,57,60,63,66,69,72,75,78,81,84,87,91,94,97,101,104,107,110,113,116,119,122,125};
void Write595(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
SCK=0;
if(dat&0x80)
SDI=1;
else
SDI=0;
dat<<=1;
SCK=1;
}
LE=0;
_nop_();
LE=1;
}
void init_TLC()
{
Write595(0x00);
Write595(0x00);
Write595(0x00);
Write595(0x00);
}
void main()
{
uchar i=0,j=0;
init_TLC();
while(1)
{
if(KEY1==0)
i++;
if(KEY2==0)
i--;
if(KEY3==0)
j++;
if(i>127)
i=0;
if(i<0)
i=127;
if(j>255)
j=0;
Write595(SIN[i]);
Write595(SIN[i]);
Write595(SIN[i]);
Write595(SIN[i]);
}
}
```
注:这里使用了一个名为SIN的数组,存储了一个周期内的正弦波数据。根据你的具体需求,可以替换为其他类型的波形数据。
阅读全文