dushyant 0 Posted July 28, 2016 Share Posted July 28, 2016 (edited) Hello, I have made the program in which i used timer to generate the pulse. but now i don't want to use timer instead i want to use counter. I have to generate the output pulse when my input counter is more than 200. if it is less than 200, don't want to generate the pulse. I am pasting here my code, help me to generate the output pulse when input pulse is more than 200 input is in pin1.0 and output is from pin 2.0, pin 2.1, pin 2.2. I am using msp430g2553 launchpad #include "msp430G2553.h" #include "stdint.h" volatile uint16_t down_counter; // Variable for counting down volatile uint8_t output_control = 0; // Variable controls output activity volatile uint8_t input_state = 0; // Variable holds actual input state volatile uint8_t counter = 0; void main( void ) { WDTCTL = (WDTPW | WDTHOLD); // Stop watchdog timer #if defined(CALBC1_16MHZ_) && F_CPU >= 16000000L BCSCTL1 = CALBC1_16MHZ; // Set range DCOCTL = CALDCO_16MHZ; // Set DCO step and modulation #endif P2REN |= BIT4; // Enable resistor for P2.4 P2OUT |= (BIT0 | BIT1 | BIT2 | BIT4); // P2.0/1/2 high, pull-up resistor for P2.4 P2DIR |= (BIT0 | BIT1 | BIT2); // P2.0/1/2 to output direction P2IES |= BIT4; // Interrupt on high->low transition P2IFG &= ~BIT4; // Clear P2.4 IFG P2IE |= BIT4; // Enable interrupts for P2.4 TA0CCR0 = 2000; // Timer value for 2ms interrupt TA0CCTL0 = CCIE; // Enable compare interrupt TA0CTL = (TASSEL_2 | ID_0 | MC_2 | TACLR); // SMCLK, divider 1, continuous mode, clear // Input emulation P1OUT |= BIT0; // P1.0 high P1DIR |= BIT0; // P1.0 output - emulates output of sensor TA1CCR0 = 62500; // Timer value for 500ms interrupt 62500 TA1CCTL0 = CCIE; // Enable compare interrupt TA1CTL = (TASSEL_2 | ID_3 | MC_2 | TACLR); // SMCLK, divider 8, continuous mode, clear // Input emulation __bis_SR_register( GIE ); // Enable global interrupts while( 1 ) // Main program { } } // Input emulation // Timer 1 A0 interrupt service routine #pragma vector = TIMER1_A0_VECTOR __interrupt void Timer1_A0_ISR( void ) { static uint8_t timer_state = 0; // Variable holds actual timer state static uint8_t multiplier = 0; // Variable to extend timer interval TA1CCR0 += 2000; // Timer value for next interrupt in 2ms switch( timer_state ) // Determine actual timer state { case 0: // Delay between pulses { if( ++multiplier < 2 ) // Not 1s elapsed yet { TA1CCR0 += 62500; // Add 500ms 62500 } else // 1s elapsed { TA1CCR0 += 12500; // Add 100ms 12500 P1OUT &= ~BIT0; // P1.0 low multiplier = 0; // Reset multiplier timer_state++; // Increment timer state } break; } case 1: // Signal was low for 100ms { TA1CCR0 += 50000; // Add 400ms 50000 P1OUT |= BIT0; // P1.0 high timer_state++; // Increment timer state break; } case 2: // Signal was high for 400ms { TA1CCR0 += 12500; // Add 100ms 12500 P1OUT &= ~BIT0; // P1.0 low timer_state++; // Increment timer state break; } case 3: // Signal was low for 100ms { TA1CCR0 += 62500; // Add 500ms 62500 P1OUT |= BIT0; // P1.0 high timer_state = 0; // Reset timer state break; } } } // Input emulation // Timer 0 A0 interrupt service routine #pragma vector = TIMER0_A0_VECTOR __interrupt void Timer0_A0_ISR( void ) { static uint8_t state = 0; // Variable holds actual output state // TA0CCR0 += 2000; // Timer value for next interrupt in 2ms if(counter>200) { output_control = 1; } else { output_control = 0; } if( output_control = 1 ) // Test if down counter became 0 now { input_state = 0; // Reset input state P2IE &= ~BIT4; // Disable interrupts for P2.4 P2IES |= BIT4; // Interrupt on high->low transition P2IFG &= ~BIT4; // Clear P2.4 IFG P2IE |= BIT4; // Enable interrupts for P2.4 } if( output_control == 1 ) // Test if output is active { switch( state ) { case 0: // State 0 { P2OUT |= BIT2; // Set P2.2 high P2OUT &= ~BIT0; break; } case 1: // State 1 { P2OUT |= BIT0; // Set P2.0 high P2OUT &= ~BIT1; // Set P2.1 low break; } case 2: // State 2 { P2OUT |= BIT1; // Set P2.1 high P2OUT &= ~BIT2; // Set P2.2 low break; } } if( ++state >= 3 ) // Increment state and test if >= 3 { state = 0; // Reset state to 0 } else (output_control == 0); { } } } // Port 2 interrupt service routine #pragma vector = PORT2_VECTOR __interrupt void Port2_ISR( void ) { switch( input_state ) // Determine actual input state { case 0: // State 0: High->low transition of first pulse { down_counter = 60; // Timeout of 120ms 60 input_state++; // Increment input state P2IES &= ~BIT4; // Interrupt on low->high transition break; } case 1: // State 1: Low->high transition of first pulse { down_counter = 225; // Timeout of 450ms 225 input_state++; // Increment input state P2IES |= BIT4; // Interrupt on high->low transition break; } case 2: // State 2: High->low transition of second pulse { down_counter = 60; // Timeout of 120ms 60 input_state++; // Increment input state P2IES &= ~BIT4; // Interrupt on low->high transition break; } case 3: // State 3: Low->high transition of second pulse { P2IES |= BIT4; // Interrupt on high->low transition output_control = 1; // Activate output P2IE &= ~BIT4; // Disable interrupts for P2.4 input_state = 0; // Reset input state break; } } P2IFG &= ~BIT4; // Clear P2.4 IFG } Edited July 28, 2016 by bluehash [ADMIN] Please use code tags<> next time. Quote Link to post Share on other sites
abecedarian 330 Posted July 28, 2016 Share Posted July 28, 2016 A timer is a counter. It counts up or down based on its frequency and how it is configured. It might "roll over" or "overflow" if it exceeds its bit depth. You can configure the counter to reset when some event happens or do something when its value exceeds some other value. So, what it seems like is you want to wait for something to happen, then start counting and do something else if that first something is still happening and the counter has counted past 200. I think the answer is in your question. Maybe search for button debounce techniques. Quote Link to post Share on other sites
dushyant 0 Posted July 29, 2016 Author Share Posted July 29, 2016 I tried but couldn't find the solution. i am not getting the output as i want. After 200 counts the output will be one pulse not the train of pulse. i checked everything how to get one pulse output, but no success. can you help me how to have the output one not train? Quote Link to post Share on other sites
chicken 630 Posted July 29, 2016 Share Posted July 29, 2016 It might help if you try to explain in a bit more detail what your code is supposed to do. You won't get many answers if you expect people to parse 100+ lines of code. There are a few errors in the if/else in the ISRs. The compiler will probably tell you about these if you look at the warnings. E.g. : if ( output_control = 1 ) .. i and: else ( ouput_control == 0 ) In the timer ISR. yyrkoon 1 Quote Link to post Share on other sites
dushyant 0 Posted July 29, 2016 Author Share Posted July 29, 2016 yup i am getting the warnings So, basically i have to generate the output pulse on the pin2.0, pin2.1 and pin 2.2 of the msp430g2553 launchpad. pin1.0 is the input So, at first when i touch the input pin1.0 with the oscilloscope probe; my output won't generate. but after 200 input pulses my output will generate on the pins 2.0/2.1/2.2 it means after passing of 200 input pulses, the output will generate. the above code i pasted here is giving me the input and output both. but the pulses i am getting is the train of pulses and i want just only oune pulse. Quote Link to post Share on other sites
chicken 630 Posted July 29, 2016 Share Posted July 29, 2016 Then try to fix the warnings. The if statement I mentioned above probably does not what you expect. It will always set output_control to 1. tripwire 1 Quote Link to post Share on other sites
dushyant 0 Posted August 1, 2016 Author Share Posted August 1, 2016 main thing is i want to generate the pulse after 200 input pulses. but i am not getting it. help me please. the warning i fixed it but then also i am not getting the desired result. help me!! Quote Link to post Share on other sites
chicken 630 Posted August 1, 2016 Share Posted August 1, 2016 Your program doesn't make any sense as is. You have counter variables that are never counted up or down. I think you copied a program that you don't understand. Someone from a forum rewriting it for you won't help you achieve your goal. My suggestion is to either start from scratch and build the program step by step, or at least try to understand how the original is supposed to work before you change it: https://e2e.ti.com/support/microcontrollers/msp430/f/166/t/529768 I see that Dennis tried to be really helpful on E2E but ultimately lost patience. Starting from scratch with Energia or a beginners tutorial might be your best bet. Frida and tripwire 2 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.