/* * timerA_output_compare_example.c * * Created on: 10/9/18 * Author: johnsontimoj */ ////////////////////////////////////// // // Timer A setup to generate a 40% duty cycle // 1KHz square wave on Pin 36 // //////////////////////////////////// // // Pin 36 is P6.6 // P6.6 is TimerA_2, Output 3 // // Use SMCLK for timer clock // SMCLK defaults to HSMCLK which defaults to HFXTCLK // all with a default of divide by 1 // --> SMCLK = 3MHz // with no divide on the Timer // 1KHz --> 3000 counts // 40% duty cycle --> 1200 counts high, 1800 counts low // // Approach 1 - Only enable int on CTL and CCTL3 - // then only need TA2_N_IRQ // // Note - not concerned about the first cycle so not resetting the counter // /////////////////////////////////// #include "msp432.h" #include void setup_timer(void); int main(){ setup_timer(); while(1){ ; // do nothing } // end while return 0; } // end main void setup_timer(void){ // // setup pins // // P6.6 in PSEL=01 mode and set as output P6->SEL0 |= 0x40; P6->SEL1 &= ~0x40; P6->DIR |= 0x40; // // TIMER_A2 // CTL // SMCLK /1 up noclr int(TAIFG) // xxxx xx 10 00 01 x 0 1 x TIMER_A2->CTL = 0x0212; // // CCR[0] - 1KHz TIMER_A2->CCR[0] = 3000; // // CCR[3] TIMER_A2->CCR[3] = 1200; // // CCTL[3] // no cap xx x x x comp out_mode int x out x x // 00 0 000 1 0 // default output to 0 TIMER_A2->CCTL[3] = 0x0010; // // setup NVIC // // Enable Timer A2 // Note: Timer A2 is INTISR(13) for channel N NVIC->IP[13] |= 0x40; // Set a priority NVIC->ISER[0] |= 0x00002000; // ISER1 starts at 32 return; } // end setup_timer // //Interrupt routines // // Approach 1 - Only enable int on CTL and CCTL3 - // then only need TA2_N_IRQ //void TA2_N_IRQHandler(void){ // // weak version // if(TIMER_A2->IV == 0x06){ // // at switch point - output 0 // TIMER_A2->CCTL[3] &= ~0x0004; // } // else{ // // output 1 // TIMER_A2->CCTL[3] |= 0x0004; // } // // return; //} // end TA2_N_IRQHandler void TA2_N_IRQHandler(void){ // better version int tmp; tmp = TIMER_A2->IV; if(tmp == 0x06){ // at switch point - output 0 TIMER_A2->CCTL[3] &= ~0x0004; } else if(tmp == 0x0E) { // output 1 TIMER_A2->CCTL[3] |= 0x0004; } return; } // end TA2_N_IRQHandler