Created
June 7, 2011 18:20
-
-
Save nicholasjconn/1012810 to your computer and use it in GitHub Desktop.
Revisions
-
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,99 @@ /************************************************************************ * 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; } }