This post was last edited by QuaX_Chow on 2024-8-21 22:12
Please read the statement and notes of this series first:
The project source code and simulation file entry is at the end of the article. If it is not displayed, it is still under review.
Task requirements: Use C51 to realize four-button control of MAX517 to generate square wave, triangle wave, sawtooth wave and sine wave respectively. The waveform is displayed through the oscilloscope in Proteus. Connect four button switches to P2.0~P2.3, corresponding to the four waveforms respectively; the clock signal is generated by, and there is no output when no button is pressed at the beginning
To achieve the above functions, we need to split the task into the following parts:
- MAX517 Communications
- Interrupt key judgment
1. Proteus part
The waveform display uses the "OSCILLOSCOPE" four-channel oscilloscope in virtual instrument mode:
The constructed simulation circuit is as follows:
If you accidentally close the waveform display window during simulation, just click "Debug"-"Digital Oscilloscope" in the menu bar in the simulation state to display the oscilloscope interface.
2. Code part
(1) MAX517 communication
Before we drive the MAX517, we first need to sort out the timing of this chip
Driving MAX517 requires a two-wire serial interface, compatible with IIC
See the figure below:
One clock line and one data line. The declaration is as follows:
sbit SCL = P1^0;//MAX517串行时钟
sbit SDA = P1^1;//MAX517串行数据
The start state is marked by the clock line high and the data line falling edge. The end state is marked by the clock line high and the data line rising edge.
When programming, we need to use GPIO for simulation according to the chip timing.
Take SC as an example to write a function:
void start(void)//起始条件子程序
{
SDA= 1;
SCL = 1;
_nop_();
SDA =0;
_nop_();
}
The complete transmission process is as follows:
To control the analog output of MAX517, you need to write a function with a complete process according to the steps in the figure above: start-send address-response-command-response-data-response-end:
void DACOut (uchar ch)//串行D/A转换子程序
{
start();//发送启动信号
send(0x58);//发送地址字节
ack();
send(0x00);//发送命令字节
ack();
send(ch);//发送数据字节
ack();
stop();//结束一次转换
}
If we need to output a specified voltage, we only need DACOut(required value);
The formula is as follows:
But what we need to output is a waveform, not a stable level, so we need to keep changing the output analog value ch.
Taking the output of sine wave as an example, create an array to store the value of ch , and only need to increment the element when outputting:
uchar sindata[64]=
{ 0x80,0x8c,0x98,0xa5,0xb0,0xbc,0xc7,0xd1,
0xda,0xe2,0xea,0xf0,0xf6,0xfa,0xfd,0xff,
0xff,0xff,0xfd,0xfa,0xf6,0xf0,0xea,0xe3,
0xda,0xd1,0xc7,0xbc,0xb1,0xa5,0x99,0x8c,
0x80,0x73,0x67,0x5b,0x4f,0x43,0x39,0x2e,
0x25,0x1d,0x15,0xf,0x9,0x5,0x2,0x0,0x0,
0x0,0x2,0x5,0x9,0xe,0x15,0x1c,0x25,0x2e,
0x38,0x43,0x4e,0x5a,0x66,0x73};
The implementation ideas of square wave, sawtooth wave and triangle wave are similar. You can increase, decrease and delay in the loop. For specific methods, please refer to the source code at the end of the article.
(2) Interrupt key judgment
To realize the function of switching the output waveform immediately, we need to use external interrupt 0. The interrupt service function is as follows:
void intp0() interrupt 0
{
if(P2_0 == 0) key = 0;//三角波
if(P2_1 == 0) key = 1;//方波
if(P2_2 == 0) key = 2;//正弦
if(P2_3 == 0) key = 3;//锯齿波
}
The four waveform output effects are as follows:
Mission accomplished
2024/8/21
Source code and simulation files
#This article was first published on EEWORLD, copyright belongs to the author