用C语言中的数组编写出正弦波、方波和三角波,写一种就可以了

2025-01-02 18:24:17
推荐回答(1个)
回答1:

本程序加了一个LCD1602,,,,你自己选择使用哈

#include //包含头文件
#include
#define uchar unsigned char //宏定义
#define uint unsigned int
uchar i , j ,n,z; //定义全局变量
sbit RS=P2^0; //定义端口
sbit RW=P2^1;
sbit E =P2^2;
sbit k=P2^7;
sbit k1=P2^6;
uchar code str0[]={"Sine Wave "}; //定义LCD1602显示字符数组
uchar code str1[]={"Triangle Wave"};
uchar code str2[]={"Square Wave "};
uchar code table[3][40]= //定义显示波形的二维数组
{
0x80,0x94,0xa7,0xb9,0xca,0xd9,0xe7,0xf1,0xf8,0xfd,
0xff,0xfd,0xf8,0xf1,0xe7,0xd9,0xca,0xb9,0xa7,0x94,
0x80,0x6c,0x55,0x46,0x35,0x25,0x18,0x0e,0x06,0x02,
0x00,0x02,0x06,0x0e,0x18,0x25,0x35,0x46,0x55,0x6c,
0x80,0x8c,0x99,0xa6,0xb3,0xc0,0xcc,0xd9,0xe6,0xf2,
0xff,0xf2,0xe6,0xd9,0xcc,0xc0,0xb3,0xa6,0x99,0x8c,
0x80,0x73,0x66,0x5a,0x4d,0x40,0x33,0x26,0x1a,0x0d,
0x00,0x0d,0x1a,0x26,0x33,0x40,0x4d,0x5a,0x66,0x73,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
void delay(uint z) //延时程序
{
for(z;z>0;z--)
{

}
}
void Write_command(uchar cmd) //LCD1602写入命令程序
{
RS=0; //选择指令寄存器
RW=0; //写操作
E =1; //使能端
P1=cmd;
delay(1);
E =0;
delay(10); //延时 使LCD1602处于不忙状态
}
void Write_data(uchar cmd) //LCD1602写入数据命令
{
RS=1; //选择数据寄存器
RW=0; //写操作
E =1; //时能端
P1=cmd;
delay(1);
E=0;
delay(10); //延时 使LCD1602处于不忙状态
}
void Lcd_init() //LCD1602初始化程序
{
Write_command(0x38); //设置16*2显示 5*7点阵 8位数据接口
Write_command(0x0c); //LCD1602开显示 ,不显示光标
Write_command(0x06); //读或写一字符后指针加一 ,显示不移动
Write_command(0x80+0x00); //数据指针地址设置
}
void Display() //LCD1602显示英文程序
{
Lcd_init();
if (i==0)
{
for (n=0;n<13;n++)
{
Write_data(str0[n]);
}
}
else if(i==1)
{
for(n=0;n<13;n++)
{
Write_data(str1[n]);
}
}
else
{
for(n=0;n<13;n++)
{
Write_data(str2[n]);
}
}
}
void main() //主函数
{
Display();
while(1)
{
for(j=0;j<40;j++)
{
P0=table[i][j];

}
if(k==0) //开关程序
{
delay(1);
if(k==0);
{
if(k==0)
{
i++;
if (i==3) i=0;
Display();
}

}
while(k==0);
}
}
}