#include <reg51.h> #include "ABSACC.H" #include "INTRINS.H" #define IN0 0x7000 sbit CS=P2^0; sbit sclk = P2^1; sbit din = P2^2; sbit fang=P1^0; sbit sin=P1^1; sbit sanjiao=P1^2; sbit jc=P1^3; unsigned char code zhx[]={64,67,70,73,76,79,82,85,88,91,94,96, 99,102,104,106,109,111,113,115,117,118,120,121,123,124,125,126, 126,127,127,127,127,127,127,127,126,126,125,124,123,121,120,118, 117,115,113,111,109,106,104,102,99,96,94,91,88,85,82,79,76,73,70, 67,64,60,57,54,51,48,45,42,39,36,33,31,28,25,23,21,18,16,14,12,10, 9,7,6,4,3,2,1,0,1,2,3,4,6,7,9,10,12,14,16,18,21, 23,25,28,31,33,36,39,42,45,48,51,54,57,60}; unsigned char code table[] = { 0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0, 0xFE, 0xF6, 0xEE, 0x3E, 0x9C, 0x7A, 0x9E, 0x8E}; unsigned char Volt; unsigned char Volt1; void putch(unsigned char n) { SBUF=n; while(TI==0); TI=0; } void Selfdelay(unsigned char n) { unsigned i; while(n--) for(i=150;i>0;i--); } void disp(unsigned char Volt) { unsigned char i,j; i=table[Volt/10]|0x01; j=table[Volt%10]; putch(i); putch(j); Selfdelay(200); } void transition() { XBYTE[IN0]=0; Selfdelay(300); Volt=XBYTE[IN0]; Volt1=Volt*50/0xff; disp(Volt1); Selfdelay(500); } void conv(unsigned int q) { unsigned char i; q <<= 6; sclk = 0; CS = 1; CS = 0; for(i=0;i<12;i++) { din=(bit)(q & 0x8000); sclk=1; q <<= 1; sclk=0; } sclk=0; CS=1; } void juchi(){ int i=0; for(i=0;i<=500;i++) { conv(i); } } void sjb() { int i=0; for(i=0;i<=500;i++) { conv(i); } for(i=500;i>=0;i--) { conv(i); } } void fb() { conv(Volt); transition(); conv(0); transition(); } void zxhs() { int i = 0; for(i=0;i<=120;i++) { conv(zhx[i]); } } void main() { SCON=0x00; TI=0; while(1) { fb(); } }改进上述程序,使其能够调节波形
时间: 2023-09-18 20:11:24 浏览: 121
要实现波形的调节,可以在程序中添加一个变量来控制波形的形状,然后根据变量的值不同输出不同的波形。例如,可以添加一个变量waveType,用来表示波形的类型,然后在transition()函数中根据waveType的值输出不同的波形。
以下是修改后的程序,添加了波形调节功能:
``` c
#include <reg51.h>
#include "ABSACC.H"
#include "INTRINS.H"
#define IN0 0x7000
sbit CS = P2^0;
sbit sclk = P2^1;
sbit din = P2^2;
sbit fang = P1^0;
sbit sin = P1^1;
sbit sanjiao = P1^2;
sbit jc = P1^3;
unsigned char code zhx[] = {
64,67,70,73,76,79,82,85,88,91,94,96,
99,102,104,106,109,111,113,115,117,118,
120,121,123,124,125,126,126,127,127,127,
127,127,127,127,126,126,125,124,123,121,
120,118,117,115,113,111,109,106,104,102,
99,96,94,91,88,85,82,79,76,73,70,
67,64,60,57,54,51,48,45,42,39,36,
33,31,28,25,23,21,18,16,14,12,10,
9,7,6,4,3,2,1,0,1,2,3,
4,6,7,9,10,12,14,16,18,21,
23,25,28,31,33,36,39,42,45,48,
51,54,57,60
};
unsigned char code table[] = {
0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0,
0xFE, 0xF6, 0xEE, 0x3E, 0x9C, 0x7A, 0x9E, 0x8E
};
unsigned char Volt;
unsigned char Volt1;
unsigned char waveType = 0; // 波形类型,0表示正弦波,1表示方波,2表示三角波
void putch(unsigned char n) {
SBUF = n;
while(TI == 0);
TI = 0;
}
void Selfdelay(unsigned char n) {
unsigned int i;
while(n--) {
for(i = 150; i > 0; i--);
}
}
void disp(unsigned char Volt) {
unsigned char i, j;
i = table[Volt / 10] | 0x01;
j = table[Volt % 10];
putch(i);
putch(j);
Selfdelay(200);
}
void transition() {
XBYTE[IN0] = 0;
Selfdelay(300);
Volt = XBYTE[IN0];
Volt1 = Volt * 50 / 0xff;
if(waveType == 0) { // 正弦波
conv(zhx[Volt1]);
} else if(waveType == 1) { // 方波
if(Volt1 < 25) {
conv(0);
} else {
conv(255);
}
} else if(waveType == 2) { // 三角波
if(Volt1 < 25) {
conv(Volt1 * 10);
} else {
conv((50 - Volt1) * 10);
}
}
transition();
conv(0);
transition();
}
void conv(unsigned int q) {
unsigned char i;
q <<= 6;
sclk = 0;
CS = 1;
CS = 0;
for(i = 0; i < 12; i++) {
din = (bit)(q & 0x8000);
sclk = 1;
q <<= 1;
sclk = 0;
}
sclk = 0;
CS = 1;
}
void juchi() {
int i = 0;
for(i = 0; i <= 500; i++) {
conv(i);
}
}
void sjb() {
int i = 0;
for(i = 0; i <= 500; i++) {
conv(i);
}
for(i = 500; i >= 0; i--) {
conv(i);
}
}
void fb() {
conv(Volt);
transition();
}
void zxhs() {
int i = 0;
for(i = 0; i <= 120; i++) {
conv(zhx[i]);
}
}
void main() {
SCON = 0x00;
TI = 0;
while(1) {
// 调节波形类型
if(fang == 0) { // 方波
waveType = 1;
} else if(sanjiao == 0) { // 三角波
waveType = 2;
} else {
waveType = 0;
}
fb();
}
}
```
在程序中添加了一个变量waveType,用来表示波形的类型,然后在transition()函数中根据waveType的值输出不同的波形。在main函数中,可以通过按下不同的按键来切换波形类型。按下方波按键后,波形类型变为1,表示输出方波;按下三角波按键后,波形类型变为2,表示输出三角波;否则波形类型为0,表示输出正弦波。
阅读全文