80C51单片机连接DAC0832和LCD1602,输出锯齿波,正弦波,三角波,矩形波 C语言代码
时间: 2024-05-16 18:16:55 浏览: 194
以下是80C51单片机连接DAC0832和LCD1602,输出锯齿波,正弦波,三角波,矩形波的C语言代码:
```c
#include <reg51.h>
#include <math.h>
// 定义LCD1602引脚
sbit RS = P2^0;
sbit EN = P2^1;
sbit RW = P2^2;
sbit LCD_D4 = P2^4;
sbit LCD_D5 = P2^5;
sbit LCD_D6 = P2^6;
sbit LCD_D7 = P2^7;
// 定义DAC0832引脚
sbit DAC_CS = P1^0;
sbit DAC_CLK = P1^1;
sbit DAC_DIN = P1^2;
// 定义函数
void Delay(int time);
void LCD_Init();
void LCD_WriteCommand(unsigned char command);
void LCD_WriteData(unsigned char data);
void DAC_Write(unsigned int data);
void SawtoothWave();
void SineWave();
void TriangleWave();
void SquareWave();
// 主函数
void main()
{
LCD_Init(); // 初始化LCD1602
while(1)
{
SawtoothWave(); // 输出锯齿波
Delay(1000);
SineWave(); // 输出正弦波
Delay(1000);
TriangleWave(); // 输出三角波
Delay(1000);
SquareWave(); // 输出矩形波
Delay(1000);
}
}
// 延时函数
void Delay(int time)
{
int i, j;
for(i=0; i<time; i++)
for(j=0; j<100; j++);
}
// LCD1602初始化函数
void LCD_Init()
{
LCD_WriteCommand(0x38); // 设置8位数据线,2行显示,5x7点阵字符
LCD_WriteCommand(0x0C); // 开启显示,光标关闭
LCD_WriteCommand(0x06); // 光标右移,字符不移动
LCD_WriteCommand(0x01); // 清屏
}
// 写入命令到LCD1602
void LCD_WriteCommand(unsigned char command)
{
RS = 0; // RS置低,表示写入的是命令
RW = 0; // RW置低,表示写入模式
LCD_D7 = command & 0x80;
LCD_D6 = command & 0x40;
LCD_D5 = command & 0x20;
LCD_D4 = command & 0x10;
EN = 1; // EN置高,使能
Delay(1);
EN = 0; // EN置低,完成写入
LCD_D7 = command & 0x08;
LCD_D6 = command & 0x04;
LCD_D5 = command & 0x02;
LCD_D4 = command & 0x01;
EN = 1; // EN置高,使能
Delay(1);
EN = 0; // EN置低,完成写入
}
// 写入数据到LCD1602
void LCD_WriteData(unsigned char data)
{
RS = 1; // RS置高,表示写入的是数据
RW = 0; // RW置低,表示写入模式
LCD_D7 = data & 0x80;
LCD_D6 = data & 0x40;
LCD_D5 = data & 0x20;
LCD_D4 = data & 0x10;
EN = 1; // EN置高,使能
Delay(1);
EN = 0; // EN置低,完成写入
LCD_D7 = data & 0x08;
LCD_D6 = data & 0x04;
LCD_D5 = data & 0x02;
LCD_D4 = data & 0x01;
EN = 1; // EN置高,使能
Delay(1);
EN = 0; // EN置低,完成写入
}
// 写入DAC0832数据
void DAC_Write(unsigned int data)
{
unsigned char i;
DAC_CS = 0; // 使能DAC0832
for(i=0; i<16; i++)
{
if(data & 0x8000)
DAC_DIN = 1;
else
DAC_DIN = 0;
DAC_CLK = 0;
data = data << 1;
DAC_CLK = 1;
}
DAC_CS = 1; // 禁用DAC0832
}
// 输出锯齿波
void SawtoothWave()
{
unsigned int i, j;
for(i=0; i<256; i++)
{
j = i*16;
DAC_Write(j); // 写入DAC0832数据
LCD_WriteCommand(0x80); // 光标回到第一行第一列
LCD_WriteData('S');
LCD_WriteData('a');
LCD_WriteData('w');
LCD_WriteData('t');
LCD_WriteData('o');
LCD_WriteData('o');
LCD_WriteData('t');
LCD_WriteData('h');
LCD_WriteData(':');
LCD_WriteData(i/100+48);
LCD_WriteData(i%100/10+48);
LCD_WriteData(i%10+48);
Delay(1);
}
}
// 输出正弦波
void SineWave()
{
unsigned int i, j;
for(i=0; i<256; i++)
{
j = (unsigned int)(127*sin(2*3.1415926*i/256)+127);
DAC_Write(j); // 写入DAC0832数据
LCD_WriteCommand(0xC0); // 光标回到第二行第一列
LCD_WriteData('S');
LCD_WriteData('i');
LCD_WriteData('n');
LCD_WriteData('e');
LCD_WriteData(':');
LCD_WriteData(j/100+48);
LCD_WriteData(j%100/10+48);
LCD_WriteData(j%10+48);
Delay(1);
}
}
// 输出三角波
void TriangleWave()
{
unsigned int i, j;
for(i=0; i<256; i++)
{
if(i<128)
j = i*2;
else
j = (255-i)*2;
DAC_Write(j); // 写入DAC0832数据
LCD_WriteCommand(0x80); // 光标回到第一行第一列
LCD_WriteData('T');
LCD_WriteData('r');
LCD_WriteData('i');
LCD_WriteData('a');
LCD_WriteData('n');
LCD_WriteData('g');
LCD_WriteData('l');
LCD_WriteData('e');
LCD_WriteData(':');
LCD_WriteData(j/100+48);
LCD_WriteData(j%100/10+48);
LCD_WriteData(j%10+48);
Delay(1);
}
}
// 输出矩形波
void SquareWave()
{
unsigned int i, j;
for(i=0; i<256; i++)
{
if(i<128)
j = 0;
else
j = 255;
DAC_Write(j); // 写入DAC0832数据
LCD_WriteCommand(0xC0); // 光标回到第二行第一列
LCD_WriteData('S');
LCD_WriteData('q');
LCD_WriteData('u');
LCD_WriteData('a');
LCD_WriteData('r');
LCD_WriteData('e');
LCD_WriteData(':');
LCD_WriteData(j/100+48);
LCD_WriteData(j%100/10+48);
LCD_WriteData(j%10+48);
Delay(1);
}
}
```
注意:此代码仅供参考,具体实现还需根据硬件电路进行调整。
阅读全文