Generate a sine signal using the MSP430 internal DAC

Publisher:姑苏清风泉源客Latest update time:2015-05-18 Source: 51heiKeywords:MSP430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/*****************************************************************

               File name: DAC_10k
               Function: Use the internal DAC module of msp430f169 to generate a 10kHz sine signal through the transmission of the DMA module . This program takes                      100 points               
                     in one cycle **********************************************************************/

#include
static int Sin_tab[100] = {
1638,1740,1843,1944,2045,2143,2240,2335,2426,2515,2600,2681,2758,2831,
2899,2962,3020,3072 ,3119,3160,3195,3224,3246,3262,3272,3272,3263,3247,
3224,3196,3161,3120,3074,3021,2964,2901,2833,2760,2683,2602,2517,2429,
2337,2243,2146,2047,1947,1845,1743,1640,1537,1435,1333,1233,1134,1037,
943,851,762,677,596,519,446,378,314,256,204,157,116 ,81,52,29,13,3,0,2,
12,28,50, 78,113,154,200,252,310,373,440,513,590,671,756,756,844,936,
1030,1127,1225,1326,1427,1529};
void INIT_XT2();
void main(void)
{
  WDTCTL = WDTPW + WDTHOLD; // Stop watchdog
 
  INIT_XT2();
 
  P5SEL=0XFF;
  ADC12CTL0 = REFON; // Reference voltage is internal 2.5v
  DMA0SA = (int) Sin_tab; / / Source address register
  DMA0DA = DAC12_0DAT_; // Destination address register
  DMA0SZ = 100; // Number of basic units of transfer
  DMACTL0 = DMA0TSEL_5; // DAC12IFG flag of DAC12_0CTL 
  DMA0CTL = DMADT_4 + DMASRCINCR_3 + DMAEN; // DMADT_4: repeated block Transmission mode Rpt, inc src, word-word
  DAC12_0CTL = DAC12LSEL_2 + DAC12IR + DAC12AMP_5 + DAC12IFG + DAC12ENC; // Config
                                            // **force first interrupt**
  CCTL1 = OUTMOD_3; // Set/reset
  CCR1 = 1; // PWM Duty Cycle 
  CCR0 = 8-1; // ~1kHz Clock period
  TACTL = TASSEL_2 + MC_1; // SMCLK, up-mode

  _BIS_SR(LPM0_bits); // Enter LPM0
}
void INIT_XT2()
{
  unsigned char i;
  BCSCTL1&=~XT2OFF;//Turn on XT2 oscillator
  BCSCTL2|=SELM_2+SELS;//SELM_2: Select XT2 as the clock source of MCLK SELS: Select XT2 as the clock source of SMCLK
  do
  {
    IFG1&=~OFIFG;//Clear oscillation error flag
    for(i=0;i<100;i++);
    _NOP();
  }
  while((IFG1&OFIFG)!=0);
  IFG1&=~OFIFG;
}

==================================================================================
/******************************************************************************
* File: main.c
* Description: MSP430F5438 development board  ADC12 conversion experiment 
* Set breakpoints to observe the voltage value in voltage
* Change the variable resistor R25 to observe the change of the voltage value  in voltage
* Compiler: IAR Embedded Workbench IDE for MSP430 v5.10
*******************************************************************************/
#include
#include "GPIO.h" 
#include "BoardConfig.h"
#include "Clock.h"

/**********************************************************************************
*Macro definition area
**************************************************************************/
#define Num_of_Results 8 //Maximum value of data storage
/**************************************************************************
*Function declaration area
**********************************************************************/
void ADC_init(void);
float ADC_opera(void);
/**********************************************************************************
*Variable definition area
**************************************************************************/
volatile unsigned int results[Num_of_Results]; //Store ADC12  sampling conversion results   
static unsigned char index = 0;
float voltage; //   Store voltage value operation results
/**********************************System main function****************************************/
int main( void )
{
  WDTCTL=WDTPW+WDTHOLD; 
  BoardConfig(0xEF); // Board resource configuration 
  Init_CLK(); // Initialize system clock   
  WDTCTL = WDT_ADLY_1_9;
  SFRIE1 |= WDTIE; // WDT interrupt enable
  ADC_init(); // ADC12 conversion initialization  
  _EINT(); // Open global interrupt
    
  while(1)
  {
    if(index==Num_of_Results-1)
    voltage=ADC_opera();
    _NOP();
    LPM1;
  }
}
/**************************************************************************************
* Function name: void ADC_init(void)
* Function: ADC12 initialization
* Parameters: None
* Return value: None
********************************************************************************/
void ADC_init(void)
{
  P6SEL |= 0x01; // Open channel 1
  ADC12CTL0 = ADC12ON+ADC12SHT0_8+ADC12MSC;  
                                             
  ADC12CTL1 = ADC12SHP+ADC12CONSEQ_2;        
  ADC12IE = 0x01;                           
  ADC12CTL0 |= ADC12ENC;                     
  ADC12CTL0 |= ADC12SC;                          
}
/******************************************************************************
* Function name: void ADC_opera(void)
* Function: Convert the data sampled by ADC into a decimal number
* Parameter: None
* Return value: None
************************************************************************************/

float ADC_opera(void)
{
  unsigned long temp=0;
  float voltage;
  for(unsigned char i=0;i   {
    temp +=results[i];
  }
  voltage=((temp/Num_of_Results)*3.3/4095);
  return voltage;
}
/******************************************************************************
* Function Name: __interrupt void ADC12ISR (void)
* Function: ADC12 interrupt service function
* Parameter: None
* Return Value: None
********************************************************************************/
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
  switch(__even_in_range(ADC12IV,34))
  {
  case 6: // ADC12IFG0
    results[index] = ADC12MEM0; // 
    index++; // 
   
    if (index == 8)
    {
      index = 0;
    }
  default: break; 
  }
  LPM1_EXIT;

-----------------------------------------------------------------------------------------------------------------

This picture helped me a lot in my learning process. If you don’t understand the program, you can take a look at these, maybe you will gain something. I am studying integrated circuit design and I find it very helpful.

 

Here are some of my own test programs. Don't underestimate these few lines of programs, but I tested them N times.

#include "msp430x14x.h"

void InitADC12();

unsigned char wait;

int main( void )

{

  // Stop watchdog timer to prevent time out reset

  WDTCTL = WDTPW + WDTHOLD;

  InitADC12();

  return 0;

}

void InitADC12()

{

    //_DINT();

    ADC12CTL0 &= ~ENC; //ADC12CTL0 and 1 can only be set when ENC is reset!!! This is very important!!!

    ADC12CTL0 = MSC + REFON + REF2_5V + SHT0_15 + SHT1_15;

    /*MSC: Multiple sampling conversion bit, only valid for sequence sampling or multiple multiple conversions.

        0: The sampling timer needs the rising edge of the SHI signal to trigger

        1: The first time the sampling timer SHI signal is triggered, the subsequent conversions will start immediately after the last conversion. */

    ADC12CTL1 |= SHP + CONSEQ_0;

    /*SHP: 1: The sampling signal comes from the sampling timer 0: The sampling signal comes from the sampling input signal

    CONSEQ: 0: Single channel single conversion 1: Sequential channel word conversion 2: Single channel multiple conversion 3: Sequential channel multiple conversion

    CSTARTADD_x(0~15):Conversion starting position*/

    ADC12MCTL0 = EOS + SREF_0 + INCH_7;

    /* EOS: sequence end bit

    SREF: Reference voltage selection

    INCH: Analog input channel selection*/

    ADC12IE |= 0x0001; //Open the corresponding analog channel interrupt, and trigger an interrupt after the conversion is completed

    _UNITE();

    //ADC12CTL0 |= ADC12ON + ENC; //Modify!!! // This is wrong

    ADC12CTL0 |= ADC12ON;

    ADC12CTL0 |= ENC;

    wait = 0; //Variables can be defined here to indicate whether the conversion is completed

    ADC12CTL0 |= ENC+ADC12SC; //Conversion start If you want to start the conversion at other times, you can put this sentence somewhere else.

    while(wait==0) ;    

}

#pragma vector=ADC_VECTOR

__interrupt void ADC()

{int  result[7];

        wait=1;

        result[0] = ADC12MEM0;

        /*unsigned char q0;

 int *pmem=ADC12MEM;

     //The conversion is complete.

 for(q0=0;q0<16;q0++)

 {

  AdMem[q0]= *pmem;

  pmem++;

 }*/

}


Let's briefly introduce the difficult-to-understand aspects of this module.

1. MSP430 can use internal and external reference voltages, internal 1.5 or 2.5, external 0~3.3, which can be set through registers . The maximum input range of the sampling voltage is 0~3.3, that is, AVss~AVcc. Negative voltage cannot be detected. If negative voltage needs to be detected, an op amp can be used to bias the voltage into a positive voltage and then detect it.

2. Using external AVss~AVcc as the reference voltage is not stable enough, but has high accuracy. Using internal voltage is more stable.

3. The global interrupt _EINT() must be enabled in the program;

ADC12 
********************************************************** / 
#define__ MSP430_HAS_ADC12__ /* define to indicate the module is */

#define ADC12CTL0_(0x01A0u) /* ADC12 control 0 */ 
DEFW(ADC12CTL0, ADC12CTL0_) 
#define ADC12CTL1_(0x01A2u) /* ADC12 control 1 */ 
DEFW(ADC12CTL1, ADC12CTL1_) 
#define ADC12IFG_(0x01A4u) /* ADC12 interrupt flag */ 
DEFW(ADC12IFG, ADC12IFG_) 
#define ADC12IE_(0x01A6u) /* ADC12 interrupt enable */ 
DEFW(ADC12IE, ADC12IE_) 
#define ADC12IV_(0x01A8u) /* ADC12 interrupt vector word */ 
DEFW(ADC12IV, ADC12IV_)

#define ADC12MEM_(0x0140u) /* ADC12 conversion memory */ 
#ifndef __IAR_SYSTEMS_ICC__ 
#define ADC12MEM(ADC12MEM_) /* ADC12 conversion memory (Assembly) */ 
#else 
#define ADC12MEM((int *)ADC12MEM_) /* ADC12 conversion memory ( C) */ 
#ENDIF 
#define ADC12MEM0_(0x0140u) /* ADC12 conversion memory 0 */ 
defw(ADC12MEM0, ADC12MEM0_) 
#define ADC12MEM1_(0x0142u) /* ADC12 conversion memory 1 */ 
defw(ADC12MEM1, ADC12MEM1_) 
#define ADC12MEM2_(0x0144u) /* ADC12 conversion memory 2 */ 
defw(ADC12MEM2, ADC12MEM2_) 
#define ADC12MEM3_(0x0146u) /* ADC12 conversion memory 3 */ 
DEFW(ADC12MEM3, ADC12MEM3_) 
#define ADC12MEM4_(0x0148u) /* ADC12 conversion memory 4 */ 
DEFW(ADC12MEM4, ADC12MEM4_) 
#define ADC12MEM5_(0x014Au) /* ADC12 conversion memory 5 */ 
DEFW(ADC12MEM5, ADC12MEM5_) 
#define ADC12MEM6_(0x014Cu) /* ADC12 conversion memory 6 */ 
DEFW(ADC12MEM6, ADC12MEM6_) 
#define ADC12MEM7_(0x014Eu) /* ADC12 conversion memory 7 */ 
DEFW(ADC12MEM7, ADC12MEM7_) 
#define ADC12MEM8_(0x0150u) /* ADC12 conversion memory 8 */  DEFW
(ADC12MEM8, ADC12MEM8_) 
#define ADC12MEM9_ (0x0152u) /* ADC12 conversion memory 9 */  DEFW (ADC12MEM9, ADC12MEM9_)  #define ADC12MEM10_ (0x0154u) /* ADC12 conversion memory 10 */  DEFW (ADC12MEM10, ADC12MEM10_)  #define ADC12MEM11_ (0x0156u) /* ADC12 conversion memory 11 */  DEFW (ADC12MEM11, ADC12MEM11_)  #define ADC12MEM12_ (0x0158u) /* ADC12 conversion memory 12 */  DEFW (ADC12MEM12, ADC12MEM12_)  #define ADC12MEM13_ (0x015Au) /* ADC12 conversion memory 13 */  DEFW (ADC12MEM13, ADC12MEM13_)  #define ADC12MEM14_ (0x015Cu) /* ADC12 conversion memory 14 */  DEFW (ADC12MEM14, ADC12MEM14_)  #define ADC12MEM15_ (0x015Eu) /* ADC12 conversion memory 15 */  DEFW (ADC12MEM15, ADC12MEM15_)












#define ADC12MCTL_(0x0080u) /* ADC12 memory control */ 
#ifndef __IAR_SYSTEMS_ICC__ 
#define ADC12MCTL(ADC12MCTL_) /* ADC12 memory control (for assembly) */ 
#else 
#define ADC12MCTL((char *)ADC12MCTL_) /* ADC12 memory control (for C) */ 
#ENDIF 
#define ADC12MCTL0_(0x0080u) /* ADC12 memory control 0 */ 
defc(ADC12MCTL0, ADC12MCTL0_) 
#define ADC12MCTL1_(0x0081u) /* ADC12 memory control 1 */ 
defc(ADC12MCTL1, ADC12MCTL1_) 
#define ADC12MCTL2_(0x0082u) /* ADC12 memory control 2 */ 
DEFC (ADC12MCTL2, ADC12MCTL2_) 
#define ADC12MCTL3_ (0x0083u) /* ADC12 memory control 3 */ 
DEFC (ADC12MCTL3, ADC12MCTL3_) 
#define ADC12MCTL4_ (0x0084u) /* ADC12 memory control 4 */ 
DEFC (ADC12MCTL4, ADC12MCTL4_) 
#define ADC12MCTL5_ (0x0085u) /* ADC12 memory control 5 */ 
DEFC (ADC12MCTL5, ADC12MCTL5_) 
#define ADC12MCTL6_ (0x0086u) /* ADC12 memory control 6 */ 
DEFC (ADC12MCTL6, ADC12MCTL6_) 
#define ADC12MCTL7_ (0x0087u) /* ADC12 memory control 7 */ 
DEFC (ADC12MCTL7, ADC12MCTL7_) 
#define ADC12MCTL8_ (0x0088u) /* ADC12 memory control 8 */ 
DEFC (ADC12MCTL8, ADC12MCTL8_) 
#define ADC12MCTL9_ (0x0089u) /* ADC12 memory control 9 */ 
DEFC (ADC12MCTL9, ADC12MCTL9_) 
#define ADC12MCTL10_ (0x008Au) /* ADC12 memory control 10 */ 
DEFC (ADC12MCTL10, ADC12MCTL10_) 
#define ADC12MCTL11_ (0x008Bu) /* ADC12 memory control 11 */ 
DEFC (ADC12MCTL11, ADC12MCTL11_) 
#define ADC12MCTL12_ (0x008Cu) /* ADC12 memory control 12 */ 
DEFC (ADC12MCTL12, ADC12MCTL12_) 
#define ADC12MCTL13_ (0x008Du) /* ADC12 memory control 13 */ 
DEFC (ADC12MCTL13, ADC12MCTL13_) 
#define ADC12MCTL14_ (0x008Eu) /* ADC12 memory control 14 */ 
DEFC (ADC12MCTL14, ADC12MCTL14_) 
#define ADC12MCTL15_ (0x008Fu) /* ADC12 memory control 15 */ 
DEFC (ADC12MCTL15, ADC12MCTL15_)

/* ADC12CTL0 */ 
#define ADC12SC (0x001) /* ADC12 start conversion */ 
#define ENC (0x002) /* ADC12 enable conversion */ 
#define ADC12TOVIE (0x004) /* ADC12 timer overflow interrupt enable */ 
#define ADC12OVIE (0x008) /* ADC12 overflow interrupt enable */ 
#define ADC12ON (0x010) /* ADC12 on/enable */ 
#define REFON (0x020) /* ADC12 reference / 
#define REF2_5V (0x040) /* ADC12 reference 0:1.5 V / 1:2.5 V */ 
#define MSC (0x080) /* ADC12 multi-SampleConversion */ 
#define SHT00 (0x0100u) /* ADC12 Sample Hold 0 Select 0 */ 
#define SHT01 (0x0200u) /* ADC12 Sample Hold 0 Select 1 */ 
#define SHT02 (0x0400u) /* ADC12 Sample Hold 0 Select 2 */ 
#define SHT03 (0x0800u) /* ADC12 Sample Hold 0 Select 3 */ 
#define SHT10 (0x1000u) /* ADC12 Sample Hold 0 Select 0 */ 
#define SHT11 (0x2000u) /* ADC12 Sample Hold 1 Select 1 */ 
#define SHT12 (0x4000u) /* ADC12 Sample Hold 2 Select 2 */ 
#define SHT13 (0x8000u) /* ADC12 Sample Hold 3 Select 3 */ 
#define MSH (0x0800u)

#define SHT0_0 (0 * 0x100u) /* ADC12 Sample Hold 0 Select Bit: 0 */ 
#define SHT0_1 (1 * 0x100u) /* ADC12 Sample Hold 0 Select Bit: 1 */ 
#define SHT0_2 (2 * 0x100u) /* ADC12 Sample Hold 0 Select Bit: 2 */ 
#define SHT0_3 (3 * 0x100u) /* ADC12 Sample Hold 0 Select Bit: 3 */ 
#define SHT0_4 (4 * 0x100u) /* ADC12 Sample Hold 0 Select Bit: 4 */ 
#define SHT0_5 (5 * 0x100u) /* ADC12 Sample Hold 0 Select Bit: 5 */ 
#define SHT0_6 (6 * 0x100u) /* ADC12 Sample Hold 0 Select Bit: 6 */ 
#define SHT0_7 (7 * 0x100u) /* ADC12 sample hold 0 select bit: 7 */ 
#define SHT0_8 (8 * 0x100u) /* ADC12 sample hold 0 select bit: 8 */ 
#define SHT0_9 (9 * 0x100u) /* ADC12 sample hold 0 select bit: 9 */ 
#define SHT0_10 (10 * 0x100u) /* ADC12 sample hold 0 select bit: 10 */ 
#define SHT0_11 (11 * 0x100u) /* ADC12 sample hold 0 select bit: 11 */ 
#define SHT0_12 (12 * 0x100u) /* ADC12 sample hold 0 select bit: 12 */ 
#define SHT0_13 (13 * 0x100u) /* ADC12 sample hold 0 select bit:

Keywords:MSP430 Reference address:Generate a sine signal using the MSP430 internal DAC

Previous article:Power consumption problem of Songhan MCU 8p2501
Next article:Screen snow program implemented by linked list

Recommended ReadingLatest update time:2024-11-23 10:51

Design of non-magnetic water meter based on MSP430FW427
   1 Introduction to MSP430FW42x MCU       The MSP430FW42x series MCU is the latest dedicated MCU chip developed by TI for electronic flow and rotational motion detection. It perfectly combines ultra-low power MCU, rotation scanning interface (SCAN IF) and LCD driver module. The ultra-low power structure and flow dete
[Microcontroller]
Design of non-magnetic water meter based on MSP430FW427
MSP430 Frequently Asked Questions and Answers
①Q: MSP430 pin multiplexing between JTAG and I/O functions A: Four pins, P1.7 – P1.4, have both I/O and JTAG functionality on the 20- and 28-pin MSP430F1xx devices. The default functionality of these pins is I/O functionality when the device is powered up. When the test pins are pulled high, these pins are selected fo
[Microcontroller]
Research on dynamic power management of wireless sensor nodes based on MSP430
0 Introduction Wireless sensor networks are networks composed of multiple nodes with sensors, data processing units and communication modules. They have become a research hotspot in recent years because of their huge application prospects in military, industrial, medical, agricultural and other fields. Sinc
[Industrial Control]
CAN bus and MSP430 infrared detection system circuit
  This article introduces a new detection system, which uses infrared CO sensor and MSP430 microcontroller as the core signal processing circuit, combined with digital filtering and temperature compensation operation. It has the advantages of wide detection concentration range and long service life.   System compositi
[Microcontroller]
CAN bus and MSP430 infrared detection system circuit
Improved float type liquid level meter based on MSP430F133 single chip microcomputer
Based on the analysis of the advantages and disadvantages of traditional float-type liquid level gauges, this paper introduces the method of improving the liquid level measurement of traditional float-type liquid level gauges using TI's MSP430F133 microcontroller, and gives the specific circuit design meth
[Industrial Control]
Improved float type liquid level meter based on MSP430F133 single chip microcomputer
MSP430F1121 and PCF8576 Driver
#include "MSP430x11x1.h"  #define uint unsigned int  #define uchar unsigned char  //Device address  uchar PCF8576=0x70;  //Memory data definition  uchar ByteCnt; //I2C data byte counter  uchar SlvAdr; //Slave address  uchar SubAdr; //Slave unit address  uchar XmtDat ; //Send data buffer  //uchar MODE1=0x45;  uch
[Microcontroller]
Design of MSP430 single chip microcomputer and multifunctional telephone message system
Preface MSP430 is a microprocessor recently introduced to China. It has outstanding performance in ultra-low power consumption and is known as a green MCU in the industry. At the same time, it has a rich internal peripheral modules and is a typical system on chip (SOC). It also has a 16-bit reduced instruction
[Microcontroller]
Design of MSP430 single chip microcomputer and multifunctional telephone message system
msp430AD data collection, recording and analysis
1. Use Pioneer Pulse Data Acquisition Simulation Program to test the implementation board.    The signal of the test pin is: the period is 2.4S and the high level time is 0.3s.    When setting AD: P6SEL |= 0x08;                ADC12CTL0 = ADC12ON+MSC+REFON+REF2_5V + SHT0_8;              ADC12CTL1 = SHP + CONSEQ_2 +
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号