#include void init_1232_e72(void); void main() { int pwm1val, pwm2val, adval; init_1232_e72(); //For E72, this should always be first line of code. // Pins reserved for special use in "init_1232_e72": // P1.0: LED // P1.2: TA1 - PWM1 output // P1.3: TA2 - PWM2 output // P1.4: SMCLK (a 250 kHz clock we need) // P2.0: Pot input - analog channel 0 // P3.0: Push button input // Setup A/D ADC10CTL1 = INCH_0; //Set to channel 0 ADC10CTL0 = ADC10ON | ENC; //Turn ADC on, enable conversions // Setup PWM CCR0 = 16-1; // CCR0 = PWM Period-1 (15625Hz w/ SMCLK=250kHz) pwm1val = 4; // PWM val is 0 to 16, 8 is 50% pwm2val = 12; while (1) { CCR1 = pwm1val; // CCR1 PWM duty cycle CCR2 = pwm2val; // CCR2 PWM duty cycle ADC10CTL0 |= ADC10SC; // Start conversion while (ADC10CTL1 & ADC10BUSY); // Wait for conversion to finish adval = ADC10MEM; // Get A/D conversion result } } //------------------------------------------------------------------------------ //---- Code adapted from TI web site, example code for MSP430F1232 ------------- //------------------------ Edit at your own risk ------------------------------- //------------------------------------------------------------------------------ void init_1232_e72 (void) { // Pins reserved for special use in "init_1232_e72": // P1.0: LED // P1.2: TA1 - PWM1 output // P1.3: TA2 - PWM2 output // P1.4: SMCLK (a 250 kHz clock we need) // P2.0: Pot input - analog channel 0 // P3.0: Push button input //#define DELTA 900 // target DCO = DELTA*(4096) = 3686400 //#define DELTA 256 // target DCO = DELTA*(4096) = 1048576 //#define DELTA 244 // target DCO = 999424 (1MHz, approx); #define DELTA 488 // target DCO = 1998848 (2MHz, approx); //#define DELTA 70 // target DCO = DELTA*(4096) = 286720 unsigned int i; WDTCTL = WDTPW + WDTHOLD; // Stop WDT P1DIR |= 0x1D; // P1.0, 2, 3, 4 output; P1SEL |= 0x1C; // P1.2=TA1, P1.3=TA2, P1.4=SMCLK P1OUT = 0x01; // Blink LED (Sanity check) for (i=0; i<50000; i++) ; P1OUT = 0x00; // Software FLL (to set frequency) begins ************************************ unsigned int Compare, Oldcapture = 0; BCSCTL1 |= DIVA_3; // ACLK= LFXT1CLK/8 CCTL2 = CM_1 + CCIS_1 + CAP; // CAP, ACLK TACTL = TASSEL_2 + MC_2 + TACLR; // SMCLK, cont-mode, clear while (1) { while (!(CCIFG & CCTL2)); // Wait until capture occured CCTL2 &= ~CCIFG; // Capture occured, clear flag Compare = CCR2; // Get current captured SMCLK Compare = Compare - Oldcapture; // SMCLK difference Oldcapture = CCR2; // Save current captured SMCLK if (DELTA == Compare) break; // If equal, leave "while(1)" else if (DELTA < Compare) // DCO is too fast, slow it down { DCOCTL--; if (DCOCTL == 0xFF) { if (!(BCSCTL1 == (XT2OFF + DIVA_3))) BCSCTL1--; // Did DCO roll under?, Sel lower RSEL } } else { DCOCTL++; if (DCOCTL == 0x00) { if (!(BCSCTL1 == (XT2OFF + DIVA_3 + 0x07))) BCSCTL1++; // Did DCO roll over? Sel higher RSEL } } } CCTL2 = 0; // Stop CCR2 TACTL = 0; // Stop Timer_A // End FLL ******************************************************** for (i=0; i<10000; i++) ; // Blink again (more sanity) P1OUT = 0x01; for (i=0; i<10000; i++) ; P1OUT = 0x00; BCSCTL2 |= 0x06; // SMCLK is DCOCLK/8 (250kHz if DCO=2MHz) TACTL = TASSEL_2 + MC_1; // Timer A, SMCLK, up mode CCTL1 = OUTMOD_7; // CCR1 reset/set CCTL2 = OUTMOD_7; // CCR2 reset/set }