MSP430 MCU Simulation Example 19-D/A Conversion Sine Wave Generator Based on Proteus
[Copy link]
1. Task Requirements
Use the MSP30F247 microcontroller as a controller to achieve a sine wave signal output with a maximum value of +/- 5V and a frequency of 50Hz.
2. Hardware circuit
The hardware circuit is shown in the figure below.
The maximum output value is positive and negative 5V sinusoidal signal. The operational amplifier circuit adopts a two-stage design. The second stage is an adding circuit, which can achieve bipolar output.
3. Programming
//main.c
#include "msp430f247.h"
#include "stdlib.h"
#include "string.h"
/************************************************Software delay, main frequency 1M*******************/
#define CPU_F1 ((double)1000000)
#define delay_us1M(x) __delay_cycles((long)(CPU_F1*(double)x/1000000.0))
#define delay_ms1M(x) __delay_cycles((long)(CPU_F1*(double)x/1000.0))
/****************************************************** ***************************/
/************************************************Software delay, main frequency 1M*******************/
#define CPU_F8 ((double)8000000)
#define delay_us8M(x) __delay_cycles((long)(CPU_F8*(double)x/1000000.0))
#define delay_ms8M(x) __delay_cycles((long)(CPU_F8*(double)x/1000.0))
/****************************************************** ***************************/
//128 points output sine wave sample value
char data_Sin[128]={
128,134,140,147,153,159,165,171,177,182,188,193,199,204,209,213,
218,222,226,230,234,237,240,243,245,248,250,251,253,254,254,255,
255,255,254,254,253,251,250,248,245,243,240,237,234,230,226,222,
218,213,209,204,199,193,188,182,177,171,165,159,153,147,140,134,
128,122,116,109,103,97,91,85,79,74,68,63,57,52,47,43,38,34,30,26,
22,19,16,13,11,8,6,5,3,2,2,1,1,1,2,2,3,5,6,8,11,13,16,19,22, 26,30,
34,38,43,47,52,57,63,68,74,79,85,91,97,103,109,116,122};
/************************************************
Function name: main function
Function: D/A converter application 2: sine wave generator
Entry parameters: None
Export parameters: None
************************************************/
main()
{
unsigned int i;
WDTCTL = WDTPW + WDTHOLD; // Turn off the watchdog
P4DIR=0xff;
while(1)
{
for(i=0;i<128;i++)
{
P4OUT=data_Sin;
delay_us1M(156);
}
}
}
4. Program Description
The general method for a single-chip microcomputer to generate a sine wave signal is: first create a sine wave data table, and the single-chip microcomputer obtains a sine wave signal through D/A output according to the table lookup method. The D/A converter used in the example is 8-bit, so the sine wave data table has a maximum of 256 values. Generally, the single-chip microcomputer outputs 64 points or 128 points. The sine wave waveform obtained after the D/A circuit is relatively perfect. .
5. Simulation results and analysis
After drawing the circuit diagram in Proteus, double-click the MCU to load the executable file into the MCU, click Run, and observe the waveform of the virtual oscilloscope. As shown in the figure below.
|