Tool/software: Code Composer Studio
Hi,
I want to generate a random binary number to turn on P1.0 at different frequencies each time. At the same time, I want to generate a PWM on P1.6.
To generate a random number, I am using the timing difference between DCO and VLO. I set DCO using TA0 and VLO using TA1. Then I use an interrupt vector to pause TA0 and TA1 and save the number of ticks in SMCLK. I then use an if loop to toggle P1.0 based on the LSB of the number of ticks recorded each time.
Pausing of TA0 and TA1 causes the PWM to stop working. Also, P1.0 is always at high. Can you help me figure out the mistake I am making.
Please see the below code:
//RNG #include "msp430G2553.h" #include <stdint.h> void main( void ) { WDTCTL = (WDTPW | WDTHOLD); // Stop watchdog timer BCSCTL1 = CALBC1_16MHZ; // Set range to 16MHz DCOCTL = CALDCO_16MHZ; // Set DCO step and modulation to 16MHz P1SEL |= 0x40; // Set special function of P1.6 to timer module P1DIR |= 0x41; // Set P1.6, P1.0 to output direction P1OUT |= 0x00; // Set P1 OFF //Setting DCO TA0CCR0 = 400; //PWM Frequency 40kHz TA0CCR1 = 65; //PWM Duty cycle TA0CCTL0 = CAP | CM_1 | CCIS_1 | OUTMOD_7; TA0CTL = (TASSEL_2 | ID_0 | MC_1 | TACLR | TAIE); // SMCLK, divider 1, up-mode, clear, interrupt enabled //Setting VLO TA1CCR0 = 1200; TA1CCTL0 = CCIE | OUTMOD_3; TA1CTL = TASSEL_1 | MC_1; _BIS_SR(GIE); // Enable global interrupts while( 1 ); // Endless loop } // Timer0 A1 interrupt service routine #pragma vector = TIMER0_A1_VECTOR __interrupt void Timer0_A1_ISR( void ) { static uint8_t random = 0; //register to save the random number TA0CTL = MC_0; //Pause TA0 TA1CTL = MC_0; //Pause TA1 random = TA0R; //Save number of ticks SMCLK TA0CTL &= ~TAIFG; if (random) { P1OUT = 0X01; } else { P1OUT = 0X00; } }