当前位置:网站首页>DAC0832 waveform generator based on 51 single chip microcomputer
DAC0832 waveform generator based on 51 single chip microcomputer
2022-07-29 08:11:00 【Half life fireworks I blurred】
Output 1HZ Sine wave 、 Triangle wave 、 square wave 、 Sawtooth wave
Use LCD1602 Show
DAC0832 Of D0~D7 Data input port ,IOUT1、IOUT2 Complementary output port ,RFB Feedback port 、
VREF Reference voltage value ( It's usually 5V). Use the timer to control the output speed of the segment code table to control the frequency of the waveform .
( Timing value )*10^(-6)*256= Waveform frequency .
(1) Implementation principle of sawtooth wave : The implementation process of sawtooth wave is to first define an initial value and then add do , The number of steps added depends on the required frequency . Then add it to a certain number and reset it to initial value , Repeat the operation just now , Go on like this . In this procedure, the initial value is 00H, The maximum value is FFH.
(2) Realization principle of triangular wave : The realization of triangular wave is to set an initial value , Then add , It's the same with Subtract after a certain number , Reduce to the initial value and then return to the previous operation . This program inputs VREF What's the voltage +5V, Therefore, the maximum frequency of the waveform output is the initial value 00H And the final value is FFH, And the number of steps is 1, In this way, the output waveform is the largest .
(3) The realization principle of square wave : The realization of this waveform only needs to set an initial value at the beginning and then output directly Just this value , Output for a period of time , Then reset a data , Then output this data for a period between , But the time at this time must be equal to the previous period . This is a square wave , If the two times don't coincide Same as , That's equivalent to a pulse wave .
Main function and interrupt service function
#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;} // Sine wave
case 1: {BO=thr[n]; break;} // Triangle wave
case 2: {
for(i=0;i<256;i++)
{BO=squ[i];}
break;
} // square wave
case 3: {BO=256-n; break;} // Sawtooth wave
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 Show function
#include <REGX52.H>
// Pin configuration :
sbit LCD_RS=P2^0;
sbit LCD_RW=P2^1;
sbit LCD_EN=P2^2;
#define LCD_DataPort P0
// Function definition :
/**
* @brief LCD1602 The time delay function ,12MHz Call can be delayed 1ms
* @param nothing
* @retval nothing
*/
void LCD_Delay()
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
/**
* @brief LCD1602 Write orders
* @param Command Command to write
* @retval nothing
*/
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 Writing data
* @param Data The data to be written
* @retval nothing
*/
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 Set cursor position
* @param Line Row position , Range :1~2
* @param Column Column position , Range :1~16
* @retval nothing
*/
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 Initialization function
* @param nothing
* @retval nothing
*/
void LCD_Init()
{
LCD_WriteCommand(0x38);// Eight bit data interface , Two lines show ,5*7 Lattice
LCD_WriteCommand(0x0c);// Show on , The cursor is off , Flashing off
LCD_WriteCommand(0x06);// After data reading and writing , The cursor automatically adds one , The picture doesn't move
LCD_WriteCommand(0x01);// Cursor reset , Clear the screen
}
/**
* @brief stay LCD1602 Display a character at the specified position
* @param Line Row position , Range :1~2
* @param Column Column position , Range :1~16
* @param Char Characters to display
* @retval nothing
*/
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{
LCD_SetCursor(Line,Column);
LCD_WriteData(Char);
}
/**
* @brief stay LCD1602 Start displaying the given string at the specified position
* @param Line Start line position , Range :1~2
* @param Column Start column position , Range :1~16
* @param String String to display
* @retval nothing
*/
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 Return value =X Of Y Power
*/
int LCD_Pow(int X,int Y)
{
unsigned char i;
int Result=1;
for(i=0;i<Y;i++)
{
Result*=X;
}
return Result;
}
/**
* @brief stay LCD1602 Start displaying the given number at the specified position
* @param Line Start line position , Range :1~2
* @param Column Start column position , Range :1~16
* @param Number Number to display , Range :0~65535
* @param Length The length of the number to display , Range :1~5
* @retval nothing
*/
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 stay LCD1602 Displays the given number in signed decimal starting at the specified position
* @param Line Start line position , Range :1~2
* @param Column Start column position , Range :1~16
* @param Number Number to display , Range :-32768~32767
* @param Length The length of the number to display , Range :1~5
* @retval nothing
*/
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 stay LCD1602 The given number is displayed in hexadecimal from the specified position
* @param Line Start line position , Range :1~2
* @param Column Start column position , Range :1~16
* @param Number Number to display , Range :0~0xFFFF
* @param Length The length of the number to display , Range :1~4
* @retval nothing
*/
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 stay LCD1602 The given number is displayed in binary from the specified position
* @param Line Start line position , Range :1~2
* @param Column Start column position , Range :1~16
* @param Number Number to display , Range :0~1111 1111 1111 1111
* @param Length The length of the number to display , Range :1~16
* @retval nothing
*/
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');
}
}
Timer configuration function
#include <REGX52.H>
extern unsigned int freq;
void Timer0Init(void)
{
unsigned int THHL;
TMOD &= 0xF0; // Set timer mode
TMOD |= 0x01; // Set timer mode
THHL=65536-freq;
TL0 =THHL%256; // Set initial value of timing
TH0 =THHL/256; // Set initial value of timing
TF0 = 0; // eliminate TF0 sign
TR0 = 1; // Timer 0 Start timing
ET0=1;
EA=1;
PT0=0;
}
Waveform segment code table
#ifndef _DATA_H
#define _DATA_H
unsigned code sin[256]={
0x80,0x83,0x85,0x88,0x8A,0x8D,0x8F,0x92,
0x94,0x97,0x99,0x9B,0x9E,0xA0,0xA3,0xA5,
0xA7,0xAA,0xAC,0xAE,0xB1,0xB3,0xB5,0xB7,
0xB9,0xBB,0xBD,0xBF,0xC1,0xC3,0xC5,0xC7,
0xC9,0xCB,0xCC,0xCE,0xD0,0xD1,0xD3,0xD4,
0xD6,0xD7,0xD8,0xDA,0xDB,0xDC,0xDD,0xDE,
0xDF,0xE0,0xE1,0xE2,0xE3,0xE3,0xE4,0xE4,
0xE5,0xE5,0xE6,0xE6,0xE7,0xE7,0xE7,0xE7,
0xE7,0xE7,0xE7,0xE7,0xE6,0xE6,0xE5,0xE5,
0xE4,0xE4,0xE3,0xE3,0xE2,0xE1,0xE0,0xDF,
0xDE,0xDD,0xDC,0xDB,0xDA,0xD8,0xD7,0xD6,
0xD4,0xD3,0xD1,0xD0,0xCE,0xCC,0xCB,0xC9,
0xC7,0xC5,0xC3,0xC1,0xBF,0xBD,0xBB,0xB9,
0xB7,0xB5,0xB3,0xB1,0xAE,0xAC,0xAA,0xA7,
0xA5,0xA3,0xA0,0x9E,0x9B,0x99,0x97,0x94,
0x92,0x8F,0x8D,0x8A,0x88,0x85,0x83,0x80,
0x7D,0x7B,0x78,0x76,0x73,0x71,0x6E,0x6C,
0x69,0x67,0x65,0x62,0x60,0x5D,0x5B,0x59,
0x56,0x54,0x52,0x4F,0x4D,0x4B,0x49,0x47,
0x45,0x43,0x41,0x3F,0x3D,0x3B,0x39,0x37,
0x35,0x34,0x32,0x30,0x2E,0x2D,0x2C,0x2A,
0x29,0x28,0x26,0x25,0x24,0x23,0x22,0x21,
0x20,0x1F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x78,
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
Display some function codes from a station UP Lord , Simulation results
边栏推荐
- STM32 serial port garbled
- Mqtt server setup and mqtt.fx testing
- Internet worm
- Redshift 2.6.41 for maya2018 watermark removal
- Unity Shader学习(六)实现雷达扫描效果
- Ansible (automation software)
- Dynamically load data
- [beauty of software engineering - column notes] 21 | architecture design: can ordinary programmers also implement complex systems?
- Day 014 2D array exercise
- [beauty of software engineering - column notes] "one question and one answer" issue 2 | 30 common software development problem-solving strategies
猜你喜欢
STM32 printf problem summary semihosting microlib understanding
Lora opens a new era of Internet of things -asr6500s, asr6501/6502, asr6505, asr6601
Eps32+platform+arduino running lantern
【学术相关】为什么很多国内学者的AI的论文复现不了?
[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification
Simplefoc+platformio stepping on the path of the pit
Arduinoide + stm32link burning debugging
Some tools, plug-ins and software links are shared with you~
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
Security baseline of network security
随机推荐
Redshift 2.6.41 for maya2018 watermark removal
[academic related] why can't many domestic scholars' AI papers be reproduced?
Effective learning of medical image segmentation annotation based on noise pseudo tags and adversarial learning
Ansible (automation software)
Dynamic thresholds buffer management in a shared buffer packet switch paper summary
torch.Tensor和torch.tensor的区别
Arduinoide + stm32link burning debugging
[beauty of software engineering - column notes] 24 | technical debt: continue to make do with it, or overthrow it and start over?
[beauty of software engineering - column notes] 27 | what is the core competitiveness of software engineers? (top)
Matrix decomposition and gradient descent
Implementation of simple cubecap+fresnel shader in unity
Simplefoc parameter adjustment 1-torque control
Mqtt server setup and mqtt.fx testing
Alibaba political commissar system - Chapter 1: political commissars are built on companies
Lora opens a new era of Internet of things -asr6500s, asr6501/6502, asr6505, asr6601
网络安全之安全基线
Rotation in model space and rotation in world space
Detailed explanation of the find command (the most common operation of operation and maintenance at the end of the article)
Research on autojs wechat: the final product of wechat automatic information sending robot (effective demonstration)
Preparation of SQL judgment statement