Skip to content

Instantly share code, notes, and snippets.

@nicholasjconn
Created June 7, 2011 18:20
Show Gist options
  • Save nicholasjconn/1012810 to your computer and use it in GitHub Desktop.
Save nicholasjconn/1012810 to your computer and use it in GitHub Desktop.
MSP430 LaunchPad Based DAC (http://msp430launchpad.com)
/************************************************************************
* NJC's Simple LaunchPad DAC
*
* Description: This code turns the LaunchPad into a simple (slow)
* function generator that outputs a sine wave at 128Hz.
* For each point in a period signal, there is one
* smaller PWM period. For this example there are 32
* points per period. The duty cycle of each PWM period
* determines the analog value at that point in time.
* By continually changing the duty cycle, an analog
* signal can be generated by filtering the digital
* output signal. The analog filter can be as simple as
* a single order low-pass filter which has a corner
* frequency slightly greater than the desired signal.
*
* This code was originally created for "NJC's MSP430
* LaunchPad Blog".
*
* See http://msp430launchpad.com for more information.
*
*
* ACLK = n/a, MCLK = SMCLK = default DCO
*
* Output Frequency = SMCLK/( # of points * PWM period)
* ~128Hz = (1.0MHz to 1.1MHz)/(32*256)
*
* MSP430G2231
* ---------------
* /|\| |
* | | P1.0| --> Analog Filters --> ~128Hz Sine Wave
* --|RST |
* | |
*
*
* Author: Nicholas J. Conn - http://msp430launchpad.com
* Email: webmaster at msp430launchpad.com
* Date: 06-05-11
*
************************************************************************/
#include <msp430g2231.h>
unsigned char counter; // Current location in wave array
unsigned char wave[32] = { // Wave array, preset to values of sine
128, 140, 152, 164, 173, 181, 187, 191,
192, 191, 187, 181, 173, 164, 152, 140,
128, 116, 104, 92, 83, 75, 69, 65,
64, 65, 69, 75, 83, 92, 104, 116 };
unsigned int i; // Used for 'for' loops.
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= BIT0; // P1.0 output
counter = 0; // Reset counter
// Initialize Timer
CCTL0 = CCIE; // CCR0 interrupt enabled
CCTL1 = CCIE; // CCR1 interrupt enabled
CCR0 = 256; // Set PWM period to 256 clock ticks
CCR1 = wave[counter]; // Set first duty cycle value
TACTL = TASSEL_2 + MC_1 + TAIE + TACLR; // SMCLK, upmode, enable interrupt, clear TA1R
_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt
}
/**
* TimerA0 interrupt service routine
**/
#pragma vector=TIMERA0_VECTOR
__interrupt void TIMERA0_ISR(void)
{
P1OUT |= BIT0; // Set P1.0
CCR1 = wave[counter]; // Set next duty cycle value
counter += 1; // Add Offset to CCR0
if ( counter == 32) // If counter is at the end of the array
{
counter = 0; // Reset counter
}
}
/**
* TimerA1 Interrupt Vector (TAIV) handler
**/
#pragma vector=TIMERA1_VECTOR
__interrupt void TIMERA1_ISR(void)
{
switch( TAIV )
{
case 2: // CCR1 interrupt
P1OUT &= ~BIT0; // Clear P1.0 to determine duty cycle.
break;
default:
break;
}
}
@DhwmPhawa
Copy link

I can't seem to generate a sinusoidal signal with this code using a MSP430F5438A controller, I've change the Registers an Timer Codes, Acronyms as per the MS430F5xxx Family user guide. But nothing seems to work. The Graph only shows the PWM part and nothing after the Interrupts. Could you check please, Perhaps I did something wrong with the code, I'm very new to Code composer and any help would be nice. Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment