输出1HZ正弦波、三角波、方波、锯齿波
使用LCD1602显示
DAC0832的D0~D7数据输入口,IOUT1、IOUT2互补输出口,RFB反馈端口、
VREF基准电压值(一般是5V)。用定时器控制段码表输出速度来控制波形的频率。
(定时数值)*10^(-6)*256=波形频率。
(1)锯齿波的实现原理: 锯齿波的实现过程是首先定义一个初值然后进行加法操 作, 加的步数的多少则根据要求的频率来进行。然后加到某个数之后就再重新设置为初 值, 再重复执行刚刚的操作, 如此循环下去。在本程序中初值为 00H,最大值为 FFH。
(2)三角波的实现原理: 三角波的实现是设置一个初值, 然后进行加数, 同样是加 到某个数之后再进行减数, 减到初值之后就再返回到先前的操作。此程序输入的 VREF 的电压是+5V,因此该波形输出的最大频率是初值为 00H 和最终值为 FFH,且步数为 1,这样输出的波形是最大的。
(3)方波的实现原理: 此波形的实现只需开始的时候设置一个初值然后直接输出 这个值就行了, 输出一段时间后, 然后再重新置一个数据, 然后再输出这个数据一段时 间, 但是此时的时间一定要等于前面那段时间。这样才是一个方波, 如果两个时间不相 同,那就相当于一个脉冲波了。
主函数和中断服务函数
#include <REGX52.H>
#include <Delay.h>
#include <LCD1602.h>
#include <data.h>
#include "Timer0.h"
#define BO P3
#define KEY0 P1_0
#define KEY1 P1_1
#define KEY2 P1_2
#define KEY3 P1_3
#define KEY4 P1_4
unsigned int Number=1,Signal_chose=0,n=0,THHL,x,i;
unsigned int freq=3906;
void main()
{
Timer0Init();
LCD_Init();
while(1)
{
// if(KEY4==0)
// {
// Delay(20);
// while(KEY4==0);
// Delay(20);
// freq=freq/10;
// Number=Number*10;
// }
if(KEY0==0)
{
TF0 = 0;
LCD_ShowString(1,1,"sin:");
LCD_ShowString(2,1,"FREQ:");
LCD_ShowNum(2,8,Number,3);
LCD_ShowString(2,11,"Hz");
TR0 = 1;
Signal_chose=0;
}
if(KEY1==0)
{
TF0 = 0;
LCD_ShowString(1,1,"squ:");
LCD_ShowString(2,1,"FREQ:");
LCD_ShowNum(2,8,000,3);
LCD_ShowString(2,11,"Hz");
TR0 = 1;
Signal_chose=2;
}
if(KEY2==0)
{
TF0 = 0;
LCD_ShowString(1,1,"thr:");
LCD_ShowString(2,1,"FREQ:");
LCD_ShowNum(2,8,Number,3);
LCD_ShowString(2,11,"Hz");
TR0 = 1;
Signal_chose=1;
}
if(KEY3==0)
{
TF0 = 0;
LCD_ShowString(1,1,"jch:");
LCD_ShowString(2,1,"FREQ:");
LCD_ShowNum(2,8,Number,3);
LCD_ShowString(2,11,"Hz");
TR0 = 1;
Signal_chose=3;
}
switch(Signal_chose)
{
case 0: {BO=sin[n]; break;} //正弦波
case 1: {BO=thr[n]; break;} //三角波
case 2: {
for(i=0;i<256;i++)
{BO=squ[i];}
break;
} //方波
case 3: {BO=256-n; break;} //锯齿波
default:{break;}
}
}
}
void Timer0_Routine() interrupt 1
{
THHL=65536-freq;//(freq=3906)*10^(-6)*256=1Hz
TL0=THHL%256;
TH0=THHL/256;
if(n>=255)
{n=0;}
else
{n++;}
}
LCD显示函数
#include <REGX52.H>
//引脚配置:
sbit LCD_RS=P2^0;
sbit LCD_RW=P2^1;
sbit LCD_EN=P2^2;
#define LCD_DataPort P0
//函数定义:
/**
* @brief LCD1602延时函数,12MHz调用可延时1ms
* @param 无
* @retval 无
*/
void LCD_Delay()
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
/**
* @brief LCD1602写命令
* @param Command 要写入的命令
* @retval 无
*/
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602写数据
* @param Data 要写入的数据
* @retval 无
*/
void LCD_WriteData(unsigned char Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602设置光标位置
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @retval 无
*/
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
else if(Line==2)
{
LCD_WriteCommand(0x80|(Column-1+0x40));
}
}
/**
* @brief LCD1602初始化函数
* @param 无
* @retval 无
*/
void LCD_Init()
{
LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
LCD_WriteCommand(0x01);//光标复位,清屏
}
/**
* @brief 在LCD1602指定位置上显示一个字符
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @param Char 要显示的字符
* @retval 无
*/
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{
LCD_SetCursor(Line,Column);
LCD_WriteData(Char);
}
/**
* @brief 在LCD1602指定位置开始显示所给字符串
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param String 要显示的字符串
* @retval 无
*/
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
/**
* @brief 返回值=X的Y次方
*/
int LCD_Pow(int X,int Y)
{
unsigned char i;
int Result=1;
for(i=0;i<Y;i++)
{
Result*=X;
}
return Result;
}
/**
* @brief 在LCD1602指定位置开始显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~65535
* @param Length 要显示数字的长度,范围:1~5
* @retval 无
*/
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
}
}
/**
* @brief 在LCD1602指定位置开始以有符号十进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:-32768~32767
* @param Length 要显示数字的长度,范围:1~5
* @retval 无
*/
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{
unsigned char i;
unsigned int Number1;
LCD_SetCursor(Line,Column);
if(Number>=0)
{
LCD_WriteData('+');
Number1=Number;
}
else
{
LCD_WriteData('-');
Number1=-Number;
}
for(i=Length;i>0;i--)
{
LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
}
}
/**
* @brief 在LCD1602指定位置开始以十六进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~0xFFFF
* @param Length 要显示数字的长度,范围:1~4
* @retval 无
*/
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i,SingleNumber;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
SingleNumber=Number/LCD_Pow(16,i-1)%16;
if(SingleNumber<10)
{
LCD_WriteData(SingleNumber+'0');
}
else
{
LCD_WriteData(SingleNumber-10+'A');
}
}
}
/**
* @brief 在LCD1602指定位置开始以二进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~1111 1111 1111 1111
* @param Length 要显示数字的长度,范围:1~16
* @retval 无
*/
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');
}
}
定时器配置函数
#include <REGX52.H>
extern unsigned int freq;
void Timer0Init(void)
{
unsigned int THHL;
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
THHL=65536-freq;
TL0 =THHL%256; //设置定时初值
TH0 =THHL/256; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
PT0=0;
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0x1D,0x1D,0x1E,0x1F,0x20,
0x21,0x22,0x23,0x24,0x25,0x26,0x28,0x29,
0x2A,0x2C,0x2D,0x2F,0x30,0x32,0x34,0x35,
0x37,0x39,0x3B,0x3D,0x3F,0x41,0x43,0x45,
0x47,0x49,0x4B,0x4D,0x4F,0x52,0x54,0x56,
0x59,0x5B,0x5D,0x60,0x62,0x65,0x67,0x69,
0x6C,0x6E,0x71,0x73,0x76,0x78,0x7B,0x7D
};
unsigned code thr[256]={
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,
0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,
0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,
0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,
0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
0xBF,0xBE,0xBD,0xBC,0xBB,0xBA,0xB9,0xB8,
0xB7,0xB6,0xB5,0xB4,0xB3,0xB2,0xB1,0xB0,
0xAF,0xAE,0xAD,0xAC,0xAB,0xAA,0xA9,0xA8,
0xA7,0xA6,0xA5,0xA4,0xA3,0xA2,0xA1,0xA0,
0x9F,0x9E,0x9D,0x9C,0x9B,0x9A,0x99,0x98,
0x97,0x96,0x95,0x94,0x93,0x92,0x91,0x90,
0x8F,0x8E,0x8D,0x8C,0x8B,0x8A,0x89,0x88,
0x87,0x86,0x85,0x84,0x83,0x82,0x81,0x80,
0x7F,0x7E,0x7D,0x7C,0x7B,0x7A,0x79,0x78,
0x77,0x76,0x75,0x74,0x73,0x72,0x71,0x70,
0x6F,0x6E,0x6D,0x6C,0x6B,0x6A,0x69,0x68,
0x67,0x66,0x65,0x64,0x63,0x62,0x61,0x60,
0x5F,0x5E,0x5D,0x5C,0x5B,0x5A,0x59,0x58,
0x57,0x56,0x55,0x54,0x53,0x52,0x51,0x50,
0x4F,0x4E,0x4D,0x4C,0x4B,0x4A,0x49,0x48,
0x47,0x46,0x45,0x44,0x43,0x42,0x41,0x40,
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,
0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,
0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,
0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F
};
unsigned code squ[256]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
};
#endif
显示部分功能代码来源某站UP主,仿真结果
文章来源:https://www.toymoban.com/news/detail-462004.html
文章来源地址https://www.toymoban.com/news/detail-462004.html
到了这里,关于基于51单片机的DAC0832波形发生器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!